all repos — mgba @ c14da05d8dca225010677643c32fea5c0ac8517a

mGBA Game Boy Advance Emulator

src/platform/3ds/ctru-heap.c (view raw)

 1/* This code is mostly from ctrulib, which contains the following license:
 2
 3	This software is provided 'as-is', without any express or implied
 4	warranty. In no event will the authors be held liable for any damages
 5	arising from the use of this software.
 6
 7	Permission is granted to anyone to use this software for any purpose,
 8	including commercial applications, and to alter it and redistribute it
 9	freely, subject to the following restrictions:
10
11	* The origin of this software must not be misrepresented; you must not
12	  claim that you wrote the original software. If you use this software in
13	  a product, an acknowledgment in the product documentation would be
14	  appreciated but is not required.
15
16	* Altered source versions must be plainly marked as such, and must not be
17	  misrepresented as being the original software.
18
19	* This notice may not be removed or altered from any source distribution.
20*/
21
22#include <3ds/types.h>
23#include <3ds/svc.h>
24
25extern char* fake_heap_start;
26extern char* fake_heap_end;
27u32 __linear_heap;
28u32 __heapBase;
29static u32 __heap_size = 0x02400000;
30static u32 __linear_heap_size = 0x01400000;
31
32extern void (*__system_retAddr)(void);
33
34void __destroy_handle_list(void);
35void __appExit();
36
37void __libc_fini_array(void);
38
39uint32_t* romBuffer;
40size_t romBufferSize;
41
42bool allocateRomBuffer(void) {
43	romBuffer = malloc(0x02000000);
44	if (romBuffer) {
45		romBufferSize = 0x02000000;
46		return true;
47	}
48	romBuffer = malloc(0x01000000);
49	if (romBuffer) {
50		romBufferSize = 0x01000000;
51		return true;
52	}
53	return false;
54}
55
56void __system_allocateHeaps() {
57	u32 tmp=0;
58
59	// Allocate the application heap
60	__heapBase = 0x08000000;
61	svcControlMemory(&tmp, __heapBase, 0x0, __heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE);
62
63	// Allocate the linear heap
64	svcControlMemory(&__linear_heap, 0x0, 0x0, __linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);
65	// Set up newlib heap
66	fake_heap_start = (char*)__heapBase;
67	fake_heap_end = fake_heap_start + __heap_size;
68}
69
70void __attribute__((noreturn)) __libctru_exit(int rc)
71{
72	u32 tmp=0;
73
74	// Unmap the linear heap
75	svcControlMemory(&tmp, __linear_heap, 0x0, __linear_heap_size, MEMOP_FREE, 0x0);
76
77	// Unmap the application heap
78	svcControlMemory(&tmp, __heapBase, 0x0, __heap_size, MEMOP_FREE, 0x0);
79
80	// Close some handles
81	__destroy_handle_list();
82
83	// Jump to the loader if it provided a callback
84	if (__system_retAddr)
85		__system_retAddr();
86
87	// Since above did not jump, end this process
88	svcExitProcess();
89}