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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
#ifndef LINEAR_ALLOCATOR_H_
#define LINEAR_ALLOCATOR_H_
#include <cstddef>
#include <list>
#include "assert.h"
#include "Configuration/UnityConfigure.h"
#if UNITY_XENON
#include <malloc.h>
#endif
#if UNITY_LINUX
#include <stdint.h> // uintptr_t
#endif
#if ENABLE_THREAD_CHECK_IN_ALLOCS
#include "Runtime/Threads/Thread.h"
#endif
#include "Runtime/Allocator/MemoryMacros.h"
struct LinearAllocatorBase
{
static const int kMinimalAlign = 4;
struct Block
{
char* m_Begin;
char* m_Current;
size_t m_Size;
MemLabelId m_Label;
void initialize (size_t size, MemLabelId label)
{
m_Label = label;
m_Current = m_Begin = (char*)UNITY_MALLOC(label,size);
m_Size = size;
}
void reset ()
{
m_Current = m_Begin;
}
void purge ()
{
UNITY_FREE(m_Label, m_Begin);
}
size_t used () const
{
return m_Current - m_Begin;
}
void* current () const
{
return m_Current;
}
size_t available () const
{
return m_Size - used ();
}
size_t padding (size_t alignment) const
{
size_t pad = ((uintptr_t)m_Current - 1 | alignment - 1) + 1 - (uintptr_t)m_Current;
return pad;
}
void* bump (size_t size)
{
Assert (size <= available());
char* p = m_Current;
m_Current += size;
return p;
}
void roll_back (size_t size)
{
Assert (used () >= size);
m_Current -= size;
}
bool belongs (const void* p)
{
//if (p >= m_Begin && p <= m_Begin + m_Size)
// return true;
//return false;
//return p >= m_Begin && p <= m_Begin + m_Size;
return (uintptr_t)p - (uintptr_t)m_Begin <= (uintptr_t)m_Size;
}
void set (void* p)
{
Assert (p >= m_Begin && p < m_Begin + m_Size);
m_Current = (char*)p;
}
};
typedef std::list<Block, STL_ALLOCATOR(kMemPoolAlloc, Block) > block_container;
LinearAllocatorBase (size_t blockSize, MemLabelId label)
: m_Blocks(), m_BlockSize (blockSize), m_AllocLabel (label)
{
}
void add_block (size_t size)
{
m_Blocks.push_back (Block());
size_t blockSize = size > m_BlockSize ? size : m_BlockSize;
m_Blocks.back ().initialize (blockSize, m_AllocLabel);
}
void purge (bool releaseAllBlocks = false)
{
if (m_Blocks.empty ())
return;
block_container::iterator begin = m_Blocks.begin ();
if (!releaseAllBlocks)
begin++;
for (block_container::iterator it = begin, end = m_Blocks.end (); it != end; ++it)
it->purge ();
m_Blocks.erase (begin, m_Blocks.end ());
if (!releaseAllBlocks)
m_Blocks.back ().reset ();
}
bool belongs (const void* p)
{
for (block_container::iterator it = m_Blocks.begin (), end = m_Blocks.end (); it != end; ++it)
{
if (it->belongs (p))
return true;
}
return false;
}
void* current () const
{
return m_Blocks.empty () ? 0 : m_Blocks.back ().current ();
}
void rewind (void* mark)
{
for (block_container::iterator it = m_Blocks.end (); it != m_Blocks.begin (); --it)
{
block_container::iterator tit = it;
--tit;
if (tit->belongs (mark)) {
tit->set (mark);
for (block_container::iterator temp = it; temp != m_Blocks.end (); ++temp)
temp->purge ();
m_Blocks.erase (it, m_Blocks.end ());
break;
}
}
}
protected:
block_container m_Blocks;
size_t m_BlockSize;
MemLabelId m_AllocLabel;
};
struct ForwardLinearAllocator : public LinearAllocatorBase
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
Thread::ThreadID m_AllocThread, m_DeallocThread;
int m_Allocated;
bool m_RequireDeallocation;
void SetThreadIDs (Thread::ThreadID allocThread, Thread::ThreadID deallocThread)
{
m_AllocThread = allocThread;
m_DeallocThread = deallocThread;
}
void SetRequireDeallocation (bool v)
{
m_RequireDeallocation = v;
}
#endif
ForwardLinearAllocator (size_t blockSize, MemLabelId label)
: LinearAllocatorBase (blockSize, label)
#if ENABLE_THREAD_CHECK_IN_ALLOCS
, m_AllocThread (0), m_DeallocThread (0), m_Allocated(0), m_RequireDeallocation(false)
#endif
{
}
~ForwardLinearAllocator ()
{
purge (true);
}
size_t GetAllocatedBytes() const
{
size_t s = 0;
for (block_container::const_iterator it = m_Blocks.begin (); it != m_Blocks.end(); ++it)
s += it->used();
return s;
}
void* allocate (size_t size, size_t alignment = 4)
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
ErrorIf (!Thread::EqualsCurrentThreadIDForAssert (m_AllocThread));
m_Allocated++;
#endif
// Assert (size == AlignUIntPtr (size, kMinimalAlign));
if (m_Blocks.empty ())
add_block (size);
Block* block = &m_Blocks.back ();
size_t padding = block->padding (alignment);
if (size + padding > block->available ()) {
add_block (size);
block = &m_Blocks.back ();
}
uintptr_t p = (uintptr_t)block->bump (size + padding);
return (void*)(p + padding);
}
void deallocate (void* dealloc)
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
ErrorIf (Thread::GetCurrentThreadID () != m_DeallocThread);
m_Allocated--;
#endif
}
void deallocate_no_thread_check (void* dealloc)
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
m_Allocated--;
#endif
}
void purge (bool releaseAllBlocks = false)
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
ErrorIf (Thread::GetCurrentThreadID () != m_DeallocThread && m_DeallocThread != 0);
ErrorIf (m_RequireDeallocation && m_Allocated != 0);
#endif
LinearAllocatorBase::purge (releaseAllBlocks);
}
void rewind (void* mark)
{
#if ENABLE_THREAD_CHECK_IN_ALLOCS
ErrorIf (Thread::GetCurrentThreadID () != m_DeallocThread);
#endif
LinearAllocatorBase::rewind (mark);
}
using LinearAllocatorBase::current;
using LinearAllocatorBase::belongs;
};
/*
// std::allocator concept implementation for ForwardLinearAllocator objects.
// use it to make STL use your *locally* created ForwardLinearAllocator object
// example:
// void HeavyMemoryAllocationFuncion ()
// {
// ForwardLinearAllocator fwdalloc (1024);
// std::vector<int, forward_linear_allocator<int> > container (forward_linear_allocator<int>(fwdalloc));
//
// // use vector
//
// // memory is clean up automatically
// }
template<class T>
class forward_linear_allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef forward_linear_allocator<U> other; };
forward_linear_allocator(ForwardLinearAllocator& al) throw() : m_LinearAllocator (al) {}
forward_linear_allocator(const forward_linear_allocator& al) throw() : m_LinearAllocator (al.m_LinearAllocator) {}
template <class U> forward_linear_allocator(const forward_linear_allocator<U>& al) throw() : m_LinearAllocator (al.m_LinearAllocator) {}
~forward_linear_allocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type count, void const* hint = 0)
{ return (pointer)m_LinearAllocator.allocate (count * sizeof(T)); }
void deallocate(pointer p, size_type n)
{ m_LinearAllocator.deallocate(p); }
size_type max_size() const throw()
{ return 0x80000000; }
void construct(pointer p, const T& val)
{ new (p) T( val ); }
void destroy(pointer p)
{ p->~T(); }
private:
ForwardLinearAllocator& m_LinearAllocator;
};
// this a global ForwardLinearAllocator object that can be used to allocate (and release) memory from
// anywhere in the program. Caller is responsible for tracking memory used in total
// (use ForwardLinearAllocator::current/ForwardLinearAllocator::rewind to save and restore memory pointer)
extern ForwardLinearAllocator g_ForwardFrameAllocator;
// std::allocator concept for global ForwardLinearAllocator object
// this is just to save one indirection, otherwise forward_linear_allocator referencing a global ForwardLinearAllocator object could be used
template<class T>
class global_linear_allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef global_linear_allocator<U> other; };
global_linear_allocator() {}
global_linear_allocator(const global_linear_allocator&) throw() {}
template <class U> global_linear_allocator(const global_linear_allocator<U>&) throw() {}
~global_linear_allocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type count, void const* hint = 0)
{ return (pointer)g_ForwardFrameAllocator.allocate (count * sizeof(T)); }
void deallocate(pointer p, size_type n)
{ g_ForwardFrameAllocator.deallocate(p); }
bool operator==(global_linear_allocator const& a) const
{ return true; }
bool operator!=(global_linear_allocator const& a) const
{ return false; }
size_type max_size() const throw()
{ return 0x7fffffff; }
void construct(pointer p, const T& val)
{ new (p) T( val ); }
void destroy(pointer p)
{ p->~T(); }
};
#define DECLARE_GLOBAL_LINEAR_ALLOCATOR_MEMBER_NEW_DELETE \
public: \
inline void* operator new( size_t size ) { return g_ForwardFrameAllocator.allocate(size); } \
inline void operator delete( void* p ) { g_ForwardFrameAllocator.deallocate(p); }
inline void* operator new (size_t size, ForwardLinearAllocator& al) { return al.allocate (size); }
inline void* operator new [] (size_t size, ForwardLinearAllocator& al) { return al.allocate (size); }
inline void operator delete (void* p, ForwardLinearAllocator& al) { }
inline void operator delete [] (void* p, ForwardLinearAllocator& al) { }
*/
#endif
|