-
Notifications
You must be signed in to change notification settings - Fork 72
/
objalloc.c
161 lines (145 loc) · 3.94 KB
/
objalloc.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
#include "objalloc.h"
#include "initialize.h"
#include "memory.h"
#include <__mem.h>
#include <dolphin/os/OSAlloc.h>
static objheap obj_heap = { 0, 0, -1, -1 };
static HSD_ObjAllocData* alloc_datas;
void HSD_ObjSetHeap(u32 size, void* ptr)
{
obj_heap.curr = (u32) ptr;
obj_heap.top = (u32) ptr;
obj_heap.remain = size;
obj_heap.size = size;
}
s32 HSD_ObjAllocAddFree(HSD_ObjAllocData* data, u32 num)
{
u32 computed_start;
u32 pool_end;
u32 pool_size;
u8* pool_start;
u8 _[4];
HSD_ASSERT(0xEE, data);
pool_size = data->size * num;
if (obj_heap.top != 0) {
pool_end = obj_heap.top + obj_heap.size;
computed_start = (obj_heap.curr + data->align) & ~data->align;
pool_start = (void*) computed_start;
if (computed_start > pool_end) {
return 0;
}
if (pool_end - (u32) pool_start < pool_size) {
pool_size = pool_end - (u32) pool_start -
(pool_end - (u32) pool_start) % data->size;
}
num = pool_size / data->size;
if (num == 0) {
return 0;
}
obj_heap.curr = (u32) pool_start + pool_size;
obj_heap.remain = pool_end - obj_heap.curr;
} else {
pool_start = HSD_MemAlloc(pool_size);
if (pool_start == 0) {
return 0;
}
obj_heap.remain -= pool_size;
}
{
int i;
for (i = 0; (unsigned) i < num - 1; i++) {
*(void**) (pool_start + data->size * i) =
(void*) (pool_start + data->size * (i + 1));
}
*(void**) (pool_start + data->size * i) = data->freehead;
}
data->freehead = (HSD_ObjAllocLink*) pool_start;
data->free += num;
return num;
}
void* HSD_ObjAlloc(HSD_ObjAllocData* data)
{
HSD_ObjAllocLink* cur;
u32 size;
if (data->num_limit_flag && data->used >= data->num_limit) {
return NULL;
}
if (data->heap_limit_flag) {
if (data->heap_limit_num == (unsigned) -1) {
if (obj_heap.top != 0) {
size = obj_heap.remain;
} else {
size = OSCheckHeap(HSD_GetHeap());
}
if (size <= data->heap_limit_size) {
data->heap_limit_num = data->used + data->free;
}
} else {
if (obj_heap.top != 0) {
size = obj_heap.remain;
} else {
size = OSCheckHeap(HSD_GetHeap());
}
if (size > data->heap_limit_size) {
data->heap_limit_num = -1;
}
}
if (data->used >= data->heap_limit_num) {
return NULL;
}
}
if (data->free == 0) {
HSD_ObjAllocAddFree(data, 1);
if (data->free == 0) {
return NULL;
}
}
cur = data->freehead;
data->freehead = cur->next;
data->used += 1;
data->free -= 1;
if (data->used > data->peak) {
data->peak = data->used;
}
return cur;
}
void HSD_ObjFree(HSD_ObjAllocData* data, void* obj)
{
HSD_ObjAllocLink* link = obj;
link->next = data->freehead;
data->freehead = link;
data->free += 1;
data->used -= 1;
}
inline void removeAll(HSD_ObjAllocData* data)
{
HSD_ObjAllocData** cur = &alloc_datas;
while (*cur != NULL) {
if (*cur == data) {
*cur = (*cur)->next;
} else {
cur = &(*cur)->next;
}
}
}
void HSD_ObjAllocInit(HSD_ObjAllocData* data, size_t size, u32 align)
{
HSD_ASSERT(0x185, data);
if (data != NULL) {
removeAll(data);
} else {
alloc_datas = NULL;
}
memset(data, 0, sizeof(HSD_ObjAllocData));
data->num_limit = -1;
data->heap_limit_size = 0;
data->heap_limit_num = -1;
data->align = align - 1;
data->size = (size + data->align) & ~data->align;
data->next = alloc_datas;
alloc_datas = data;
}
void _HSD_ObjAllocForgetMemory(void* low, void* high)
{
alloc_datas = NULL;
}