summaryrefslogtreecommitdiff
path: root/lib/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/malloc.c')
-rw-r--r--lib/malloc.c57
1 files changed, 35 insertions, 22 deletions
diff --git a/lib/malloc.c b/lib/malloc.c
index c4a09e1..3ade35c 100644
--- a/lib/malloc.c
+++ b/lib/malloc.c
@@ -1,38 +1,51 @@
-/* Work around bug on some systems where malloc (0) fails.
- Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+/* malloc() function that is glibc compatible.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3, or (at your option)
- any later version.
+ Copyright (C) 1997-1998, 2006-2007, 2009-2023 Free Software Foundation, Inc.
- This program is distributed in the hope that it will be useful,
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ GNU Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
-/* written by Jim Meyering */
+/* written by Jim Meyering and Bruno Haible */
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-#undef malloc
+#define _GL_USE_STDLIB_ALLOC 1
+#include <config.h>
-#include <sys/types.h>
+#include <stdlib.h>
-char *malloc ();
+#include <errno.h>
-/* Allocate an N-byte block of memory from the heap.
- If N is zero, allocate a 1-byte block. */
+#include "xalloc-oversized.h"
-char *
+/* Allocate an N-byte block of memory from the heap, even if N is 0. */
+
+void *
rpl_malloc (size_t n)
{
if (n == 0)
n = 1;
- return malloc (n);
+
+ if (xalloc_oversized (n, 1))
+ {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ void *result = malloc (n);
+
+#if !HAVE_MALLOC_POSIX
+ if (result == NULL)
+ errno = ENOMEM;
+#endif
+
+ return result;
}