-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
mem_utils.c
577 lines (503 loc) · 15.6 KB
/
mem_utils.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* This file is part of vitaGL
* Copyright 2017, 2018, 2019, 2020 Rinnegatamante
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* mem_utils.c:
* Utilities for memory management
*/
#include "../shared.h"
#ifndef HAVE_CUSTOM_HEAP
static void *mempool_mspace[VGL_MEM_ALL] = {NULL, NULL, NULL, NULL, NULL}; // mspace creations (VRAM, RAM, PHYCONT RAM, CDLG, EXTERNAL)
#endif
static void *mempool_addr[VGL_MEM_ALL] = {NULL, NULL, NULL, NULL, NULL}; // addresses of heap memblocks (VRAM, RAM, PHYCONT RAM, CDLG, EXTERNAL)
static SceUID mempool_id[VGL_MEM_ALL] = {0, 0, 0, 0, 0}; // UIDs of heap memblocks (VRAM, RAM, PHYCONT RAM, EXTERNAL)
static size_t mempool_size[VGL_MEM_ALL] = {0, 0, 0, 0, 0}; // sizes of heap memlbocks (VRAM, RAM, PHYCONT RAM, EXTERNAL)
static int mempool_initialized = 0;
#ifdef HAVE_CUSTOM_HEAP
typedef struct tm_block_s {
struct tm_block_s *next; // next block in list (either free or allocated)
int32_t type; // one of vglMemType
uintptr_t base; // block start address
uint32_t offset; // offset for USSE stuff (unused)
uint32_t size; // block size
} tm_block_t;
static tm_block_t *tm_alloclist; // list of allocated blocks
static tm_block_t *tm_freelist; // list of free blocks
static uint32_t tm_free[VGL_MEM_ALL]; // see enum vglMemType
// get new block header
static inline tm_block_t *heap_blk_new(void) {
return calloc(1, sizeof(tm_block_t));
}
// release block header
static inline void heap_blk_release(tm_block_t *block) {
free(block);
}
// determine if two blocks can be merged into one
// blocks of different types can't be merged,
// blocks of same type can only be merged if they're next to each other
// in memory and have matching offsets
static inline int heap_blk_mergeable(tm_block_t *a, tm_block_t *b) {
return a->type == b->type
&& a->base + a->size == b->base
&& a->offset + a->size == b->offset;
}
// inserts a block into the free list and merges with neighboring
// free blocks if possible
static void heap_blk_insert_free(tm_block_t *block) {
tm_block_t *curblk = tm_freelist;
tm_block_t *prevblk = NULL;
while (curblk && curblk->base < block->base) {
prevblk = curblk;
curblk = curblk->next;
}
if (prevblk)
prevblk->next = block;
else
tm_freelist = block;
block->next = curblk;
tm_free[block->type] += block->size;
if (curblk && heap_blk_mergeable(block, curblk)) {
block->size += curblk->size;
block->next = curblk->next;
heap_blk_release(curblk);
}
if (prevblk && heap_blk_mergeable(prevblk, block)) {
prevblk->size += block->size;
prevblk->next = block->next;
heap_blk_release(block);
}
}
// allocates a block from the heap
// (removes it from free list and adds to alloc list)
static tm_block_t *heap_blk_alloc(int32_t type, uint32_t size, uint32_t alignment) {
tm_block_t *curblk = tm_freelist;
tm_block_t *prevblk = NULL;
while (curblk) {
const uint32_t skip = ALIGN(curblk->base, alignment) - curblk->base;
if (curblk->type == type && skip + size <= curblk->size) {
tm_block_t *skipblk = NULL;
tm_block_t *unusedblk = NULL;
if (skip != 0) {
skipblk = heap_blk_new();
if (!skipblk)
return NULL;
}
if (skip + size != curblk->size) {
unusedblk = heap_blk_new();
if (!unusedblk) {
if (skipblk)
heap_blk_release(skipblk);
return NULL;
}
}
if (skip != 0) {
if (prevblk)
prevblk->next = skipblk;
else
tm_freelist = skipblk;
skipblk->next = curblk;
skipblk->type = curblk->type;
skipblk->base = curblk->base;
skipblk->offset = curblk->offset;
skipblk->size = skip;
curblk->base += skip;
curblk->offset += skip;
curblk->size -= skip;
prevblk = skipblk;
}
if (size != curblk->size) {
unusedblk->next = curblk->next;
curblk->next = unusedblk;
unusedblk->type = curblk->type;
unusedblk->base = curblk->base + size;
unusedblk->offset = curblk->offset + size;
unusedblk->size = curblk->size - size;
curblk->size = size;
}
if (prevblk)
prevblk->next = curblk->next;
else
tm_freelist = curblk->next;
curblk->next = tm_alloclist;
tm_alloclist = curblk;
tm_free[type] -= size;
return curblk;
}
prevblk = curblk;
curblk = curblk->next;
}
return NULL;
}
// frees a previously allocated heap block
// (removes from alloc list and inserts into free list)
static void heap_blk_free(uintptr_t base) {
tm_block_t *curblk = tm_alloclist;
tm_block_t *prevblk = NULL;
while (curblk && curblk->base != base) {
prevblk = curblk;
curblk = curblk->next;
}
if (!curblk) {
#ifndef SKIP_ERROR_HANDLING
vgl_log("%s:%d An internal free failed (possible double free call) on pointer: 0x%08X!\n", __FILE__, __LINE__, base);
#endif
return;
}
if (prevblk)
prevblk->next = curblk->next;
else
tm_alloclist = curblk->next;
curblk->next = NULL;
heap_blk_insert_free(curblk);
}
// initializes heap variables and blockpool
static void heap_init(void) {
tm_alloclist = NULL;
tm_freelist = NULL;
for (int i = 0; i < VGL_MEM_ALL; i++)
tm_free[i] = 0;
}
// resets heap state and frees allocated block headers
static void heap_destroy(void) {
tm_block_t *n;
tm_block_t *p = tm_alloclist;
while (p) {
n = p->next;
heap_blk_release(p);
p = n;
}
p = tm_freelist;
while (p) {
n = p->next;
heap_blk_release(p);
p = n;
}
}
// adds a memblock to the heap
static void heap_extend(int32_t type, void *base, uint32_t size) {
tm_block_t *block = heap_blk_new();
block->next = NULL;
block->type = type;
block->base = (uintptr_t)base;
block->offset = 0;
block->size = size;
heap_blk_insert_free(block);
}
// allocates memory from the heap (basically malloc())
static void *heap_alloc(int32_t type, uint32_t size, uint32_t alignment) {
tm_block_t *block = heap_blk_alloc(type, size, alignment);
if (!block)
return NULL;
return (void *)block->base;
}
// frees previously allocated heap memory (basically free())
static void heap_free(void *addr) {
heap_blk_free((uintptr_t)addr);
}
#endif
#ifdef PHYCONT_ON_DEMAND
void *vgl_alloc_phycont_block(uint32_t size) {
size = ALIGN(size, 1024 * 1024);
SceUID blk = sceKernelAllocMemBlock("phycont_blk", SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_NC_RW, size, NULL);
if (blk < 0)
return NULL;
void *res;
sceKernelGetMemBlockBase(blk, &res);
sceGxmMapMemory(res, size, SCE_GXM_MEMORY_ATTRIB_RW);
return res;
}
#endif
void vgl_mem_term(void) {
if (!mempool_initialized)
return;
#ifdef HAVE_CUSTOM_HEAP
heap_destroy();
#endif
for (int i = 0; i < VGL_MEM_EXTERNAL; i++) {
#ifndef HAVE_CUSTOM_HEAP
sceClibMspaceDestroy(mempool_mspace[i]);
mempool_mspace[i] = NULL;
#endif
sceKernelFreeMemBlock(mempool_id[i]);
mempool_addr[i] = NULL;
mempool_id[i] = 0;
mempool_size[i] = 0;
}
mempool_initialized = 0;
}
void vgl_mem_init(size_t size_ram, size_t size_cdram, size_t size_phycont, size_t size_cdlg) {
if (mempool_initialized)
vgl_mem_term();
#ifndef HAVE_CACHED_MEM
if (size_ram > 0xC800000) // Vita has a smaller address mapping for uncached mem
size_ram = 0xC800000;
#endif
mempool_size[VGL_MEM_VRAM] = ALIGN(size_cdram, 256 * 1024);
mempool_size[VGL_MEM_RAM] = ALIGN(size_ram, 4 * 1024);
#ifdef PHYCONT_ON_DEMAND
mempool_size[VGL_MEM_SLOW] = 0;
#else
mempool_size[VGL_MEM_SLOW] = ALIGN(size_phycont, 1024 * 1024);
#endif
mempool_size[VGL_MEM_BUDGET] = ALIGN(size_cdlg, 4 * 1024);
#ifdef HAVE_CUSTOM_HEAP
// Initialize heap
heap_init();
#endif
if (mempool_size[VGL_MEM_VRAM])
mempool_id[VGL_MEM_VRAM] = sceKernelAllocMemBlock("cdram_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, mempool_size[VGL_MEM_VRAM], NULL);
#ifdef HAVE_CACHED_MEM
if (mempool_size[VGL_MEM_RAM])
mempool_id[VGL_MEM_RAM] = sceKernelAllocMemBlock("ram_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, mempool_size[VGL_MEM_RAM], NULL);
if (mempool_size[VGL_MEM_SLOW])
mempool_id[VGL_MEM_SLOW] = sceKernelAllocMemBlock("phycont_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_RW, mempool_size[VGL_MEM_SLOW], NULL);
if (mempool_size[VGL_MEM_BUDGET])
mempool_id[VGL_MEM_BUDGET] = sceKernelAllocMemBlock("cdlg_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_CDIALOG_RW, mempool_size[VGL_MEM_BUDGET], NULL);
#else
if (mempool_size[VGL_MEM_RAM])
mempool_id[VGL_MEM_RAM] = sceKernelAllocMemBlock("ram_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, mempool_size[VGL_MEM_RAM], NULL);
if (mempool_size[VGL_MEM_SLOW])
mempool_id[VGL_MEM_SLOW] = sceKernelAllocMemBlock("phycont_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_NC_RW, mempool_size[VGL_MEM_SLOW], NULL);
if (mempool_size[VGL_MEM_BUDGET])
mempool_id[VGL_MEM_BUDGET] = sceKernelAllocMemBlock("cdlg_mempool", SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_CDIALOG_NC_RW, mempool_size[VGL_MEM_BUDGET], NULL);
#endif
for (int i = 0; i < VGL_MEM_EXTERNAL; i++) {
if (mempool_size[i]) {
mempool_addr[i] = NULL;
sceKernelGetMemBlockBase(mempool_id[i], &mempool_addr[i]);
if (mempool_addr[i]) {
sceGxmMapMemory(mempool_addr[i], mempool_size[i], SCE_GXM_MEMORY_ATTRIB_RW);
#ifndef HAVE_CUSTOM_HEAP
mempool_mspace[i] = sceClibMspaceCreate(mempool_addr[i], mempool_size[i]);
#else
heap_extend(i, mempool_addr[i], mempool_size[i]);
#endif
}
}
}
#if defined(PHYCONT_ON_DEMAND) && !defined(HAVE_CUSTOM_HEAP)
// Getting total available phycont mem
if (system_app_mode) {
SceAppMgrBudgetInfo info;
info.size = sizeof(SceAppMgrBudgetInfo);
sceAppMgrGetBudgetInfo(&info);
mempool_size[VGL_MEM_SLOW] = info.total_phycont_mem;
} else {
SceKernelFreeMemorySizeInfo info;
info.size = sizeof(SceKernelFreeMemorySizeInfo);
sceKernelGetFreeMemorySize(&info);
mempool_size[VGL_MEM_SLOW] = info.size_phycont;
}
#endif
// Mapping newlib heap into sceGxm
void *dummy = malloc(1);
free(dummy);
SceKernelMemBlockInfo info;
info.size = sizeof(SceKernelMemBlockInfo);
sceKernelGetMemBlockInfoByAddr(dummy, &info);
sceGxmMapMemory(info.mappedBase, info.mappedSize, SCE_GXM_MEMORY_ATTRIB_RW);
mempool_size[VGL_MEM_EXTERNAL] = info.mappedSize;
mempool_addr[VGL_MEM_EXTERNAL] = info.mappedBase;
mempool_initialized = 1;
}
vglMemType vgl_mem_get_type_by_addr(void *addr) {
#ifdef HAVE_CUSTOM_HEAP
if (addr >= mempool_addr[VGL_MEM_EXTERNAL] && (addr < mempool_addr[VGL_MEM_EXTERNAL] + mempool_size[VGL_MEM_EXTERNAL]))
return VGL_MEM_EXTERNAL;
return -1;
#else
if (addr >= mempool_addr[VGL_MEM_VRAM] && (addr < mempool_addr[VGL_MEM_VRAM] + mempool_size[VGL_MEM_VRAM]))
return VGL_MEM_VRAM;
else if (addr >= mempool_addr[VGL_MEM_RAM] && (addr < mempool_addr[VGL_MEM_RAM] + mempool_size[VGL_MEM_RAM]))
return VGL_MEM_RAM;
#ifndef PHYCONT_ON_DEMAND
else if (addr >= mempool_addr[VGL_MEM_SLOW] && (addr < mempool_addr[VGL_MEM_SLOW] + mempool_size[VGL_MEM_SLOW]))
return VGL_MEM_SLOW;
#endif
else if (addr >= mempool_addr[VGL_MEM_BUDGET] && (addr < mempool_addr[VGL_MEM_BUDGET] + mempool_size[VGL_MEM_BUDGET]))
return VGL_MEM_BUDGET;
else if (addr >= mempool_addr[VGL_MEM_EXTERNAL] && (addr < mempool_addr[VGL_MEM_EXTERNAL] + mempool_size[VGL_MEM_EXTERNAL]))
return VGL_MEM_EXTERNAL;
#ifdef PHYCONT_ON_DEMAND
return VGL_MEM_SLOW;
#else
return -1;
#endif
#endif
}
size_t vgl_mem_get_free_space(vglMemType type) {
if (type == VGL_MEM_EXTERNAL) {
return 0;
#if defined(PHYCONT_ON_DEMAND) && !defined(HAVE_CUSTOM_HEAP)
} else if (type == VGL_MEM_SLOW) {
if (system_app_mode) {
SceAppMgrBudgetInfo info;
info.size = sizeof(SceAppMgrBudgetInfo);
sceAppMgrGetBudgetInfo(&info);
return info.free_phycont_mem;
} else {
SceKernelFreeMemorySizeInfo info;
info.size = sizeof(SceKernelFreeMemorySizeInfo);
sceKernelGetFreeMemorySize(&info);
return info.size_phycont;
}
#endif
} else if (type == VGL_MEM_ALL) {
size_t size = 0;
for (int i = 0; i < VGL_MEM_EXTERNAL; i++) {
size += vgl_mem_get_free_space(i);
}
return size;
#ifdef HAVE_CUSTOM_HEAP
} else {
return tm_free[type];
}
#else
} else if (mempool_size[type]) {
SceClibMspaceStats stats;
sceClibMspaceMallocStats(mempool_mspace[type], &stats);
return stats.capacity - stats.current_in_use;
} else
return 0;
#endif
}
size_t vgl_mem_get_total_space(vglMemType type) {
if (type == VGL_MEM_ALL) {
size_t size = 0;
for (int i = 0; i < VGL_MEM_EXTERNAL; i++) {
size += vgl_mem_get_total_space(i);
}
return size;
} else {
return mempool_size[type];
}
}
size_t vgl_malloc_usable_size(void *ptr) {
vglMemType type = vgl_mem_get_type_by_addr(ptr);
if (type == VGL_MEM_EXTERNAL)
return malloc_usable_size(ptr);
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW) {
SceKernelMemBlockInfo info;
info.size = sizeof(SceKernelMemBlockInfo);
sceKernelGetMemBlockInfoByAddr(ptr, &info);
return info.mappedSize;
}
#endif
else
return sceClibMspaceMallocUsableSize(ptr);
}
void vgl_free(void *ptr) {
vglMemType type = vgl_mem_get_type_by_addr(ptr);
if (type == VGL_MEM_EXTERNAL)
free(ptr);
#ifdef HAVE_CUSTOM_HEAP
else
heap_free(ptr);
#else
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW) {
sceGxmUnmapMemory(ptr);
sceKernelFreeMemBlock(sceKernelFindMemBlockByAddr(ptr, 0));
}
#endif
else if (mempool_mspace[type])
sceClibMspaceFree(mempool_mspace[type], ptr);
#endif
}
void *vgl_malloc(size_t size, vglMemType type) {
if (type == VGL_MEM_EXTERNAL)
return malloc(size);
#ifdef HAVE_CUSTOM_HEAP
else if (size <= tm_free[type])
return heap_alloc(type, size, MEM_ALIGNMENT);
#else
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW)
return vgl_alloc_phycont_block(size);
#endif
else if (mempool_mspace[type])
return sceClibMspaceMalloc(mempool_mspace[type], size);
#endif
return NULL;
}
void *vgl_calloc(size_t num, size_t size, vglMemType type) {
if (type == VGL_MEM_EXTERNAL)
return calloc(num, size);
#ifdef HAVE_CUSTOM_HEAP
else if (num * size <= tm_free[type])
return heap_alloc(type, num * size, MEM_ALIGNMENT);
#else
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW)
return vgl_alloc_phycont_block(num * size);
#endif
else if (mempool_mspace[type])
return sceClibMspaceCalloc(mempool_mspace[type], num, size);
#endif
return NULL;
}
void *vgl_memalign(size_t alignment, size_t size, vglMemType type) {
if (type == VGL_MEM_EXTERNAL)
return memalign(alignment, size);
#ifdef HAVE_CUSTOM_HEAP
else if (size <= tm_free[type])
return heap_alloc(type, size, alignment);
#else
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW)
return vgl_alloc_phycont_block(size);
#endif
else if (mempool_mspace[type])
return sceClibMspaceMemalign(mempool_mspace[type], alignment, size);
#endif
return NULL;
}
void *vgl_realloc(void *ptr, size_t size) {
vglMemType type = vgl_mem_get_type_by_addr(ptr);
if (type == VGL_MEM_EXTERNAL)
return realloc(ptr, size);
#ifndef HAVE_CUSTOM_HEAP
#ifdef PHYCONT_ON_DEMAND
else if (type == VGL_MEM_SLOW) {
if (vgl_malloc_usable_size(ptr) >= size)
return ptr;
void *res = vgl_alloc_phycont_block(size);
if (res) {
vgl_fast_memcpy(res, ptr, size);
vgl_free(ptr);
return res;
}
}
#endif
else if (mempool_mspace[type])
return sceClibMspaceRealloc(mempool_mspace[type], ptr, size);
#endif
return NULL;
}
void vgl_memcpy(void *dst, const void *src, size_t size) {
#ifndef DEBUG_MEMCPY
#ifndef HAVE_CACHED_MEM
if (size >= 0x2000)
sceDmacMemcpy(dst, src, size);
else
#endif
vgl_fast_memcpy(dst, src, size);
#else
memcpy(dst, src, size);
#endif
}