aboutsummaryrefslogtreecommitdiff
path: root/src/structures
diff options
context:
space:
mode:
authorJP Cimalando <jpcima@users.noreply.github.com>2018-11-10 19:32:51 +0100
committerJP Cimalando <jpcima@users.noreply.github.com>2018-11-10 19:32:51 +0100
commitc2f914ddfa09f8fa1b2f93eb264b536ac6a85699 (patch)
tree9200417e4998015b7a718852bcc0b78ae1314d0c /src/structures
parent63ae4b909946eca9be11f2c5b0f0dc08ae706597 (diff)
downloadlibADLMIDI-c2f914ddfa09f8fa1b2f93eb264b536ac6a85699.tar.gz
libADLMIDI-c2f914ddfa09f8fa1b2f93eb264b536ac6a85699.tar.bz2
libADLMIDI-c2f914ddfa09f8fa1b2f93eb264b536ac6a85699.zip
support zero-alloc + fix
Diffstat (limited to 'src/structures')
-rw-r--r--src/structures/pl_list.hpp7
-rw-r--r--src/structures/pl_list.tcc23
2 files changed, 22 insertions, 8 deletions
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<T> *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<T> *free_;
// value-less cell which terminates the linked list
pl_basic_cell<T> endcell_;
+ // whether cell storage is allocated
+ bool cells_allocd_;
- void initialize(std::size_t capacity);
+ void initialize(std::size_t capacity, pl_cell<T> *extcells = NULL);
pl_cell<T> *allocate(pl_cell<T> *pos);
void deallocate(pl_cell<T> *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<T>::pl_list(std::size_t capacity)
template <class T>
pl_list<T>::~pl_list()
{
- delete[] cells_;
+ if (cells_allocd_)
+ delete[] cells_;
+}
+
+template <class T>
+pl_list<T>::pl_list(pl_cell<T> *cells, std::size_t ncells, external_storage_policy)
+{
+ initialize(ncells, cells);
}
template <class T>
@@ -97,14 +104,15 @@ pl_list<T> &pl_list<T>::operator=(const pl_list &other)
if(this != &other)
{
std::size_t size = other.size();
- if(size <= capacity())
- clear();
- else
+ if(size > capacity())
{
pl_cell<T> *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<T>::const_iterator pl_list<T>::find_if(const Pred &p) const
}
template <class T>
-void pl_list<T>::initialize(std::size_t capacity)
+void pl_list<T>::initialize(std::size_t capacity, pl_cell<T> *extcells)
{
- cells_ = new pl_cell<T>[capacity];
+ cells_ = extcells ? extcells : new pl_cell<T>[capacity];
+ cells_allocd_ = extcells ? false : true;
capacity_ = capacity;
endcell_.next = NULL;
clear();