summaryrefslogtreecommitdiffstats
path: root/include/xalloc.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:33:34 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:33:34 +0000
commit1272be04be0cb803eec87f602edb2e3e6f111aea (patch)
treebce17f6478cdd9f3c4ec3d751135dc42786d6a56 /include/xalloc.h
parentReleasing progress-linux version 2.39.3-11~progress7.99u1. (diff)
downloadutil-linux-1272be04be0cb803eec87f602edb2e3e6f111aea.tar.xz
util-linux-1272be04be0cb803eec87f602edb2e3e6f111aea.zip
Merging upstream version 2.40.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'include/xalloc.h')
-rw-r--r--include/xalloc.h50
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))