forked from MrColdbird/aemu
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlittlec.c
More file actions
346 lines (287 loc) · 6.79 KB
/
littlec.c
File metadata and controls
346 lines (287 loc) · 6.79 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <pspsdk.h>
#include <pspsysmem.h>
#include <pspthreadman.h>
#include <pspintrman.h>
#include "tinyalloc-master/tinyalloc.h"
#include <stddef.h>
//#define DEBUG 1
#include "atpro/logs.h"
#define USE_TINYALLOC 1
//#define TINYALLOC_USE_PARTITION_MEM 0
#define LOG_MALLOC 0
#define DEBUG_MALLOC 0
#define LOG_CSTRING 0
#define LOG_CSTDLIB 0
#if USE_TINYALLOC
#if TINYALLOC_USE_PARTITION_MEM
static SceUID memblockid = -1;
#elif HEAP_SIZE
static uint8_t heap[HEAP_SIZE * 1024];
#endif
// from atpro
int partition_to_use();
static void tinyalloc_allocate_partition_memory()
{
int interrupts = sceKernelCpuSuspendIntr();
#if TINYALLOC_USE_PARTITION_MEM && HEAP_SIZE
if (memblockid >= 0){
sceKernelCpuResumeIntrWithSync(interrupts);
printk("%s: not allocating heap again\n", __func__);
return;
}
static const SceSize size = HEAP_SIZE * 1024;
memblockid = sceKernelAllocPartitionMemory(partition_to_use(), "tinyalloc heap", PSP_SMEM_High, size, NULL);
if (memblockid < 0)
{
sceKernelCpuResumeIntrWithSync(interrupts);
printk("%s: sceKernelAllocPartitionMemory failed with 0x%x\n", __func__, memblockid);
return;
}
void *block = sceKernelGetBlockHeadAddr(memblockid);
ta_init(block, block + size, 256, 16, 8);
#elif HEAP_SIZE
ta_init(heap, heap + sizeof(heap), 256, 16, 8);
#endif
sceKernelCpuResumeIntrWithSync(interrupts);
#if TINYALLOC_USE_PARTITION_MEM && HEAP_SIZE
printk("%s: allocated %d, id 0x%x, 0x%x\n", __func__, size, memblockid, block);
#else
printk("%s: done\n", __func__);
#endif
}
static void tinyalloc_free_partition_memory()
{
#if TINYALLOC_USE_PARTITION_MEM
int interrupts = sceKernelCpuSuspendIntr();
SceUID old_memblockid = memblockid;
if (memblockid < 0)
{
sceKernelCpuResumeIntrWithSync(interrupts);
printk("%s: no partition memory was allocated\n", __func__);
return;
}
sceKernelFreePartitionMemory(memblockid);
memblockid = -1;
sceKernelCpuResumeIntrWithSync(interrupts);
#endif
#if TINYALLOC_USE_PARTITION_MEM
printk("%s: freed partition memory 0x%x\n", __func__, old_memblockid);
#else
printk("%s: nothing to do in global heap mode\n", __func__);
#endif
}
static void *malloc_tinyalloc(uint32_t size)
{
int interrupts = sceKernelCpuSuspendIntr();
#if TINYALLOC_USE_PARTITION_MEM
if (__builtin_expect(memblockid < 0, 0))
{
sceKernelCpuResumeIntrWithSync(interrupts);
return NULL;
}
#endif
void *buf = ta_alloc(size);
sceKernelCpuResumeIntrWithSync(interrupts);
return buf;
}
static void free_tinyalloc(void *buf)
{
int interrupts = sceKernelCpuSuspendIntr();
#if TINYALLOC_USE_PARTITION_MEM
if (__builtin_expect(memblockid < 0, 0))
{
sceKernelCpuResumeIntrWithSync(interrupts);
return;
}
#endif
ta_free(buf);
sceKernelCpuResumeIntrWithSync(interrupts);
}
#else
static void *malloc_partition(uint32_t size)
{
SceUID blockid = sceKernelAllocPartitionMemory(2, "littlec malloc", PSP_SMEM_Low, size + sizeof(SceUID), NULL);
if (blockid < 0)
{
return NULL;
}
void *block = sceKernelGetBlockHeadAddr(blockid);
((SceUID *)block)[0] = blockid;
printk("%s: allocated %d 0x%x 0x%x", size, blockid, block);
return block + sizeof(SceUID);
}
static void free_partition(void *buf)
{
SceUID blockid = ((SceUID *)buf)[-1];
printk("%s: freeing 0x%x 0x%x", blockid, buf);
sceKernelFreePartitionMemory(blockid);
return;
}
#endif
void init_littlec()
{
#if USE_TINYALLOC
tinyalloc_allocate_partition_memory();
#endif
}
void clean_littlec()
{
#if USE_TINYALLOC
tinyalloc_free_partition_memory();
#endif
}
#if DEBUG_MALLOC
static uint32_t allocated_heap = 0;
#endif
void *malloc(uint32_t size)
{
#if LOG_MALLOC
printk("%s: allocating %d\n", __func__, size);
#endif
#if DEBUG_MALLOC
size += sizeof(uint32_t);
#endif
#if USE_TINYALLOC
void *buf = malloc_tinyalloc(size);
#else
void *buf = malloc_partition(size);
#endif
#if LOG_MALLOC
if (__builtin_expect(buf != NULL, 1))
{
printk("%s: allocated %d, 0x%x\n", __func__, size, buf);
}
else
{
printk("%s: failed alocating %d\n", __func__, size);
return NULL;
}
#endif
#if DEBUG_MALLOC
((uint32_t *)buf)[0] = size;
allocated_heap += size;
printk("%s: allocated heap %d\n", __func__, allocated_heap);
return buf + sizeof(uint32_t);
#else
return buf;
#endif
}
void free(void *buf)
{
#if LOG_MALLOC
printk("%s: freeing 0x%x\n", __func__, buf);
#endif
#if DEBUG_MALLOC
if (buf == 0)
{
printk("%s: freeing NULL from 0x%x\n", __func__, __builtin_return_address(0));
return;
}
#endif
#if DEBUG_MALLOC
buf -= sizeof(uint32_t);
uint32_t size = ((uint32_t *)buf)[0];
allocated_heap -= size;
printk("%s: allocated heap %d\n", __func__, allocated_heap);
#endif
#if USE_TINYALLOC
free_tinyalloc(buf);
#else
free_partition(buf);
#endif
#if LOG_MALLOC
printk("%s: freed 0x%x\n", __func__, buf);
#endif
}
void *memcpy(void *dst, const void *src, size_t len)
{
#if LOG_CSTRING
printk("%s: 0x%x 0x%x %d\n", __func__, dst, src, len);
#endif
for (size_t i = 0;i < len; i++)
{
((uint8_t *)dst)[i] = ((uint8_t *)src)[i];
}
return dst;
}
void *memset(void *dst, int val, size_t len)
{
#if LOG_CSTRING
printk("%s: 0x%x 0x%x %d\n", __func__, dst, val, len);
#endif
for (size_t i = 0;i < len;i++)
{
((uint8_t *)dst)[i] = (uint8_t)val;
}
return dst;
}
int memcmp(const void *lhs, const void *rhs, size_t len)
{
#if LOG_CSTRING
printk("%s: 0x%x 0x%x %d\n", __func__, lhs, rhs, len);
#endif
for (size_t i = 0;i < len;i++)
{
if (((uint8_t *)lhs)[i] < ((uint8_t *)rhs)[i])
{
return -1;
}
else if (((uint8_t *)lhs)[i] > ((uint8_t *)rhs)[i])
{
return 1;
}
}
return 0;
}
size_t strlen(const char *s)
{
#if LOG_CSTRING
printk("%s: 0x%x\n", __func__, s);
#endif
size_t offset = 0;
while(s[offset] != '\0')
{
offset++;
}
return offset;
}
int strcmp(const char *lhs, const char *rhs)
{
#if LOG_CSTRING
printk("%s: 0x%x 0x%x\n", __func__, lhs, rhs);
#endif
int lhs_len = strlen(lhs);
int rhs_len = strlen(rhs);
if (lhs_len < rhs_len)
{
return -1;
}
else if (lhs_len > rhs_len)
{
return 1;
}
return memcmp(lhs, rhs, lhs_len);
}
void *memmove(void *dst, const void *src, size_t len)
{
#if LOG_CSTRING
printk("%s: 0x%x 0x%x %d\n", __func__, dst, src, len);
#endif
return memcpy(dst, src, len);
}
int atoi(const char *s)
{
int result = 0;
size_t offset = 0;
while(s[offset] != '\0' && s[offset] != '\r' && s[offset] != '\n')
{
int new_digit = s[offset] - '0';
result *= 10;
result += new_digit;
offset++;
}
#if LOG_CSTDLIB
printk("%s: %s %d\n", __func__, s, result);
#endif
return result;
}