1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
27 #define _(str) gettext (str)
30 /* Exit value when the requested amount of memory is not available.
31 The caller may set it to some other value. */
32 int xmalloc_exit_failure
= EXIT_FAILURE
;
37 error (xmalloc_exit_failure
, 0, _("memory exhausted"));
38 /* _Noreturn cannot be given to error, since it may return if
39 its first argument is 0. To help compilers understand the
40 xalloc_die does terminate, call exit. */
45 fixup_null_alloc (size_t n
)
51 p
= malloc ((size_t) 1);
57 /* Allocate N bytes of memory dynamically, with error checking. */
66 p
= fixup_null_alloc (n
);
70 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
74 xnmalloc (size_t nmemb
, size_t size
)
79 if (xalloc_oversized (nmemb
, size
))
84 p
= fixup_null_alloc (n
);
88 /* Allocate SIZE bytes of memory dynamically, with error checking,
101 /* Allocate memory for N elements of S bytes, with error checking,
105 xcalloc (size_t n
, size_t s
)
111 p
= fixup_null_alloc (n
);
115 /* Change the size of an allocated block of memory P to N bytes,
117 If P is NULL, run xmalloc. */
120 xrealloc (void *p
, size_t n
)
126 p
= fixup_null_alloc (n
);