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