1 /* malloc with out of memory checking.
2    Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4 
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)
8    any later version.
9 
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.
14 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 
19 #ifndef _XALLOC_H
20 #define _XALLOC_H
21 
22 #include <stddef.h>
23 
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 
30 /* Defined in xmalloc.c.  */
31 
32 /* Allocate SIZE bytes of memory dynamically, with error checking.  */
33 extern void *xmalloc (size_t size);
34 
35 /* Allocate SIZE bytes of memory dynamically, with error checking,
36    and zero it.  */
37 extern void *xzalloc (size_t size);
38 
39 /* Allocate memory for NMEMB elements of SIZE bytes, with error checking,
40    and zero it.  */
41 extern void *xcalloc (size_t nmemb, size_t size);
42 
43 /* Change the size of an allocated block of memory PTR to SIZE bytes,
44    with error checking.  If PTR is NULL, run xmalloc.  */
45 extern void *xrealloc (void *ptr, size_t size);
46 
47 /* This function is always triggered when memory is exhausted.  It is
48    in charge of honoring the three previous items.  This is the
49    function to call when one wants the program to die because of a
50    memory allocation failure.  */
51 extern void xalloc_die (void)
52 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__
53      __attribute__ ((__noreturn__))
54 #endif
55      ;
56 
57 
58 /* Defined in xstrdup.c.  */
59 
60 /* Return a newly allocated copy of STRING.  */
61 extern char *xstrdup (const char *string);
62 
63 
64 /* Return 1 if an array of N objects, each of size S, cannot exist due
65    to size arithmetic overflow.  S must be positive and N must be
66    nonnegative.  This is a macro, not an inline function, so that it
67    works correctly even when SIZE_MAX < N.
68 
69    By gnulib convention, SIZE_MAX represents overflow in size
70    calculations, so the conservative dividend to use here is
71    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
72    However, malloc (SIZE_MAX) fails on all known hosts where
73    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
74    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
75    branch when S is known to be 1.  */
76 # define xalloc_oversized(n, s) \
77     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
78 
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 
85 #endif /* _XALLOC_H */
86