xref: /netbsd-src/external/gpl2/diffutils/dist/lib/xalloc.h (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: xalloc.h,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* xalloc.h -- malloc with out-of-memory checking
4*75f6d617Schristos    Copyright (C) 1990-1998, 1999, 2000, 2002 Free Software Foundation, Inc.
5*75f6d617Schristos 
6*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
7*75f6d617Schristos    it under the terms of the GNU General Public License as published by
8*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
9*75f6d617Schristos    any later version.
10*75f6d617Schristos 
11*75f6d617Schristos    This program is distributed in the hope that it will be useful,
12*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*75f6d617Schristos    GNU General Public License for more details.
15*75f6d617Schristos 
16*75f6d617Schristos    You should have received a copy of the GNU General Public License
17*75f6d617Schristos    along with this program; if not, write to the Free Software Foundation,
18*75f6d617Schristos    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19*75f6d617Schristos 
20*75f6d617Schristos #ifndef XALLOC_H_
21*75f6d617Schristos # define XALLOC_H_
22*75f6d617Schristos 
23*75f6d617Schristos # ifndef PARAMS
24*75f6d617Schristos #  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
25*75f6d617Schristos #   define PARAMS(Args) Args
26*75f6d617Schristos #  else
27*75f6d617Schristos #   define PARAMS(Args) ()
28*75f6d617Schristos #  endif
29*75f6d617Schristos # endif
30*75f6d617Schristos 
31*75f6d617Schristos # ifndef __attribute__
32*75f6d617Schristos #  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
33*75f6d617Schristos #   define __attribute__(x)
34*75f6d617Schristos #  endif
35*75f6d617Schristos # endif
36*75f6d617Schristos 
37*75f6d617Schristos # ifndef ATTRIBUTE_NORETURN
38*75f6d617Schristos #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
39*75f6d617Schristos # endif
40*75f6d617Schristos 
41*75f6d617Schristos /* If this pointer is non-zero, run the specified function upon each
42*75f6d617Schristos    allocation failure.  It is initialized to zero. */
43*75f6d617Schristos extern void (*xalloc_fail_func) PARAMS ((void));
44*75f6d617Schristos 
45*75f6d617Schristos /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
46*75f6d617Schristos    message is output.  It is translated via gettext.
47*75f6d617Schristos    Its value is "memory exhausted".  */
48*75f6d617Schristos extern char const xalloc_msg_memory_exhausted[];
49*75f6d617Schristos 
50*75f6d617Schristos /* This function is always triggered when memory is exhausted.  It is
51*75f6d617Schristos    in charge of honoring the three previous items.  This is the
52*75f6d617Schristos    function to call when one wants the program to die because of a
53*75f6d617Schristos    memory allocation failure.  */
54*75f6d617Schristos extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN;
55*75f6d617Schristos 
56*75f6d617Schristos void *xmalloc PARAMS ((size_t n));
57*75f6d617Schristos void *xcalloc PARAMS ((size_t n, size_t s));
58*75f6d617Schristos void *xrealloc PARAMS ((void *p, size_t n));
59*75f6d617Schristos char *xstrdup PARAMS ((const char *str));
60*75f6d617Schristos 
61*75f6d617Schristos # define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items)))
62*75f6d617Schristos # define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items)))
63*75f6d617Schristos # define XREALLOC(Ptr, Type, N_items) \
64*75f6d617Schristos   ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items)))
65*75f6d617Schristos 
66*75f6d617Schristos /* Declare and alloc memory for VAR of type TYPE. */
67*75f6d617Schristos # define NEW(Type, Var)  Type *(Var) = XMALLOC (Type, 1)
68*75f6d617Schristos 
69*75f6d617Schristos /* Free VAR only if non NULL. */
70*75f6d617Schristos # define XFREE(Var)	\
71*75f6d617Schristos    do {                 \
72*75f6d617Schristos       if (Var)          \
73*75f6d617Schristos         free (Var);     \
74*75f6d617Schristos    } while (0)
75*75f6d617Schristos 
76*75f6d617Schristos /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
77*75f6d617Schristos # define CCLONE(Src, Num) \
78*75f6d617Schristos   (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num)))
79*75f6d617Schristos 
80*75f6d617Schristos /* Return a malloc'ed copy of SRC. */
81*75f6d617Schristos # define CLONE(Src) CCLONE (Src, 1)
82*75f6d617Schristos 
83*75f6d617Schristos 
84*75f6d617Schristos #endif /* !XALLOC_H_ */
85