summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-10-15 12:05:54 +0200
committerSven Eden <sven.eden@prydeworx.com>2018-10-29 10:18:39 +0100
commit36770c7d5854a17800583f24bf2ae69f5b68cb21 (patch)
treecd058e38f85bbde0f9444be74e16ccbbb338a6cd
parent11ddca4e536ec6d3ae6b216a8db7d196a4ca4a01 (diff)
alloc-util: add alloca() counterparts for memdup() and memdup_suffix0()
(cherry picked from commit 242c41b850af8ebc0c7a0cd44be057e50b5a027a)
-rw-r--r--src/basic/alloc-util.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index ebe42889e..2a6deb12c 100644
--- a/src/basic/alloc-util.h
+++ b/src/basic/alloc-util.h
@@ -46,6 +46,21 @@ static inline void *mfree(void *memory) {
void* memdup(const void *p, size_t l) _alloc_(2);
void* memdup_suffix0(const void *p, size_t l) _alloc_(2);
+#define memdupa(p, l) \
+ ({ \
+ void *_q_; \
+ _q_ = alloca(l); \
+ memcpy(_q_, p, l); \
+ })
+
+#define memdupa_suffix0(p, l) \
+ ({ \
+ void *_q_; \
+ _q_ = alloca(l + 1); \
+ ((uint8_t*) _q_)[l] = 0; \
+ memcpy(_q_, p, l); \
+ })
+
static inline void freep(void *p) {
free(*(void**) p);
}