diff --git a/src/ds/cdllist.h b/src/ds/cdllist.h index 3b7e146..db52efc 100644 --- a/src/ds/cdllist.h +++ b/src/ds/cdllist.h @@ -1,13 +1,14 @@ #pragma once #include "defines.h" +#include "ptrwrap.h" #include #include namespace snmalloc { - template + template typename Ptr = Pointer> class CDLLNodeBase { /** @@ -18,9 +19,9 @@ namespace snmalloc ptrdiff_t to_next = 0; protected: - void set_next(T* c) + void set_next(Ptr c) { - to_next = pointer_diff_signed(this, c); + to_next = pointer_diff_signed(Ptr>(this), c); } public: @@ -29,13 +30,13 @@ namespace snmalloc return to_next == 0; } - SNMALLOC_FAST_PATH T* get_next() + SNMALLOC_FAST_PATH Ptr get_next() { - return pointer_offset_signed(this, to_next); + return static_cast>(pointer_offset_signed(this, to_next)); } }; - template + template typename Ptr = Pointer> class CDLLNodeBaseNext { /** @@ -45,12 +46,12 @@ namespace snmalloc * */ - T* next = nullptr; + Ptr next = nullptr; protected: - void set_next(T* c) + void set_next(Ptr c) { - next = (c == static_cast(this)) ? nullptr : c; + next = (c == static_cast>(this)) ? nullptr : c; } public: @@ -59,17 +60,17 @@ namespace snmalloc return next == nullptr; } - SNMALLOC_FAST_PATH T* get_next() + SNMALLOC_FAST_PATH Ptr get_next() { - return next == nullptr ? static_cast(this) : next; + return next == nullptr ? static_cast>(this) : next; } }; - template + template typename Ptr = Pointer> using CDLLNodeParent = std::conditional_t< aal_supports, - CDLLNodeBaseNext, - CDLLNodeBase>; + CDLLNodeBaseNext, + CDLLNodeBase>; /** * Special class for cyclic doubly linked non-empty linked list @@ -77,9 +78,10 @@ namespace snmalloc * This code assumes there is always one element in the list. The client * must ensure there is a sentinal element. */ - class CDLLNode : public CDLLNodeParent + template typename Ptr = Pointer> + class CDLLNode : public CDLLNodeParent, Ptr> { - CDLLNode* prev = nullptr; + Ptr prev = nullptr; public: /** @@ -87,8 +89,8 @@ namespace snmalloc */ CDLLNode() { - this->set_next(this); - prev = this; + this->set_next(Ptr(this)); + prev = Ptr(this); } /** @@ -98,14 +100,14 @@ namespace snmalloc { SNMALLOC_ASSERT(!this->is_empty()); debug_check(); - get_next()->prev = prev; - prev->set_next(get_next()); + this->get_next()->prev = prev; + prev->set_next(this->get_next()); // As this is no longer in the list, check invariant for // neighbouring element. - get_next()->debug_check(); + this->get_next()->debug_check(); #ifndef NDEBUG - set_next(nullptr); + this->set_next(nullptr); prev = nullptr; #endif } @@ -121,27 +123,27 @@ namespace snmalloc prev = nullptr; } - SNMALLOC_FAST_PATH CDLLNode* get_prev() + SNMALLOC_FAST_PATH Ptr get_prev() { return prev; } - SNMALLOC_FAST_PATH void insert_next(CDLLNode* item) + SNMALLOC_FAST_PATH void insert_next(Ptr item) { debug_check(); - item->set_next(get_next()); - get_next()->prev = item; + item->set_next(this->get_next()); + this->get_next()->prev = item; item->prev = this; set_next(item); debug_check(); } - SNMALLOC_FAST_PATH void insert_prev(CDLLNode* item) + SNMALLOC_FAST_PATH void insert_prev(Ptr item) { debug_check(); item->prev = prev; prev->set_next(item); - item->set_next(this); + item->set_next(Ptr(this)); prev = item; debug_check(); } @@ -154,15 +156,15 @@ namespace snmalloc void debug_check() { #ifndef NDEBUG - CDLLNode* item = get_next(); - CDLLNode* p = this; + Ptr item = this->get_next(); + auto p = Ptr(this); do { SNMALLOC_ASSERT(item->prev == p); p = item; item = item->get_next(); - } while (item != this); + } while (item != Ptr(this)); #endif } }; diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index ac4f8b3..04a1d38 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -27,8 +27,8 @@ namespace snmalloc SlabNext* value = nullptr; }; - using SlabList = CDLLNode; - using SlabLink = CDLLNode; + using SlabList = CDLLNode<>; + using SlabLink = CDLLNode<>; SNMALLOC_FAST_PATH Slab* get_slab(SlabLink* sl) {