diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:33:32 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:33:32 +0000 |
commit | 8bb05ac73a5b448b339ce0bc8d396c82c459b47f (patch) | |
tree | 1fdda006866bca20d41cb206767ea5241e36852f /include/xalloc.h | |
parent | Adding debian version 2.39.3-11. (diff) | |
download | util-linux-8bb05ac73a5b448b339ce0bc8d396c82c459b47f.tar.xz util-linux-8bb05ac73a5b448b339ce0bc8d396c82c459b47f.zip |
Merging upstream version 2.40.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
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)) |