Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

xalloc.h

Go to the documentation of this file.
00001 /* xalloc.h -- malloc with out-of-memory checking
00002    Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software Foundation,
00016    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 #ifndef XALLOC_H_
00019 # define XALLOC_H_
00020 
00021 # ifndef PARAMS
00022 #  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
00023 #   define PARAMS(Args) Args
00024 #  else
00025 #   define PARAMS(Args) ()
00026 #  endif
00027 # endif
00028 
00029 # ifndef __attribute__
00030 #  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
00031 #   define __attribute__(x)
00032 #  endif
00033 # endif
00034 
00035 # ifndef ATTRIBUTE_NORETURN
00036 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
00037 # endif
00038 
00039 /* Exit value when the requested amount of memory is not available.
00040    It is initialized to EXIT_FAILURE, but the caller may set it to
00041    some other value.  */
00042 extern int xalloc_exit_failure;
00043 
00044 /* If this pointer is non-zero, run the specified function upon each
00045    allocation failure.  It is initialized to zero. */
00046 extern void (*xalloc_fail_func) PARAMS ((void));
00047 
00048 /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
00049    message is output.  It is translated via gettext.
00050    Its value is "memory exhausted".  */
00051 extern char const xalloc_msg_memory_exhausted[];
00052 
00053 /* This function is always triggered when memory is exhausted.  It is
00054    in charge of honoring the three previous items.  This is the
00055    function to call when one wants the program to die because of a
00056    memory allocation failure.  */
00057 extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN;
00058 
00059 void *xmalloc PARAMS ((size_t n));
00060 void *xcalloc PARAMS ((size_t n, size_t s));
00061 void *xrealloc PARAMS ((void *p, size_t n));
00062 char *xstrdup PARAMS ((const char *str));
00063 
00064 # define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items)))
00065 # define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items)))
00066 # define XREALLOC(Ptr, Type, N_items) \
00067   ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items)))
00068 
00069 /* Declare and alloc memory for VAR of type TYPE. */
00070 # define NEW(Type, Var)  Type *(Var) = XMALLOC (Type, 1)
00071 
00072 /* Free VAR only if non NULL. */
00073 # define XFREE(Var)  \
00074    do {                 \
00075       if (Var)          \
00076         free (Var);     \
00077    } while (0)
00078 
00079 /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
00080 # define CCLONE(Src, Num) \
00081   (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num)))
00082 
00083 /* Return a malloc'ed copy of SRC. */
00084 # define CLONE(Src) CCLONE (Src, 1)
00085 
00086 
00087 #endif /* !XALLOC_H_ */

Generated on Mon Apr 7 19:41:47 2003 for DuTree by doxygen1.2.18