summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-04-06 14:52:50 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-14 16:08:12 -0400
commitd83553b4a2e942f410d56d92bef34def9424a494 (patch)
tree52c04b89b19eabc73cec2dd356784499eda613c1 /src
parent29719ace5192ff5c2d81c29d1947e32d7889f62b (diff)
downloadbfs-d83553b4a2e942f410d56d92bef34def9424a494.tar.xz
bfstd: Add an aligned_alloc()/posix_memalign() wrapper
Diffstat (limited to 'src')
-rw-r--r--src/bfstd.c14
-rw-r--r--src/bfstd.h12
2 files changed, 26 insertions, 0 deletions
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);
diff --git a/src/bfstd.h b/src/bfstd.h
index e4fd1f1..ee4cf16 100644
--- a/src/bfstd.h
+++ b/src/bfstd.h
@@ -106,6 +106,18 @@ char *xgetdelim(FILE *file, char delim);
// #include <stdlib.h>
/**
+ * Portable version of aligned_alloc()/posix_memalign().
+ *
+ * @param align
+ * The allocation's alignment.
+ * @param size
+ * The allocation's size.
+ * @return
+ * The allocation, or NULL on failure.
+ */
+void *xmemalign(size_t align, size_t size);
+
+/**
* Process a yes/no prompt.
*
* @return 1 for yes, 0 for no, and -1 for unknown.