summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/alloc.c10
-rw-r--r--src/alloc.h5
2 files changed, 10 insertions, 5 deletions
diff --git a/src/alloc.c b/src/alloc.c
index ff3ec6d..97f90bc 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -14,7 +14,7 @@
static void *xmemalign(size_t align, size_t size) {
bfs_assert(has_single_bit(align));
bfs_assert(align >= sizeof(void *));
- bfs_assert((size & (align - 1)) == 0);
+ bfs_assert(is_aligned(align, size));
#if __APPLE__
void *ptr = NULL;
@@ -27,7 +27,7 @@ static void *xmemalign(size_t align, size_t size) {
void *alloc(size_t align, size_t size) {
bfs_assert(has_single_bit(align));
- bfs_assert((size & (align - 1)) == 0);
+ bfs_assert(is_aligned(align, size));
if (size >> (SIZE_WIDTH - 1)) {
errno = EOVERFLOW;
@@ -43,7 +43,7 @@ void *alloc(size_t align, size_t size) {
void *zalloc(size_t align, size_t size) {
bfs_assert(has_single_bit(align));
- bfs_assert((size & (align - 1)) == 0);
+ bfs_assert(is_aligned(align, size));
if (size >> (SIZE_WIDTH - 1)) {
errno = EOVERFLOW;
@@ -90,7 +90,7 @@ static void chunk_set_next(const struct arena *arena, union chunk *chunk, union
void arena_init(struct arena *arena, size_t align, size_t size) {
bfs_assert(has_single_bit(align));
- bfs_assert((size & (align - 1)) == 0);
+ bfs_assert(is_aligned(align, size));
if (align < alignof(union chunk)) {
align = alignof(union chunk);
@@ -98,7 +98,7 @@ void arena_init(struct arena *arena, size_t align, size_t size) {
if (size < sizeof(union chunk)) {
size = sizeof(union chunk);
}
- bfs_assert((size & (align - 1)) == 0);
+ bfs_assert(is_aligned(align, size));
arena->chunks = NULL;
arena->nslabs = 0;
diff --git a/src/alloc.h b/src/alloc.h
index 34f6949..15e4983 100644
--- a/src/alloc.h
+++ b/src/alloc.h
@@ -11,6 +11,11 @@
#include "config.h"
#include <stdlib.h>
+/** Check if a size is properly aligned. */
+static inline bool is_aligned(size_t align, size_t size) {
+ return (size & (align - 1)) == 0;
+}
+
/** Round down to a multiple of an alignment. */
static inline size_t align_floor(size_t align, size_t size) {
return size & ~(align - 1);