diff options
Diffstat (limited to 'experimental/lfpAlloc/Pool.hpp')
-rw-r--r-- | experimental/lfpAlloc/Pool.hpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/experimental/lfpAlloc/Pool.hpp b/experimental/lfpAlloc/Pool.hpp new file mode 100644 index 0000000..370dab7 --- /dev/null +++ b/experimental/lfpAlloc/Pool.hpp @@ -0,0 +1,48 @@ +#ifndef LF_POOL_ALLOC_POOL +#define LF_POOL_ALLOC_POOL + +#include <lfpAlloc/Utils.hpp> +#include <lfpAlloc/ChunkList.hpp> + +namespace lfpAlloc { +template <std::size_t Size, std::size_t AllocationsPerChunk> +class Pool { + using ChunkList_t = ChunkList<Size, AllocationsPerChunk>; + +public: + static constexpr auto CellSize = + (Size > sizeof(void*)) ? Size - sizeof(void*) : 0; + using Cell_t = Cell<CellSize>; + + Pool() : head_(nullptr) {} + + ~Pool() { ChunkList_t::getInstance().deallocateChain(head_); } + + void* allocate() { + // Head loaded from head_ + Cell_t* currentHead = head_; + Cell_t* next; + + // Out of cells to allocate + if (!currentHead) { + currentHead = ChunkList_t::getInstance().allocateChain(); + } + + next = currentHead->next_; + head_ = next; + return ¤tHead->val_; + } + + void deallocate(void* p) noexcept { + auto newHead = reinterpret_cast<Cell_t*>(p); + Cell_t* currentHead = head_; + newHead->next_ = currentHead; + head_ = newHead; + } + +private: + Cell_t* head_; +}; +} + +#endif |