cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TPoolAllocator.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2025 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27#include <string>
28
29namespace cppcore {
30
31//-------------------------------------------------------------------------------------------------
47//-------------------------------------------------------------------------------------------------
48template <class T>
50public:
52 TPoolAllocator() = default;
53
56 TPoolAllocator(size_t numItems);
57
60
63 T *alloc();
64
66 void release();
67
70 void reserve(size_t size);
71
74 void clear();
75
78 size_t capacity() const;
79
82 size_t reservedMem() const;
83
86 size_t freeMem() const;
87
90 void dumpAllocations(std::string &allocs);
91
94 void resize(size_t growSize);
95
97 void reset();
98
101
102private:
103 struct Pool {
104 size_t mPoolsize{0u};
105 T *mPool{nullptr};
106 size_t mCurrentIdx{0u};
107 Pool *mNext{nullptr};
108
109 Pool() = default;
110
111 Pool(size_t numItems, Pool *prev) :
112 mPoolsize(numItems),
113 mNext(prev) {
114 mPool = new T[mPoolsize];
115 }
116
117 ~Pool() {
118 delete[] mPool;
119 }
120
122 };
123
124 Pool *getFreePool() {
125 Pool *current = mFreeList;
126 if (nullptr != mFreeList) {
127 mFreeList = mFreeList->mNext;
128 }
129 return current;
130 }
131
132 Pool *mFirst{nullptr};
133 Pool *mCurrent{nullptr};
134 Pool *mFreeList{nullptr};
135 size_t mCapacity{0L};
136};
137
138template <class T>
139inline TPoolAllocator<T>::TPoolAllocator(size_t numItems) {
140 mFirst = new Pool(numItems, nullptr);
141 mCapacity += numItems;
142 mCurrent = mFirst;
143}
144
145template <class T>
149
150template <class T>
152 if (nullptr == mCurrent) {
153 return nullptr;
154 }
155
156 if (mCurrent->mCurrentIdx == mCurrent->mPoolsize) {
157 resize(mCurrent->mPoolsize);
158 }
159
160 T *ptr = &mCurrent->mPool[mCurrent->mCurrentIdx];
161 mCurrent->mCurrentIdx++;
162
163 return ptr;
164}
165
166template <class T>
168 if (nullptr == mCurrent) {
169 return;
170 }
171
172 Pool *current = mFirst;
173 while (nullptr != current) {
174 current->mCurrentIdx = 0;
175 current = current->mNext;
176 }
177 mFreeList = mFirst->mNext;
178 mCurrent = mFirst;
179}
180
181template <class T>
182inline void TPoolAllocator<T>::reserve(size_t size) {
183 clear();
184
185 mFirst = new Pool(size, nullptr);
186 mCurrent = mFirst;
187
188 mCurrent->mPool = new T[size];
189 mCurrent->mPoolsize = size;
190
191 mCapacity = size;
192}
193
194template <class T>
196 if (nullptr == mCurrent) {
197 return;
198 }
199
200 Pool *next = mFirst;
201 while (nullptr != next) {
202 Pool *current = next;
203 next = current->mNext;
204 delete current;
205 }
206 mCurrent = nullptr;
207 mFreeList = nullptr;
208}
209
210template <class T>
211inline size_t TPoolAllocator<T>::capacity() const {
212 return mCapacity;
213}
214
215template <class T>
216inline size_t TPoolAllocator<T>::reservedMem() const {
217 return mCapacity * sizeof(T);
218}
219
220template <class T>
221inline size_t TPoolAllocator<T>::freeMem() const {
222 if (nullptr == mCurrent) {
223 return 0L;
224 }
225
226 return (mCurrent->mPoolsize - mCurrent->mCurrentIdx);
227}
228
229template <class T>
230inline void TPoolAllocator<T>::dumpAllocations(std::string &allocs) {
231 allocs.clear();
232 allocs += "Number allocations = ";
233 allocs += std::to_string(mCurrent->mCurrentIdx);
234 allocs += "\n";
235}
236
237template <class T>
238inline void TPoolAllocator<T>::resize(size_t growSize) {
239 if (nullptr != mCurrent) {
240 if (growSize < mCurrent->mPoolsize) {
241 return;
242 }
243 }
244
245 if (nullptr == mFirst) {
246 mFirst = new Pool(growSize, nullptr);
247 mCurrent = mFirst;
248 mCapacity += mCurrent->mPoolsize;
249 } else {
250 Pool *pool = getFreePool();
251 if (nullptr == pool) {
252 pool = new Pool(growSize, nullptr);
253 mCapacity += growSize;
254 }
255 mCurrent->mNext = pool;
256 mCurrent = mCurrent->mNext;
257 }
258}
259
260template<class T>
262 mCurrent = mFirst;
263}
264
265} // namespace cppcore
#define CPPCORE_NONE_COPYING(name)
This macro marks a class a not copyable.
size_t capacity() const
Returns the capacity of items in the pool allocator.
void reset()
Will reset the allocator.
TPoolAllocator()=default
The default class constructor.
T * alloc()
WIll alloc one item from the pool.
void reserve(size_t size)
Will reserve the pool.
void release()
Will release all allocated items.
void dumpAllocations(std::string &allocs)
Will dump a statistic overview into the given string.
void resize(size_t growSize)
Will perform a resize for the pool.
void clear()
Will clear the pool, memory will be deallocated.
size_t freeMem() const
Will return the free memory in the pool in bytes.
size_t reservedMem() const
WIll return the reserved memory in bytes.
~TPoolAllocator()
The class destructor.