summaryrefslogtreecommitdiffstats
path: root/src/alloc.h
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-01 22:03:43 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-01 22:03:43 +0200
commit181715c5bc6e74f87fe284b063ca301a300ad098 (patch)
treed8fd0089df6521cb69d12971efba5d8e90c611cf /src/alloc.h
parent1a30018711528fe51fb17c70b8c1d9300c925c1c (diff)
downloadfastd-181715c5bc6e74f87fe284b063ca301a300ad098.tar
fastd-181715c5bc6e74f87fe284b063ca301a300ad098.zip
Add alloc helpers for aligned allocations
Diffstat (limited to 'src/alloc.h')
-rw-r--r--src/alloc.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/alloc.h b/src/alloc.h
index 7b3e93a..740ec24 100644
--- a/src/alloc.h
+++ b/src/alloc.h
@@ -49,6 +49,20 @@ static inline void * fastd_alloc(size_t size) {
}
/**
+ 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
Terminates the process on failure.
@@ -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)))