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
|
#include "slang-memory-arena.h"
namespace Slang {
MemoryArena::MemoryArena()
{
// Mark as invalid so any alloc call will fail
m_blockAlignment = 0;
m_blockAllocSize = 0;
// Set up as empty
m_usedOddBlocks = nullptr;
m_availableBlocks = nullptr;
_resetCurrentBlock();
m_blockFreeList.init(sizeof(Block), sizeof(void*), 16);
}
MemoryArena::~MemoryArena()
{
reset();
}
MemoryArena::MemoryArena(size_t blockPayloadSize, size_t blockAlignment)
{
_initialize(blockPayloadSize, blockAlignment);
}
void MemoryArena::init(size_t blockPayloadSize, size_t blockAlignment)
{
reset();
_initialize(blockPayloadSize, blockAlignment);
}
void MemoryArena::_initialize(size_t blockPayloadSize, size_t alignment)
{
// Alignment must be a power of 2
assert(((alignment - 1) & alignment) == 0);
// Must be at least sizeof(void*) in size, as that is the minimum the backing allocator will be
alignment = (alignment < kMinAlignment) ? kMinAlignment : alignment;
m_blockPayloadSize = blockPayloadSize;
size_t blockAllocSize = blockPayloadSize;
// If alignment required is larger then the backing allocators then
// make larger to ensure when alignment correction takes place it will be aligned
if (alignment > kMinAlignment)
{
blockAllocSize += alignment;
}
m_blockAllocSize = blockAllocSize;
m_blockAlignment = alignment;
m_availableBlocks = nullptr;
m_usedOddBlocks = nullptr;
m_blockFreeList.init(sizeof(Block), sizeof(void*), 16);
_resetCurrentBlock();
}
void MemoryArena::_resetCurrentBlock()
{
m_start = nullptr;
m_end = nullptr;
m_current = nullptr;
m_usedBlocks = nullptr;
}
void MemoryArena::_addCurrentBlock(Block* block)
{
// Set up for allocation from
m_end = block->m_end;
m_start = block->m_start;
m_current = m_start;
// Add to linked list of used block, making it the top used block
block->m_next = m_usedBlocks;
m_usedBlocks = block;
}
void MemoryArena::_deallocateBlocksPayload(Block* start)
{
Block* cur = start;
while (cur)
{
// Deallocate the block
::free(cur->m_alloc);
cur = cur->m_next;
}
}
void MemoryArena::_deallocateBlocks(Block* start)
{
Block* cur = start;
while (cur)
{
Block* next = cur->m_next;
// Deallocate the block
::free(cur->m_alloc);
m_blockFreeList.deallocate(cur);
cur = next;
}
}
/* static */MemoryArena::Block* MemoryArena::_joinBlocks(Block* pre, Block* post)
{
if (pre && post)
{
// If both are actual lists, concat post at end of pre
Block* cur = pre;
while (cur->m_next)
{
cur = cur->m_next;
}
// Attach post to end of pre
cur->m_next = post;
}
return pre ? pre : post;
}
void MemoryArena::deallocateAll()
{
// Free all oversized
_deallocateBlocks(m_usedOddBlocks);
m_usedOddBlocks = nullptr;
// We want to put used blocks onto the start of the available list
m_availableBlocks = _joinBlocks(m_usedBlocks, m_availableBlocks);
// Reset current block
_resetCurrentBlock();
}
void MemoryArena::reset()
{
_deallocateBlocksPayload(m_usedOddBlocks);
_deallocateBlocksPayload(m_usedBlocks);
_deallocateBlocksPayload(m_availableBlocks);
m_blockFreeList.reset();
m_usedOddBlocks = nullptr;
m_availableBlocks = nullptr;
_resetCurrentBlock();
}
const MemoryArena::Block* MemoryArena::_findNonCurrent(const void* data, size_t size) const
{
// It must either be m_usedOversizedBlocks or after m_usedBlocks (because m_usedBlocks is m_current)
const Block* block = _findInBlocks(m_usedOddBlocks, data, size);
if (block)
{
return block;
}
return m_usedBlocks ? _findInBlocks(m_usedBlocks->m_next, data, size) : nullptr;
}
const MemoryArena::Block* MemoryArena::_findInBlocks(const Block* block, const void* data, size_t size) const
{
const uint8_t* ptr = (const uint8_t*)data;
while (block)
{
if (ptr >= block->m_start && ptr + size <= block->m_end)
{
return block;
}
block = block->m_next;
}
return nullptr;
}
MemoryArena::Block* MemoryArena::_newNormalBlock()
{
if (m_availableBlocks)
{
// We have an available block..
Block* block = m_availableBlocks;
m_availableBlocks = block->m_next;
return block;
}
return _newBlock(m_blockAllocSize, m_blockAlignment);
}
MemoryArena::Block* MemoryArena::_newBlock(size_t allocSize, size_t alignment)
{
assert(alignment >= m_blockAlignment);
// Allocate block
Block* block = (Block*)m_blockFreeList.allocate();
if (!block)
{
return nullptr;
}
// Allocate the memory
uint8_t* alloc = (uint8_t*)::malloc(allocSize);
if (!alloc)
{
m_blockFreeList.deallocate(block);
return nullptr;
}
const size_t alignMask = alignment - 1;
// Do the alignment on the allocation
uint8_t* const start = (uint8_t*)((size_t(alloc) + alignMask) & ~alignMask);
// Setup the block
block->m_alloc = alloc;
block->m_start = start;
block->m_end = alloc + allocSize;
block->m_next = nullptr;
return block;
}
void* MemoryArena::_allocateAlignedFromNewBlockAndZero(size_t sizeInBytes, size_t alignment)
{
void* mem = _allocateAlignedFromNewBlock(sizeInBytes, alignment);
if (mem)
{
::memset(mem, 0, sizeInBytes);
}
return mem;
}
void* MemoryArena::_allocateAlignedFromNewBlock(size_t size, size_t alignment)
{
// Make sure init has been called (or has been set up in parameterized constructor)
assert(m_blockAllocSize > 0);
// Alignment must be a power of 2
assert(((alignment - 1) & alignment) == 0);
// Alignment must at a minimum be block alignment (such if reused the constraints hold)
alignment = (alignment < m_blockAlignment) ? m_blockAlignment : alignment;
const size_t alignMask = alignment - 1;
// The size of the block must be at least large enough to take into account alignment
size_t allocSize = (alignment <= kMinAlignment) ? size : (size + alignment);
const size_t currentRemainSize = size_t(m_end - m_current);
// a) Allocate a new normal block and make current
// b) Allocate a new normal block (leave current)
// c) Allocate a new 'oversized' block (leave current)
//
// We only bother with a and c here, as b is only really usable if size is close to m_blockAllocSize
// If there is > 1/3 of block remaining, or the block required is too big to fit use an 'oversized' block
if ((currentRemainSize * 3 > m_blockPayloadSize) || (allocSize > m_blockAllocSize))
{
// We don't change current because there is enough remaining
Block* block = _newBlock(allocSize, alignment);
if (!block)
{
return nullptr;
}
// Add to odd used blocks list
block->m_next = m_usedOddBlocks;
m_usedOddBlocks = block;
// NOTE! We are keeping the previous m_current current, so any remaining space can still be used
// Return the address
return block->m_start;
}
// Must be allocatable within a normal block
assert(allocSize <= m_blockAllocSize);
Block* block = _newNormalBlock();
if (!block)
{
return nullptr;
}
// It's a new regular block...
_addCurrentBlock(block);
// Do the aligned allocation (which must fit) by aligning the pointer
uint8_t* memory = (uint8_t*)((size_t(m_current) + alignMask) & ~alignMask);
// It must fit if the previous code is correct...
assert(memory + size <= m_end);
// Move the current pointer
m_current = memory + size;
return memory;
}
size_t MemoryArena::_calcBlocksUsedMemory(const Block* block) const
{
size_t total = 0;
while (block)
{
total += size_t(block->m_end - block->m_start);
block = block->m_next;
}
return total;
}
size_t MemoryArena::_calcBlocksAllocatedMemory(const Block* block) const
{
size_t total = 0;
while (block)
{
total += size_t(block->m_end - block->m_alloc);
block = block->m_next;
}
return total;
}
size_t MemoryArena::calcTotalMemoryUsed() const
{
return _calcBlocksUsedMemory(m_usedOddBlocks)
+ (m_usedBlocks ? _calcBlocksUsedMemory(m_usedBlocks->m_next) : 0) +
size_t(m_current - m_start);
}
size_t MemoryArena::calcTotalMemoryAllocated() const
{
return _calcBlocksAllocatedMemory(m_usedOddBlocks) +
_calcBlocksAllocatedMemory(m_usedBlocks) +
_calcBlocksAllocatedMemory(m_availableBlocks);
}
} // namespace Slang
|