1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-25 09:45:02 +00:00

Match retail system_malloc.c and z_malloc.c (#1683)

* Match system_malloc.c and z_malloc.c

* Rename CHECK_POINTER
This commit is contained in:
cadmic 2024-02-02 12:01:49 -08:00 committed by GitHub
parent 0444a6f094
commit 8db76a27da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 73 additions and 28 deletions

View file

@ -901,22 +901,24 @@ Lights* Lights_NewAndDraw(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8
Lights* Lights_New(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8 ambientB); Lights* Lights_New(GraphicsContext* gfxCtx, u8 ambientR, u8 ambientG, u8 ambientB);
void Lights_GlowCheck(PlayState* play); void Lights_GlowCheck(PlayState* play);
void Lights_DrawGlow(PlayState* play); void Lights_DrawGlow(PlayState* play);
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action);
void* ZeldaArena_Malloc(u32 size); void* ZeldaArena_Malloc(u32 size);
void* ZeldaArena_MallocDebug(u32 size, const char* file, s32 line);
void* ZeldaArena_MallocR(u32 size); void* ZeldaArena_MallocR(u32 size);
void* ZeldaArena_MallocRDebug(u32 size, const char* file, s32 line);
void* ZeldaArena_Realloc(void* ptr, u32 newSize); void* ZeldaArena_Realloc(void* ptr, u32 newSize);
void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line);
void ZeldaArena_Free(void* ptr); void ZeldaArena_Free(void* ptr);
void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line);
void* ZeldaArena_Calloc(u32 num, u32 size); void* ZeldaArena_Calloc(u32 num, u32 size);
void ZeldaArena_Display(void);
void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc); void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
void ZeldaArena_Check(void); void ZeldaArena_Check(void);
void ZeldaArena_Init(void* start, u32 size); void ZeldaArena_Init(void* start, u32 size);
void ZeldaArena_Cleanup(void); void ZeldaArena_Cleanup(void);
u8 ZeldaArena_IsInitialized(void); u8 ZeldaArena_IsInitialized(void);
#if OOT_DEBUG
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action);
void* ZeldaArena_MallocDebug(u32 size, const char* file, s32 line);
void* ZeldaArena_MallocRDebug(u32 size, const char* file, s32 line);
void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line);
void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line);
void ZeldaArena_Display(void);
#endif
void MapMark_Init(PlayState* play); void MapMark_Init(PlayState* play);
void MapMark_ClearPointers(PlayState* play); void MapMark_ClearPointers(PlayState* play);
void MapMark_Draw(PlayState* play); void MapMark_Draw(PlayState* play);
@ -1759,22 +1761,23 @@ f32 ceilf(f32 x);
f32 truncf(f32 x); f32 truncf(f32 x);
f32 roundf(f32 x); f32 roundf(f32 x);
f32 nearbyintf(f32 x); f32 nearbyintf(f32 x);
void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action);
void* SystemArena_Malloc(u32 size); void* SystemArena_Malloc(u32 size);
void* SystemArena_MallocDebug(u32 size, const char* file, s32 line);
void* SystemArena_MallocR(u32 size); void* SystemArena_MallocR(u32 size);
void* SystemArena_MallocRDebug(u32 size, const char* file, s32 line);
void* SystemArena_Realloc(void* ptr, u32 newSize); void* SystemArena_Realloc(void* ptr, u32 newSize);
void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line);
void SystemArena_Free(void* ptr); void SystemArena_Free(void* ptr);
void SystemArena_FreeDebug(void* ptr, const char* file, s32 line);
void* SystemArena_Calloc(u32 num, u32 size); void* SystemArena_Calloc(u32 num, u32 size);
void SystemArena_Display(void);
void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc); void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
void SystemArena_Check(void); void SystemArena_Check(void);
void SystemArena_Init(void* start, u32 size); void SystemArena_Init(void* start, u32 size);
void SystemArena_Cleanup(void); void SystemArena_Cleanup(void);
u8 SystemArena_IsInitialized(void); u8 SystemArena_IsInitialized(void);
#if OOT_DEBUG
void* SystemArena_MallocDebug(u32 size, const char* file, s32 line);
void* SystemArena_MallocRDebug(u32 size, const char* file, s32 line);
void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line);
void SystemArena_FreeDebug(void* ptr, const char* file, s32 line);
void SystemArena_Display(void);
#endif
u32 Rand_Next(void); u32 Rand_Next(void);
void Rand_Seed(u32 seed); void Rand_Seed(u32 seed);
f32 Rand_ZeroOne(void); f32 Rand_ZeroOne(void);

