1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990-1996, 2000-2003, 2005-2007, 2012, 2015 Free
3 Software Foundation, Inc.
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 by
7 the Free Software Foundation; either version 3 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/>. */
28 #define _(str) gettext (str)
31 /* Exit value when the requested amount of memory is not available.
32 The caller may set it to some other value. */
33 int xmalloc_exit_failure = EXIT_FAILURE;
38 error (xmalloc_exit_failure, 0, _("memory exhausted"));
39 /* _Noreturn cannot be given to error, since it may return if
40 its first argument is 0. To help compilers understand the
41 xalloc_die does terminate, call exit. */
46 fixup_null_alloc (size_t n)
52 p = malloc ((size_t) 1);
58 /* Allocate N bytes of memory dynamically, with error checking. */
67 p = fixup_null_alloc (n);
71 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
75 xnmalloc (size_t nmemb, size_t size)
80 if (xalloc_oversized (nmemb, size))
85 p = fixup_null_alloc (n);
89 /* Allocate SIZE bytes of memory dynamically, with error checking,
102 /* Allocate memory for N elements of S bytes, with error checking,
106 xcalloc (size_t n, size_t s)
112 p = fixup_null_alloc (n);
116 /* Change the size of an allocated block of memory P to N bytes,
118 If P is NULL, run xmalloc. */
121 xrealloc (void *p, size_t n)
127 p = fixup_null_alloc (n);