From 9a7c63c46cfcb3531c8ae1c0a555b4978556537f Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Sat, 2 Apr 2022 13:47:22 +0200 Subject: [PATCH] Allocate aligned heaps --- soh/include/functions.h | 3 +++ soh/include/variables.h | 4 ++-- soh/src/buffers/heaps.c | 45 +++++++++++++++++++++++++++++++++++++---- soh/src/code/main.c | 3 +++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/soh/include/functions.h b/soh/include/functions.h index 060022ac9..a4b1a9a99 100644 --- a/soh/include/functions.h +++ b/soh/include/functions.h @@ -2397,6 +2397,9 @@ void FileChoose_Destroy(GameState* thisx); char* SetQuote(); +void Heaps_Alloc(void); +void Heaps_Free(void); + #ifdef __cplusplus }; #endif diff --git a/soh/include/variables.h b/soh/include/variables.h index 80e3b792d..b58166b60 100644 --- a/soh/include/variables.h +++ b/soh/include/variables.h @@ -237,8 +237,8 @@ extern void(*D_801755D0)(void); extern u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE]; // 0xC00 bytes extern u8 gGfxSPTaskStack[0x400]; // 0x400 bytes extern GfxPool gGfxPools[2]; // 0x24820 bytes - extern u8 gAudioHeap[0x38000]; // 0x38000 bytes - extern u8 gSystemHeap[]; + extern u8* gAudioHeap; + extern u8* gSystemHeap; #ifdef __cplusplus }; diff --git a/soh/src/buffers/heaps.c b/soh/src/buffers/heaps.c index 4309acd18..6d162bc9e 100644 --- a/soh/src/buffers/heaps.c +++ b/soh/src/buffers/heaps.c @@ -1,7 +1,44 @@ #include "z64.h" +#include +#include -// 0x38000 bytes -u8 gAudioHeap[0x38000]; +#ifndef _MSC_VER +#include +#endif -//u8 gSystemHeap[UNK_SIZE]; -u8 gSystemHeap[1024 * 1024 * 128]; +#define AUDIO_HEAP_SIZE 0x38000 +#define SYSTEM_HEAP_SIZE (1024 * 1024 * 128) + +u8* gAudioHeap; + +u8* gSystemHeap; + +void Heaps_Alloc(void) +{ +#ifdef _MSC_VER + gAudioHeap = (u8*)_aligned_malloc(AUDIO_HEAP_SIZE, 16); + gSystemHeap = (u8*)_aligned_malloc(SYSTEM_HEAP_SIZE, 16); +#elif defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L) + if (posix_memalign((void**)&gAudioHeap, 16, AUDIO_HEAP_SIZE) != 0) + gAudioHeap = NULL; + if (posix_memalign((void**)&gSystemHeap, 16, SYSTEM_HEAP_SIZE) != 0) + gSystemHeap = NULL; +#else + gAudioHeap = (u8*)memalign(16, AUDIO_HEAP_SIZE); + gSystemHeap = (u8*)memalign(16, SYSTEM_HEAP_SIZE); +#endif + + assert(gAudioHeap != NULL); + assert(gSystemHeap != NULL); +} + +void Heaps_Free(void) +{ +#ifdef _MSC_VER + _aligned_free(gAudioHeap); + _aligned_free(gSystemHeap); +#else + free(gAudioHeap); + free(gSystemHeap); +#endif +} diff --git a/soh/src/code/main.c b/soh/src/code/main.c index eaaa7b388..0680aad3e 100644 --- a/soh/src/code/main.c +++ b/soh/src/code/main.c @@ -63,6 +63,7 @@ void Main(void* arg) { PreNmiBuff_Init(gAppNmiBufferPtr); Fault_Init(); SysCfb_Init(0); + Heaps_Alloc(); sysHeap = gSystemHeap; fb = SysCfb_GetFbPtr(0); gSystemHeapSize = 1024 * 1024 * 4; @@ -131,4 +132,6 @@ void Main(void* arg) { osDestroyThread(&sGraphThread); func_800FBFD8(); osSyncPrintf("mainproc 実行終了\n"); // "End of execution" + + Heaps_Free(); }