aboutsummaryrefslogtreecommitdiffstats
path: root/src/system.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-04-09 10:28:59 +0200
committerDavid Robillard <d@drobilla.net>2019-12-20 10:26:55 -0500
commit570c4da40a76ac9ec8fddfbab202b9290d7d44e5 (patch)
tree63a0d88b7b9a012f766cc05e5389610b793c8bc1 /src/system.c
parent7efe089be0ea0c130c7c12d598adda990b581053 (diff)
downloadserd-570c4da40a76ac9ec8fddfbab202b9290d7d44e5.tar.gz
serd-570c4da40a76ac9ec8fddfbab202b9290d7d44e5.tar.bz2
serd-570c4da40a76ac9ec8fddfbab202b9290d7d44e5.zip
Allocate nodes with posix_memalign when available
This fixes platforms where the system malloc returns memory that is not aligned to sizeof(SerdNode) (128 bits).
Diffstat (limited to 'src/system.c')
-rw-r--r--src/system.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/system.c b/src/system.c
index 1af179f5..4d8633ee 100644
--- a/src/system.c
+++ b/src/system.c
@@ -1,5 +1,5 @@
/*
- Copyright 2011-2018 David Robillard <http://drobilla.net>
+ Copyright 2011-2019 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -21,15 +21,36 @@
#include "serd_config.h"
#include <stdlib.h>
+#include <string.h>
void*
-serd_allocate_buffer(size_t size)
+serd_malloc_aligned(size_t size, size_t alignment)
{
#ifdef HAVE_POSIX_MEMALIGN
- void* ptr;
- const int ret = posix_memalign(&ptr, SERD_PAGE_SIZE, size);
+ void* ptr = NULL;
+ const int ret = posix_memalign(&ptr, alignment, size);
return ret ? NULL : ptr;
#else
return malloc(size);
#endif
}
+
+void*
+serd_calloc_aligned(size_t size, size_t alignment)
+{
+#ifdef HAVE_POSIX_MEMALIGN
+ void* ptr = serd_malloc_aligned(size, alignment);
+ if (ptr) {
+ memset(ptr, 0, size);
+ }
+ return ptr;
+#else
+ return calloc(1, size);
+#endif
+}
+
+void*
+serd_allocate_buffer(size_t size)
+{
+ return serd_malloc_aligned(size, SERD_PAGE_SIZE);
+}