-
Notifications
You must be signed in to change notification settings - Fork 26
/
fb_allocator.c
214 lines (166 loc) · 5.32 KB
/
fb_allocator.c
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
#include "fb_allocator.h"
#include "DataTypes.h"
#include "Fault.h"
#include <string.h>
// Define USE_LOCK to use the default lock implementation
#define USE_LOCKS
#ifdef USE_LOCKS
#include "LockGuard.h"
static LOCK_HANDLE _hLock;
#else
#pragma message("WARNING: Define software lock.")
typedef int LOCK_HANDLE;
static LOCK_HANDLE _hLock;
#define LK_CREATE() (1)
#define LK_DESTROY(h)
#define LK_LOCK(h)
#define LK_UNLOCK(h)
#endif
// Get a pointer to the client's area within a memory block
#define GET_CLIENT_PTR(_block_ptr_) \
(_block_ptr_ ? ((void*)((char*)_block_ptr_)) : NULL)
// Get a pointer to the block using a client pointer
#define GET_BLOCK_PTR(_client_ptr_) \
(_client_ptr_ ? ((void*)((char*)_client_ptr_)) : NULL)
static void* ALLOC_NewBlock(ALLOC_Allocator* alloc);
static void ALLOC_Push(ALLOC_Allocator* alloc, void* pBlock);
static void* ALLOC_Pop(ALLOC_Allocator* alloc);
//----------------------------------------------------------------------------
// ALLOC_NewBlock
//----------------------------------------------------------------------------
static void* ALLOC_NewBlock(ALLOC_Allocator* self)
{
ALLOC_Block* pBlock = NULL;
LK_LOCK(_hLock);
// If we have not exceeded the pool maximum
if (self->poolIndex < self->maxBlocks)
{
// Get pointer to a new fixed memory block within the pool
pBlock = (void*)(self->pPool + (self->poolIndex++ * self->blockSize));
}
LK_UNLOCK(_hLock);
if (!pBlock)
{
// Out of fixed block memory
ASSERT();
}
return pBlock;
}
//----------------------------------------------------------------------------
// ALLOC_Push
//----------------------------------------------------------------------------
static void ALLOC_Push(ALLOC_Allocator* self, void* pBlock)
{
if (!pBlock)
return;
// Get a pointer to the client's location within the block
ALLOC_Block* pClient = (ALLOC_Block*)GET_CLIENT_PTR(pBlock);
LK_LOCK(_hLock);
// Point client block's next pointer to head
pClient->pNext = self->pHead;
// The client block is now the new head
self->pHead = pClient;
LK_UNLOCK(_hLock);
}
//----------------------------------------------------------------------------
// ALLOC_Pop
//----------------------------------------------------------------------------
static void* ALLOC_Pop(ALLOC_Allocator* self)
{
ALLOC_Block* pBlock = NULL;
LK_LOCK(_hLock);
// Is the free-list empty?
if (self->pHead)
{
// Remove the head block
pBlock = self->pHead;
// Set the head to the next block
self->pHead = self->pHead->pNext;
}
LK_UNLOCK(_hLock);
return GET_BLOCK_PTR(pBlock);
}
//----------------------------------------------------------------------------
// ALLOC_Init
//----------------------------------------------------------------------------
void ALLOC_Init()
{
_hLock = LK_CREATE();
}
//----------------------------------------------------------------------------
// ALLOC_Term
//----------------------------------------------------------------------------
void ALLOC_Term()
{
LK_DESTROY(_hLock);
}
//----------------------------------------------------------------------------
// ALLOC_Alloc
//----------------------------------------------------------------------------
void* ALLOC_Alloc(ALLOC_HANDLE hAlloc, size_t size)
{
ALLOC_Allocator* self = NULL;
void* pBlock = NULL;
ASSERT_TRUE(hAlloc);
// Convert handle to an ALLOC_Allocator instance
self = (ALLOC_Allocator*)hAlloc;
// Ensure requested size fits within memory block
ASSERT_TRUE(size <= self->blockSize);
// Get a block from the free-list
pBlock = ALLOC_Pop(self);
// If the free-list empty?
if (!pBlock)
{
// Get a new block from the pool
pBlock = ALLOC_NewBlock(self);
}
if (pBlock)
{
// Keep track of usage statistics
self->allocations++;
self->blocksInUse++;
if (self->blocksInUse > self->maxBlocksInUse)
{
self->maxBlocksInUse = self->blocksInUse;
}
}
return GET_CLIENT_PTR(pBlock);
}
//----------------------------------------------------------------------------
// ALLOC_Calloc
//----------------------------------------------------------------------------
void* ALLOC_Calloc(ALLOC_HANDLE hAlloc, size_t num, size_t size)
{
void* pMem = NULL;
size_t n = 0;
ASSERT_TRUE(hAlloc);
// Compute the total size of the block
n = num * size;
// Allocate the memory
pMem = ALLOC_Alloc(hAlloc, n);
if (pMem != NULL)
{
// Initialize memory to 0 per calloc behavior
memset(pMem, 0, n);
}
return pMem;
}
//----------------------------------------------------------------------------
// ALLOC_Free
//----------------------------------------------------------------------------
void ALLOC_Free(ALLOC_HANDLE hAlloc, void* pBlock)
{
ALLOC_Allocator* self = NULL;
if (!pBlock)
return;
ASSERT_TRUE(hAlloc);
// Cast handle to an allocator instance
self = (ALLOC_Allocator*)hAlloc;
// Get a pointer to the block
pBlock = GET_BLOCK_PTR(pBlock);
// Push the block onto a stack (i.e. the free-list)
ALLOC_Push(self, pBlock);
// Keep track of usage statistics
self->deallocations++;
self->blocksInUse--;
}