1/* malloc with out of memory checking. 2 Copyright (C) 2001-2004 Free Software Foundation, Inc. 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2, or (at your option) 8 any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 19#ifndef _XALLOC_H 20#define _XALLOC_H 21 22#include <stddef.h> 23 24 25#ifdef __cplusplus 26extern "C" { 27#endif 28 29 30/* Defined in xmalloc.c. */ 31 32/* Allocate SIZE bytes of memory dynamically, with error checking. */ 33extern void *xmalloc (size_t size); 34 35/* Allocate memory for NMEMB elements of SIZE bytes, with error checking. */ 36extern void *xcalloc (size_t nmemb, size_t size); 37 38/* Change the size of an allocated block of memory PTR to SIZE bytes, 39 with error checking. If PTR is NULL, run xmalloc. */ 40extern void *xrealloc (void *ptr, size_t size); 41 42/* This function is always triggered when memory is exhausted. It is 43 in charge of honoring the three previous items. This is the 44 function to call when one wants the program to die because of a 45 memory allocation failure. */ 46extern void xalloc_die (void) 47#if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__ 48 __attribute__ ((__noreturn__)) 49#endif 50 ; 51 52 53/* Defined in xstrdup.c. */ 54 55/* Return a newly allocated copy of STRING. */ 56extern char *xstrdup (const char *string); 57 58 59/* Return 1 if an array of N objects, each of size S, cannot exist due 60 to size arithmetic overflow. S must be positive and N must be 61 nonnegative. This is a macro, not an inline function, so that it 62 works correctly even when SIZE_MAX < N. 63 64 By gnulib convention, SIZE_MAX represents overflow in size 65 calculations, so the conservative dividend to use here is 66 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. 67 However, malloc (SIZE_MAX) fails on all known hosts where 68 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for 69 exactly-SIZE_MAX allocations on such hosts; this avoids a test and 70 branch when S is known to be 1. */ 71# define xalloc_oversized(n, s) \ 72 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) 73 74 75#ifdef __cplusplus 76} 77#endif 78 79 80#endif /* _XALLOC_H */ 81