From d83553b4a2e942f410d56d92bef34def9424a494 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 6 Apr 2023 14:52:50 -0400 Subject: bfstd: Add an aligned_alloc()/posix_memalign() wrapper --- src/bfstd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/bfstd.c') diff --git a/src/bfstd.c b/src/bfstd.c index 37f5276..4e1175f 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: 0BSD #include "bfstd.h" +#include "bit.h" #include "config.h" #include "diag.h" #include "xregex.h" @@ -142,6 +143,19 @@ char *xgetdelim(FILE *file, char delim) { } } +void *xmemalign(size_t align, size_t size) { + bfs_assert(has_single_bit(align)); + bfs_assert((size & (align - 1)) == 0); + +#if __APPLE__ + void *ptr = NULL; + errno = posix_memalign(&ptr, align, size); + return ptr; +#else + return aligned_alloc(align, size); +#endif +} + /** Compile and execute a regular expression for xrpmatch(). */ static int xrpregex(nl_item item, const char *response) { const char *pattern = nl_langinfo(item); -- cgit v1.2.3