the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1/* SCE CONFIDENTIAL
2 PlayStation(R)4 Programmer Tool Runtime Library Release 01.600.051
3 * Copyright (C) 2013 Sony Computer Entertainment Inc.
4 * All Rights Reserved.
5 */
6extern "C"
7{
8
9#include <stdlib.h>
10#include <mspace.h>
11#include <kernel.h>
12#include "..\Minecraft.World\System.h"
13
14static size_t s_heapLength;
15static size_t s_memLength;
16
17static SceLibcMspace s_mspace;
18static off_t s_memStart;
19static size_t s_memAlign = 2 * 1024 * 1024;
20
21int user_malloc_init(void);
22int user_malloc_finalize(void);
23void *user_malloc(size_t size);
24void user_free(void *ptr);
25void *user_calloc(size_t nelem, size_t size);
26void *user_realloc(void *ptr, size_t size);
27void *user_memalign(size_t boundary, size_t size);
28int user_posix_memalign(void **ptr, size_t boundary, size_t size);
29void *user_reallocalign(void *ptr, size_t size, size_t boundary);
30int user_malloc_stats(SceLibcMallocManagedSize *mmsize);
31int user_malloc_stats_fast(SceLibcMallocManagedSize *mmsize);
32size_t user_malloc_usable_size(void *ptr);
33
34
35//E Replace _malloc_init function.
36int user_malloc_init(void)
37{
38 int res;
39 void *addr;
40 uint64_t dmemSize = SCE_KERNEL_MAIN_DMEM_SIZE;
41
42 s_heapLength = ((size_t)4608) * 1024 * 1024; // Initial allocation for the application
43 s_heapLength -= ((size_t)4) * 1024 * 1024; // Allocated for TLS
44 s_heapLength -= ((size_t)2) * 1024 * 1024; // 64K (sometimes?) allocated for razor - rounding up to 2MB here to match our alignment
45 s_heapLength -= ((size_t)24) * 1024 * 1024; // 24MB for save file, which is handled by virtual allocs
46 s_heapLength -= ((size_t)512) * 1024 * 1024; // 512MB allocated for GPU by renderer
47 s_heapLength -= ((size_t)112) * 1024 * 1024; // 112MB allocated for CPU by renderer
48 s_memLength = s_heapLength;
49
50 //E Allocate direct memory
51 res = sceKernelAllocateDirectMemory(0, SCE_KERNEL_MAIN_DMEM_SIZE, s_memLength, s_memAlign, SCE_KERNEL_WB_ONION, &s_memStart);
52 if (res < 0) {
53 //E Error handling
54 return 1;
55 }
56
57 addr = NULL;
58 //E Map direct memory to the process address space
59 res = sceKernelMapDirectMemory(&addr, s_heapLength, SCE_KERNEL_PROT_CPU_READ | SCE_KERNEL_PROT_CPU_WRITE, 0, s_memStart, s_memAlign);
60 if (res < 0) {
61 //E Error handling
62 return 1;
63 }
64
65 //E Generate mspace
66 s_mspace = sceLibcMspaceCreate("User Malloc", addr, s_heapLength, 0);
67 if (s_mspace == NULL) {
68 //E Error handling
69 return 1;
70 }
71
72 return 0;
73}
74
75//E Replace _malloc_finalize function.
76int user_malloc_finalize(void)
77{
78 int res;
79
80 if (s_mspace != NULL) {
81 //E Free mspace
82 //J mspace を解放する
83 res = sceLibcMspaceDestroy(s_mspace);
84 if (res != 0) {
85 //E Error handling
86 //J エラー処理
87 return 1;
88 }
89 }
90
91 //E Release direct memory
92 //J ダイレクトメモリを解放する
93 res = sceKernelReleaseDirectMemory(s_memStart, s_memLength);
94 if (res < 0) {
95 //E Error handling
96 //J エラー処理
97 return 1;
98 }
99
100 return 0;
101}
102
103//E Replace malloc function.
104//J malloc 関数と置き換わる
105void *user_malloc(size_t size)
106{
107#if 0
108 static int throttle = 0;
109 static __int64 lasttime = 0;
110 if( ( throttle % 100 ) == 0 )
111 {
112 __int64 nowtime = System::currentTimeMillis();
113 if( ( nowtime - lasttime ) > 20000 )
114 {
115 lasttime = nowtime;
116 SceLibcMallocManagedSize mmsize;
117 SCE_LIBC_INIT_MALLOC_MANAGED_SIZE(mmsize);
118 int ret = sceLibcMspaceMallocStats( s_mspace, &mmsize);
119 printf("%d max %d of %d\n",mmsize.currentInuseSize/(1024*1024),mmsize.maxInuseSize/(1024*1024),mmsize.currentSystemSize/(1024*1024));
120 }
121 }
122 throttle++;
123#endif
124
125 return sceLibcMspaceMalloc(s_mspace, size);
126}
127
128//E Replace free function.
129//J free 関数と置き換わる
130void user_free(void *ptr)
131{
132 sceLibcMspaceFree(s_mspace, ptr);
133}
134
135//E Replace calloc function.
136//J calloc 関数と置き換わる
137void *user_calloc(size_t nelem, size_t size)
138{
139 return sceLibcMspaceCalloc(s_mspace, nelem, size);
140}
141
142//E Replace realloc function.
143//J realloc 関数と置き換わる
144void *user_realloc(void *ptr, size_t size)
145{
146 return sceLibcMspaceRealloc(s_mspace, ptr, size);
147}
148
149//E Replace memalign function.
150//J memalign 関数と置き換わる
151void *user_memalign(size_t boundary, size_t size)
152{
153 return sceLibcMspaceMemalign(s_mspace, boundary, size);
154}
155
156//E Replace posix_memalign function.
157//J posix_memalign 関数と置き換わる
158int user_posix_memalign(void **ptr, size_t boundary, size_t size)
159{
160 return sceLibcMspacePosixMemalign(s_mspace, ptr, boundary, size);
161}
162
163//E Replace reallocalign function.
164//J reallocalign 関数と置き換わる
165void *user_reallocalign(void *ptr, size_t size, size_t boundary)
166{
167 return sceLibcMspaceReallocalign(s_mspace, ptr, boundary, size);
168}
169
170//E Replace malloc_stats function.
171//J malloc_stats 関数と置き換わる
172int user_malloc_stats(SceLibcMallocManagedSize *mmsize)
173{
174 return sceLibcMspaceMallocStats(s_mspace, mmsize);
175}
176
177//E Replace malloc_stats_fast function.
178//J malloc_stata_fast 関数と置き換わる
179int user_malloc_stats_fast(SceLibcMallocManagedSize *mmsize)
180{
181 return sceLibcMspaceMallocStatsFast(s_mspace, mmsize);
182}
183
184//E Replace malloc_usable_size function.
185//J malloc_usable_size 関数と置き換わる
186size_t user_malloc_usable_size(void *ptr)
187{
188 return sceLibcMspaceMallocUsableSize(ptr);
189}
190
191
192} // extern "c"