CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/MemArenaHorizon.cpp
Views: 1401
1
// Copyright (C) 2023 M4xw
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
#include "ppsspp_config.h"
16
#if PPSSPP_PLATFORM(SWITCH)
17
18
#include <stdio.h>
19
#include <malloc.h> // memalign
20
#include <switch.h>
21
22
#include "Common/MemArena.h"
23
24
static uintptr_t memoryBase = 0;
25
static uintptr_t memoryCodeBase = 0;
26
static uintptr_t memorySrcBase = 0;
27
28
size_t MemArena::roundup(size_t x) {
29
return x;
30
}
31
32
bool MemArena::NeedsProbing() {
33
return false;
34
}
35
36
bool MemArena::GrabMemSpace(size_t size) {
37
return true;
38
}
39
40
void MemArena::ReleaseSpace() {
41
if (R_FAILED(svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), (u64)memoryCodeBase, (u64)memorySrcBase, 0x10000000)))
42
printf("Failed to release view space...\n");
43
44
free((void *)memorySrcBase);
45
memorySrcBase = 0;
46
}
47
48
void *MemArena::CreateView(s64 offset, size_t size, void *base) {
49
Result rc = svcMapProcessMemory(base, envGetOwnProcessHandle(), (u64)(memoryCodeBase + offset), size);
50
if (R_FAILED(rc)) {
51
printf("Fatal error creating the view... base: %p offset: %p size: %p src: %p err: %d\n",
52
(void *)base, (void *)offset, (void *)size, (void *)(memoryCodeBase + offset), rc);
53
} else {
54
printf("Created the view... base: %p offset: %p size: %p src: %p err: %d\n",
55
(void *)base, (void *)offset, (void *)size, (void *)(memoryCodeBase + offset), rc);
56
}
57
58
return base;
59
}
60
61
void MemArena::ReleaseView(s64 offset, void *view, size_t size) {
62
if (R_FAILED(svcUnmapProcessMemory(view, envGetOwnProcessHandle(), (u64)(memoryCodeBase + offset), size)))
63
printf("Failed to unmap view...\n");
64
}
65
66
u8 *MemArena::Find4GBBase() {
67
memorySrcBase = (uintptr_t)memalign(0x1000, 0x10000000);
68
69
if (!memoryBase)
70
memoryBase = (uintptr_t)virtmemReserve(0x10000000);
71
72
if (!memoryCodeBase)
73
memoryCodeBase = (uintptr_t)virtmemReserve(0x10000000);
74
75
if (R_FAILED(svcMapProcessCodeMemory(envGetOwnProcessHandle(), (u64)memoryCodeBase, (u64)memorySrcBase, 0x10000000)))
76
printf("Failed to map memory...\n");
77
if (R_FAILED(svcSetProcessMemoryPermission(envGetOwnProcessHandle(), memoryCodeBase, 0x10000000, Perm_Rx)))
78
printf("Failed to set perms...\n");
79
80
return (u8 *)memoryBase;
81
}
82
83
#endif // PPSSPP_PLATFORM(SWITCH)
84
85