diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2024-10-30 13:36:49 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2024-11-02 11:25:10 -0400 |
commit | bed9b9171cad11e3a8dd0dea90856779a683a9a5 (patch) | |
tree | b3305ccd99da8fd19e25ba0f648e74f1b0183d0f | |
parent | 789e19f9dca87780aff3485d4a120d8b4ddedfb1 (diff) | |
download | bfs-bed9b9171cad11e3a8dd0dea90856779a683a9a5.tar.xz |
alloc: Add macro versions of alignment utils
-rw-r--r-- | src/alloc.h | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/alloc.h b/src/alloc.h index 7865d5d..34d9273 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -14,19 +14,28 @@ #include <stddef.h> #include <stdlib.h> +#define IS_ALIGNED(align, size) \ + (((size) & ((align) - 1)) == 0) + /** Check if a size is properly aligned. */ static inline bool is_aligned(size_t align, size_t size) { - return (size & (align - 1)) == 0; + return IS_ALIGNED(align, size); } +#define ALIGN_FLOOR(align, size) \ + ((size) & ~((align) - 1)) + /** Round down to a multiple of an alignment. */ static inline size_t align_floor(size_t align, size_t size) { - return size & ~(align - 1); + return ALIGN_FLOOR(align, size); } +#define ALIGN_CEIL(align, size) \ + ((((size) - 1) | ((align) - 1)) + 1) + /** Round up to a multiple of an alignment. */ static inline size_t align_ceil(size_t align, size_t size) { - return align_floor(align, size + align - 1); + return ALIGN_CEIL(align, size); } /** |