48 |
memory allocation failure. */ |
memory allocation failure. */ |
49 |
extern void xalloc_die (void) ATTRIBUTE_NORETURN; |
extern void xalloc_die (void) ATTRIBUTE_NORETURN; |
50 |
|
|
51 |
void *xmalloc (size_t n); |
void *xmalloc (size_t s); |
52 |
|
void *xnmalloc (size_t n, size_t s); |
53 |
|
void *xzalloc (size_t s); |
54 |
void *xcalloc (size_t n, size_t s); |
void *xcalloc (size_t n, size_t s); |
55 |
void *xrealloc (void *p, size_t n); |
void *xrealloc (void *p, size_t s); |
56 |
|
void *xnrealloc (void *p, size_t n, size_t s); |
57 |
|
void *x2realloc (void *p, size_t *pn); |
58 |
|
void *x2nrealloc (void *p, size_t *pn, size_t s); |
59 |
|
void *xclone (void const *p, size_t s); |
60 |
char *xstrdup (const char *str); |
char *xstrdup (const char *str); |
61 |
|
|
62 |
# define XMALLOC(Type, N_items) xmalloc (sizeof (Type) * (N_items)) |
/* Return 1 if an array of N objects, each of size S, cannot exist due |
63 |
# define XCALLOC(Type, N_items) xcalloc (sizeof (Type), N_items) |
to size arithmetic overflow. S must be positive and N must be |
64 |
# define XREALLOC(Ptr, Type, N_items) xrealloc (Ptr, sizeof (Type) * (N_items)) |
nonnegative. This is a macro, not an inline function, so that it |
65 |
|
works correctly even when SIZE_MAX < N. |
66 |
/* Declare and alloc memory for VAR of type TYPE. */ |
|
67 |
# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) |
By gnulib convention, SIZE_MAX represents overflow in size |
68 |
|
calculations, so the conservative dividend to use here is |
69 |
/* Free VAR only if non NULL. */ |
SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. |
70 |
# define XFREE(Var) \ |
However, malloc (SIZE_MAX) fails on all known hosts where |
71 |
do { \ |
sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for |
72 |
if (Var) \ |
exactly-SIZE_MAX allocations on such hosts; this avoids a test and |
73 |
free (Var); \ |
branch when S is known to be 1. */ |
74 |
} while (0) |
# define xalloc_oversized(n, s) \ |
75 |
|
((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) |
76 |
/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ |
|
77 |
# define CCLONE(Src, Num) \ |
/* These macros are deprecated; they will go away soon, and are retained |
78 |
(memcpy (xmalloc (sizeof *(Src) * (Num)), Src, sizeof *(Src) * (Num))) |
temporarily only to ease conversion to the functions described above. */ |
79 |
|
# define CCLONE(p, n) xclone (p, (n) * sizeof *(p)) |
80 |
/* Return a malloc'ed copy of SRC. */ |
# define CLONE(p) xclone (p, sizeof *(p)) |
81 |
# define CLONE(Src) CCLONE (Src, 1) |
# 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 |
|
|
87 |
#endif /* !XALLOC_H_ */ |
#endif /* !XALLOC_H_ */ |