Back to index

tetex-bin  3.0
xalloc.h
Go to the documentation of this file.
00001 /* xalloc.h -- malloc with out-of-memory checking
00002 
00003    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
00004    1999, 2000, 2003, 2004 Free Software Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #ifndef XALLOC_H_
00021 # define XALLOC_H_
00022 
00023 # include <stddef.h>
00024 
00025 
00026 # ifdef __cplusplus
00027 extern "C" {
00028 # endif
00029 
00030 
00031 # ifndef __attribute__
00032 #  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
00033 #   define __attribute__(x)
00034 #  endif
00035 # endif
00036 
00037 # ifndef ATTRIBUTE_NORETURN
00038 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
00039 # endif
00040 
00041 /* This function is always triggered when memory is exhausted.
00042    It must be defined by the application, either explicitly
00043    or by using gnulib's xalloc-die module.  This is the
00044    function to call when one wants the program to die because of a
00045    memory allocation failure.  */
00046 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
00047 
00048 void *xmalloc (size_t s);
00049 void *xnmalloc (size_t n, size_t s);
00050 void *xzalloc (size_t s);
00051 void *xcalloc (size_t n, size_t s);
00052 void *xrealloc (void *p, size_t s);
00053 void *xnrealloc (void *p, size_t n, size_t s);
00054 void *x2realloc (void *p, size_t *pn);
00055 void *x2nrealloc (void *p, size_t *pn, size_t s);
00056 void *xclone (void const *p, size_t s);
00057 char *xstrdup (const char *str);
00058 
00059 /* Return 1 if an array of N objects, each of size S, cannot exist due
00060    to size arithmetic overflow.  S must be positive and N must be
00061    nonnegative.  This is a macro, not an inline function, so that it
00062    works correctly even when SIZE_MAX < N.
00063 
00064    By gnulib convention, SIZE_MAX represents overflow in size
00065    calculations, so the conservative dividend to use here is
00066    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
00067    However, malloc (SIZE_MAX) fails on all known hosts where
00068    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
00069    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
00070    branch when S is known to be 1.  */
00071 # define xalloc_oversized(n, s) \
00072     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
00073 
00074 /* These macros are deprecated; they will go away soon, and are retained
00075    temporarily only to ease conversion to the functions described above.  */
00076 # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
00077 # define CLONE(p) xclone (p, sizeof *(p))
00078 # define NEW(type, var) type *var = xmalloc (sizeof (type))
00079 # define XCALLOC(type, n) xcalloc (n, sizeof (type))
00080 # define XMALLOC(type, n) xnmalloc (n, sizeof (type))
00081 # define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
00082 # define XFREE(p) free (p)
00083 
00084 
00085 # ifdef __cplusplus
00086 }
00087 # endif
00088 
00089 
00090 #endif /* !XALLOC_H_ */