1 /* malloc with out of memory checking.
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 2, or (at your option)
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. */
30 /* Defined in xmalloc.c. */
32 /* Allocate SIZE bytes of memory dynamically, with error checking. */
33 extern void *xmalloc (size_t size
);
35 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking. */
36 extern void *xcalloc (size_t nmemb
, size_t size
);
38 /* Change the size of an allocated block of memory PTR to SIZE bytes,
39 with error checking. If PTR is NULL, run xmalloc. */
40 extern void *xrealloc (void *ptr
, size_t size
);
42 /* This function is always triggered when memory is exhausted. It is
43 in charge of honoring the three previous items. This is the
44 function to call when one wants the program to die because of a
45 memory allocation failure. */
46 extern void xalloc_die (void)
47 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__
48 __attribute__ ((__noreturn__
))
53 /* Defined in xstrdup.c. */
55 /* Return a newly allocated copy of STRING. */
56 extern char *xstrdup (const char *string
);
59 /* Return 1 if an array of N objects, each of size S, cannot exist due
60 to size arithmetic overflow. S must be positive and N must be
61 nonnegative. This is a macro, not an inline function, so that it
62 works correctly even when SIZE_MAX < N.
64 By gnulib convention, SIZE_MAX represents overflow in size
65 calculations, so the conservative dividend to use here is
66 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
67 However, malloc (SIZE_MAX) fails on all known hosts where
68 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
69 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
70 branch when S is known to be 1. */
71 # define xalloc_oversized(n, s) \
72 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
80 #endif /* _XALLOC_H */