init.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <3ds.h>
  2. #include "miscdef.h"
  3. void __system_allocateHeaps(void)
  4. {
  5. extern char* fake_heap_start;
  6. extern char* fake_heap_end;
  7. extern u32 __ctru_heap;
  8. extern u32 __ctru_heap_size;
  9. extern u32 __ctru_linear_heap;
  10. extern u32 __ctru_linear_heap_size;
  11. extern int __stacksize__;
  12. u32 tmp = 0;
  13. Result res = 0;
  14. u32 mem = APPMEMTYPE > 5 ? 0x320000 : 0x64000; //max is 0x88000 on old3DS, but it prevents programs from starting
  15. // Distribute available memory into halves, aligning to page size.
  16. //u32 size = (osGetMemRegionFree(MEMREGION_SYSTEM) / 2) & 0xFFFFF000;
  17. __ctru_heap_size = (mem + 0xFFF) & ~0xFFF;
  18. __ctru_linear_heap_size = APPMEMTYPE > 5 ? 0x2A0000 : 0;
  19. if(APPMEMTYPE > 5) __stacksize__ = 0x10000; else __stacksize__ = 0x8000;
  20. //*(u32*)0x00100998 = size;
  21. // Allocate the application heap6
  22. __ctru_heap = 0x08000000;
  23. res = svcControlMemory(&tmp, __ctru_heap, 0x0, __ctru_heap_size, (MemOp)MEMOP_ALLOC, (MemPerm)(MEMPERM_READ | MEMPERM_WRITE));
  24. if(res < 0) *(u32*)0x00100100 = res;
  25. // Allocate the linear heap
  26. //__ctru_linear_heap = 0x14000000;
  27. //svcControlMemory(&tmp, 0x1C000000 - __ctru_linear_heap_size, 0x0, __ctru_linear_heap_size, (MemOp)MEMOP_FREE, (MemPerm)(0));
  28. if(__ctru_linear_heap_size)
  29. {
  30. res = svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, (MemOp)MEMOP_ALLOC_LINEAR, (MemPerm)(MEMPERM_READ | MEMPERM_WRITE));
  31. if(res < 0) *(u32*)0x00100200 = res;
  32. if(__ctru_linear_heap < 0x10000000) *(u32*)0x00100071 = __ctru_linear_heap;
  33. }
  34. // Set up newlib heap
  35. fake_heap_start = (char*)__ctru_heap;
  36. fake_heap_end = fake_heap_start + __ctru_heap_size;
  37. }