1114472Sru/* xalloc.h -- malloc with out-of-memory checking
2114472Sru
3146515Sru   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4146515Sru   1999, 2000, 2003, 2004 Free Software Foundation, Inc.
5146515Sru
6114472Sru   This program is free software; you can redistribute it and/or modify
7114472Sru   it under the terms of the GNU General Public License as published by
8114472Sru   the Free Software Foundation; either version 2, or (at your option)
9114472Sru   any later version.
10114472Sru
11114472Sru   This program is distributed in the hope that it will be useful,
12114472Sru   but WITHOUT ANY WARRANTY; without even the implied warranty of
13114472Sru   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14114472Sru   GNU General Public License for more details.
15114472Sru
16114472Sru   You should have received a copy of the GNU General Public License
17114472Sru   along with this program; if not, write to the Free Software Foundation,
18114472Sru   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19114472Sru
20114472Sru#ifndef XALLOC_H_
21114472Sru# define XALLOC_H_
22114472Sru
23116525Sru# include <stddef.h>
24116525Sru
25146515Sru
26146515Sru# ifdef __cplusplus
27146515Sruextern "C" {
28114472Sru# endif
29114472Sru
30146515Sru
31114472Sru# ifndef __attribute__
32114472Sru#  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
33114472Sru#   define __attribute__(x)
34114472Sru#  endif
35114472Sru# endif
36114472Sru
37114472Sru# ifndef ATTRIBUTE_NORETURN
38114472Sru#  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
39114472Sru# endif
40114472Sru
41146515Sru/* This function is always triggered when memory is exhausted.
42146515Sru   It must be defined by the application, either explicitly
43146515Sru   or by using gnulib's xalloc-die module.  This is the
44114472Sru   function to call when one wants the program to die because of a
45114472Sru   memory allocation failure.  */
46146515Sruextern void xalloc_die (void) ATTRIBUTE_NORETURN;
47114472Sru
48146515Sruvoid *xmalloc (size_t s);
49146515Sruvoid *xnmalloc (size_t n, size_t s);
50146515Sruvoid *xzalloc (size_t s);
51146515Sruvoid *xcalloc (size_t n, size_t s);
52146515Sruvoid *xrealloc (void *p, size_t s);
53146515Sruvoid *xnrealloc (void *p, size_t n, size_t s);
54146515Sruvoid *x2realloc (void *p, size_t *pn);
55146515Sruvoid *x2nrealloc (void *p, size_t *pn, size_t s);
56146515Sruvoid *xclone (void const *p, size_t s);
57146515Sruchar *xstrdup (const char *str);
58114472Sru
59146515Sru/* Return 1 if an array of N objects, each of size S, cannot exist due
60146515Sru   to size arithmetic overflow.  S must be positive and N must be
61146515Sru   nonnegative.  This is a macro, not an inline function, so that it
62146515Sru   works correctly even when SIZE_MAX < N.
63114472Sru
64146515Sru   By gnulib convention, SIZE_MAX represents overflow in size
65146515Sru   calculations, so the conservative dividend to use here is
66146515Sru   SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
67146515Sru   However, malloc (SIZE_MAX) fails on all known hosts where
68146515Sru   sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
69146515Sru   exactly-SIZE_MAX allocations on such hosts; this avoids a test and
70146515Sru   branch when S is known to be 1.  */
71146515Sru# define xalloc_oversized(n, s) \
72146515Sru    ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
73114472Sru
74146515Sru/* These macros are deprecated; they will go away soon, and are retained
75146515Sru   temporarily only to ease conversion to the functions described above.  */
76146515Sru# define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
77146515Sru# define CLONE(p) xclone (p, sizeof *(p))
78146515Sru# define NEW(type, var) type *var = xmalloc (sizeof (type))
79146515Sru# define XCALLOC(type, n) xcalloc (n, sizeof (type))
80146515Sru# define XMALLOC(type, n) xnmalloc (n, sizeof (type))
81146515Sru# define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
82146515Sru# define XFREE(p) free (p)
83114472Sru
84114472Sru
85146515Sru# ifdef __cplusplus
86146515Sru}
87146515Sru# endif
88114472Sru
89114472Sru
90114472Sru#endif /* !XALLOC_H_ */
91