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