chiark / gitweb /
util: add alloca_align()
authorDavid Herrmann <dh.herrmann@gmail.com>
Mon, 22 Sep 2014 10:05:16 +0000 (12:05 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Mon, 22 Sep 2014 12:27:02 +0000 (14:27 +0200)
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
src/test/test-util.c

index 08d556fc924a47aabe4c86d0554c49f3f39a40b0..a1d5657237368803ef9c6ab8e781d3670ddff132 100644 (file)
@@ -852,6 +852,22 @@ int unlink_noerrno(const char *path);
                 (void *) memset(_new_, 0, _len_);       \
         })
 
                 (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);                            \
 #define strappenda(a, ...)                                       \
         ({                                                       \
                 int _len = strlen(a);                            \
index f8e42f3a550de6cd01ac2c0507100edd215b8ae6..1311184815e31d082fafe599acfec0a700feaf68 100644 (file)
@@ -131,6 +131,19 @@ static void test_container_of(void) {
                                v1) == &myval);
 }
 
                                v1) == &myval);
 }
 
+static void test_alloca(void) {
+        static const uint8_t zero[997] = { };
+        char *t;
+
+        t = alloca_align(17, 512);
+        assert_se(!((uintptr_t)t & 0xff));
+        memzero(t, 17);
+
+        t = alloca0_align(997, 1024);
+        assert_se(!((uintptr_t)t & 0x1ff));
+        assert_se(!memcmp(t, zero, 997));
+}
+
 static void test_first_word(void) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));
 static void test_first_word(void) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));
@@ -1272,6 +1285,7 @@ int main(int argc, char *argv[]) {
         test_align_power2();
         test_max();
         test_container_of();
         test_align_power2();
         test_max();
         test_container_of();
+        test_alloca();
         test_first_word();
         test_close_many();
         test_parse_boolean();
         test_first_word();
         test_close_many();
         test_parse_boolean();