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
|
//
// Created by mart on 4/23/20.
//
#include "os_alloc.h"
#include "consts.h"
#include "os_system.h"
extern Cell* DLInsert(Cell* list, Cell* cell);
extern Cell* DLAddFront(Cell* list, Cell* cell);
void* OSiHeapInfo[OS_ARENA_MAX] = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
#ifdef MATCH_ASM
asm static Cell* DLExtract(Cell* list, Cell* cell)
{
ldr r3, [r1, #0x4]
cmp r3, #0x0
ldrne r2, [r1, #0x0]
strne r2, [r3, #0x0]
ldr r2, [r1, #0x0]
cmp r2, #0x0
ldreq r0, [r1, #0x4]
ldrne r1, [r1, #0x4]
strne r1, [r2, #0x4]
bx lr
}
#else
static Cell* DLExtract(Cell* list, Cell* cell)
{
if (cell->next) {
cell->prev = cell->next->prev;
}
if (cell->prev) {
cell->prev->next = cell->next;
return list;
}
else {
return cell->next;
}
}
#endif
#define HEADERSIZE OSi_ROUND(sizeof(Cell), 32)
#define MINOBJSIZE (HEADERSIZE+32)
#ifdef MATCH_ASM
asm void OS_FreeToHeap(OSArenaId id, OSHeapHandle heap, void* ptr) {
stmdb sp!, {r4-r7, lr}
sub sp,sp,#0x4
mov r7,r0
mov r5,r1
mov r4,r2
bl OS_DisableInterrupts
ldr r1,=OSiHeapInfo
mov r6,r0
ldr r0,[r1,r7,lsl #0x2]
cmp r5,#0x0
ldrlt r5,[r0,#0x0]
ldr r1,[r0,#0x10]
mov r0,#0xc
mla r7,r5,r0,r1
sub r4,r4,#0x20
ldr r0,[r7,#0x8]
mov r1,r4
bl DLExtract
str r0,[r7,#0x8]
ldr r0,[r7,#0x4]
mov r1,r4
bl DLInsert
str r0,[r7,#0x4]
mov r0,r6
bl OS_RestoreInterrupts
add sp,sp,#0x4
ldmia sp!,{ r4-r7, lr }
bx lr
}
#else
void OS_FreeToHeap(OSArenaId id, OSHeapHandle heap, void* ptr) {
OSHeapInfo *heapInfo;
HeapDesc *hd;
Cell *cell;
OSIntrMode enabled = OS_DisableInterrupts();
heapInfo = OSiHeapInfo[id];
if (heap < 0) {
heap = heapInfo->currentHeap;
}
cell = (Cell *) ((char *)ptr - HEADERSIZE);
hd = &heapInfo->heapArray[heap];
hd->allocated = DLExtract(hd->allocated, cell);
hd->free = DLInsert(hd->free, cell);
OS_RestoreInterrupts(enabled);
}
#endif
#ifdef MATCH_ASM
asm void* OS_AllocFromHeap(OSArenaId id, OSHeapHandle heap, u32 size) {
stmdb sp!, {r4-r7,lr}
sub sp, sp, #0x4
mov r6, r0
mov r5, r1
mov r7, r2
bl OS_DisableInterrupts
ldr r1, =OSiHeapInfo
mov r4, r0
ldr r1, [r1, r6, lsl #0x2]
cmp r1, #0x0
bne _020CC7AC
bl OS_RestoreInterrupts
add sp, sp, #0x4
mov r0, #0x0
ldmia sp!, {r4-r7,lr}
bx lr
_020CC7AC:
cmp r5, #0x0
ldrlt r5, [r1, #0x0]
ldr r1, [r1, #0x10]
mov r0, #0xc
mla r6, r5, r0, r1
ldr r0, [r6, #0x4]
add r1, r7, #0x20
add r1, r1, #0x1f
mov r5, r0
cmp r0, #0x0
bic r7, r1, #0x1f
beq _020CC7F4
_020CC7DC:
ldr r1, [r5, #0x8]
cmp r7, r1
ble _020CC7F4
ldr r5, [r5, #0x4]
cmp r5, #0x0
bne _020CC7DC
_020CC7F4:
cmp r5, #0x0
bne _020CC814
mov r0, r4
bl OS_RestoreInterrupts
add sp, sp, #0x4
mov r0, #0x0
ldmia sp!, {r4-r7,lr}
bx lr
_020CC814:
ldr r1, [r5, #0x8]
sub r1, r1, r7
cmp r1, #0x40
bhs _020CC834
mov r1, r5
bl DLExtract
str r0, [r6, #0x4]
b _020CC86C
_020CC834:
str r7, [r5, #0x8]
add r2, r5, r7
str r1, [r2, #0x8]
ldr r0, [r5, #0x0]
str r0, [r5, r7]
ldr r0, [r5, #0x4]
str r0, [r2, #0x4]
ldr r0, [r2, #0x4]
cmp r0, #0x0
strne r2, [r0, #0x0]
ldr r0, [r2, #0x0]
cmp r0, #0x0
strne r2, [r0, #0x4]
streq r2, [r6, #0x4]
_020CC86C:
ldr r0, [r6, #0x8]
mov r1, r5
bl DLAddFront
str r0, [r6, #0x8]
mov r0, r4
bl OS_RestoreInterrupts
add r0, r5, #0x20
add sp, sp, #0x4
ldmia sp!, {r4-r7,lr}
bx lr
}
#else
void* OS_AllocFromHeap(OSArenaId id, OSHeapHandle heap, u32 size) {
OSHeapInfo* heapInfo;
HeapDesc* hd;
Cell* cell;
Cell* newCell;
long leftoverSize;
OSIntrMode enabled = OS_DisableInterrupts();
heapInfo = OSiHeapInfo[id];
if (!heapInfo) {
OS_RestoreInterrupts(enabled);
return NULL;
}
if (heap < 0) {
heap = heapInfo->currentHeap;
}
hd = &heapInfo->heapArray[heap];
size += HEADERSIZE;
size = OSi_ROUND(size, 32);
for (cell = hd->free; cell != NULL; cell = cell->next) {
if ((long)size <= cell->size) {
break;
}
}
if (cell == NULL) {
OS_RestoreInterrupts(enabled);
return NULL;
}
leftoverSize = cell->size - (long)size;
if (leftoverSize < MINOBJSIZE) {
hd->free = DLExtract(hd->free, cell);
} else {
cell->size = (long)size;
newCell = (Cell *) ((char *)cell + size);
newCell->size = leftoverSize;
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;
}
}
hd->allocated = DLAddFront(hd->allocated, cell);
OS_RestoreInterrupts(enabled);
return (void *)((char *)cell + HEADERSIZE);
}
#endif
|