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
341
|
#ifndef _ALLOCATOR_FIXEDSIZE_ALLOCATOR
#define _ALLOCATOR_FIXEDSIZE_ALLOCATOR
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Misc/AllocatorLabels.h"
template <unsigned BlockSize>
class
FixedSizeAllocator
{
public:
FixedSizeAllocator(MemLabelId memLabel);
~FixedSizeAllocator();
void* alloc();
void free( void* mem );
void reset();
void free_memory();
unsigned total_allocated() const;
unsigned total_free() const;
unsigned capacity() const;
private:
// we can do this template parameter
static const UInt8 BlocksInChunk = 255;
struct
Chunk
{
UInt8 data[BlocksInChunk*BlockSize];
Chunk* next;
UInt8 first_available;
UInt8 total_available;
};
Chunk* m_Chunk;
// we can store pointers -- they will be updated anyway should the new chunk be added
Chunk* m_AllocChunk;
Chunk* m_DeallocChunk;
MemLabelId m_MemLabel;
void create_chunk();
void reset_chunk( Chunk* chunk );
void* alloc_from( Chunk* chunk );
void dealloc_from( void* mem, Chunk* chunk );
bool mem_from( void* mem, Chunk* chunk );
};
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::reset_chunk( Chunk* chunk )
{
AssertBreak(chunk);
chunk->first_available = 0;
chunk->total_available = BlocksInChunk;
// store index of next available block in-place, as first byte of the block
UInt8 next_available_i = 1;
UInt8* next_available_mem = (UInt8*)chunk->data;
while( next_available_i != BlocksInChunk )
{
*next_available_mem = next_available_i;
++next_available_i;
next_available_mem += BlockSize;
}
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::create_chunk()
{
Chunk* newChunk = (Chunk*)UNITY_MALLOC(m_MemLabel, sizeof(Chunk));
reset_chunk(newChunk);
newChunk->next = 0;
if( m_Chunk )
{
Chunk* tail_chunk = m_Chunk;
while( tail_chunk->next )
tail_chunk = tail_chunk->next;
tail_chunk->next = newChunk;
}
else
{
m_Chunk = newChunk;
}
m_AllocChunk = m_DeallocChunk = newChunk;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline
FixedSizeAllocator<BlockSize>::FixedSizeAllocator(MemLabelId memLabel)
: m_Chunk(0),
m_AllocChunk(0),
m_DeallocChunk(0),
m_MemLabel(memLabel)
{
// TODO: create chunk right away?
//create_chunk()
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline
FixedSizeAllocator<BlockSize>::~FixedSizeAllocator()
{
free_memory();
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline bool
FixedSizeAllocator<BlockSize>::mem_from( void* mem, Chunk* chunk )
{
return ( (UInt8*)mem >= (UInt8*)chunk->data
// TODO: align into account
&& (UInt8*)mem < (UInt8*)chunk->data + BlocksInChunk*BlockSize );
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void*
FixedSizeAllocator<BlockSize>::alloc_from( Chunk* chunk )
{
AssertBreak( chunk );
AssertBreak( chunk->total_available );
UInt8* ret = (UInt8*)chunk->data + chunk->first_available*BlockSize;
chunk->first_available = *ret;
--chunk->total_available;
return ret;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::dealloc_from( void* mem, Chunk* chunk )
{
AssertBreak( chunk );
AssertBreak( mem_from(mem, chunk) );
UInt8* release_ptr = (UInt8*)mem;
AssertBreak( (release_ptr - (UInt8*)chunk->data) % BlockSize == 0 );
*release_ptr = chunk->first_available;
chunk->first_available = (release_ptr - (UInt8*)chunk->data) / BlockSize;
++chunk->total_available;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void*
FixedSizeAllocator<BlockSize>::alloc()
{
if( m_AllocChunk == 0 || m_AllocChunk->total_available == 0 )
{
// fallback to linear search
m_AllocChunk = m_Chunk;
while( m_AllocChunk )
{
if( m_AllocChunk->total_available )
break;
m_AllocChunk = m_AllocChunk->next;
}
if( !m_AllocChunk )
create_chunk();
}
return alloc_from(m_AllocChunk);
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::free( void* mem )
{
AssertBreak(m_DeallocChunk);
if( !mem )
return;
if( !mem_from(mem, m_DeallocChunk) )
{
// we want to exploit possible locality
// but for now we store chunks in single-linked list
// fallback to simple linear search;
m_DeallocChunk = m_Chunk;
while( m_DeallocChunk )
{
if( mem_from(mem, m_DeallocChunk) )
break;
m_DeallocChunk = m_DeallocChunk->next;
}
}
AssertBreak(m_DeallocChunk);
dealloc_from( mem, m_DeallocChunk );
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::reset()
{
Chunk* target_chunk = m_Chunk;
while( target_chunk )
{
reset_chunk(target_chunk);
target_chunk = target_chunk->next;
}
m_AllocChunk = m_DeallocChunk = m_Chunk;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline void
FixedSizeAllocator<BlockSize>::free_memory()
{
Chunk* target_chunk = m_Chunk;
while( target_chunk )
{
Chunk* nextChunk = target_chunk->next;
UNITY_FREE(m_MemLabel, target_chunk);
target_chunk = nextChunk;
}
m_AllocChunk = m_DeallocChunk = m_Chunk = 0;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline unsigned
FixedSizeAllocator<BlockSize>::capacity() const
{
unsigned ret = 0;
Chunk* target_chunk = m_Chunk;
while( target_chunk )
{
ret += BlocksInChunk*BlockSize;
target_chunk = target_chunk->next;
}
return ret;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline unsigned
FixedSizeAllocator<BlockSize>::total_free() const
{
unsigned ret = 0;
Chunk* target_chunk = m_Chunk;
while( target_chunk )
{
ret += target_chunk->total_available * BlockSize;
target_chunk = target_chunk->next;
}
return ret;
}
//------------------------------------------------------------------------------
template <unsigned BlockSize>
inline unsigned
FixedSizeAllocator<BlockSize>::total_allocated() const
{
return capacity() - total_free();
}
//==============================================================================
#endif // _ALLOCATOR_FIXEDSIZE_ALLOCATOR
|