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