-
Notifications
You must be signed in to change notification settings - Fork 72
/
OSAlloc.c
275 lines (234 loc) · 7.24 KB
/
OSAlloc.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
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
#include <platform.h>
#include <dolphin/os.h>
#include <dolphin/os/OSAlloc.h>
static Heap* HeapArray;
static ssize_t NumHeaps;
static void* ArenaStart;
static void* ArenaEnd;
volatile OSHeapHandle __OSCurrHeap = -1;
#define InRange(addr, start, end) \
((u8*) (start) <= (u8*) (addr) && (u8*) (addr) < (u8*) (end))
#define OFFSET(addr, align) (((uintptr_t) (addr) & ((align) - 1)))
#define ALIGNMENT 32
#define MINOBJSIZE 64
/// Inserts @p cell before @p neighbor and returns @p cell.
static void* DLAddFront(HeapCell* neighbor, HeapCell* cell)
{
cell->next = neighbor;
cell->prev = NULL;
if (neighbor != NULL) {
neighbor->prev = cell;
}
return cell;
}
/// Removes @p cell from @p list and returns @p list. */
static HeapCell* DLExtract(HeapCell* list, HeapCell* cell)
{
if (cell->next != NULL) {
cell->next->prev = cell->prev;
}
if (cell->prev == NULL) {
list = cell->next;
} else {
cell->prev->next = cell->next;
}
return list;
}
/// @param list
/// @param cell
/// @param _ Needed to match #OSFreeToHeap.
static HeapCell* DLInsert(HeapCell* list, HeapCell* cell, UNK_T _)
{
HeapCell* before = NULL;
HeapCell* after = list;
while (after != NULL) {
if (cell <= after) {
break;
}
before = after;
after = after->next;
}
cell->next = after;
cell->prev = before;
if (after != NULL) {
after->prev = cell;
if ((u8*) cell + cell->size == (u8*) after) {
cell->size += after->size;
after = after->next;
cell->next = after;
if (after != NULL) {
after->prev = cell;
}
}
}
if (before != NULL) {
before->next = cell;
if ((u8*) before + before->size == (u8*) cell) {
before->size += cell->size;
before->next = after;
if (after != NULL) {
after->prev = before;
}
}
return list;
}
return cell;
}
void* OSAllocFromHeap(OSHeapHandle heap, size_t size)
{
Heap* hd = &HeapArray[heap];
size_t sizeAligned = OSRoundUp32B(ALIGNMENT + size);
HeapCell* cell;
size_t leftoverSpace;
// find first cell with enough capacity
for (cell = hd->free; cell != NULL; cell = cell->next) {
if ((signed) sizeAligned <= (signed) cell->size) {
break;
}
}
if (cell == NULL) {
return NULL;
}
leftoverSpace = cell->size - sizeAligned;
if (leftoverSpace < MINOBJSIZE) {
// remove this cell from the free list
hd->free = DLExtract(hd->free, cell);
} else {
// remove this cell from the free list and make a new cell out of the
// remaining space
HeapCell* newcell = (void*) ((u8*) cell + sizeAligned);
cell->size = sizeAligned;
newcell->size = leftoverSpace;
newcell->prev = cell->prev;
newcell->next = cell->next;
if (newcell->next != NULL) {
newcell->next->prev = newcell;
}
if (newcell->prev != NULL) {
newcell->prev->next = newcell;
} else {
hd->free = newcell;
}
}
// add the cell to the beginning of the allocated list
hd->allocated = DLAddFront(hd->allocated, cell);
return (u8*) cell + ALIGNMENT;
}
void OSFreeToHeap(OSHeapHandle heap, void* ptr)
{
HeapCell* cell = (void*) ((u8*) ptr - ALIGNMENT);
Heap* hd = &HeapArray[heap];
HeapCell* list = hd->allocated;
// remove cell from the allocated list
// hd->allocated = DLExtract(hd->allocated, cell);
if (cell->next != NULL) {
cell->next->prev = cell->prev;
}
if (cell->prev == NULL) {
list = cell->next;
} else {
cell->prev->next = cell->next;
}
hd->allocated = list;
hd->free = DLInsert(hd->free, cell, list);
}
OSHeapHandle OSSetCurrentHeap(OSHeapHandle heap)
{
OSHeapHandle old = __OSCurrHeap;
__OSCurrHeap = heap;
return old;
}
void* OSInitAlloc(void* arenaStart, void* arenaEnd, int maxHeaps)
{
u32 totalSize = maxHeaps * sizeof(Heap);
int i;
HeapArray = arenaStart;
NumHeaps = maxHeaps;
for (i = 0; i < NumHeaps; i++) {
Heap* heap = &HeapArray[i];
heap->size = -1;
heap->free = heap->allocated = NULL;
}
__OSCurrHeap = -1;
arenaStart = (u8*) HeapArray + totalSize;
arenaStart = (void*) OSRoundUp32B(arenaStart);
ArenaStart = arenaStart;
ArenaEnd = (void*) OSRoundDown32B(arenaEnd);
return arenaStart;
}
OSHeapHandle OSCreateHeap(void* start, void* end)
{
int i;
HeapCell* cell = (void*) OSRoundUp32B(start);
end = (void*) OSRoundDown32B(end);
for (i = 0; i < NumHeaps; i++) {
Heap* hd = &HeapArray[i];
if (hd->size < 0) {
hd->size = (u8*) end - (u8*) cell;
cell->prev = NULL;
cell->next = NULL;
cell->size = hd->size;
hd->free = cell;
hd->allocated = NULL;
return i;
}
}
return -1;
}
void OSDestroyHeap(size_t idx)
{
*(s32*) &HeapArray[idx] = -1;
}
size_t OSCheckHeap(OSHeapHandle heap)
{
Heap* hd;
HeapCell* cell;
int total = 0;
int totalFree = 0;
#define CHECK(line, condition) \
if (!(condition)) { \
OSReport("OSCheckHeap: Failed " #condition " in %d", line); \
return -1; \
}
CHECK(893, HeapArray)
CHECK(894, 0 <= heap && heap < NumHeaps)
hd = &HeapArray[heap];
CHECK(897, 0 <= hd->size)
CHECK(899, hd->allocated == NULL || hd->allocated->prev == NULL)
for (cell = hd->allocated; cell != NULL; cell = cell->next) {
CHECK(902, InRange(cell, ArenaStart, ArenaEnd))
CHECK(903, OFFSET(cell, ALIGNMENT) == 0)
CHECK(904, cell->next == NULL || cell->next->prev == cell)
CHECK(905, MINOBJSIZE <= cell->size)
CHECK(906, OFFSET(cell->size, ALIGNMENT) == 0)
total += cell->size;
CHECK(909, 0 < total && total <= hd->size)
}
CHECK(917, hd->free == NULL || hd->free->prev == NULL)
for (cell = hd->free; cell != NULL; cell = cell->next) {
CHECK(920, InRange(cell, ArenaStart, ArenaEnd))
CHECK(921, OFFSET(cell, ALIGNMENT) == 0)
CHECK(922, cell->next == NULL || cell->next->prev == cell)
CHECK(923, MINOBJSIZE <= cell->size)
CHECK(924, OFFSET(cell->size, ALIGNMENT) == 0)
CHECK(925, cell->next == NULL ||
(char*) cell + cell->size < (char*) cell->next)
total += cell->size;
totalFree += cell->size - 32;
CHECK(929, 0 < total && total <= hd->size)
}
CHECK(936, total == hd->size);
#undef CHECK
return totalFree;
}
#ifndef BUGFIX
#pragma push
#pragma force_active on
static char _unused_str0[] = "\nOSDumpHeap(%d):\n";
static char _unused_str1[] = "--------Inactive\n";
static char _unused_str2[] = "addr\tsize\t\tend\tprev\tnext\n";
static char _unused_str3[] = "--------Allocated\n";
static char _unused_str4[] = "%x\t%d\t%x\t%x\t%x\n";
static char _unused_str5[] = "--------Free\n";
#pragma pop
#endif