1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-14 21:40:03 +00:00
oot/src/code/z_malloc.c

113 lines
3.2 KiB
C
Raw Normal View History

2020-03-17 04:31:30 +00:00
#include <global.h>
2020-03-22 21:19:43 +00:00
#define LOG_SEVERITY_NOLOG 0
#define LOG_SEVERITY_ERROR 2
#define LOG_SEVERITY_VERBOSE 3
2020-03-17 04:31:30 +00:00
s32 gZeldaArenaLogSeverity = LOG_SEVERITY_ERROR;
Arena sZeldaArena;
2020-03-22 21:19:43 +00:00
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
if (!ptr) {
if (gZeldaArenaLogSeverity >= LOG_SEVERITY_ERROR) {
// "%s: %u bytes %s failed\n"
2020-03-17 04:31:30 +00:00
osSyncPrintf("%s: %u バイトの%sに失敗しました\n", name, size, action);
2020-03-22 21:19:43 +00:00
__osDisplayArena(&sZeldaArena);
2020-03-17 04:31:30 +00:00
return;
}
2020-03-22 21:19:43 +00:00
} else if (gZeldaArenaLogSeverity >= LOG_SEVERITY_VERBOSE) {
// "%s: %u bytes %s succeeded\n"
2020-03-17 04:31:30 +00:00
osSyncPrintf("%s: %u バイトの%sに成功しました\n", name, size, action);
}
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_Malloc(u32 size) {
2020-03-17 04:31:30 +00:00
void* ptr;
ptr = __osMalloc(&sZeldaArena, size);
2020-03-22 21:19:43 +00:00
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc", "確保"); // Secure
2020-03-17 04:31:30 +00:00
return ptr;
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_MallocDebug(u32 size, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
void* ptr;
ptr = __osMallocDebug(&sZeldaArena, size, file, line);
2020-03-22 21:19:43 +00:00
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_DEBUG", "確保"); // Secure
2020-03-17 04:31:30 +00:00
return ptr;
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_MallocR(u32 size) {
2020-03-17 04:31:30 +00:00
void* ptr;
ptr = __osMallocR(&sZeldaArena, size);
2020-03-22 21:19:43 +00:00
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_r", "確保"); // Secure
2020-03-17 04:31:30 +00:00
return ptr;
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_MallocRDebug(u32 size, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
void* ptr;
ptr = __osMallocRDebug(&sZeldaArena, size, file, line);
2020-03-22 21:19:43 +00:00
ZeldaArena_CheckPointer(ptr, size, "zelda_malloc_r_DEBUG", "確保"); // Secure
2020-03-17 04:31:30 +00:00
return ptr;
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_Realloc(void* ptr, u32 newSize) {
2020-03-17 04:31:30 +00:00
ptr = __osRealloc(&sZeldaArena, ptr, newSize);
ZeldaArena_CheckPointer(ptr, newSize, "zelda_realloc", "再確保"); // Re-securing
return ptr;
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
ptr = __osReallocDebug(&sZeldaArena, ptr, newSize, file, line);
ZeldaArena_CheckPointer(ptr, newSize, "zelda_realloc_DEBUG", "再確保"); // Re-securing
return ptr;
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_Free(void* ptr) {
2020-03-17 04:31:30 +00:00
__osFree(&sZeldaArena, ptr);
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_FreeDebug(void* ptr, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
__osFreeDebug(&sZeldaArena, ptr, file, line);
}
2020-03-22 21:19:43 +00:00
void* ZeldaArena_Calloc(u32 num, u32 size) {
2020-03-17 04:31:30 +00:00
void* ret;
u32 n;
2020-03-22 21:19:43 +00:00
n = num * size;
2020-03-17 04:31:30 +00:00
ret = __osMalloc(&sZeldaArena, n);
2020-03-22 21:19:43 +00:00
if (ret) {
2020-03-17 04:31:30 +00:00
bzero(ret, n);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
ZeldaArena_CheckPointer(ret, n, "zelda_calloc", "確保");
return ret;
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_Display() {
// Zelda heap display
2020-03-17 04:31:30 +00:00
osSyncPrintf("ゼルダヒープ表示\n");
__osDisplayArena(&sZeldaArena);
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) {
2020-03-17 04:31:30 +00:00
ArenaImpl_GetSizes(&sZeldaArena, outMaxFree, outFree, outAlloc);
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_Check() {
2020-03-17 04:31:30 +00:00
__osCheckArena(&sZeldaArena);
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_Init(void* start, u32 size) {
2020-03-17 04:31:30 +00:00
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
__osMallocInit(&sZeldaArena, start, size);
}
2020-03-22 21:19:43 +00:00
void ZeldaArena_Cleanup() {
2020-03-17 04:31:30 +00:00
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
__osMallocCleanup(&sZeldaArena);
}
2020-03-22 21:19:43 +00:00
u8 ZeldaArena_IsInitalized() {
2020-03-17 04:31:30 +00:00
return __osMallocIsInitalized(&sZeldaArena);
}