diff options
Diffstat (limited to '')
-rw-r--r-- | include/xalloc.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/include/xalloc.h b/include/xalloc.h index 1dee5fe..f7d42c9 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -17,6 +17,7 @@ #include <string.h> #include "c.h" +#include "strutils.h" #ifndef XALLOC_EXIT_CODE # define XALLOC_EXIT_CODE EXIT_FAILURE @@ -47,6 +48,18 @@ void *xrealloc(void *ptr, const size_t size) } static inline +__ul_calloc_size(2, 3) +__ul_returns_nonnull +void *xreallocarray(void *ptr, const size_t nelems, const size_t size) +{ + void *ret = reallocarray(ptr, nelems, size); + + if (!ret && size && nelems) + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); + return ret; +} + +static inline __ul_calloc_size(1, 2) __ul_returns_nonnull void *xcalloc(const size_t nelems, const size_t size) @@ -113,6 +126,43 @@ int xvasprintf(char **strp, const char *fmt, va_list ap) return ret; } +static inline void xstrappend(char **a, const char *b) +{ + if (strappend(a, b) < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); +} + +static inline void xstrputc(char **a, char c) +{ + char b[] = {c, '\0'}; + xstrappend(a, b); +} + +static inline +__attribute__((__format__(printf, 2, 0))) +int xstrvfappend(char **a, const char *format, va_list ap) +{ + int ret = strvfappend(a, format, ap); + + if (ret < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); + return ret; + +} + +static inline +__attribute__ ((__format__ (__printf__, 2, 3))) +int xstrfappend(char **a, const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = xstrvfappend(a, format, ap); + va_end(ap); + + return ret; +} static inline __attribute__((warn_unused_result)) |