commit dfeb089a5def8b30d5c8b8a485d7c473032d7c2c
parent 388ef950bf8ef5239c30e27c344ebb64592927f4
Author: sin <sin@2f30.org>
Date: Tue, 30 Jul 2013 10:51:46 +0100
Grab OpenBSD's memalign
Diffstat:
M | alloc.c | | | 23 | ++++++++++++++++++++--- |
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/alloc.c b/alloc.c
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
enum chunk_state {
FREE = 0,
@@ -149,8 +150,24 @@ malloc_size(void *p)
}
int
-posix_memalign(void **ptr, size_t alignment, size_t size)
+posix_memalign(void **memptr, size_t alignment, size_t size)
{
- fprintf(stderr, "%s: not implemented\n", __func__);
- abort();
+ void *result;
+
+ /* Make sure that alignment is a large enough power of 2. */
+ if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *))
+ return EINVAL;
+
+ /*
+ * max(size, alignment) is enough to assure the requested alignment,
+ * since the allocator always allocates power-of-two blocks.
+ */
+ if (size < alignment)
+ size = alignment;
+ result = malloc(size);
+ if (!result)
+ return ENOMEM;
+
+ *memptr = result;
+ return 0;
}