1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
26 #if STDC_HEADERS || _LIBC
28 static VOID
*fixup_null_alloc (size_t n
) __THROW
;
29 VOID
*xmalloc (size_t n
) __THROW
;
30 VOID
*xcalloc (size_t n
, size_t s
) __THROW
;
31 VOID
*xrealloc (VOID
*p
, size_t n
) __THROW
;
43 # define _(str) gettext (str)
47 #define EXIT_FAILURE 4
50 /* Exit value when the requested amount of memory is not available.
51 The caller may set it to some other value. */
52 int xmalloc_exit_failure
= EXIT_FAILURE
;
55 fixup_null_alloc (size_t n
)
61 p
= malloc ((size_t) 1);
63 error (xmalloc_exit_failure
, 0, _("memory exhausted"));
67 /* Allocate N bytes of memory dynamically, with error checking. */
76 p
= fixup_null_alloc (n
);
80 /* Allocate memory for N elements of S bytes, with error checking. */
83 xcalloc (size_t n
, size_t s
)
89 p
= fixup_null_alloc (n
);
93 /* Change the size of an allocated block of memory P to N bytes,
95 If P is NULL, run xmalloc. */
98 xrealloc (VOID
*p
, size_t n
)
104 p
= fixup_null_alloc (n
);