forked from IBM/simpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicSizePool.hpp
234 lines (192 loc) · 5.76 KB
/
DynamicSizePool.hpp
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
#ifndef _DYNAMICSIZEPOOL_HPP
#define _DYNAMICSIZEPOOL_HPP
#include <cstddef>
#include <cassert>
#include "StdAllocator.hpp"
#include "FixedSizePool.hpp"
template <class MA, class IA = StdAllocator>
class DynamicSizePool
{
protected:
struct Block
{
char *data;
std::size_t size;
bool isHead;
Block *next;
};
// Allocator for the underlying data
typedef FixedSizePool<struct Block, IA, IA, (1<<6)> BlockPool;
BlockPool blockPool;
// Start of the nodes of used and free block lists
struct Block *usedBlocks;
struct Block *freeBlocks;
// Total size allocated (bytes)
std::size_t totalBytes;
// Allocated size (bytes)
std::size_t allocBytes;
// Minimum size for allocations
std::size_t minBytes;
// Search the list of free blocks and return a usable one if that exists, else NULL
void findUsableBlock(struct Block *&best, struct Block *&prev, std::size_t size) {
best = prev = NULL;
for ( struct Block *iter = freeBlocks, *iterPrev = NULL ; iter ; iter = iter->next ) {
if ( iter->size >= size && (!best || iter->size < best->size) ) {
best = iter;
prev = iterPrev;
}
iterPrev = iter;
}
}
// Allocate a new block and add it to the list of free blocks
void allocateBlock(struct Block *&curr, struct Block *&prev, const std::size_t size) {
const std::size_t sizeToAlloc = std::max(size, minBytes);
curr = prev = NULL;
void *data = NULL;
// Allocate data
data = MA::allocate(sizeToAlloc);
totalBytes += sizeToAlloc;
assert(data);
// Find next and prev such that next->data is still smaller than data (keep ordered)
struct Block *next;
for ( next = freeBlocks; next && next->data < data; next = next->next ) {
prev = next;
}
// Allocate the block
curr = (struct Block *) blockPool.allocate();
if (!curr) return;
curr->data = static_cast<char *>(data);
curr->size = sizeToAlloc;
curr->isHead = true;
curr->next = next;
// Insert
if (prev) prev->next = curr;
else freeBlocks = curr;
}
void splitBlock(struct Block *&curr, struct Block *&prev, const std::size_t size) {
struct Block *next;
if ( curr->size == size ) {
// Keep it
next = curr->next;
}
else {
// Split the block
std::size_t remaining = curr->size - size;
struct Block *newBlock = (struct Block *) blockPool.allocate();
if (!newBlock) return;
newBlock->data = curr->data + size;
newBlock->size = remaining;
newBlock->isHead = false;
newBlock->next = curr->next;
next = newBlock;
curr->size = size;
}
if (prev) prev->next = next;
else freeBlocks = next;
}
void releaseBlock(struct Block *curr, struct Block *prev) {
assert(curr != NULL);
if (prev) prev->next = curr->next;
else usedBlocks = curr->next;
// Find location to put this block in the freeBlocks list
prev = NULL;
for ( struct Block *temp = freeBlocks ; temp && temp->data < curr->data ; temp = temp->next ) {
prev = temp;
}
// Keep track of the successor
struct Block *next = prev ? prev->next : freeBlocks;
// Check if prev and curr can be merged
if ( prev && prev->data + prev->size == curr->data && !curr->isHead ) {
prev->size = prev->size + curr->size;
blockPool.deallocate(curr); // keep data
curr = prev;
}
else if (prev) {
prev->next = curr;
}
else {
freeBlocks = curr;
}
// Check if curr and next can be merged
if ( next && curr->data + curr->size == next->data && !next->isHead ) {
curr->size = curr->size + next->size;
curr->next = next->next;
blockPool.deallocate(next); // keep data
}
else {
curr->next = next;
}
}
void freeAllBlocks() {
// Release the used blocks
while(usedBlocks) {
releaseBlock(usedBlocks, NULL);
}
// Release the unused blocks
while(freeBlocks) {
assert(freeBlocks->isHead);
MA::deallocate(freeBlocks->data);
totalBytes -= freeBlocks->size;
struct Block *curr = freeBlocks;
freeBlocks = freeBlocks->next;
blockPool.deallocate(curr);
}
}
public:
static inline DynamicSizePool &getInstance() {
static DynamicSizePool instance;
return instance;
}
DynamicSizePool(const std::size_t _minBytes = (1 << 8))
: blockPool(),
usedBlocks(NULL),
freeBlocks(NULL),
totalBytes(0),
allocBytes(0),
minBytes(_minBytes) { }
~DynamicSizePool() { freeAllBlocks(); }
void *allocate(std::size_t size) {
struct Block *best, *prev;
findUsableBlock(best, prev, size);
// Allocate a block if needed
if (!best) allocateBlock(best, prev, size);
assert(best);
// Split the free block
splitBlock(best, prev, size);
// Push node to the list of used nodes
best->next = usedBlocks;
usedBlocks = best;
// Increment the allocated size
allocBytes += size;
// Return the new pointer
return usedBlocks->data;
}
void deallocate(void *ptr) {
assert(ptr);
// Find the associated block
struct Block *curr = usedBlocks, *prev = NULL;
for ( ; curr && curr->data != ptr; curr = curr->next ) {
prev = curr;
}
if (!curr) return;
// Remove from allocBytes
allocBytes -= curr->size;
// Release it
releaseBlock(curr, prev);
}
std::size_t allocatedSize() const { return allocBytes; }
std::size_t totalSize() const {
return totalBytes + blockPool.totalSize();
}
std::size_t numFreeBlocks() const {
std::size_t nb = 0;
for (struct Block *temp = freeBlocks; temp; temp = temp->next) nb++;
return nb;
}
std::size_t numUsedBlocks() const {
std::size_t nb = 0;
for (struct Block *temp = usedBlocks; temp; temp = temp->next) nb++;
return nb;
}
};
#endif