1 /** \file xmalloc.h -- Declarations for the fail-on-OOM string functions */
9 #if defined(HAVE_VOIDPOINTER)
10 #define XMALLOCTYPE void
12 #define XMALLOCTYPE char
15 /** Allocate \a n characters of memory, abort program on failure. */
16 XMALLOCTYPE *xmalloc(size_t n);
18 /** Reallocate \a n characters of memory, abort program on failure. */
19 XMALLOCTYPE *xrealloc(/*@null@*/ XMALLOCTYPE *, size_t n);
21 /** Free memory at position \a p and set pointer \a p to NULL afterwards. */
22 #define xfree(p) { if (p) { free(p); } (p) = 0; }
24 /** Duplicate string \a src to a newly malloc()d memory region and return its
25 * pointer, abort program on failure. */
26 char *xstrdup(const char *src);
28 /** Duplicate at most the first \a n characters from \a src to a newly
29 * malloc()d memory region and NUL-terminate it, and return its pointer, abort
30 * program on failure. The memory size is the lesser of either the string
31 * length including NUL byte or n + 1. */
32 char *xstrndup(const char *src, size_t n);