aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Orbis/user_malloc_for_tls.cpp
blob: d862b6b9f874864334dcc65d17280f7969d3597a (plain)
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
/* SCE CONFIDENTIAL
 PlayStation(R)4 Programmer Tool Runtime Library Release 01.600.051
 * Copyright (C) 2013 Sony Computer Entertainment Inc.
 * All Rights Reserved.
 */
extern "C"
{

#include <stdlib.h>
#include <mspace.h>
#include <kernel.h>

#define HEAP_SIZE (4 * 1024 * 1024)

static SceLibcMspace s_mspace;
static off_t s_memStart;
static size_t s_memLength = 4 * 1024 * 1024;
static size_t s_memAlign = 2 * 1024 * 1024;

int user_malloc_init_for_tls(void);
int user_malloc_fini_for_tls(void);
void *user_malloc_for_tls(size_t size);
void user_free_for_tls(void *ptr);
int user_posix_memalign_for_tls(void **ptr, size_t boundary, size_t size);

//E Replace _malloc_init_for_tls function.
//J _malloc_init_for_tls 関数と置き換わる
int user_malloc_init_for_tls(void)
{
	int res;
	void *addr;
	
	//E Allocate direct memory
	//J ダイレクトメモリを割り当てる
	res = sceKernelAllocateDirectMemory(0, SCE_KERNEL_MAIN_DMEM_SIZE, s_memLength, s_memAlign, SCE_KERNEL_WB_ONION, &s_memStart);
	if (res < 0) {
		//E Error handling
		//J エラー処理
		return 1;
	}

	addr = nullptr;
	//E Map direct memory to the process address space
	//J ダイレクトメモリをプロセスアドレス空間にマップする
	res = sceKernelMapDirectMemory(&addr, HEAP_SIZE, SCE_KERNEL_PROT_CPU_READ | SCE_KERNEL_PROT_CPU_WRITE, 0, s_memStart, s_memAlign);
	if (res < 0) {
		//E Error handling
		//J エラー処理
		return 1;
	}

	//E Generate mspace
	//J mspace を生成する
	s_mspace = sceLibcMspaceCreate("User Malloc For TLS", addr, HEAP_SIZE, 0);
	if (s_mspace == nullptr) {
		//E Error handling
		//J エラー処理
		return 1;
	}

	return 0;
}

//E Replace _malloc_fini_for_tls function.
//J _malloc_fini_for_tls 関数と置き換わる
int user_malloc_fini_for_tls(void)
{
	int res;

	if (s_mspace != nullptr) {
		//E Free mspace
		//J mspace を解放する
		res = sceLibcMspaceDestroy(s_mspace);
		if (res != 0) {
			//E Error handling
			//J エラー処理
			return 1;
		}
	}

	//E Release direct memory
	//J ダイレクトメモリを解放する
	res = sceKernelReleaseDirectMemory(s_memStart, s_memLength);
	if (res < 0) {
		//E Error handling
		//J エラー処理
		return 1;
	}

	return 0;
}

//E Replace _malloc_for_tls function.
//J _malloc_for_tls 関数と置き換わる
void *user_malloc_for_tls(size_t size)
{
	return sceLibcMspaceMalloc(s_mspace, size);
}

//E Replace _free_for_tls function.
//J _free_for_tls 関数と置き換わる
void user_free_for_tls(void *ptr)
{
	sceLibcMspaceFree(s_mspace, ptr);
}

//E Replace _posix_memalign_for_tls function.
//J _posix_memalign_for_tls 関数と置き換わる
int user_posix_memalign_for_tls(void **ptr, size_t boundary, size_t size)
{
	return sceLibcMspacePosixMemalign(s_mspace, ptr, boundary, size);
}

} // extern "c"