all repos — mgba @ 50402c830729f2ba5a6fc3e6facfd8b258f7f97d

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
25#include "util/common.h"
26
27extern char* fake_heap_start;
28extern char* fake_heap_end;
29u32 __linear_heap;
30u32 __heapBase;
31static u32 __heap_size = 0x02400000;
32static u32 __linear_heap_size = 0x01400000;
33
34extern void (*__system_retAddr)(void);
35
36void __destroy_handle_list(void);
37void __appExit();
38
39void __libc_fini_array(void);
40
41uint32_t* romBuffer;
42size_t romBufferSize;
43
44bool allocateRomBuffer(void) {
45	romBuffer = malloc(0x02000000);
46	if (romBuffer) {
47		romBufferSize = 0x02000000;
48		return true;
49	}
50	romBuffer = malloc(0x01000000);
51	if (romBuffer) {
52		romBufferSize = 0x01000000;
53		return true;
54	}
55	return false;
56}
57
58void __system_allocateHeaps() {
59	u32 tmp=0;
60
61	// Allocate the application heap
62	__heapBase = 0x08000000;
63	svcControlMemory(&tmp, __heapBase, 0x0, __heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE);
64
65	// Allocate the linear heap
66	svcControlMemory(&__linear_heap, 0x0, 0x0, __linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);
67	// Set up newlib heap
68	fake_heap_start = (char*)__heapBase;
69	fake_heap_end = fake_heap_start + __heap_size;
70}
71
72void __attribute__((noreturn)) __libctru_exit(int rc)
73{
74	UNUSED(rc);
75
76	u32 tmp=0;
77
78	// Unmap the linear heap
79	svcControlMemory(&tmp, __linear_heap, 0x0, __linear_heap_size, MEMOP_FREE, 0x0);
80
81	// Unmap the application heap
82	svcControlMemory(&tmp, __heapBase, 0x0, __heap_size, MEMOP_FREE, 0x0);
83
84	// Close some handles
85	__destroy_handle_list();
86
87	// Jump to the loader if it provided a callback
88	if (__system_retAddr)
89		__system_retAddr();
90
91	// Since above did not jump, end this process
92	svcExitProcess();
93}