diff options
author | Denis Kenzior <denkenz@gmail.com> | 2023-10-29 21:12:07 -0500 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2023-10-29 21:12:44 -0500 |
commit | 77eb67fd737abb0c46a6236591f2160c395a5313 (patch) | |
tree | b5c52651b5d408392c3aef7a57f120d7f470b945 | |
parent | a11b48eacf2ced1b408cd70fc72f41a4e9ffc122 (diff) |
util: Add l_newa for on-stack allocations
-rw-r--r-- | ell/util.h | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -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, ...) |