-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.c
205 lines (171 loc) · 6.22 KB
/
alloc.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
#include "alloc.h"
#include "vsprintf.h"
u8 *RAM;//[_AHEAP_SIZE_TOTAL];
HeapInfoEntry *HeapInfoEntries=(HeapInfoEntry *)NULL;
extern u32 DRAMRead( u32 a );
extern void DRAMWrite( u32 a, u32 b );
void HeapInit( u8 *Offset )
{
//RAM = (u8*)0xFFFE4000;
//RAM = (u8*)0x13600000;
//RAM = (u8*)0x00600000;
RAM = Offset;
HeapInfoEntries = (HeapInfoEntry*)(RAM+_AHEAP_SIZE);
memset32( HeapInfoEntries, 0, _AHEAP_INFO_SIZE );
while( HeapInfoEntries[0].Offset != 0 )
{
EXIControl(1);
dbgprintf("Alloc:Failed to clear memory!:%08X", HeapInfoEntries[0].Offset );
Shutdown();
}
// dbgprintf("Cleared 0x%04X bytes Space for %d allocs\n", _AHEAP_INFO_SIZE, _AHEAP_INFO_SIZE / 8 );
}
void *malloc( u32 _size )
{
if( _size == 0 )
return NULL;
if( _size > _AHEAP_SIZE )
return NULL;
//align size to 32, easy cheat toallow all allocs to be aligned easily
u32 size = (_size+0x1F) & (~0x1F);
//find a free entry to be used
u32 entry = 0xdeadbeef;
u32 i;
for( i=0; i < _AHEAP_INFO_SIZE / sizeof(HeapInfoEntry); ++i )
{
if( HeapInfoEntries[i].Offset == 0 )
{
entry = i;
break;
}
}
if( entry == 0xdeadbeef )
{
EXIControl(1);
dbgprintf("Alloc: run out of entries!\n");
while(1);
}
//dbgprintf("Alloc:Using entry:%d to alloc %u(%u) bytes...\n", entry, size, _size );
//Now we search a used entry
u32 used_entry = 0xdeadbeef;
for( i=0; i < _AHEAP_INFO_SIZE / sizeof(HeapInfoEntry); ++i )
{
if( HeapInfoEntries[i].Offset == 0 )
continue;
used_entry = i;
break;
}
if( used_entry == 0xdeadbeef )
{
//dbgprintf("There are no other entries used atm\n");
HeapInfoEntries[entry].Offset = RAM;
HeapInfoEntries[entry].Size = size;
//dbgprintf("alloc1: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[entry].Offset, HeapInfoEntries[entry].Size, entry );
return HeapInfoEntries[entry].Offset;
}
find_space:
;
//dbgprintf("[%02d]Offset:%08X Size:%08X\n", used_entry, HeapInfoEntries[used_entry].Offset, HeapInfoEntries[used_entry].Size );
//now we search for the next closest and the previous closest entry
u32 next = 0xdeadbeef;
u32 prev = 0xdeadbeef;
for( i=0; i < _AHEAP_INFO_SIZE / sizeof(HeapInfoEntry); ++i )
{
if( HeapInfoEntries[i].Offset == 0 )
continue;
if( used_entry == i )
continue;
if( next == 0xdeadbeef )
{
if( HeapInfoEntries[i].Offset > HeapInfoEntries[used_entry].Offset )
next = i;
} else {
if( HeapInfoEntries[i].Offset < HeapInfoEntries[next].Offset && HeapInfoEntries[i].Offset > HeapInfoEntries[used_entry].Offset )
next = i;
}
if( prev == 0xdeadbeef )
{
if( HeapInfoEntries[i].Offset < HeapInfoEntries[used_entry].Offset )
prev = i;
} else {
if( HeapInfoEntries[i].Offset > HeapInfoEntries[prev].Offset && HeapInfoEntries[i].Offset < HeapInfoEntries[used_entry].Offset )
prev = i;
}
}
if( next == 0xdeadbeef )
{
//dbgprintf("This is the last entry\n");
//check if there is engough space left for our alloc
if( (u32)(HeapInfoEntries[used_entry].Offset-RAM) + HeapInfoEntries[used_entry].Size + size <= _AHEAP_SIZE )
{
HeapInfoEntries[entry].Offset = HeapInfoEntries[used_entry].Offset + HeapInfoEntries[used_entry].Size;
HeapInfoEntries[entry].Size = size;
//dbgprintf("alloc2: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[entry].Offset, HeapInfoEntries[entry].Size, entry );
return HeapInfoEntries[entry].Offset;
}
;//dbgprintf("2Not enough space left only had:%d\n", HEAP_SIZE - ((u32)(HeapInfoEntries[used_entry].Offset-RAM) + HeapInfoEntries[used_entry].Size) );
} else if( (u32)(HeapInfoEntries[used_entry].Offset) + HeapInfoEntries[used_entry].Size + size < (u32)(HeapInfoEntries[next].Offset) )
{
HeapInfoEntries[entry].Offset = HeapInfoEntries[used_entry].Offset + HeapInfoEntries[used_entry].Size;
HeapInfoEntries[entry].Size = size;
//dbgprintf("alloc4: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[entry].Offset, HeapInfoEntries[entry].Size, entry );
return HeapInfoEntries[entry].Offset;
} else {
;//dbgprintf("4Not enough space left only had:%d %d:%d\n", (u32)( HeapInfoEntries[next].Offset - HeapInfoEntries[used_entry].Offset ) - HeapInfoEntries[used_entry].Size, next, used_entry );
}
if( prev == 0xdeadbeef )
{
//dbgprintf("This is the first entry\n");
if( (u32)(HeapInfoEntries[used_entry].Offset-RAM) >= size )
{
HeapInfoEntries[entry].Offset = HeapInfoEntries[used_entry].Offset - size;
HeapInfoEntries[entry].Size = size;
//dbgprintf("alloc3: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[entry].Offset, HeapInfoEntries[entry].Size, entry );
return HeapInfoEntries[entry].Offset;
}
;//dbgprintf("3Not enough space left only had:%d\n", (u32)(HeapInfoEntries[used_entry].Offset-RAM) );
} else if( (u32)(HeapInfoEntries[prev].Offset) + HeapInfoEntries[prev].Size + size < (u32)(HeapInfoEntries[used_entry].Offset) )
{
HeapInfoEntries[entry].Offset = HeapInfoEntries[prev].Offset + HeapInfoEntries[prev].Size;
HeapInfoEntries[entry].Size = size;
//dbgprintf("alloc5: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[entry].Offset, HeapInfoEntries[entry].Size, entry );
return HeapInfoEntries[entry].Offset;
} else {
;//dbgprintf("5Not enough space left only had:%d\n", (u32)(HeapInfoEntries[used_entry].Offset-HeapInfoEntries[prev].Offset) - HeapInfoEntries[prev].Size );
}
//if we land here we have to go to the next entry
u32 temp = used_entry + 1;
used_entry = 0xdeadbeef;
for( i=temp; i < _AHEAP_INFO_SIZE / sizeof(HeapInfoEntry); ++i )
{
if( HeapInfoEntries[i].Offset == 0 )
continue;
used_entry = i;
break;
}
if( used_entry != 0xdeadbeef )
goto find_space;
dbgprintf("Alloc:failed to alloc %d bytes\n", size );
return NULL;
}
void *malloca( u32 size, u32 align )
{
return malloc( size );
}
void free( void *ptr )
{
if( ptr == NULL )
return;
u32 i;
for( i=0; i < _AHEAP_INFO_SIZE / sizeof(HeapInfoEntry); ++i )
{
if( HeapInfoEntries[i].Offset == ptr )
{
//dbgprintf("free: ptr:%p size:%08X Entry:%d\n", HeapInfoEntries[i].Offset, HeapInfoEntries[i].Size, i );
HeapInfoEntries[i].Offset = NULL;
HeapInfoEntries[i].Size = 0;
ptr = NULL;
return;
}
}
}