diff options
author | David Robillard <d@drobilla.net> | 2021-09-14 20:35:30 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-09-14 23:55:44 -0400 |
commit | 939a47fb457128358b1c6553893be26b9b4fe060 (patch) | |
tree | bfd2c18db5add83dcd2c64c07b25638ec2c3bf09 /src/allocator.c | |
parent | c45ea80190de9745bcfaabb4858815a85d93c92a (diff) | |
download | zix-939a47fb457128358b1c6553893be26b9b4fe060.tar.gz zix-939a47fb457128358b1c6553893be26b9b4fe060.tar.bz2 zix-939a47fb457128358b1c6553893be26b9b4fe060.zip |
Add aligned allocation interface and use it in ZixBTree
Diffstat (limited to 'src/allocator.c')
-rw-r--r-- | src/allocator.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/allocator.c b/src/allocator.c index c998463..bedef47 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -1,7 +1,18 @@ // Copyright 2011-2021 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC +#define _POSIX_C_SOURCE 200809L + +#include "zix_config.h" + #include "zix/allocator.h" +#include "zix/attributes.h" + +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN 1 +# include <malloc.h> +# include <windows.h> +#endif #include <stdlib.h> @@ -39,6 +50,37 @@ zix_default_free(ZixAllocator* const allocator, void* const ptr) free(ptr); } +ZIX_MALLOC_FUNC +static void* +zix_default_aligned_alloc(ZixAllocator* const allocator, + const size_t alignment, + const size_t size) +{ + (void)allocator; + +#if defined(_WIN32) + return _aligned_malloc(size, alignment); +#elif USE_POSIX_MEMALIGN + void* ptr = NULL; + const int ret = posix_memalign(&ptr, alignment, size); + return ret ? NULL : ptr; +#else +# error No aligned memory allocation available +#endif +} + +static void +zix_default_aligned_free(ZixAllocator* const allocator, void* const ptr) +{ + (void)allocator; + +#if defined(_WIN32) + _aligned_free(ptr); +#else + free(ptr); +#endif +} + ZixAllocator* zix_default_allocator(void) { @@ -47,6 +89,8 @@ zix_default_allocator(void) zix_default_calloc, zix_default_realloc, zix_default_free, + zix_default_aligned_alloc, + zix_default_aligned_free, }; return &default_allocator; |