#ifndef LF_POOL_ALLOC_POOL #define LF_POOL_ALLOC_POOL #include #include namespace lfpAlloc { template class Pool { using ChunkList_t = ChunkList; public: static constexpr auto CellSize = (Size > sizeof(void*)) ? Size - sizeof(void*) : 0; using Cell_t = Cell; 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(p); Cell_t* currentHead = head_; newHead->next_ = currentHead; head_ = newHead; } private: Cell_t* head_; }; } #endif