View file

@ -4,9 +4,11 @@
#define LOG_SEVERITY_ERROR 2 #define LOG_SEVERITY_ERROR 2
#define LOG_SEVERITY_VERBOSE 3 #define LOG_SEVERITY_VERBOSE 3
s32 gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
Arena gSystemArena; Arena gSystemArena;
#if OOT_DEBUG
s32 gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) { void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
if (ptr == NULL) { if (ptr == NULL) {
if (gSystemArenaLogSeverity >= LOG_SEVERITY_ERROR) { if (gSystemArenaLogSeverity >= LOG_SEVERITY_ERROR) {
@ -21,53 +23,66 @@ void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char*
} }
} }
#define SYSTEM_ARENA_CHECK_POINTER(ptr, size, name, action) SystemArena_CheckPointer(ptr, size, name, action)
#else
#define SYSTEM_ARENA_CHECK_POINTER(ptr, size, name, action) (void)0
#endif
void* SystemArena_Malloc(u32 size) { void* SystemArena_Malloc(u32 size) {
void* ptr = __osMalloc(&gSystemArena, size); void* ptr = __osMalloc(&gSystemArena, size);
SystemArena_CheckPointer(ptr, size, "malloc", "確保"); // "Secure" SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc", "確保"); // "Secure"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* SystemArena_MallocDebug(u32 size, const char* file, s32 line) { void* SystemArena_MallocDebug(u32 size, const char* file, s32 line) {
void* ptr = __osMallocDebug(&gSystemArena, size, file, line); void* ptr = __osMallocDebug(&gSystemArena, size, file, line);
SystemArena_CheckPointer(ptr, size, "malloc_DEBUG", "確保"); // "Secure" SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_DEBUG", "確保"); // "Secure"
return ptr; return ptr;
} }
#endif
void* SystemArena_MallocR(u32 size) { void* SystemArena_MallocR(u32 size) {
void* ptr = __osMallocR(&gSystemArena, size); void* ptr = __osMallocR(&gSystemArena, size);
SystemArena_CheckPointer(ptr, size, "malloc_r", "確保"); // "Secure" SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_r", "確保"); // "Secure"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* SystemArena_MallocRDebug(u32 size, const char* file, s32 line) { void* SystemArena_MallocRDebug(u32 size, const char* file, s32 line) {
void* ptr = __osMallocRDebug(&gSystemArena, size, file, line); void* ptr = __osMallocRDebug(&gSystemArena, size, file, line);
SystemArena_CheckPointer(ptr, size, "malloc_r_DEBUG", "確保"); // "Secure" SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_r_DEBUG", "確保"); // "Secure"
return ptr; return ptr;
} }
#endif
void* SystemArena_Realloc(void* ptr, u32 newSize) { void* SystemArena_Realloc(void* ptr, u32 newSize) {
ptr = __osRealloc(&gSystemArena, ptr, newSize); ptr = __osRealloc(&gSystemArena, ptr, newSize);
SystemArena_CheckPointer(ptr, newSize, "realloc", "再確保"); // "Re-securing" SYSTEM_ARENA_CHECK_POINTER(ptr, newSize, "realloc", "再確保"); // "Re-securing"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) { void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) {
ptr = __osReallocDebug(&gSystemArena, ptr, newSize, file, line); ptr = __osReallocDebug(&gSystemArena, ptr, newSize, file, line);
SystemArena_CheckPointer(ptr, newSize, "realloc_DEBUG", "再確保"); // "Re-securing" SYSTEM_ARENA_CHECK_POINTER(ptr, newSize, "realloc_DEBUG", "再確保"); // "Re-securing"
return ptr; return ptr;
} }
#endif
void SystemArena_Free(void* ptr) { void SystemArena_Free(void* ptr) {
__osFree(&gSystemArena, ptr); __osFree(&gSystemArena, ptr);
} }
#if OOT_DEBUG
void SystemArena_FreeDebug(void* ptr, const char* file, s32 line) { void SystemArena_FreeDebug(void* ptr, const char* file, s32 line) {
__osFreeDebug(&gSystemArena, ptr, file, line); __osFreeDebug(&gSystemArena, ptr, file, line);
} }
#endif
void* SystemArena_Calloc(u32 num, u32 size) { void* SystemArena_Calloc(u32 num, u32 size) {
void* ret; void* ret;
@ -78,14 +93,16 @@ void* SystemArena_Calloc(u32 num, u32 size) {
bzero(ret, n); bzero(ret, n);
} }
SystemArena_CheckPointer(ret, n, "calloc", "確保"); SYSTEM_ARENA_CHECK_POINTER(ret, n, "calloc", "確保");
return ret; return ret;
} }
#if OOT_DEBUG
void SystemArena_Display(void) { void SystemArena_Display(void) {
PRINTF("システムヒープ表示\n"); // "System heap display" PRINTF("システムヒープ表示\n"); // "System heap display"
__osDisplayArena(&gSystemArena); __osDisplayArena(&gSystemArena);
} }
#endif
void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) { void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) {
ArenaImpl_GetSizes(&gSystemArena, outMaxFree, outFree, outAlloc); ArenaImpl_GetSizes(&gSystemArena, outMaxFree, outFree, outAlloc);
@ -96,12 +113,16 @@ void SystemArena_Check(void) {
} }
void SystemArena_Init(void* start, u32 size) { void SystemArena_Init(void* start, u32 size) {
#if OOT_DEBUG
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG; gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocInit(&gSystemArena, start, size); __osMallocInit(&gSystemArena, start, size);
} }
void SystemArena_Cleanup(void) { void SystemArena_Cleanup(void) {
#if OOT_DEBUG
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG; gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocCleanup(&gSystemArena); __osMallocCleanup(&gSystemArena);
} }

