all repos — mgba @ 78ef8523ac3c49ccce9108427895dfd49cd829ce

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;
29extern u32 __ctru_linear_heap;
30extern u32 __ctru_heap;
31extern u32 __ctru_heap_size;
32extern u32 __ctru_linear_heap_size;
33static u32 __custom_heap_size = 0x02400000;
34static u32 __custom_linear_heap_size = 0x01400000;
35
36extern void (*__system_retAddr)(void);
37
38void envDestroyHandles(void);
39
40void __appExit();
41
42void __libc_fini_array(void);
43
44Result __sync_fini(void) __attribute__((weak));
45
46uint32_t* romBuffer;
47size_t romBufferSize;
48
49bool allocateRomBuffer(void) {
50	romBuffer = malloc(0x02000000);
51	if (romBuffer) {
52		romBufferSize = 0x02000000;
53		return true;
54	}
55	romBuffer = malloc(0x01000000);
56	if (romBuffer) {
57		romBufferSize = 0x01000000;
58		return true;
59	}
60	return false;
61}
62
63void __system_allocateHeaps() {
64	u32 tmp=0;
65
66	__ctru_heap_size = __custom_heap_size;
67	__ctru_linear_heap_size = __custom_linear_heap_size;
68
69	// Allocate the application heap
70	__ctru_heap = 0x08000000;
71	svcControlMemory(&tmp, __ctru_heap, 0x0, __ctru_heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE);
72
73	// Allocate the linear heap
74	svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);
75	// Set up newlib heap
76	fake_heap_start = (char*)__ctru_heap;
77	fake_heap_end = fake_heap_start + __ctru_heap_size;
78}