From ef7985510cbf594c087de6702600069e5a4a65cc Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 7 May 2019 13:21:02 +0100 Subject: [PATCH] address.h: add pointer alignment functions More explicit than address_cast and bitwise and --- src/ds/address.h | 31 +++++++++++++++++++++++++++++++ src/mem/largealloc.h | 10 +++++----- src/mem/mediumslab.h | 2 +- src/mem/metaslab.h | 2 +- src/mem/pagemap.h | 4 +--- src/mem/superslab.h | 2 +- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/ds/address.h b/src/ds/address.h index 34c8aa9..75c2e09 100644 --- a/src/ds/address.h +++ b/src/ds/address.h @@ -56,4 +56,35 @@ namespace snmalloc 0; } + /** + * Align a pointer down to a statically specified granularity, which must be a + * power of two. + */ + template + inline T* pointer_align_down(void* p) + { + static_assert(alignment > 0); + static_assert(bits::next_pow2_const(alignment) == alignment); +#if __has_builtin(__builtin_align_down) + return static_cast(__builtin_align_down(p, alignment)); +#else + return pointer_cast(bits::align_down(address_cast(p), alignment)); +#endif + } + + /** + * Align a pointer up to a statically specified granularity, which must be a + * power of two. + */ + template + inline T* pointer_align_up(void* p) + { + static_assert(alignment > 0); + static_assert(bits::next_pow2_const(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 + } } // namespace snmalloc diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index 80116d7..a0301ca 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -166,11 +166,12 @@ namespace snmalloc remaining -= size; } - auto page_start = bits::align_down(address_cast(p), OS_PAGE_SIZE); - auto page_end = bits::align_up(address_cast(p) + size, OS_PAGE_SIZE); + auto page_start = pointer_align_down(p); + auto page_end = + pointer_align_up(pointer_offset(p, size)); PAL::template notify_using( - pointer_cast(page_start), page_end - page_start); + page_start, static_cast(page_end - page_start)); return new (p) T(std::forward(args)...); } @@ -295,8 +296,7 @@ namespace snmalloc reserved_start = memory_provider.template reserve(&add, SUPERSLAB_SIZE); reserved_end = pointer_offset(reserved_start, add); - reserved_start = pointer_cast( - bits::align_up(address_cast(reserved_start), SUPERSLAB_SIZE)); + reserved_start = pointer_align_up(reserved_start); if (add < need) return false; diff --git a/src/mem/mediumslab.h b/src/mem/mediumslab.h index ed5928e..826f0fa 100644 --- a/src/mem/mediumslab.h +++ b/src/mem/mediumslab.h @@ -41,7 +41,7 @@ namespace snmalloc static Mediumslab* get(void* p) { - return pointer_cast(address_cast(p) & SUPERSLAB_MASK); + return pointer_align_down(p); } void init(RemoteAllocator* alloc, sizeclass_t sc, size_t rsize) diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index f96ff76..f39caa0 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -15,7 +15,7 @@ namespace snmalloc Slab* get_slab() { - return pointer_cast(address_cast(this) & SLAB_MASK); + return pointer_align_down(this); } }; diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 0de4d93..8526c09 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -273,9 +273,7 @@ namespace snmalloc void* page_for_address(uintptr_t p) { bool success; - return reinterpret_cast( - ~(OS_PAGE_SIZE - 1) & - reinterpret_cast(get_addr(p, success))); + return pointer_align_down(get_addr(p, success)); } T get(uintptr_t p) diff --git a/src/mem/superslab.h b/src/mem/superslab.h index c77e57f..9a9d13c 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -67,7 +67,7 @@ namespace snmalloc static Superslab* get(void* p) { - return pointer_cast(address_cast(p) & SUPERSLAB_MASK); + return pointer_align_down(p); } static bool is_short_sizeclass(sizeclass_t sizeclass)