1131554Stjr/* xalloc.h -- malloc with out-of-memory checking 2131554Stjr Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc. 3131554Stjr 4131554Stjr This program is free software; you can redistribute it and/or modify 5131554Stjr it under the terms of the GNU General Public License as published by 6131554Stjr the Free Software Foundation; either version 2, or (at your option) 7131554Stjr any later version. 8131554Stjr 9131554Stjr This program is distributed in the hope that it will be useful, 10131554Stjr but WITHOUT ANY WARRANTY; without even the implied warranty of 11131554Stjr MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12131554Stjr GNU General Public License for more details. 13131554Stjr 14131554Stjr You should have received a copy of the GNU General Public License 15131554Stjr along with this program; if not, write to the Free Software Foundation, 16131554Stjr Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17131554Stjr 18131554Stjr#ifndef XALLOC_H_ 19131554Stjr# define XALLOC_H_ 20131554Stjr 21131554Stjr# ifndef PARAMS 22131554Stjr# if defined PROTOTYPES || (defined __STDC__ && __STDC__) 23131554Stjr# define PARAMS(Args) Args 24131554Stjr# else 25131554Stjr# define PARAMS(Args) () 26131554Stjr# endif 27131554Stjr# endif 28131554Stjr 29131554Stjr# ifndef __attribute__ 30131554Stjr# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ 31131554Stjr# define __attribute__(x) 32131554Stjr# endif 33131554Stjr# endif 34131554Stjr 35131554Stjr# ifndef ATTRIBUTE_NORETURN 36131554Stjr# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 37131554Stjr# endif 38131554Stjr 39131554Stjr/* Exit value when the requested amount of memory is not available. 40131554Stjr It is initialized to EXIT_FAILURE, but the caller may set it to 41131554Stjr some other value. */ 42131554Stjrextern int xalloc_exit_failure; 43131554Stjr 44131554Stjr/* If this pointer is non-zero, run the specified function upon each 45131554Stjr allocation failure. It is initialized to zero. */ 46131554Stjrextern void (*xalloc_fail_func) PARAMS ((void)); 47131554Stjr 48131554Stjr/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this 49131554Stjr message is output. It is translated via gettext. 50131554Stjr Its value is "memory exhausted". */ 51131554Stjrextern char const xalloc_msg_memory_exhausted[]; 52131554Stjr 53131554Stjr/* This function is always triggered when memory is exhausted. It is 54131554Stjr in charge of honoring the three previous items. This is the 55131554Stjr function to call when one wants the program to die because of a 56131554Stjr memory allocation failure. */ 57131554Stjrextern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; 58131554Stjr 59131554Stjrvoid *xmalloc PARAMS ((size_t n)); 60131554Stjrvoid *xcalloc PARAMS ((size_t n, size_t s)); 61131554Stjrvoid *xrealloc PARAMS ((void *p, size_t n)); 62131554Stjrchar *xstrdup PARAMS ((const char *str)); 63131554Stjr 64131554Stjr# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) 65131554Stjr# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) 66131554Stjr# define XREALLOC(Ptr, Type, N_items) \ 67131554Stjr ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) 68131554Stjr 69131554Stjr/* Declare and alloc memory for VAR of type TYPE. */ 70131554Stjr# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) 71131554Stjr 72131554Stjr/* Free VAR only if non NULL. */ 73131554Stjr# define XFREE(Var) \ 74131554Stjr do { \ 75131554Stjr if (Var) \ 76131554Stjr free (Var); \ 77131554Stjr } while (0) 78131554Stjr 79131554Stjr/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ 80131554Stjr# define CCLONE(Src, Num) \ 81131554Stjr (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) 82131554Stjr 83131554Stjr/* Return a malloc'ed copy of SRC. */ 84131554Stjr# define CLONE(Src) CCLONE (Src, 1) 85131554Stjr 86131554Stjr 87131554Stjr#endif /* !XALLOC_H_ */ 88