From 181715c5bc6e74f87fe284b063ca301a300ad098 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 1 Sep 2014 22:03:43 +0200 Subject: Add alloc helpers for aligned allocations --- src/alloc.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/alloc.h') diff --git a/src/alloc.h b/src/alloc.h index 7b3e93a..740ec24 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -48,6 +48,20 @@ static inline void * fastd_alloc(size_t size) { return ret; } +/** + Allocates a block of uninitialized memory on the heap, aligned to 16 bytes + + Terminates the process on failure. +*/ +static inline void * fastd_alloc_aligned(size_t size, size_t align) { + void *ret; + int err = posix_memalign(&ret, align, size); + if (err) + exit_error("posix_memalign: %s", strerror(err)); + + return ret; +} + /** Allocates a block of memory set to zero for an array on the heap @@ -87,6 +101,9 @@ static inline void * fastd_realloc(void *ptr, size_t size) { /** Allocates a block of uninitialized memory in the size of a given type */ #define fastd_new(type) ((type *)fastd_alloc(sizeof(type))) +/** Allocates a block of uninitialized memory in the size of a given type, aligned to 16 bytes */ +#define fastd_new_aligned(type, align) ((type *)fastd_alloc_aligned(sizeof(type), align)) + /** Allocates a block of memory set to zero in the size of a given type */ #define fastd_new0(type) ((type *)fastd_alloc0(sizeof(type))) -- cgit v1.2.3