2011-07-01 13:46:39 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-01 13:46:39 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-01 13:46:39 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-07-01 13:46:39 +10:00
|
|
|
*
|
2016-01-19 18:29:13 +11:00
|
|
|
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
|
2011-07-01 13:46:39 +10:00
|
|
|
*/
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#ifndef CRUFT_UTIL_POOL_HPP
|
|
|
|
#define CRUFT_UTIL_POOL_HPP
|
2011-07-01 13:46:39 +10:00
|
|
|
|
|
|
|
#include "nocopy.hpp"
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "debug.hpp"
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
#include <atomic>
|
2014-12-31 19:07:25 +11:00
|
|
|
#include <cstdlib>
|
2018-02-28 11:49:13 +11:00
|
|
|
#include <cstdint>
|
2014-12-31 19:07:25 +11:00
|
|
|
|
2011-10-07 21:58:16 +11:00
|
|
|
namespace util {
|
2016-02-04 12:41:23 +11:00
|
|
|
/// a simple pre-allocated pool for storage of PODs.
|
|
|
|
///
|
|
|
|
/// non-POD types can be stored, but there are no guarantees for calling
|
|
|
|
/// item destructors at pool destruction time.
|
2012-08-22 16:12:26 +10:00
|
|
|
template <typename T>
|
2018-03-14 18:15:07 +11:00
|
|
|
class pool {
|
2017-01-05 19:50:00 +11:00
|
|
|
protected:
|
2018-03-14 18:15:07 +11:00
|
|
|
union node;
|
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
union alignas (T) node {
|
2018-03-14 18:15:07 +11:00
|
|
|
std::atomic<node*> next;
|
|
|
|
char data[sizeof (T)];
|
2017-01-05 19:50:00 +11:00
|
|
|
};
|
2011-10-07 21:58:16 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
static_assert (std::atomic<node*>::is_always_lock_free);
|
|
|
|
|
|
|
|
// root address of allocation. used in deletion at destruction time.
|
|
|
|
node* m_head;
|
2016-01-19 18:29:13 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
// the next available entry in the linked list
|
|
|
|
std::atomic<node *> m_next;
|
|
|
|
|
|
|
|
// the total number of items that could be stored
|
2017-01-05 19:50:00 +11:00
|
|
|
const size_t m_capacity;
|
2018-03-14 18:15:07 +11:00
|
|
|
|
|
|
|
// the number of items currently stored.
|
|
|
|
std::atomic<size_t> m_size;
|
2011-10-07 21:58:16 +11:00
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
public:
|
2018-03-14 18:15:07 +11:00
|
|
|
pool (const pool&) = delete;
|
|
|
|
pool& operator= (const pool&) = delete;
|
|
|
|
|
|
|
|
pool (pool&&);
|
|
|
|
pool& operator= (pool&&);
|
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
explicit
|
2018-02-28 11:49:13 +11:00
|
|
|
pool (unsigned int _capacity):
|
|
|
|
m_capacity (_capacity),
|
2018-03-14 18:15:07 +11:00
|
|
|
m_size (0u)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
|
|
|
// allocate the memory and note the base address for deletion in destructor
|
2018-03-14 18:15:07 +11:00
|
|
|
m_next = m_head = new node[m_capacity];
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
// build out a complete singly linked list from all the nodes.
|
|
|
|
for (size_t i = 0; i < m_capacity - 1; ++i)
|
|
|
|
m_next[i].next = m_next + i + 1;
|
|
|
|
m_next[m_capacity - 1].next = nullptr;
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
~pool ()
|
|
|
|
{
|
|
|
|
// don't check if everything's been returned as pools are often used
|
|
|
|
// for PODs which don't need to be destructed via calling release.
|
|
|
|
delete [] m_head;
|
|
|
|
}
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
// Data management
|
2018-03-14 18:15:07 +11:00
|
|
|
[[nodiscard]] T*
|
|
|
|
allocate (void)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
|
|
|
// double check we have enough capacity left
|
|
|
|
if (!m_next)
|
|
|
|
throw std::bad_alloc ();
|
|
|
|
CHECK_LT (m_size, m_capacity);
|
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
// unlink the current cursor
|
|
|
|
do {
|
|
|
|
node* curr = m_next;
|
|
|
|
node* soon = curr->next;
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
if (m_next.compare_exchange_weak (curr, soon)) {
|
|
|
|
++m_size;
|
2018-03-22 13:10:18 +11:00
|
|
|
return std::launder (reinterpret_cast<T*> (curr));
|
2018-03-14 18:15:07 +11:00
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
deallocate (T *base)
|
|
|
|
{
|
|
|
|
auto soon = reinterpret_cast<node*> (base);
|
|
|
|
|
|
|
|
do {
|
|
|
|
node *curr = m_next;
|
|
|
|
soon->next = curr;
|
|
|
|
if (m_next.compare_exchange_weak (curr, soon)) {
|
|
|
|
--m_size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
T*
|
|
|
|
construct (Args &&...args)
|
|
|
|
{
|
|
|
|
auto ptr = allocate ();
|
2018-02-28 11:49:13 +11:00
|
|
|
try {
|
2018-03-14 18:15:07 +11:00
|
|
|
return new (ptr) T (std::forward<Args> (args)...);
|
2018-02-28 11:49:13 +11:00
|
|
|
} catch (...) {
|
2018-03-14 18:15:07 +11:00
|
|
|
deallocate (ptr);
|
2018-02-28 11:49:13 +11:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
void
|
|
|
|
destroy (T *ptr)
|
|
|
|
{
|
|
|
|
ptr->~T ();
|
|
|
|
deallocate (ptr);
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2014-12-31 19:07:25 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
size_t capacity (void) const
|
|
|
|
{
|
|
|
|
return m_capacity;
|
|
|
|
}
|
2018-03-14 18:15:07 +11:00
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
size_t size (void) const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
2018-03-14 18:15:07 +11:00
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
bool empty (void) const
|
|
|
|
{
|
|
|
|
return m_size == m_capacity;
|
|
|
|
}
|
2014-12-31 19:07:25 +11:00
|
|
|
|
2018-03-14 18:15:07 +11:00
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
// Indexing
|
|
|
|
size_t index (const T*) const;
|
2014-12-31 19:07:25 +11:00
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
T& operator[] (size_t idx) &;
|
2018-03-14 18:15:07 +11:00
|
|
|
|
2017-01-05 19:50:00 +11:00
|
|
|
const T& operator[] (size_t idx) const&;
|
2011-10-07 21:58:16 +11:00
|
|
|
};
|
|
|
|
}
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#endif
|