-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmalloc.c
243 lines (196 loc) · 6.47 KB
/
malloc.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
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
#include "global.h"
#include "malloc.h"
#if TESTING
#include "test/test.h"
#endif
static void *sHeapStart;
static u32 sHeapSize;
ALIGNED(4) EWRAM_DATA u8 gHeap[HEAP_SIZE] = {0};
void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size)
{
struct MemBlock *header = (struct MemBlock *)block;
header->allocated = FALSE;
header->locationHi = 0;
header->magic = MALLOC_SYSTEM_ID;
header->size = size;
header->locationLo = 0;
header->prev = prev;
header->next = next;
}
void PutFirstMemBlockHeader(void *block, u32 size)
{
PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - sizeof(struct MemBlock));
}
void *AllocInternal(void *heapStart, u32 size, const char *location)
{
struct MemBlock *pos = (struct MemBlock *)heapStart;
struct MemBlock *head = pos;
struct MemBlock *splitBlock;
u32 foundBlockSize;
// Alignment
if (size & 3)
size = 4 * ((size / 4) + 1);
for (;;)
{
// Loop through the blocks looking for unused block that's big enough.
if (!pos->allocated)
{
foundBlockSize = pos->size;
if (foundBlockSize >= size)
{
if (foundBlockSize - size < 2 * sizeof(struct MemBlock))
{
// The block isn't much bigger than the requested size,
// so just use it.
pos->allocated = TRUE;
}
else
{
// The block is significantly bigger than the requested
// size, so split the rest into a separate block.
foundBlockSize -= sizeof(struct MemBlock);
foundBlockSize -= size;
splitBlock = (struct MemBlock *)(pos->data + size);
pos->allocated = TRUE;
pos->size = size;
PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize);
pos->next = splitBlock;
if (splitBlock->next != head)
splitBlock->next->prev = splitBlock;
}
pos->locationHi = ((uintptr_t)location) >> 14;
pos->locationLo = (uintptr_t)location;
return pos->data;
}
}
if (pos->next == head)
{
#if TESTING
const struct MemBlock *head = HeapHead();
const struct MemBlock *block = head;
do
{
if (block->allocated)
{
const char *location = MemBlockLocation(block);
if (location)
Test_MgbaPrintf("%s: %d bytes allocated", location, block->size);
else
Test_MgbaPrintf("<unknown>: %d bytes allocated", block->size);
}
block = block->next;
}
while (block != head);
Test_ExitWithResult(TEST_RESULT_ERROR, SourceLine(0), ":L%s:%d, %s: OOM allocating %d bytes", gTestRunnerState.test->filename, SourceLine(0), location, size);
#endif
return NULL;
}
pos = pos->next;
}
}
void FreeInternal(void *heapStart, void *pointer)
{
if (pointer)
{
struct MemBlock *head = (struct MemBlock *)heapStart;
struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock));
block->allocated = FALSE;
// If the freed block isn't the last one, merge with the next block
// if it's not in use.
if (block->next != head)
{
if (!block->next->allocated)
{
block->size += sizeof(struct MemBlock) + block->next->size;
block->next->magic = 0;
block->next = block->next->next;
if (block->next != head)
block->next->prev = block;
}
}
// If the freed block isn't the first one, merge with the previous block
// if it's not in use.
if (block != head)
{
if (!block->prev->allocated)
{
block->prev->next = block->next;
if (block->next != head)
block->next->prev = block->prev;
block->magic = 0;
block->prev->size += sizeof(struct MemBlock) + block->size;
}
}
}
}
void *AllocZeroedInternal(void *heapStart, u32 size, const char *location)
{
void *mem = AllocInternal(heapStart, size, location);
if (mem != NULL)
{
if (size & 3)
size = 4 * ((size / 4) + 1);
CpuFill32(0, mem, size);
}
return mem;
}
bool32 CheckMemBlockInternal(void *heapStart, void *pointer)
{
struct MemBlock *head = (struct MemBlock *)heapStart;
struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock));
if (block->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->next->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->next != head && block->next->prev != block)
return FALSE;
if (block->prev->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->prev != head && block->prev->next != block)
return FALSE;
if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size))
return FALSE;
return TRUE;
}
void InitHeap(void *heapStart, u32 heapSize)
{
sHeapStart = heapStart;
sHeapSize = heapSize;
PutFirstMemBlockHeader(heapStart, heapSize);
}
void *Alloc_(u32 size, const char *location)
{
return AllocInternal(sHeapStart, size, location);
}
void *AllocZeroed_(u32 size, const char *location)
{
return AllocZeroedInternal(sHeapStart, size, location);
}
void Free(void *pointer)
{
FreeInternal(sHeapStart, pointer);
}
bool32 CheckMemBlock(void *pointer)
{
return CheckMemBlockInternal(sHeapStart, pointer);
}
bool32 CheckHeap()
{
struct MemBlock *pos = (struct MemBlock *)sHeapStart;
do {
if (!CheckMemBlockInternal(sHeapStart, pos->data))
return FALSE;
pos = pos->next;
} while (pos != (struct MemBlock *)sHeapStart);
return TRUE;
}
const struct MemBlock *HeapHead(void)
{
return (const struct MemBlock *)sHeapStart;
}
const char *MemBlockLocation(const struct MemBlock *block)
{
if (!block->allocated)
return NULL;
return (const char *)(ROM_START | (block->locationHi << 14) | block->locationLo);
}