diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 82005df..b008e61 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -394,7 +394,7 @@ namespace snmalloc } template - static address_t external_address(void* p) + static void* external_pointer(void* p) { #ifdef USE_MALLOC error("Unsupported"); @@ -423,12 +423,13 @@ namespace snmalloc return external_pointer(p, sc, slab_end); } - auto ss = address_cast(super); + auto ss = super; while (size > 64) { // This is a large alloc redirect. - ss = ss - (1ULL << (size - 64)); + ss = pointer_offset_signed( + ss, -(static_cast(1) << (size - 64))); size = ChunkMap::get(ss); } @@ -436,28 +437,22 @@ namespace snmalloc { if constexpr ((location == End) || (location == OnePastEnd)) // We don't know the End, so return MAX_PTR - return UINTPTR_MAX; + return pointer_offset(nullptr, UINTPTR_MAX); else // We don't know the Start, so return MIN_PTR - return 0; + return nullptr; } // This is a large alloc, mask off to the slab size. if constexpr (location == Start) return ss; else if constexpr (location == End) - return (ss + (1ULL << size) - 1ULL); + return pointer_offset(ss, (1ULL << size) - 1ULL); else - return (ss + (1ULL << size)); + return pointer_offset(ss, 1ULL << size); #endif } - template - static void* external_pointer(void* p) - { - return pointer_cast(external_address(p)); - } - static size_t alloc_size(const void* p) { // This must be called on an external pointer. @@ -824,24 +819,24 @@ namespace snmalloc } template - static uintptr_t + static void* external_pointer(void* p, sizeclass_t sizeclass, void* end_point) { size_t rsize = sizeclass_to_size(sizeclass); void* end_point_correction = location == End ? - (static_cast(end_point) - 1) : - (location == OnePastEnd ? end_point : - (static_cast(end_point) - rsize)); + pointer_offset_signed(end_point, -1) : + (location == OnePastEnd ? + end_point : + pointer_offset_signed(end_point, -static_cast(rsize))); - ptrdiff_t offset_from_end = - (static_cast(end_point) - 1) - static_cast(p); + size_t offset_from_end = + pointer_diff(p, pointer_offset_signed(end_point, -1)); - size_t end_to_end = - round_by_sizeclass(rsize, static_cast(offset_from_end)); + size_t end_to_end = round_by_sizeclass(rsize, offset_from_end); - return address_cast( - static_cast(end_point_correction) - end_to_end); + return pointer_offset_signed( + end_point_correction, -static_cast(end_to_end)); } void init_message_queue()