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