Skip to content

Commit

Permalink
GameState_, GameAlloc_, SystemArena_ & ZeldaArena_
Browse files Browse the repository at this point in the history
  • Loading branch information
Baoulettes committed Jun 28, 2022
1 parent 4606929 commit 0d85caa
Show file tree
Hide file tree
Showing 38 changed files with 132 additions and 133 deletions.
20 changes: 10 additions & 10 deletions soh/include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,13 @@ void Lights_GlowCheck(GlobalContext* globalCtx);
void Lights_DrawGlow(GlobalContext* globalCtx);
void ZeldaArena_CheckPointer(void* ptr, size_t size, const char* name, const char* action);
void* ZeldaArena_Malloc(size_t size);
void* ZeldaArena_MallocDebug(size_t size, const char* file, s32 line);
void* ZeldaArena_MallocDebug(size_t size);
void* ZeldaArena_MallocR(size_t size);
void* ZeldaArena_MallocRDebug(size_t size, const char* file, s32 line);
void* ZeldaArena_MallocRDebug(size_t size);
void* ZeldaArena_Realloc(void* ptr, size_t newSize);
void* ZeldaArena_ReallocDebug(void* ptr, size_t newSize, const char* file, s32 line);
void* ZeldaArena_ReallocDebug(void* ptr, size_t newSize);
void ZeldaArena_Free(void* ptr);
void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line);
void ZeldaArena_FreeDebug(void* ptr);
void* ZeldaArena_Calloc(size_t num, size_t size);
void ZeldaArena_Display();
void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
Expand Down Expand Up @@ -1600,9 +1600,9 @@ void GameState_Init(GameState* gameState, GameStateFunc init, GraphicsContext* g
void GameState_Destroy(GameState* gameState);
GameStateFunc GameState_GetInit(GameState* gameState);
u32 GameState_IsRunning(GameState* gameState);
void* GameState_Alloc(GameState* gameState, size_t size, char* file, s32 line);
void* GameState_Alloc(GameState* gameState, size_t size);
void func_800C55D0(GameAlloc* this);
void* GameAlloc_MallocDebug(GameAlloc* this, size_t size, const char* file, s32 line);
void* GameAlloc_MallocDebug(GameAlloc* this, size_t size);
void* GameAlloc_Malloc(GameAlloc* this, size_t size);
void GameAlloc_Free(GameAlloc* this, void* data);
void GameAlloc_Cleanup(GameAlloc* this);
Expand Down Expand Up @@ -2175,13 +2175,13 @@ f32 roundf(f32 x);
f32 nearbyintf(f32 x);*/
void SystemArena_CheckPointer(void* ptr, size_t size, const char* name, const char* action);
void* SystemArena_Malloc(size_t size);
void* SystemArena_MallocDebug(size_t size, const char* file, s32 line);
void* SystemArena_MallocDebug(size_t size);
void* SystemArena_MallocR(size_t size);
void* SystemArena_MallocRDebug(size_t size, const char* file, s32 line);
void* SystemArena_MallocRDebug(size_t size);
void* SystemArena_Realloc(void* ptr, size_t newSize);
void* SystemArena_ReallocDebug(void* ptr, size_t newSize, const char* file, s32 line);
void* SystemArena_ReallocDebug(void* ptr, size_t newSize);
void SystemArena_Free(void* ptr);
void SystemArena_FreeDebug(void* ptr, const char* file, s32 line);
void SystemArena_FreeDebug(void* ptr);
void* SystemArena_Calloc(size_t num, size_t size);
void SystemArena_Display(void);
void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
Expand Down
2 changes: 1 addition & 1 deletion soh/include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
#define LOG_FLOAT(exp, value) ((void)0)
#endif

// LogUtils as macro
// LogUtils
#ifndef NDEBUG
#define LOG_POINTER(val, max, ptr, name) LogUtils_LogPointer(val, max, ptr, name, __FILE__, __LINE__)
#define LOG_CHECK_BOUNDARY(name, value, unk) LogUtils_CheckBoundary(name, value, unk, __FILE__, __LINE__)
Expand Down
8 changes: 4 additions & 4 deletions soh/src/code/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void GameState_InitArena(GameState* gameState, size_t size) {
void* arena;

osSyncPrintf("ハイラル確保 サイズ=%u バイト\n"); // "Hyrule reserved size = %u bytes"
arena = GameAlloc_MallocDebug(&gameState->alloc, size, __FILE__, __LINE__);
arena = GameAlloc_MallocDebug(&gameState->alloc, size);
if (arena != NULL) {
THA_Ct(&gameState->tha, arena, size);
osSyncPrintf("ハイラル確保成功\n"); // "Successful Hyral"
Expand Down Expand Up @@ -466,7 +466,7 @@ void GameState_Realloc(GameState* gameState, size_t size) {
}

osSyncPrintf("ハイラル再確保 サイズ=%u バイト\n", size); // "Hyral reallocate size = %u bytes"
gameArena = GameAlloc_MallocDebug(alloc, size, __FILE__, __LINE__);
gameArena = GameAlloc_MallocDebug(alloc, size);
if (gameArena != NULL) {
THA_Ct(&gameState->tha, gameArena, size);
osSyncPrintf("ハイラル再確保成功\n"); // "Successful reacquisition of Hyrule"
Expand Down Expand Up @@ -569,7 +569,7 @@ u32 GameState_IsRunning(GameState* gameState) {
return gameState->running;
}

void* GameState_Alloc(GameState* gameState, size_t size, char* file, s32 line)
void* GameState_Alloc(GameState* gameState, size_t size)
{
void* ret;

Expand All @@ -590,7 +590,7 @@ void* GameState_Alloc(GameState* gameState, size_t size, char* file, s32 line)
}
if (ret != NULL) {
osSyncPrintf(VT_FGCOL(GREEN));
osSyncPrintf("game_alloc(%08x) %08x-%08x [%s:%d]\n", size, ret, (uintptr_t)ret + size, file, line);
osSyncPrintf("game_alloc(%08x) %08x-%08x [%s:%d]\n", size, ret, (uintptr_t)ret + size, __FILE__, __LINE__);
osSyncPrintf(VT_RST);
}
return ret;
Expand Down
10 changes: 5 additions & 5 deletions soh/src/code/gamealloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ void GameAlloc_Log(GameAlloc* this) {
}
}

void* GameAlloc_MallocDebug(GameAlloc* this, size_t size, const char* file, s32 line) {
GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry), file, line);
void* GameAlloc_MallocDebug(GameAlloc* this, size_t size) {
GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry));

if (ptr != NULL) {
ptr->size = size;
Expand All @@ -29,7 +29,7 @@ void* GameAlloc_MallocDebug(GameAlloc* this, size_t size, const char* file, s32
}

void* GameAlloc_Malloc(GameAlloc* this, size_t size) {
GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry), __FILE__, __LINE__);
GameAllocEntry* ptr = SystemArena_MallocDebug(size + sizeof(GameAllocEntry));

if (ptr != NULL) {
ptr->size = size;
Expand All @@ -54,7 +54,7 @@ void GameAlloc_Free(GameAlloc* this, void* data) {
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
this->head = this->base.prev;
SystemArena_FreeDebug(ptr, __FILE__, __LINE__);
SystemArena_FreeDebug(ptr);
}
}

Expand All @@ -65,7 +65,7 @@ void GameAlloc_Cleanup(GameAlloc* this) {
while (&this->base != next) {
cur = next;
next = next->next;
SystemArena_FreeDebug(cur, __FILE__, __LINE__);
SystemArena_FreeDebug(cur);
}

this->head = &this->base;
Expand Down
4 changes: 2 additions & 2 deletions soh/src/code/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static void RunFrame()
size = runFrameContext.ovl->instanceSize;
osSyncPrintf("クラスサイズ=%dバイト\n", size); // "Class size = %d bytes"

runFrameContext.gameState = SystemArena_MallocDebug(size, __FILE__, __LINE__);
runFrameContext.gameState = SystemArena_MallocDebug(size);

if (!runFrameContext.gameState)
{
Expand Down Expand Up @@ -495,7 +495,7 @@ static void RunFrame()

runFrameContext.nextOvl = Graph_GetNextGameState(runFrameContext.gameState);
GameState_Destroy(runFrameContext.gameState);
SystemArena_FreeDebug(runFrameContext.gameState, __FILE__, __LINE__);
SystemArena_FreeDebug(runFrameContext.gameState);
Overlay_FreeGameState(runFrameContext.ovl);
}
Graph_Destroy(&runFrameContext.gfxCtx);
Expand Down
4 changes: 2 additions & 2 deletions soh/src/code/listalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ListAlloc* ListAlloc_Init(ListAlloc* this) {
}

void* ListAlloc_Alloc(ListAlloc* this, size_t size) {
ListAlloc* ptr = SystemArena_MallocDebug(size + sizeof(ListAlloc), __FILE__, __LINE__);
ListAlloc* ptr = SystemArena_MallocDebug(size + sizeof(ListAlloc));
ListAlloc* next;

if (ptr == NULL) {
Expand Down Expand Up @@ -49,7 +49,7 @@ void ListAlloc_Free(ListAlloc* this, void* data) {
this->next = ptr->prev;
}

SystemArena_FreeDebug(ptr, __FILE__, __LINE__);
SystemArena_FreeDebug(ptr);
}

void ListAlloc_FreeAll(ListAlloc* this) {
Expand Down
2 changes: 1 addition & 1 deletion soh/src/code/loadfragment2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "global.h"

void* Overlay_AllocateAndLoad(uintptr_t vRomStart, uintptr_t vRomEnd, void* vRamStart, void* vRamEnd) {
void* allocatedVRamAddr = SystemArena_MallocRDebug((intptr_t)vRamEnd - (intptr_t)vRamStart, __FILE__, __LINE__);
void* allocatedVRamAddr = SystemArena_MallocRDebug((intptr_t)vRamEnd - (intptr_t)vRamStart);

if (gOverlayLogSeverity >= 3) {
osSyncPrintf("OVL:SPEC(%08x-%08x) REAL(%08x-%08x) OFFSET(%08x)\n", vRamStart, vRamEnd, allocatedVRamAddr,
Expand Down
2 changes: 1 addition & 1 deletion soh/src/code/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void Main(void* arg) {
debugHeapSize = (0x80600000 - (uintptr_t)debugHeap);
} else {
debugHeapSize = 0x400;
debugHeap = SystemArena_MallocDebug(debugHeapSize, __FILE__, __LINE__);
debugHeap = SystemArena_MallocDebug(debugHeapSize);
}

debugHeapSize = 1024 * 64;
Expand Down
2 changes: 1 addition & 1 deletion soh/src/code/sys_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ MtxF* sMatrixStack; // "Matrix_stack"
MtxF* sCurrentMatrix; // "Matrix_now"

void Matrix_Init(GameState* gameState) {
sCurrentMatrix = GameState_Alloc(gameState, 20 * sizeof(MtxF), __FILE__, __LINE__);
sCurrentMatrix = GameState_Alloc(gameState, 20 * sizeof(MtxF));
sMatrixStack = sCurrentMatrix;
}

Expand Down
16 changes: 8 additions & 8 deletions soh/src/code/system_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void* SystemArena_Malloc(size_t size) {
return ptr;
}

void* SystemArena_MallocDebug(size_t size, const char* file, s32 line) {
void* ptr = __osMallocDebug(&gSystemArena, size, file, line);
void* SystemArena_MallocDebug(size_t size) {
void* ptr = __osMallocDebug(&gSystemArena, size, __FILE__, __LINE__);

SystemArena_CheckPointer(ptr, size, "malloc_DEBUG", "確保"); // "Secure"
return ptr;
Expand All @@ -43,8 +43,8 @@ void* SystemArena_MallocR(size_t size) {
return ptr;
}

void* SystemArena_MallocRDebug(size_t size, const char* file, s32 line) {
void* ptr = __osMallocRDebug(&gSystemArena, size, file, line);
void* SystemArena_MallocRDebug(size_t size) {
void* ptr = __osMallocRDebug(&gSystemArena, size, __FILE__, __LINE__);

SystemArena_CheckPointer(ptr, size, "malloc_r_DEBUG", "確保"); // "Secure"
return ptr;
Expand All @@ -56,8 +56,8 @@ void* SystemArena_Realloc(void* ptr, size_t newSize) {
return ptr;
}

void* SystemArena_ReallocDebug(void* ptr, size_t newSize, const char* file, s32 line) {
ptr = __osReallocDebug(&gSystemArena, ptr, newSize, file, line);
void* SystemArena_ReallocDebug(void* ptr, size_t newSize) {
ptr = __osReallocDebug(&gSystemArena, ptr, newSize, __FILE__, __LINE__);
SystemArena_CheckPointer(ptr, newSize, "realloc_DEBUG", "再確保"); // "Re-securing"
return ptr;
}
Expand All @@ -66,8 +66,8 @@ void SystemArena_Free(void* ptr) {
__osFree(&gSystemArena, ptr);
}

void SystemArena_FreeDebug(void* ptr, const char* file, s32 line) {
__osFreeDebug(&gSystemArena, ptr, file, line);
void SystemArena_FreeDebug(void* ptr) {
__osFreeDebug(&gSystemArena, ptr, __FILE__, __LINE__);
}

void* SystemArena_Calloc(size_t num, size_t size) {
Expand Down
2 changes: 1 addition & 1 deletion soh/src/code/z_DLF.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Overlay_FreeGameState(GameStateOverlay* overlayEntry) {
overlayEntry->unk_24 = NULL;
}

SystemArena_FreeDebug(overlayEntry->loadedRamAddr, __FILE__, __LINE__);
SystemArena_FreeDebug(overlayEntry->loadedRamAddr);
overlayEntry->loadedRamAddr = NULL;
}
}
Expand Down
32 changes: 16 additions & 16 deletions soh/src/code/z_actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ void func_80031C3C(ActorContext* actorCtx, GlobalContext* globalCtx) {
}

if (actorCtx->absoluteSpace != NULL) {
ZeldaArena_FreeDebug(actorCtx->absoluteSpace, __FILE__, __LINE__);
ZeldaArena_FreeDebug(actorCtx->absoluteSpace);
actorCtx->absoluteSpace = NULL;
}

Expand Down Expand Up @@ -2980,7 +2980,7 @@ void Actor_FreeOverlay(ActorOverlay* actorOverlay) {
if (HREG(20) != 0) {
osSyncPrintf("オーバーレイ解放します\n"); // "Overlay deallocated"
}
ZeldaArena_FreeDebug(actorOverlay->loadedRamAddr, __FILE__, __LINE__);
ZeldaArena_FreeDebug(actorOverlay->loadedRamAddr);
actorOverlay->loadedRamAddr = NULL;
}
}
Expand Down Expand Up @@ -3039,7 +3039,7 @@ Actor* Actor_Spawn(ActorContext* actorCtx, GlobalContext* globalCtx, s16 actorId

if (actorCtx->absoluteSpace == NULL) {
// "AMF: absolute magic field"
actorCtx->absoluteSpace = ZeldaArena_MallocRDebug(AM_FIELD_SIZE, "AMF:絶対魔法領域", 0);
actorCtx->absoluteSpace = ZeldaArena_MallocRDebug(AM_FIELD_SIZE);
if (HREG(20) != 0) {
// "Absolute magic field reservation - %d bytes reserved"
osSyncPrintf("絶対魔法領域確保 %d バイト確保\n", AM_FIELD_SIZE);
Expand All @@ -3048,9 +3048,9 @@ Actor* Actor_Spawn(ActorContext* actorCtx, GlobalContext* globalCtx, s16 actorId

overlayEntry->loadedRamAddr = actorCtx->absoluteSpace;
} else if (overlayEntry->allocType & ALLOCTYPE_PERMANENT) {
overlayEntry->loadedRamAddr = ZeldaArena_MallocRDebug(overlaySize, name, 0);
overlayEntry->loadedRamAddr = ZeldaArena_MallocRDebug(overlaySize);
} else {
overlayEntry->loadedRamAddr = ZeldaArena_MallocDebug(overlaySize, name, 0);
overlayEntry->loadedRamAddr = ZeldaArena_MallocDebug(overlaySize);
}

if (overlayEntry->loadedRamAddr == NULL) {
Expand Down Expand Up @@ -3092,7 +3092,7 @@ Actor* Actor_Spawn(ActorContext* actorCtx, GlobalContext* globalCtx, s16 actorId
return NULL;
}

actor = ZeldaArena_MallocDebug(actorInit->instanceSize, name, 1);
actor = ZeldaArena_MallocDebug(actorInit->instanceSize);

if (actor == NULL) {
// "Actor class cannot be reserved! %s <size=%d bytes>"
Expand Down Expand Up @@ -3236,7 +3236,7 @@ Actor* Actor_Delete(ActorContext* actorCtx, Actor* actor, GlobalContext* globalC

newHead = Actor_RemoveFromCategory(globalCtx, actorCtx, actor);

ZeldaArena_FreeDebug(actor, __FILE__, __LINE__);
ZeldaArena_FreeDebug(actor);

/* if (overlayEntry->vramStart == 0) {
if (HREG(20) != 0) {
Expand Down Expand Up @@ -3414,15 +3414,15 @@ void BodyBreak_Alloc(BodyBreak* bodyBreak, s32 count, GlobalContext* globalCtx)
u32 objectIdsSize;

matricesSize = (count + 1) * sizeof(*bodyBreak->matrices);
bodyBreak->matrices = ZeldaArena_MallocDebug(matricesSize, __FILE__, __LINE__);
bodyBreak->matrices = ZeldaArena_MallocDebug(matricesSize);

if (bodyBreak->matrices != NULL) {
dListsSize = (count + 1) * sizeof(*bodyBreak->dLists);
bodyBreak->dLists = ZeldaArena_MallocDebug(dListsSize, __FILE__, __LINE__);
bodyBreak->dLists = ZeldaArena_MallocDebug(dListsSize);

if (bodyBreak->dLists != NULL) {
objectIdsSize = (count + 1) * sizeof(*bodyBreak->objectIds);
bodyBreak->objectIds = ZeldaArena_MallocDebug(objectIdsSize, __FILE__, __LINE__);
bodyBreak->objectIds = ZeldaArena_MallocDebug(objectIdsSize);

if (bodyBreak->objectIds != NULL) {
memset((u8*)bodyBreak->matrices,0, matricesSize);
Expand All @@ -3435,15 +3435,15 @@ void BodyBreak_Alloc(BodyBreak* bodyBreak, s32 count, GlobalContext* globalCtx)
}

if (bodyBreak->matrices != NULL) {
ZeldaArena_FreeDebug(bodyBreak->matrices, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->matrices);
}

if (bodyBreak->dLists != NULL) {
ZeldaArena_FreeDebug(bodyBreak->dLists, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->dLists);
}

if (bodyBreak->objectIds != NULL) {
ZeldaArena_FreeDebug(bodyBreak->objectIds, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->objectIds);
}
}

Expand Down Expand Up @@ -3510,9 +3510,9 @@ s32 BodyBreak_SpawnParts(Actor* actor, BodyBreak* bodyBreak, GlobalContext* glob

bodyBreak->val = BODYBREAK_STATUS_FINISHED;

ZeldaArena_FreeDebug(bodyBreak->matrices, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->dLists, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->objectIds, __FILE__, __LINE__);
ZeldaArena_FreeDebug(bodyBreak->matrices);
ZeldaArena_FreeDebug(bodyBreak->dLists);
ZeldaArena_FreeDebug(bodyBreak->objectIds);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion soh/src/code/z_bgcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -2428,7 +2428,7 @@ void SSNodeList_Alloc(GlobalContext* globalCtx, SSNodeList* this, s32 tblMax, s3

ASSERT(this->tbl != NULL);

this->polyCheckTbl = GameState_Alloc(&globalCtx->state, numPolys, __FILE__, __LINE__);
this->polyCheckTbl = GameState_Alloc(&globalCtx->state, numPolys);

ASSERT(this->polyCheckTbl != NULL);
}
Expand Down
4 changes: 2 additions & 2 deletions soh/src/code/z_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -6757,7 +6757,7 @@ s32 Camera_Special9(Camera* camera) {
}

Camera* Camera_Create(View* view, CollisionContext* colCtx, GlobalContext* globalCtx) {
Camera* newCamera = ZeldaArena_MallocDebug(sizeof(*newCamera), __FILE__, __LINE__);
Camera* newCamera = ZeldaArena_MallocDebug(sizeof(*newCamera));

if (newCamera != NULL) {
osSyncPrintf(VT_FGCOL(BLUE) "camera: create --- allocate %d byte" VT_RST "\n", sizeof(*newCamera) * 4);
Expand All @@ -6771,7 +6771,7 @@ Camera* Camera_Create(View* view, CollisionContext* colCtx, GlobalContext* globa
void Camera_Destroy(Camera* camera) {
if (camera != NULL) {
osSyncPrintf(VT_FGCOL(BLUE) "camera: destroy ---" VT_RST "\n");
ZeldaArena_FreeDebug(camera, __FILE__, __LINE__);
ZeldaArena_FreeDebug(camera);
} else {
osSyncPrintf(VT_COL(YELLOW, BLACK) "camera: destroy: already cleared\n" VT_RST);
}
Expand Down
Loading

0 comments on commit 0d85caa

Please sign in to comment.