all repos — mgba @ 750463d85042e2eee08305cf683749d08636ff98

mGBA Game Boy Advance Emulator

src/platform/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 "util/memory.h"
 7#include "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	}
22	if (size & 0xFFF) {
23		// Align to 4kB pages
24		size += ((~size) & 0xFFF) + 1;
25	}
26	SceUID memblock = sceKernelAllocMemBlock("anon", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, size, 0);
27	if (memblock < 0) {
28		return 0;
29	}
30	*SceUIDListAppend(&uids) = memblock;
31	void* ptr;
32	if (sceKernelGetMemBlockBase(memblock, &ptr) < 0) {
33		return 0;
34	}
35	return ptr;
36}
37
38void mappedMemoryFree(void* memory, size_t size) {
39	UNUSED(size);
40	size_t i;
41	for (i = 0; i < SceUIDListSize(&uids); ++i) {
42		SceUID uid = *SceUIDListGetPointer(&uids, i);
43		void* ptr;
44		if (sceKernelGetMemBlockBase(uid, &ptr) < 0) {
45			continue;
46		}
47		if (ptr == memory) {
48			sceKernelFreeMemBlock(uid);
49			SceUIDListUnshift(&uids, i, 1);
50			return;
51		}
52	}
53}