diff --git a/README.md b/README.md index 7f5be76..c496edc 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ your system. The PAL must implement the following methods: ```c++ -void error(const char* const str) noexcept; +[[noreturn]] void error(const char* const str) noexcept; ``` Report a fatal error and exit. diff --git a/src/ds/defines.h b/src/ds/defines.h index f22dd64..7b97359 100644 --- a/src/ds/defines.h +++ b/src/ds/defines.h @@ -37,7 +37,7 @@ namespace snmalloc { // Forwards reference so that the platform can define how to handle errors. - void error(const char* const str); + [[noreturn]] void error(const char* const str); } // namespace snmalloc #define TOSTRING(expr) TOSTRING2(expr) diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 24b9e13..b238bc2 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -453,16 +453,19 @@ namespace snmalloc #endif } - static size_t alloc_size(const void* p) + private: + SNMALLOC_SLOW_PATH static size_t alloc_size_error() + { + error("Not allocated by this allocator"); + } + + public: + SNMALLOC_FAST_PATH static size_t alloc_size(const void* p) { // This must be called on an external pointer. size_t size = ChunkMap::get(address_cast(p)); - if (size == 0) - { - error("Not allocated by this allocator"); - } - else if (size == CMSuperslab) + if (likely(size == CMSuperslab)) { Superslab* super = Superslab::get(p); @@ -473,7 +476,8 @@ namespace snmalloc return sizeclass_to_size(meta.sizeclass); } - else if (size == CMMediumslab) + + if (likely(size == CMMediumslab)) { Mediumslab* slab = Mediumslab::get(p); // Reading a remote sizeclass won't fail, since the other allocator @@ -481,7 +485,12 @@ namespace snmalloc return sizeclass_to_size(slab->get_sizeclass()); } - return 1ULL << size; + if (likely(size != 0)) + { + return 1ULL << size; + } + + return alloc_size_error(); } size_t get_id() diff --git a/src/pal/pal.h b/src/pal/pal.h index 53faecb..ed5ea11 100644 --- a/src/pal/pal.h +++ b/src/pal/pal.h @@ -49,7 +49,7 @@ namespace snmalloc DefaultPal; #endif - SNMALLOC_SLOW_PATH inline void error(const char* const str) + [[noreturn]] SNMALLOC_SLOW_PATH inline void error(const char* const str) { Pal::error(str); } diff --git a/src/pal/pal_freebsd_kernel.h b/src/pal/pal_freebsd_kernel.h index 914b736..6327942 100644 --- a/src/pal/pal_freebsd_kernel.h +++ b/src/pal/pal_freebsd_kernel.h @@ -29,7 +29,7 @@ namespace snmalloc * PAL supports. */ static constexpr uint64_t pal_features = AlignedAllocation; - void error(const char* const str) + [[noreturn]] void error(const char* const str) { panic("snmalloc error: %s", str); } diff --git a/src/pal/pal_open_enclave.h b/src/pal/pal_open_enclave.h index 58de146..25983a4 100644 --- a/src/pal/pal_open_enclave.h +++ b/src/pal/pal_open_enclave.h @@ -5,7 +5,7 @@ extern "C" const void* __oe_get_heap_base(); extern "C" const void* __oe_get_heap_end(); extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size); -extern "C" void oe_abort(); +extern "C" [[noreturn]] void oe_abort(); namespace snmalloc { @@ -19,7 +19,7 @@ namespace snmalloc * PAL supports. */ static constexpr uint64_t pal_features = 0; - static void error(const char* const str) + [[noreturn]] static void error(const char* const str) { UNUSED(str); oe_abort(); diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 260bd3d..e1bef12 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -55,7 +55,7 @@ namespace snmalloc /** * Report a fatal error an exit. */ - static void error(const char* const str) noexcept + [[noreturn]] static void error(const char* const str) noexcept { puts(str); print_stack_trace(); diff --git a/src/pal/pal_windows.h b/src/pal/pal_windows.h index ba7fcea..e325d3a 100644 --- a/src/pal/pal_windows.h +++ b/src/pal/pal_windows.h @@ -105,7 +105,7 @@ namespace snmalloc low_memory_callbacks.register_notification(callback); } - static void error(const char* const str) + [[noreturn]] static void error(const char* const str) { puts(str); fflush(stdout);