libcruft-util/pool.hpp

173 lines
4.5 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
2016-01-19 18:29:13 +11:00
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_POOL_HPP
#define CRUFT_UTIL_POOL_HPP
#include "nocopy.hpp"
#include "debug.hpp"
#include "cast.hpp"
#include <atomic>
#include <new>
#include <cstddef>
#include <cstdint>
2014-12-31 19:07:25 +11:00
namespace cruft {
/// 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>
class pool {
2017-01-05 19:50:00 +11:00
protected:
union node;
union alignas(node*) node {
std::atomic<node*> next;
std::byte data[sizeof(T)];
2017-01-05 19:50:00 +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
// the next available entry in the linked list
std::atomic<node *> m_next;
// the total number of items that could be stored
std::size_t m_capacity;
// the number of items currently stored.
std::atomic<size_t> m_size;
2017-01-05 19:50:00 +11:00
public:
pool (const pool&) = delete;
pool& operator= (const pool&) = delete;
pool (pool&&) noexcept;
pool& operator= (pool&&);
2017-01-05 19:50:00 +11:00
explicit
pool (std::size_t _capacity):
m_capacity (_capacity),
m_size (0u)
{
// allocate the memory and note the base address for deletion in destructor
m_next = m_head = new node[m_capacity];
// 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;
}
~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;
}
2017-01-05 19:50:00 +11:00
// Data management
[[nodiscard]] T*
allocate (void)
{
// double check we have enough capacity left
if (!m_next)
throw std::bad_alloc ();
CHECK_LT (m_size, m_capacity);
// unlink the current cursor
do {
node* curr = m_next;
node* soon = curr->next;
if (m_next.compare_exchange_weak (curr, soon)) {
++m_size;
return std::launder (cruft::cast::alignment<T*> (curr));
}
} while (1);
}
void
deallocate (T *base)
{
auto soon = cruft::cast::alignment<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 ();
try {
return new (ptr) T (std::forward<Args> (args)...);
} catch (...) {
deallocate (ptr);
throw;
}
}
void
destroy (T *ptr)
{
ptr->~T ();
deallocate (ptr);
}
2014-12-31 19:07:25 +11:00
2018-07-04 14:04:45 +10:00
void destroy (size_t idx)
{
return destroy (base () + idx);
}
auto capacity (void) const { return m_capacity; }
auto size (void) const { return m_size.load (); }
bool empty (void) const { return m_size == m_capacity; }
2014-12-31 19:07:25 +11:00
2017-01-05 19:50:00 +11:00
// Indexing
2018-07-04 14:04:29 +10:00
size_t index (T const *ptr) const
{
CHECK_LIMIT (ptr, base (), base () + m_capacity);
return ptr - base ();
}
2018-07-04 14:04:16 +10:00
/// returns the base address of the allocation.
///
// guaranteed to point to the first _possible_ allocated value;
// however it may not be _live_ at any given moment. provided to
// facilitate indexing.
T* base (void) { return reinterpret_cast<T*> (m_head); }
T const* base (void) const { return reinterpret_cast<T const*> (m_head); }
2014-12-31 19:07:25 +11:00
2017-01-05 19:50:00 +11:00
T& operator[] (size_t idx) &;
2017-01-05 19:50:00 +11:00
const T& operator[] (size_t idx) const&;
};
}
#endif