Extra helper functions for dllist (#139)

* More helpful functions for dllist
This commit is contained in:
Alex
2020-03-11 10:07:29 +00:00
committed by GitHub
parent 76eaf1adad
commit beb99ddc38

View File

@@ -53,8 +53,11 @@ namespace snmalloc
}
};
template<class T, class Terminator = std::nullptr_t>
class DLList
template<
class T,
class Terminator = std::nullptr_t,
bool delete_on_clear = false>
class DLList final
{
private:
static_assert(
@@ -66,6 +69,32 @@ namespace snmalloc
T* tail = Terminator();
public:
~DLList()
{
clear();
}
DLList() = default;
DLList(DLList&& o) noexcept
{
head = o.head;
tail = o.tail;
o.head = nullptr;
o.tail = nullptr;
}
DLList& operator=(DLList&& o) noexcept
{
head = o.head;
tail = o.tail;
o.head = nullptr;
o.tail = nullptr;
return *this;
}
bool is_empty()
{
return head == Terminator();
@@ -76,6 +105,11 @@ namespace snmalloc
return head;
}
T* get_tail()
{
return tail;
}
T* pop()
{
T* item = head;
@@ -86,6 +120,16 @@ namespace snmalloc
return item;
}
T* pop_tail()
{
T* item = tail;
if (item != Terminator())
remove(item);
return item;
}
void insert(T* item)
{
#ifndef NDEBUG
@@ -147,6 +191,19 @@ namespace snmalloc
#endif
}
void clear()
{
while (head != nullptr)
{
auto c = head;
remove(c);
if (delete_on_clear)
{
delete c;
}
}
}
void debug_check_contains(T* item)
{
#ifndef NDEBUG