summaryrefslogtreecommitdiffstats
path: root/lib/xalloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/xalloc.h')
-rw-r--r--lib/xalloc.h88
1 files changed, 44 insertions, 44 deletions
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 098a6c2..4b65858 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -1,5 +1,7 @@
1/* xalloc.h -- malloc with out-of-memory checking 1/* xalloc.h -- malloc with out-of-memory checking
2 Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc. 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2003 Free Software Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
@@ -18,13 +20,7 @@
18#ifndef XALLOC_H_ 20#ifndef XALLOC_H_
19# define XALLOC_H_ 21# define XALLOC_H_
20 22
21# ifndef PARAMS 23# include <stddef.h>
22# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
23# define PARAMS(Args) Args
24# else
25# define PARAMS(Args) ()
26# endif
27# endif
28 24
29# ifndef __attribute__ 25# ifndef __attribute__
30# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ 26# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
@@ -36,14 +32,9 @@
36# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 32# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
37# endif 33# endif
38 34
39/* Exit value when the requested amount of memory is not available.
40 It is initialized to EXIT_FAILURE, but the caller may set it to
41 some other value. */
42extern int xalloc_exit_failure;
43
44/* If this pointer is non-zero, run the specified function upon each 35/* If this pointer is non-zero, run the specified function upon each
45 allocation failure. It is initialized to zero. */ 36 allocation failure. It is initialized to zero. */
46extern void (*xalloc_fail_func) PARAMS ((void)); 37extern void (*xalloc_fail_func) (void);
47 38
48/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this 39/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
49 message is output. It is translated via gettext. 40 message is output. It is translated via gettext.
@@ -51,37 +42,46 @@ extern void (*xalloc_fail_func) PARAMS ((void));
51extern char const xalloc_msg_memory_exhausted[]; 42extern char const xalloc_msg_memory_exhausted[];
52 43
53/* This function is always triggered when memory is exhausted. It is 44/* This function is always triggered when memory is exhausted. It is
54 in charge of honoring the three previous items. This is the 45 in charge of honoring the two previous items. It exits with status
46 exit_failure (defined in exitfail.h). This is the
55 function to call when one wants the program to die because of a 47 function to call when one wants the program to die because of a
56 memory allocation failure. */ 48 memory allocation failure. */
57extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; 49extern void xalloc_die (void) ATTRIBUTE_NORETURN;
58 50
59void *xmalloc PARAMS ((size_t n)); 51void *xmalloc (size_t s);
60void *xcalloc PARAMS ((size_t n, size_t s)); 52void *xnmalloc (size_t n, size_t s);
61void *xrealloc PARAMS ((void *p, size_t n)); 53void *xzalloc (size_t s);
62char *xstrdup PARAMS ((const char *str)); 54void *xcalloc (size_t n, size_t s);
63 55void *xrealloc (void *p, size_t s);
64# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) 56void *xnrealloc (void *p, size_t n, size_t s);
65# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) 57void *x2realloc (void *p, size_t *pn);
66# define XREALLOC(Ptr, Type, N_items) \ 58void *x2nrealloc (void *p, size_t *pn, size_t s);
67 ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) 59void *xclone (void const *p, size_t s);
68 60char *xstrdup (const char *str);
69/* Declare and alloc memory for VAR of type TYPE. */ 61
70# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) 62/* Return 1 if an array of N objects, each of size S, cannot exist due
71 63 to size arithmetic overflow. S must be positive and N must be
72/* Free VAR only if non NULL. */ 64 nonnegative. This is a macro, not an inline function, so that it
73# define XFREE(Var) \ 65 works correctly even when SIZE_MAX < N.
74 do { \ 66
75 if (Var) \ 67 By gnulib convention, SIZE_MAX represents overflow in size
76 free (Var); \ 68 calculations, so the conservative dividend to use here is
77 } while (0) 69 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
78 70 However, malloc (SIZE_MAX) fails on all known hosts where
79/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ 71 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
80# define CCLONE(Src, Num) \ 72 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
81 (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) 73 branch when S is known to be 1. */
82 74# define xalloc_oversized(n, s) \
83/* Return a malloc'ed copy of SRC. */ 75 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
84# define CLONE(Src) CCLONE (Src, 1) 76
85 77/* These macros are deprecated; they will go away soon, and are retained
78 temporarily only to ease conversion to the functions described above. */
79# define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
80# define CLONE(p) xclone (p, sizeof *(p))
81# define NEW(type, var) type *var = xmalloc (sizeof (type))
82# define XCALLOC(type, n) xcalloc (n, sizeof (type))
83# define XMALLOC(type, n) xnmalloc (n, sizeof (type))
84# define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
85# define XFREE(p) free (p)
86 86
87#endif /* !XALLOC_H_ */ 87#endif /* !XALLOC_H_ */