47 static_assert(
sizeof(T) >=
sizeof(intptr_t),
"T must be at least of pointer size.");
49 const size_t elementSize =
sizeof(T);
51 bytes.resize(pageSize * elementSize);
56 for (
size_t i = 0; i < pageSize; ++i) {
59 block->
next = block + 1;
63 (head + (pageSize - 1))->
next =
nullptr;
90 template<
typename Context,
typename Page,
typename Item>
122 void initialize(Context context,
size_t capacity,
size_t pageSize)
124 this->context = context;
125 this->pageSize = pageSize;
145 if (!head)
resize(capacity + pageSize);
150 return reinterpret_cast<Item *
>(item);
163 reinterpret_cast<ItemHeader *
>(item)->next = head;
174 while (capacity > this->capacity) {
183 pages.emplace_back(context, pageSize);
185 auto & lastPage = pages.back();
188 head =
reinterpret_cast<ItemHeader *
>(lastPage.getHead());
199 capacity += pageSize;
205 std::vector<Page> pages;
Provides a configurable pool implementation usable as base implementation for several pool-like stora...
ConfigurablePagedPool(Context context, size_t capacity, size_t pageSize)
Constructs a pool with the given context, capacity and pageSize.
void addPage()
Adds a new page to the pool, linking its reserved elements with any current free elements.
void deallocate(Item *item)
Deallocate the given item.
void resize(size_t capacity)
Resize the pool to the given capacity.
Item * allocate()
Allocates storage for a new item and returns a pointer to the item.
ConfigurablePagedPool()=default
Constructs a default, empty pool.
void initialize(Context context, size_t capacity, size_t pageSize)
Initialize the pool with the given parameters.
Contains all Cogs related functionality.
Default page type that maintains storage for elements of type T in the pool page and provides pooled ...
std::vector< uint8_t > bytes
Backing storage bytes.
DefaultPage()=default
Default constructor.
DefaultPage(DefaultContext &context, size_t pageSize)
Construct a new page with the given page size.
T * getHead()
Retrieve a pointer to the first element of the page.