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