From bed9b9171cad11e3a8dd0dea90856779a683a9a5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 30 Oct 2024 13:36:49 -0400 Subject: alloc: Add macro versions of alignment utils --- src/alloc.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') 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 #include +#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); } /** -- cgit v1.2.3