| 17 | __attribute__((weak)) u32 __ctru_linear_heap_size = 0; |
| 18 | |
| 19 | void __attribute__((weak)) __system_allocateHeaps(void) { |
| 20 | Result rc; |
| 21 | |
| 22 | // Retrieve handle to the resource limit object for our process |
| 23 | Handle reslimit = 0; |
| 24 | rc = svcGetResourceLimit(&reslimit, CUR_PROCESS_HANDLE); |
| 25 | if (R_FAILED(rc)) |
| 26 | svcBreak(USERBREAK_PANIC); |
| 27 | |
| 28 | // Retrieve information about total/used memory |
| 29 | s64 maxCommit = 0, currentCommit = 0; |
| 30 | ResourceLimitType reslimitType = RESLIMIT_COMMIT; |
| 31 | svcGetResourceLimitLimitValues(&maxCommit, reslimit, &reslimitType, 1); // for APPLICATION this is equal to APPMEMALLOC at all times |
| 32 | svcGetResourceLimitCurrentValues(¤tCommit, reslimit, &reslimitType, 1); |
| 33 | svcCloseHandle(reslimit); |
| 34 | |
| 35 | // Calculate how much remaining free memory is available |
| 36 | u32 remaining = (u32)(maxCommit - currentCommit) &~ 0xFFF; |
| 37 | |
| 38 | if (__ctru_heap_size + __ctru_linear_heap_size > remaining) |
| 39 | svcBreak(USERBREAK_PANIC); |
| 40 | |
| 41 | if (__ctru_heap_size == 0 && __ctru_linear_heap_size == 0) { |
| 42 | // Split available memory equally between linear and application heaps (with rounding in favor of the latter) |
| 43 | __ctru_linear_heap_size = (remaining / 2) & ~0xFFF; |
| 44 | __ctru_heap_size = remaining - __ctru_linear_heap_size; |
| 45 | |
| 46 | // If the application heap size is bigger than the cap, prefer to grow linear heap instead |
| 47 | if (__ctru_heap_size > HEAP_SPLIT_SIZE_CAP) { |
| 48 | __ctru_heap_size = HEAP_SPLIT_SIZE_CAP; |
| 49 | __ctru_linear_heap_size = remaining - __ctru_heap_size; |
| 50 | |
| 51 | // However if the linear heap size is bigger than the cap, prefer to grow application heap |
| 52 | if (__ctru_linear_heap_size > LINEAR_HEAP_SIZE_CAP) { |
| 53 | __ctru_linear_heap_size = LINEAR_HEAP_SIZE_CAP; |
| 54 | __ctru_heap_size = remaining - __ctru_linear_heap_size; |
| 55 | } |
| 56 | } |
| 57 | } else if (__ctru_heap_size == 0) { |
| 58 | __ctru_heap_size = remaining - __ctru_linear_heap_size; |
| 59 | } else if (__ctru_linear_heap_size == 0) { |
| 60 | __ctru_linear_heap_size = remaining - __ctru_heap_size; |
| 61 | } |
| 62 | |
| 63 | // Allocate the application heap |
| 64 | rc = svcControlMemory(&__ctru_heap, OS_HEAP_AREA_BEGIN, 0x0, __ctru_heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE); |
| 65 | if (R_FAILED(rc)) |
| 66 | svcBreak(USERBREAK_PANIC); |
| 67 | |
| 68 | // Allocate the linear heap |
| 69 | rc = svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE); |
| 70 | if (R_FAILED(rc)) |
| 71 | svcBreak(USERBREAK_PANIC); |
| 72 | |
| 73 | // Mappable allocator init |
| 74 | mappableInit(OS_MAP_AREA_BEGIN, OS_MAP_AREA_END); |
| 75 | |
| 76 | // Set up newlib heap |
no test coverage detected