diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-11-22 16:52:33 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-11-23 13:56:03 -0500 |
commit | 3fe1e7ca64bcd3724b00cf399ed9ae6f60e0a008 (patch) | |
tree | 75bb5d7ecbbc72edcd88e3d6c19c3e05be848957 | |
parent | b9ed3f51d636bb0f182b0cc8a86ebf928b37f8c5 (diff) | |
download | bfs-3fe1e7ca64bcd3724b00cf399ed9ae6f60e0a008.tar.xz |
alloc: New is_aligned() helper
-rw-r--r-- | src/alloc.c | 10 | ||||
-rw-r--r-- | src/alloc.h | 5 |
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); |