forked from DizzyEggg/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.c
210 lines (161 loc) · 5.5 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
#include "global.h"
static void *sHeapStart;
static u32 sHeapSize;
static u32 malloc_c_unused_0300000c; // needed to align dma3_manager.o(.bss)
#define MALLOC_SYSTEM_ID 0xA3A3
struct MemBlock {
// Whether this block is currently allocated.
bool16 flag;
// Magic number used for error checking. Should equal MALLOC_SYSTEM_ID.
u16 magic;
// Size of the block (not including this header struct).
u32 size;
// Previous block pointer. Equals sHeapStart if this is the first block.
struct MemBlock *prev;
// Next block pointer. Equals sHeapStart if this is the last block.
struct MemBlock *next;
// Data in the memory block. (Arrays of length 0 are a GNU extension.)
u8 data[0];
};
void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size)
{
struct MemBlock *header = (struct MemBlock *)block;
header->flag = FALSE;
header->magic = MALLOC_SYSTEM_ID;
header->size = size;
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)
{
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->flag) {
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->flag = 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->flag = TRUE;
pos->size = size;
PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize);
pos->next = splitBlock;
if (splitBlock->next != head)
splitBlock->next->prev = splitBlock;
}
return pos->data;
}
}
if (pos->next == head)
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->flag = 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->flag) {
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->flag) {
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)
{
void *mem = AllocInternal(heapStart, size);
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)
{
return AllocInternal(sHeapStart, size);
}
void *AllocZeroed(u32 size)
{
return AllocZeroedInternal(sHeapStart, size);
}
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;
}