diff options
| author | Jack Walker <7463599+Jack-Walker@users.noreply.github.com> | 2020-03-17 00:31:30 -0400 |
|---|---|---|
| committer | Jack Walker <7463599+Jack-Walker@users.noreply.github.com> | 2020-03-17 00:31:30 -0400 |
| commit | 087f561f7786a812c815b9198f24e6acd0477497 (patch) | |
| tree | 8e8b1efeb798ff0b341d39de024d7b8554013c4a /src/code/debug_malloc.c | |
| parent | be78236d36a5eb4ef80acef6188751f6b5d176ae (diff) | |
First proper commit.
Diffstat (limited to 'src/code/debug_malloc.c')
| -rw-r--r-- | src/code/debug_malloc.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/code/debug_malloc.c b/src/code/debug_malloc.c new file mode 100644 index 000000000..6e63e1c4a --- /dev/null +++ b/src/code/debug_malloc.c @@ -0,0 +1,131 @@ +#include <global.h> + +#define LOG_SEVERITY_NOLOG 0 +#define LOG_SEVERITY_ERROR 2 +#define LOG_SEVERITY_VERBOSE 3 + +s32 gDebugArenaLogSeverity = LOG_SEVERITY_ERROR; +Arena sDebugArena; + +void DebugArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) +{ + if (!ptr) + { + if (gDebugArenaLogSeverity >= LOG_SEVERITY_ERROR) + { + //"%s: %u bytes %s failed\n" + osSyncPrintf("%s: %u バイトの%sに失敗しました\n", name, size, action); + __osDisplayArena(&sDebugArena); + return; + } + } + else if (gDebugArenaLogSeverity >= LOG_SEVERITY_VERBOSE) + { + //"%s: %u bytes %s succeeded\n" + osSyncPrintf("%s: %u バイトの%sに成功しました\n", name, size, action); + } +} + +void* DebugArena_Malloc(u32 size) +{ + void* ptr; + ptr = __osMalloc(&sDebugArena, size); + DebugArena_CheckPointer(ptr, size, "debug_malloc", "確保"); //Secure + return ptr; +} + +void* DebugArena_MallocDebug(u32 size, const char* file, s32 line) +{ + void* ptr; + ptr = __osMallocDebug(&sDebugArena, size, file, line); + DebugArena_CheckPointer(ptr, size, "debug_malloc_DEBUG", "確保"); //Secure + return ptr; +} + +void* DebugArena_MallocR(u32 size) +{ + void* ptr; + ptr = __osMallocR(&sDebugArena, size); + DebugArena_CheckPointer(ptr, size, "debug_malloc_r", "確保"); //Secure + return ptr; +} + +void* DebugArena_MallocRDebug(u32 size, const char* file, s32 line) +{ + void* ptr; + ptr = __osMallocRDebug(&sDebugArena, size, file, line); + DebugArena_CheckPointer(ptr, size, "debug_malloc_r_DEBUG", "確保"); //Secure + return ptr; +} + +void* DebugArena_Realloc(void* ptr, u32 newSize) +{ + ptr = __osRealloc(&sDebugArena, ptr, newSize); + DebugArena_CheckPointer(ptr, newSize, "debug_realloc", "再確保"); // Re-securing + return ptr; +} + +void* DebugArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) +{ + ptr = __osReallocDebug(&sDebugArena, ptr, newSize, file, line); + DebugArena_CheckPointer(ptr, newSize, "debug_realloc_DEBUG", "再確保"); // Re-securing + return ptr; +} + +void DebugArena_Free(void* ptr) +{ + __osFree(&sDebugArena, ptr); +} + +void DebugArena_FreeDebug(void* ptr, const char* file, s32 line) +{ + __osFreeDebug(&sDebugArena, ptr, file, line); +} + +void* DebugArena_Calloc(u32 num, u32 size) +{ + void* ret; + u32 n; + + n = num*size; + ret = __osMalloc(&sDebugArena, n); + if (ret) + bzero(ret, n); + + DebugArena_CheckPointer(ret, n, "debug_calloc", "確保"); + return ret; +} + +void DebugArena_Display() +{ + //Zelda heap display (devs forgot to change "Zelda" to "Debug" apparently) + osSyncPrintf("ゼルダヒープ表示\n"); + __osDisplayArena(&sDebugArena); +} + +void DebugArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) +{ + ArenaImpl_GetSizes(&sDebugArena, outMaxFree, outFree, outAlloc); +} + +void DebugArena_Check() +{ + __osCheckArena(&sDebugArena); +} + +void DebugArena_Init(void* start, u32 size) +{ + gDebugArenaLogSeverity = LOG_SEVERITY_NOLOG; + __osMallocInit(&sDebugArena, start, size); +} + +void DebugArena_Cleanup() +{ + gDebugArenaLogSeverity = LOG_SEVERITY_NOLOG; + __osMallocCleanup(&sDebugArena); +} + +u8 DebugArena_IsInitalized() +{ + return __osMallocIsInitalized(&sDebugArena); +} |
