-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPoolAllocator.h
More file actions
340 lines (287 loc) · 11.9 KB
/
PoolAllocator.h
File metadata and controls
340 lines (287 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef NOWTECH_POOLALLOCATOR
#define NOWTECH_POOLALLOCATOR
#include <set>
#include <map>
#include <list>
#include <memory>
#include <forward_list>
namespace nowtech::memory {
class MeasureAllocatorBase {
protected:
MeasureAllocatorBase* mOriginal;
size_t mNodeSize = 0u;
size_t mNodeCount = 0u;
uint8_t* mMemory;
MeasureAllocatorBase(void* aMemory) noexcept
: mOriginal(this)
, mMemory(static_cast<uint8_t*>(aMemory)) {
}
MeasureAllocatorBase(MeasureAllocatorBase const &aOther) noexcept
: mOriginal(aOther.mOriginal) {
}
public:
void* allocate(std::size_t const aCount, size_t const aLength) {
mOriginal->mNodeSize = std::max<size_t>(mOriginal->mNodeSize, aLength);
mOriginal->mNodeCount = std::max<size_t>(mOriginal->mNodeCount, aCount);
auto result = mOriginal->mMemory;
mOriginal->mMemory += aCount * aLength;
return result;
}
public:
size_t getNodeSize() const noexcept {
return mNodeSize * mNodeCount;
}
};
template<typename tContainer>
class AllocatorBlockGauge;
template <typename tMeasureItem>
class MeasureAllocator : public MeasureAllocatorBase {
template<typename tContainerItem> friend class AllocatorBlockGauge;
protected:
MeasureAllocator(void* aMemory) noexcept : MeasureAllocatorBase(aMemory) {
}
size_t getNodeSize() const noexcept {
return mOriginal->getNodeSize();
}
public:
using value_type = tMeasureItem;
template <typename tOther>
struct rebind {
typedef MeasureAllocator<tOther> other;
};
template<typename tOther>
MeasureAllocator(MeasureAllocator<tOther> const &aOther) noexcept : MeasureAllocatorBase(aOther) {
}
tMeasureItem* allocate(std::size_t const aCount) {
return static_cast<tMeasureItem*>(mOriginal->allocate(aCount, sizeof(tMeasureItem)));
}
void deallocate(value_type* aPointer, std::size_t aCount) noexcept {
// nothing to do
}
std::size_t max_size() const noexcept {
return 1u;
}
};
class AllocatorBlockGaugeBase {
protected:
static constexpr size_t cMeasureBufferSize = 128u;
};
template<typename tContainerItem>
class AllocatorBlockGauge<std::forward_list<tContainerItem>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, tContainerItem const &aValue) noexcept {
MeasureAllocator<tContainerItem> gauge(aMemory);
std::forward_list<tContainerItem, MeasureAllocator<tContainerItem>> container(gauge);
container.push_front(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(tContainerItem const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
template<typename tContainerItem>
class AllocatorBlockGauge<std::list<tContainerItem>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, tContainerItem const &aValue) noexcept {
MeasureAllocator<tContainerItem> gauge(aMemory);
std::list<tContainerItem, MeasureAllocator<tContainerItem>> container(gauge);
container.push_front(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(tContainerItem const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
template<typename tKey, typename tValue>
class AllocatorBlockGauge<std::map<tKey, tValue>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, std::pair<tKey, tValue> const &aValue) noexcept {
MeasureAllocator<std::pair<tKey, tValue>> gauge(aMemory);
std::map<tKey, tValue, std::less<tKey>, MeasureAllocator<std::pair<tKey, tValue>>> container(gauge);
container.insert(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(std::pair<tKey, tValue> const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
template<typename tKey, typename tValue>
class AllocatorBlockGauge<std::multimap<tKey, tValue>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, std::pair<tKey, tValue> const &aValue) noexcept {
MeasureAllocator<std::pair<tKey, tValue>> gauge(aMemory);
std::multimap<tKey, tValue, std::less<tKey>, MeasureAllocator<std::pair<tKey, tValue>>> container(gauge);
container.insert(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(std::pair<tKey, tValue> const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
template<typename tContainerItem>
class AllocatorBlockGauge<std::multiset<tContainerItem>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, tContainerItem const &aValue) noexcept {
MeasureAllocator<tContainerItem> gauge(aMemory);
std::multiset<tContainerItem, std::less<tContainerItem>, MeasureAllocator<tContainerItem>> container(gauge);
container.insert(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(tContainerItem const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
template<typename tContainerItem>
class AllocatorBlockGauge<std::set<tContainerItem>> : public AllocatorBlockGaugeBase {
public:
static size_t getNodeSize(void* aMemory, tContainerItem const &aValue) noexcept {
MeasureAllocator<tContainerItem> gauge(aMemory);
std::set<tContainerItem, std::less<tContainerItem>, MeasureAllocator<tContainerItem>> container(gauge);
container.insert(aValue);
return gauge.getNodeSize();
}
static size_t getNodeSize(tContainerItem const &aValue) noexcept {
uint8_t buffer[cMeasureBufferSize];
return getNodeSize(buffer, aValue);
}
};
/// Only used to provide an allocator with memory
/// class Occupier {
/// public:
/// // This function is assumed to return memory aligned for void*.
/// // @returns nullptr on failure
/// void* occupy(size_t const aSize) noexcept;
/// void release(void* const aPointer) noexcept;
///
/// // May throw exception or do something else.
/// static void badAlloc();
/// };
template<typename tInterface>
class PoolAllocatorBase {
protected:
PoolAllocatorBase* mOriginal;
bool mIsOriginal;
tInterface& mOccupier;
size_t mPoolSize;
size_t mNodeSize;
size_t mBlockSizeInPointerSize;
/// This contains the linked free blocks, initially all free.
/// A block begins with the pointer to the pointer part of the next block,
/// then comes the data intended for the set node.
void* mMemory;
void** mFirst;
void** mProhibited;
public:
PoolAllocatorBase(size_t const aPoolSize, size_t aNodeSize, tInterface& aOccupier) noexcept
: mOriginal(this)
, mIsOriginal(true)
, mOccupier(aOccupier)
, mPoolSize(aPoolSize)
, mNodeSize(aNodeSize)
, mBlockSizeInPointerSize((mNodeSize + sizeof(void*) - 1u) / sizeof(void*))
, mMemory(mOccupier.occupy(mBlockSizeInPointerSize * sizeof(void*) * (mPoolSize + 1u)))
, mFirst(reinterpret_cast<void**>(mMemory))
, mProhibited(mFirst + mPoolSize * mBlockSizeInPointerSize) {
for(size_t i = 0u; i < mPoolSize; ++i) {
mFirst[i * mBlockSizeInPointerSize] = static_cast<void*>(mFirst + (i + 1u) * mBlockSizeInPointerSize);
}
mFirst[mPoolSize * mBlockSizeInPointerSize] = nullptr;
}
PoolAllocatorBase(PoolAllocatorBase const &aOther) noexcept
: mOriginal(aOther.mOriginal)
, mIsOriginal(false)
, mOccupier(aOther.mOccupier) {
}
~PoolAllocatorBase() noexcept {
if(mIsOriginal) {
mOccupier.release(mMemory);
}
else { // nothing to do
}
}
void* allocate(std::size_t const aCount, size_t aLength) {
void* result;
if(mOriginal->mFirst != mOriginal->mProhibited && aLength <= mOriginal->mNodeSize) {
result = mOriginal->mFirst;
mOriginal->mFirst = static_cast<void**>(*mOriginal->mFirst);
}
else {
result = nullptr;
mOccupier.badAlloc();
}
return result;
}
/// I suppose this receives only valid pointers and each one only once.
void deallocate(void* aPointer, std::size_t) noexcept {
void **incoming = reinterpret_cast<void**>(aPointer);
*incoming = static_cast<void*>(mOriginal->mFirst);
mOriginal->mFirst = incoming;
}
};
/// This allocator is intentionally NOT complete. It does not define copy and move constructors in a correct way,
/// so the containers using it can't copy, move or swap themselves.
///
/// Currently this allocator does not support any other alignment than what is implicitely provided by aligning
/// to void* type. For embedded applications and most other cases this will suffice. This simplification
/// is required for the small and efficient implementation important for embedded usage.
///
/// This instance contains mPoolSize pieces of blocks, each mBlockSizeInPointerSize long.
/// Beginning of each block is a pointer to the next one, and the rest is the node data.
/// For sake of simplicity, mPoolSize + 1 blocks will be used internally, so one free always remains.
/// Thus the last one will never be allocated.
/// This implementation is not thread-safe.
/// The allocator returns nullptr if it runs out of space or the size of requested item is larger than the default node size.
template<typename tContainerItem, typename tInterface>
class PoolAllocator : public PoolAllocatorBase<tInterface> {
template <typename tContainerItemA, typename tInterfaceA, typename tContainerItemB, typename tInterfaceB>
friend bool operator==(PoolAllocator<tContainerItemA, tInterfaceA> const &aAllocA, PoolAllocator<tContainerItemB, tInterfaceB> const &aAllocB);
template <typename tContainerItemA, typename tInterfaceA, typename tContainerItemB, typename tInterfaceB>
friend bool operator!=(PoolAllocator<tContainerItemA, tInterfaceA> const &aAllocA, PoolAllocator<tContainerItemB, tInterfaceB> const &aAllocB);
public:
using value_type = tContainerItem;
using difference_type = typename std::pointer_traits<tContainerItem*>::difference_type;
using size_type = std::make_unsigned_t<difference_type>;
template <typename tOther>
struct rebind {
typedef PoolAllocator<tOther, tInterface> other;
};
PoolAllocator(size_t const aPoolSize, size_t aNodeSize, tInterface &aOccupier) noexcept : PoolAllocatorBase<tInterface>(aPoolSize, aNodeSize, aOccupier) {
}
template<typename tOther>
PoolAllocator(PoolAllocator<tOther, tInterface> const &aOther) noexcept : PoolAllocatorBase<tInterface>(aOther) {
}
tContainerItem* allocate(std::size_t const aCount) {
return static_cast<tContainerItem*>(this->mOriginal->allocate(aCount, sizeof(tContainerItem)));
}
/// I suppose this receives only valid pointers and each one only once.
void deallocate(tContainerItem* aPointer, std::size_t aLen) noexcept {
this->mOriginal->deallocate(aPointer, aLen);
}
std::size_t max_size() const noexcept {
return 1u; // Supports only single node allocation.
}
PoolAllocator select_on_container_copy_construction() const {
return *this;
}
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
using is_always_equal = std::false_type;
};
template <typename tContainerItemA, typename tInterfaceA, typename tContainerItemB, typename tInterfaceB>
bool operator==(PoolAllocator<tContainerItemA, tInterfaceA> const &aAllocA, PoolAllocator<tContainerItemB, tInterfaceB> const &aAllocB) {
return aAllocA.mOriginal->mPoolSize == aAllocB.mOriginal->mPoolSize
&& aAllocA.mOriginal->mMemory == aAllocB.mOriginal->mMemory
&& aAllocA.mOriginal->mBlockSizeInPointerSize == aAllocB.mOriginal->mBlockSizeInPointerSize;
}
template <typename tContainerItemA, typename tInterfaceA, typename tContainerItemB, typename tInterfaceB>
bool operator!=(PoolAllocator<tContainerItemA, tInterfaceA> const &aAllocA, PoolAllocator<tContainerItemB, tInterfaceB> const &aAllocB) {
return !(aAllocA == aAllocB);
}
}
#endif