-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemory.c
executable file
·158 lines (133 loc) · 5.05 KB
/
memory.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
#include <vitasdk.h>
#include <taihen.h>
#include "osd.h"
#include "main.h"
#define MAX_MEM_BLOCKS 64
typedef struct {
uint8_t active;
char name[128];
SceKernelMemBlockType type;
int size;
void *optp;
SceUID ret;
void *base;
} VGi_MemBlock;
static VGi_MemBlock g_memBlocks[MAX_MEM_BLOCKS] = {0};
static uint32_t g_memBlocksCount = 0;
static SceUID sceKernelAllocMemBlock_patched(const char *name, SceKernelMemBlockType type, int size, void *optp) {
int ret = TAI_CONTINUE(SceUID, g_hookrefs[23], name, type, size, optp);
for (int i = 0; i < MAX_MEM_BLOCKS; i++) {
if (!g_memBlocks[i].active) {
g_memBlocks[i].active = 1;
strncpy(g_memBlocks[i].name, name, 128);
g_memBlocks[i].type = type;
g_memBlocks[i].size = size;
g_memBlocks[i].ret = ret;
sceKernelGetMemBlockBase(ret, &(g_memBlocks[i].base));
goto RET;
}
}
RET:
g_memBlocksCount++;
return ret;
}
static int sceKernelFreeMemBlock_patched(SceUID uid) {
for (int i = 0; i < MAX_MEM_BLOCKS; i++) {
if (g_memBlocks[i].active && g_memBlocks[i].ret == uid) {
g_memBlocks[i].active = 0;
goto RET;
}
}
RET:
g_memBlocksCount--;
return TAI_CONTINUE(int, g_hookrefs[24], uid);
}
const char *getMemBlockTypeString(SceKernelMemBlockType type) {
switch (type) {
case SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE:
return "UNCACHE";
case SCE_KERNEL_MEMBLOCK_TYPE_USER_RW:
return "RW";
case SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_RW:
return "PHYCONT";
case SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_NC_RW:
return "PHY_NC";
case SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW:
return "CDRAM";
default:
return "?";
}
}
void formatReadableSize(int bytes, char *out, size_t out_size) {
if (bytes > 1024*1024) {
snprintf(out, out_size, "%3d %s", bytes / 1024 / 1024, "MB");
} else if (bytes > 1024) {
snprintf(out, out_size, "%3d %s", bytes / 1024, "kB");
} else {
snprintf(out, out_size, "%3d %s", bytes, "B");
}
}
void drawMemoryMenu(const SceDisplayFrameBuf *pParam) {
osdSetTextScale(2);
osdDrawStringF((pParam->width / 2) - osdGetTextWidth(MENU_TITLE_MEMORY) / 2, 5, MENU_TITLE_MEMORY);
osdSetTextScale(1);
SceKernelFreeMemorySizeInfo info;
info.size = sizeof(SceKernelFreeMemorySizeInfo);
sceKernelGetFreeMemorySize(&info);
char buf[32];
formatReadableSize(info.size_user, buf, 32);
osdDrawStringF(10, 60, "Free RAM: %s", buf);
formatReadableSize(info.size_cdram, buf, 32);
osdDrawStringF(10, 82, "Free VRAM: %s", buf);
formatReadableSize(info.size_phycont, buf, 32);
osdDrawStringF(10, 104, "Free phycont: %s", buf);
// Header
osdDrawStringF(pParam->width - 398, 104, "Allocated MemBlocks (%d):", g_memBlocksCount);
if (g_memBlocksCount > MAX_MEM_BLOCKS) {
char buf[32];
snprintf(buf, 32, "!! > %d", MAX_MEM_BLOCKS);
osdDrawStringF(pParam->width - osdGetTextWidth(buf), 104, buf);
}
osdDrawStringF(30, 148, " type UID base size");
// Scrollable section
int x = 30, y = 165;
if (g_menuScroll > 0) {
// Draw scroll indicator
osdDrawStringF(pParam->width - 24 - 5, y + 22, "/\\");
osdDrawStringF(pParam->width - 24 - 5, y + 44, "%2d", g_menuScroll);
}
for (int i = g_menuScroll; i < MAX_MEM_BLOCKS; i++) {
// Draw only active blocks
if (g_memBlocks[i].active) {
formatReadableSize(g_memBlocks[i].size, buf, 32);
osdDrawStringF(x, y += 27, "'%s'", g_memBlocks[i].name);
osdDrawStringF(pParam->width - osdGetTextWidth(buf) - 40, y, "%s", buf);
osdDrawStringF(x + 24, y += 22, "%-9s 0x%-10X 0x%-10X %d B",
getMemBlockTypeString(g_memBlocks[i].type),
g_memBlocks[i].ret,
g_memBlocks[i].base,
g_memBlocks[i].size);
}
// Do not draw out of screen
if (y > pParam->height - 94) {
// Draw scroll indicator
osdDrawStringF(pParam->width - 24 - 5, pParam->height - 72, "%2d", MIN(g_memBlocksCount, MAX_MEM_BLOCKS) - i);
osdDrawStringF(pParam->width - 24 - 5, pParam->height - 50, "\\/");
break;
}
}
}
void setupMemoryMenu() {
g_hooks[23] = taiHookFunctionImport(
&g_hookrefs[23],
TAI_MAIN_MODULE,
TAI_ANY_LIBRARY,
0xB9D5EBDE,
sceKernelAllocMemBlock_patched);
g_hooks[24] = taiHookFunctionImport(
&g_hookrefs[24],
TAI_MAIN_MODULE,
TAI_ANY_LIBRARY,
0xA91E15EE,
sceKernelFreeMemBlock_patched);
}