From 2d831920ea5b700249774eaea41419258b5eece3 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Thu, 25 Apr 2019 12:12:23 +0100 Subject: [PATCH] Factored casting in external pointer calculation --- src/mem/alloc.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/mem/alloc.h b/src/mem/alloc.h index b7748db..70d6f97 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -480,7 +480,7 @@ namespace snmalloc } template - static void* external_pointer(void* p) + static uintptr_t external_uintptr(void* p) { #ifdef USE_MALLOC error("Unsupported"); @@ -522,7 +522,7 @@ namespace snmalloc { if constexpr ((location == End) || (location == OnePastEnd)) // We don't know the End, so return MAX_PTR - return (void*)-1; + return (uintptr_t)~0; else // We don't know the Start, so return MIN_PTR return 0; @@ -530,14 +530,20 @@ namespace snmalloc // This is a large alloc, mask off to the slab size. if constexpr (location == Start) - return (void*)ss; + return ss; else if constexpr (location == End) - return (void*)((size_t)ss + (1ULL << size) - 1ULL); + return (ss + (1ULL << size) - 1ULL); else - return (void*)((size_t)ss + (1ULL << size)); + return (ss + (1ULL << size)); #endif } + template + static void* external_pointer(void* p) + { + return (void*)external_uintptr(p); + } + static size_t alloc_size(void* p) { // This must be called on an external pointer. @@ -786,7 +792,7 @@ namespace snmalloc } template - static void* external_pointer(void* p, uint8_t sizeclass, size_t end_point) + static uintptr_t external_pointer(void* p, uint8_t sizeclass, size_t end_point) { size_t rsize = sizeclass_to_size(sizeclass); size_t end_point_correction = location == End ? @@ -794,7 +800,7 @@ namespace snmalloc (location == OnePastEnd ? end_point : (end_point - rsize)); size_t offset_from_end = (end_point - 1) - (size_t)p; size_t end_to_end = round_by_sizeclass(rsize, offset_from_end); - return (void*)(end_point_correction - end_to_end); + return end_point_correction - end_to_end; } void init_message_queue()