Cogs.Core
PoolBase.h
1#pragma once
2
3#include "../Memory/MemoryBuffer.h"
4
5#include <OsoMemoryProfiler/CogsMemoryProfile.h>
6
7namespace Cogs
8{
10 namespace Collections
11 {
13 typedef uint16_t ElementOffset;
14
16 typedef uint32_t ElementHandle;
17
21 struct COGSFOUNDATION_API PoolBase
22 {
32 PoolBase(size_t elementSize, size_t initialCapacity, size_t pageSize, Memory::Allocator * allocator = Memory::Allocator::defaultAllocator(), MemBlockType memType=MemBlockType::Bucket);
33
35 PoolBase(PoolBase &&) = default;
36
37 // Defaulted move assignment operator.
38 PoolBase & operator=(PoolBase &&) = default;
39
41 PoolBase(const PoolBase &) = delete;
42
44 PoolBase & operator=(const PoolBase &) = delete;
45
54 void resize(size_t capacity);
55
64 void * allocate();
65
69 void deallocate(void * element);
70
74 size_t size() const { return numAllocated; }
75
79 size_t getCapacity() const { return capacity; }
80
84 size_t getPageSize() const { return pageSize; }
85
95 ElementHandle getHandle(void * element) const;
96
104 void * getElement(ElementHandle handle);
105
109 bool isValid(ElementHandle handle) const {
110 const auto pageIndex = getPageIndex(handle);
111 const auto elementIndex = getElementOffset(handle);
112 return (pageIndex < pages.size()) && (elementIndex < pageSize);
113 }
114
119 bool isAllocated(ElementHandle handle) const;
120
121 protected:
123 constexpr uint32_t getPageIndex(ElementHandle handle) const { return handle >> 16; }
124
126 constexpr ElementOffset getElementOffset(ElementHandle handle) const { return static_cast<ElementOffset>(handle & 0x0000FFFF); }
127
128 protected:
132 void addPage(size_t pageSize);
133
135 std::vector<Memory::MemoryBuffer> pages;
136
137 void * freeHead = nullptr;
138
139 size_t numAllocated = 0;
140 size_t elementSize = 0;
141 size_t capacity = 0;
142 size_t pageSize = 0;
143
144 Memory::Allocator * allocator = nullptr;
145 MemBlockType memType = MemBlockType::Bucket;
147 };
148 }
149}
Base allocator implementation.
Definition: Allocator.h:30
uint16_t ElementOffset
Offset type used to index elements in resource pools.
Definition: PoolBase.h:13
uint32_t ElementHandle
Handle type for elements.
Definition: PoolBase.h:16
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Base untyped pool.
Definition: PoolBase.h:22
PoolBase(const PoolBase &)=delete
Disallow copying PoolBase instances.
size_t getCapacity() const
Get the capacity of the pool, in number of elements.
Definition: PoolBase.h:79
PoolBase(PoolBase &&)=default
Defaulted move constructor.
size_t getPageSize() const
Get the page size used by the pool, given in number of elements.
Definition: PoolBase.h:84
constexpr ElementOffset getElementOffset(ElementHandle handle) const
Gets the index inside a page of the element handle.
Definition: PoolBase.h:126
size_t size() const
Get the size of the pool, i.e the number of allocated elements.
Definition: PoolBase.h:74
constexpr uint32_t getPageIndex(ElementHandle handle) const
Gets the page index of the element handle.
Definition: PoolBase.h:123
PoolBase & operator=(const PoolBase &)=delete
Disallow copy-assigning PoolBase instances.
bool isValid(ElementHandle handle) const
Checks if the given handle is a valid handle to an allocated or free element.
Definition: PoolBase.h:109