From c2f914ddfa09f8fa1b2f93eb264b536ac6a85699 Mon Sep 17 00:00:00 2001 From: JP Cimalando Date: Sat, 10 Nov 2018 19:32:51 +0100 Subject: support zero-alloc + fix --- src/structures/pl_list.hpp | 7 ++++++- src/structures/pl_list.tcc | 23 ++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'src/structures') diff --git a/src/structures/pl_list.hpp b/src/structures/pl_list.hpp index 327e259..0cbd233 100644 --- a/src/structures/pl_list.hpp +++ b/src/structures/pl_list.hpp @@ -73,6 +73,9 @@ public: pl_list(std::size_t capacity = 0); ~pl_list(); + struct external_storage_policy {}; + pl_list(pl_cell *cells, std::size_t ncells, external_storage_policy); + pl_list(const pl_list &other); pl_list &operator=(const pl_list &other); @@ -117,8 +120,10 @@ private: pl_cell *free_; // value-less cell which terminates the linked list pl_basic_cell endcell_; + // whether cell storage is allocated + bool cells_allocd_; - void initialize(std::size_t capacity); + void initialize(std::size_t capacity, pl_cell *extcells = NULL); pl_cell *allocate(pl_cell *pos); void deallocate(pl_cell *cell); }; diff --git a/src/structures/pl_list.tcc b/src/structures/pl_list.tcc index 37ccc75..959b085 100644 --- a/src/structures/pl_list.tcc +++ b/src/structures/pl_list.tcc @@ -80,7 +80,14 @@ pl_list::pl_list(std::size_t capacity) template pl_list::~pl_list() { - delete[] cells_; + if (cells_allocd_) + delete[] cells_; +} + +template +pl_list::pl_list(pl_cell *cells, std::size_t ncells, external_storage_policy) +{ + initialize(ncells, cells); } template @@ -97,14 +104,15 @@ pl_list &pl_list::operator=(const pl_list &other) if(this != &other) { std::size_t size = other.size(); - if(size <= capacity()) - clear(); - else + if(size > capacity()) { pl_cell *oldcells = cells_; + bool allocd = cells_allocd_; initialize(other.capacity()); - delete[] oldcells; + if (allocd) + delete[] oldcells; } + clear(); for(const_iterator i = other.end(), b = other.begin(); i-- != b;) push_front(i->value); } @@ -280,9 +288,10 @@ typename pl_list::const_iterator pl_list::find_if(const Pred &p) const } template -void pl_list::initialize(std::size_t capacity) +void pl_list::initialize(std::size_t capacity, pl_cell *extcells) { - cells_ = new pl_cell[capacity]; + cells_ = extcells ? extcells : new pl_cell[capacity]; + cells_allocd_ = extcells ? false : true; capacity_ = capacity; endcell_.next = NULL; clear(); -- cgit v1.2.3