View file

@ -4,9 +4,11 @@
#define LOG_SEVERITY_ERROR 2 #define LOG_SEVERITY_ERROR 2
#define LOG_SEVERITY_VERBOSE 3 #define LOG_SEVERITY_VERBOSE 3
s32 gZeldaArenaLogSeverity = LOG_SEVERITY_ERROR;
Arena sZeldaArena; Arena sZeldaArena;
#if OOT_DEBUG
s32 gZeldaArenaLogSeverity = LOG_SEVERITY_ERROR;
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) { void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
if (ptr == NULL) { if (ptr == NULL) {
if (gZeldaArenaLogSeverity >= LOG_SEVERITY_ERROR) { if (gZeldaArenaLogSeverity >= LOG_SEVERITY_ERROR) {
@ -20,53 +22,66 @@ void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char*
} }
} }
#define ZELDA_ARENA_CHECK_POINTER(ptr, size, name, action) ZeldaArena_CheckPointer(ptr, size, name, action)
#else
#define ZELDA_ARENA_CHECK_POINTER(ptr, size, name, action) (void)0
#endif
void* ZeldaArena_Malloc(u32 size) { void* ZeldaArena_Malloc(u32 size) {
void* ptr = __osMalloc(&sZeldaArena, size); void* ptr = __osMalloc(&sZeldaArena, size);
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc", "確保"); // "Secure" ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc", "確保"); // "Secure"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* ZeldaArena_MallocDebug(u32 size, const char* file, s32 line) { void* ZeldaArena_MallocDebug(u32 size, const char* file, s32 line) {
void* ptr = __osMallocDebug(&sZeldaArena, size, file, line); void* ptr = __osMallocDebug(&sZeldaArena, size, file, line);
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_DEBUG", "確保"); // "Secure" ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_DEBUG", "確保"); // "Secure"
return ptr; return ptr;
} }
#endif
void* ZeldaArena_MallocR(u32 size) { void* ZeldaArena_MallocR(u32 size) {
void* ptr = __osMallocR(&sZeldaArena, size); void* ptr = __osMallocR(&sZeldaArena, size);
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_r", "確保"); // "Secure" ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_r", "確保"); // "Secure"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* ZeldaArena_MallocRDebug(u32 size, const char* file, s32 line) { void* ZeldaArena_MallocRDebug(u32 size, const char* file, s32 line) {
void* ptr = __osMallocRDebug(&sZeldaArena, size, file, line); void* ptr = __osMallocRDebug(&sZeldaArena, size, file, line);
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_r_DEBUG", "確保"); // "Secure" ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_r_DEBUG", "確保"); // "Secure"
return ptr; return ptr;
} }
#endif
void* ZeldaArena_Realloc(void* ptr, u32 newSize) { void* ZeldaArena_Realloc(void* ptr, u32 newSize) {
ptr = __osRealloc(&sZeldaArena, ptr, newSize); ptr = __osRealloc(&sZeldaArena, ptr, newSize);
ZeldaArena_CheckPointer(ptr, newSize, "zelda_realloc", "再確保"); // "Re-securing" ZELDA_ARENA_CHECK_POINTER(ptr, newSize, "zelda_realloc", "再確保"); // "Re-securing"
return ptr; return ptr;
} }
#if OOT_DEBUG
void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) { void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) {
ptr = __osReallocDebug(&sZeldaArena, ptr, newSize, file, line); ptr = __osReallocDebug(&sZeldaArena, ptr, newSize, file, line);
ZeldaArena_CheckPointer(ptr, newSize, "zelda_realloc_DEBUG", "再確保"); // "Re-securing" ZELDA_ARENA_CHECK_POINTER(ptr, newSize, "zelda_realloc_DEBUG", "再確保"); // "Re-securing"
return ptr; return ptr;
} }
#endif
void ZeldaArena_Free(void* ptr) { void ZeldaArena_Free(void* ptr) {
__osFree(&sZeldaArena, ptr); __osFree(&sZeldaArena, ptr);
} }
#if OOT_DEBUG
void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line) { void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line) {
__osFreeDebug(&sZeldaArena, ptr, file, line); __osFreeDebug(&sZeldaArena, ptr, file, line);
} }
#endif
void* ZeldaArena_Calloc(u32 num, u32 size) { void* ZeldaArena_Calloc(u32 num, u32 size) {
void* ret; void* ret;
@ -77,14 +92,16 @@ void* ZeldaArena_Calloc(u32 num, u32 size) {
bzero(ret, n); bzero(ret, n);
} }
ZeldaArena_CheckPointer(ret, n, "zelda_calloc", "確保"); // "Secure" ZELDA_ARENA_CHECK_POINTER(ret, n, "zelda_calloc", "確保"); // "Secure"
return ret; return ret;
} }
#if OOT_DEBUG
void ZeldaArena_Display(void) { void ZeldaArena_Display(void) {
PRINTF("ゼルダヒープ表示\n"); // "Zelda heap display" PRINTF("ゼルダヒープ表示\n"); // "Zelda heap display"
__osDisplayArena(&sZeldaArena); __osDisplayArena(&sZeldaArena);
} }
#endif
void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) { void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) {
ArenaImpl_GetSizes(&sZeldaArena, outMaxFree, outFree, outAlloc); ArenaImpl_GetSizes(&sZeldaArena, outMaxFree, outFree, outAlloc);
@ -95,12 +112,16 @@ void ZeldaArena_Check(void) {
} }
void ZeldaArena_Init(void* start, u32 size) { void ZeldaArena_Init(void* start, u32 size) {
#if OOT_DEBUG
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG; gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocInit(&sZeldaArena, start, size); __osMallocInit(&sZeldaArena, start, size);
} }
void ZeldaArena_Cleanup(void) { void ZeldaArena_Cleanup(void) {
#if OOT_DEBUG
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG; gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocCleanup(&sZeldaArena); __osMallocCleanup(&sZeldaArena);
} }