1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990,91,92,93,94,95,96,97,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include <sys/types.h>
31 #if STDC_HEADERS || _LIBC
33 static VOID
*fixup_null_alloc (size_t n
) __THROW
;
34 VOID
*xmalloc (size_t n
) __THROW
;
35 VOID
*xcalloc (size_t n
, size_t s
) __THROW
;
36 VOID
*xrealloc (VOID
*p
, size_t n
) __THROW
;
48 # define _(str) gettext (str)
52 #define EXIT_FAILURE 4
55 /* Exit value when the requested amount of memory is not available.
56 The caller may set it to some other value. */
57 int xmalloc_exit_failure
= EXIT_FAILURE
;
67 p
= malloc ((size_t) 1);
69 error (xmalloc_exit_failure
, 0, _("memory exhausted"));
73 /* Allocate N bytes of memory dynamically, with error checking. */
83 p
= fixup_null_alloc (n
);
87 /* Allocate memory for N elements of S bytes, with error checking. */
97 p
= fixup_null_alloc (n
);
101 /* Change the size of an allocated block of memory P to N bytes,
103 If P is NULL, run xmalloc. */
114 p
= fixup_null_alloc (n
);