src/platform/psp2/psp2-memory.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba-util/memory.h>
7#include <mgba-util/vector.h>
8
9#include <psp2/kernel/sysmem.h>
10#include <psp2/types.h>
11
12DECLARE_VECTOR(SceUIDList, SceUID);
13DEFINE_VECTOR(SceUIDList, SceUID);
14
15static struct SceUIDList uids;
16static bool listInit = false;
17
18void* anonymousMemoryMap(size_t size) {
19 if (!listInit) {
20 SceUIDListInit(&uids, 8);
21 listInit = true;
22 }
23 if (size & 0xFFF) {
24 // Align to 4kB pages
25 size += ((~size) & 0xFFF) + 1;
26 }
27 SceUID memblock = sceKernelAllocMemBlock("anon", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, size, 0);
28 if (memblock < 0) {
29 return 0;
30 }
31 *SceUIDListAppend(&uids) = memblock;
32 void* ptr;
33 if (sceKernelGetMemBlockBase(memblock, &ptr) < 0) {
34 return 0;
35 }
36 return ptr;
37}
38
39void mappedMemoryFree(void* memory, size_t size) {
40 UNUSED(size);
41 size_t i;
42 for (i = 0; i < SceUIDListSize(&uids); ++i) {
43 SceUID uid = *SceUIDListGetPointer(&uids, i);
44 void* ptr;
45 if (sceKernelGetMemBlockBase(uid, &ptr) < 0) {
46 continue;
47 }
48 if (ptr == memory) {
49 sceKernelFreeMemBlock(uid);
50 SceUIDListShift(&uids, i, 1);
51 return;
52 }
53 }
54}