From 55cd5e87c0996b68ae9d7d469f674e7fb9f4d4ea Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Tue, 21 Sep 2021 12:23:10 +0100 Subject: [PATCH] NFC: Add FreeObject::from_next_ptr Rather than open-code the conversion from &f->next_object to f, add a static method to FreeObject and take the opportunity to add a static_assert that our reinterpret_cast is sound. --- src/mem/freelist.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/mem/freelist.h b/src/mem/freelist.h index 0b882a3..9dd8d9b 100644 --- a/src/mem/freelist.h +++ b/src/mem/freelist.h @@ -184,6 +184,17 @@ namespace snmalloc return p.template as_static>(); } + /** + * A container-of operation to convert &f->next_object to f + */ + template + static FreeObject::T* + from_next_ptr(CapPtr, BQueue>* ptr) + { + static_assert(offsetof(FreeObject::T, next_object) == 0); + return reinterpret_cast*>(ptr); + } + private: /** * Involutive encryption with raw pointers @@ -655,7 +666,7 @@ namespace snmalloc // to the actual object. This isn't true if the builder is // empty, but you are not allowed to call this in the empty case. auto last = FreeObject::HeadPtr( - reinterpret_cast*>(end[0])); + FreeObject::from_next_ptr(cast_end(0))); init(); return {first, last}; }