From 95d78c7e7c81a6b788f28c33ef2cafd87471a0d7 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Mon, 22 Sep 2014 12:05:16 +0200 Subject: util: add alloca_align() The alloca_align() helper is the alloca() equivalent of posix_memalign(). As there is no such function provided by glibc, we simply account for additional memory and return a pointer offset into the allocated memory to grant the alignment. Furthermore, alloca0_align() is added, which simply clears the allocated memory. --- src/shared/util.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/shared/util.h') diff --git a/src/shared/util.h b/src/shared/util.h index 08d556fc92..a1d5657237 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -852,6 +852,22 @@ int unlink_noerrno(const char *path); (void *) memset(_new_, 0, _len_); \ }) +#define alloca_align(size, align) \ + ({ \ + void *_ptr_; \ + size_t _mask_ = (align) - 1; \ + _ptr_ = alloca((size) + _mask_); \ + (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ + }) + +#define alloca0_align(size, align) \ + ({ \ + void *_new_; \ + size_t _size_ = (size); \ + _new_ = alloca_align(_size_, (align)); \ + (void*)memset(_new_, 0, _size_); \ + }) + #define strappenda(a, ...) \ ({ \ int _len = strlen(a); \ -- cgit v1.2.3-54-g00ecf