From 5756ff0b168b5646731d9f520d6de8da40a9dab1 Mon Sep 17 00:00:00 2001 From: Justin Weiss Date: Mon, 28 Sep 2020 19:12:15 -0700 Subject: [PATCH] [3DS] Free memory in 1MB blocks When freeing large blocks of memory, the 3DS may kernel panic. Freeing in smaller blocks seems to avoid the crashes. --- ctr/ctr_memory.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ctr/ctr_memory.c b/ctr/ctr_memory.c index 2a7722dc75..bb84ff8f7d 100644 --- a/ctr/ctr_memory.c +++ b/ctr/ctr_memory.c @@ -141,9 +141,12 @@ void* _sbrk_r(struct _reent *ptr, ptrdiff_t incr) __heap_size += diff; - if (diff < 0) - svcControlMemory(&tmp, __heapBase + __heap_size, - 0x0, -diff, MEMOP_FREE, MEMPERM_READ | MEMPERM_WRITE); + while (diff < 0) { + int size = -diff < 0x100000 ? -diff : 0x100000; + diff += size; + svcControlMemory(&tmp, __heapBase + __heap_size - diff, + 0x0, size, MEMOP_FREE, 0); + } sbrk_top += incr;