571e53c0482e909b58fe00e8e784f5877c1d8188
[gnulib.git] / lib / xalloc.h
1 /* xalloc.h -- malloc with out-of-memory checking
2
3    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2003 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #ifndef XALLOC_H_
21 # define XALLOC_H_
22
23 # include <stddef.h>
24 # if HAVE_STDINT_H
25 #  include <stdint.h>
26 # endif
27
28 # ifndef __attribute__
29 #  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
30 #   define __attribute__(x)
31 #  endif
32 # endif
33
34 # ifndef ATTRIBUTE_NORETURN
35 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
36 # endif
37
38 /* If this pointer is non-zero, run the specified function upon each
39    allocation failure.  It is initialized to zero. */
40 extern void (*xalloc_fail_func) (void);
41
42 /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
43    message is output.  It is translated via gettext.
44    Its value is "memory exhausted".  */
45 extern char const xalloc_msg_memory_exhausted[];
46
47 /* This function is always triggered when memory is exhausted.  It is
48    in charge of honoring the two previous items.  It exits with status
49    exit_failure (defined in exitfail.h).  This is the
50    function to call when one wants the program to die because of a
51    memory allocation failure.  */
52 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
53
54 void *xmalloc (size_t s);
55 void *xnmalloc (size_t n, size_t s);
56 void *xzalloc (size_t s);
57 void *xcalloc (size_t n, size_t s);
58 void *xrealloc (void *p, size_t s);
59 void *xnrealloc (void *p, size_t n, size_t s);
60 void *x2realloc (void *p, size_t *pn);
61 void *x2nrealloc (void *p, size_t *pn, size_t s);
62 void *xclone (void const *p, size_t s);
63 char *xstrdup (const char *str);
64
65 /* Return 1 if an array of N objects, each of size S, cannot exist due
66    to size arithmetic overflow.  S must be positive and N must be
67    nonnegative.  This is a macro, not an inline function, so that it
68    works correctly even when SIZE_MAX < N.
69
70    By gnulib convention, SIZE_MAX represents overflow in size
71    calculations, so the conservative dividend to use here is
72    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
73    However, malloc (SIZE_MAX) fails on all known hosts where
74    PTRDIFF_MAX < SIZE_MAX, so do not bother to test for
75    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
76    branch when S is known to be 1.  */
77 # if defined PTRDIFF_MAX && PTRDIFF_MAX < SIZE_MAX
78 #  define xalloc_oversized(n, s) (SIZE_MAX / (s) < (n))
79 # else /* SIZE_MAX might not be defined, so avoid (SIZE_MAX - 1).  */
80 #  define xalloc_oversized(n, s) ((size_t) -2 / (s) < (n))
81 # endif
82
83 /* These macros are deprecated; they will go away soon, and are retained
84    temporarily only to ease conversion to the functions described above.  */
85 # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
86 # define CLONE(p) xclone (p, sizeof *(p))
87 # define NEW(type, var) type *var = xmalloc (sizeof (type))
88 # define XCALLOC(type, n) xcalloc (n, sizeof (type))
89 # define XMALLOC(type, n) xnmalloc (n, sizeof (type))
90 # define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
91 # define XFREE(p) free (p)
92
93 #endif /* !XALLOC_H_ */