aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Kenzior <denkenz@gmail.com>2023-10-29 21:12:07 -0500
committerDenis Kenzior <denkenz@gmail.com>2023-10-29 21:12:44 -0500
commit77eb67fd737abb0c46a6236591f2160c395a5313 (patch)
treeb5c52651b5d408392c3aef7a57f120d7f470b945
parenta11b48eacf2ced1b408cd70fc72f41a4e9ffc122 (diff)
util: Add l_newa for on-stack allocations
-rw-r--r--ell/util.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/ell/util.h b/ell/util.h
index 2e4bc8f3..5dfc35c8 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -252,6 +252,26 @@ static inline void auto_free(void *a)
__p; \
}))
+/**
+ * l_newa:
+ * @type: type of structure
+ * @count: amount of structures
+ *
+ * Allocates stack space for @count structures of @type. Memory is allocated
+ * using alloca and initialized to 0.
+ *
+ * Returns: Pointer to memory allocated on the stack.
+ */
+#define l_newa(type, count) \
+ (type *) (__extension__ ({ \
+ size_t __n = (size_t) (count); \
+ size_t __s = sizeof(type); \
+ void *__p; \
+ __p = alloca(__n * __s); \
+ memset(__p, 0, __n * __s); \
+ __p; \
+ }))
+
char *l_strdup(const char *str);
char *l_strndup(const char *str, size_t max);
char *l_strdup_printf(const char *format, ...)