From 263d735d0cdd041f00f1e7abf7d091e62f138d40 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Sun, 12 May 2019 20:15:46 +0100 Subject: [PATCH] ds/address: add dynamic pointer_align_up --- src/ds/address.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ds/address.h b/src/ds/address.h index 66e0fcd..4b61e50 100644 --- a/src/ds/address.h +++ b/src/ds/address.h @@ -88,6 +88,22 @@ namespace snmalloc #endif } + /** + * Align a pointer up to a dynamically specified granularity, which must + * be a power of two. + */ + template + inline T* pointer_align_up(void* p, size_t alignment) + { + assert(alignment > 0); + assert(bits::next_pow2(alignment) == alignment); +#if __has_builtin(__builtin_align_up) + return static_cast(__builtin_align_up(p, alignment)); +#else + return pointer_cast(bits::align_up(address_cast(p), alignment)); +#endif + } + /** * Compute the difference in pointers in units of char. base is * expected to point to the base of some (sub)allocation into which cursor