1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-01-15 12:47:04 +00:00
oot/src/code/z_malloc.c

131 lines
3.6 KiB
C
Raw Normal View History

#include "global.h"
#include "osMalloc.h"
2020-03-17 04:31:30 +00:00
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
Arena sZeldaArena;
#if OOT_DEBUG
s32 gZeldaArenaLogSeverity = LOG_SEVERITY_ERROR;
2020-03-22 21:19:43 +00:00
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
if (ptr == NULL) {
2020-03-22 21:19:43 +00:00
if (gZeldaArenaLogSeverity >= LOG_SEVERITY_ERROR) {
PRINTF(T("%s: %u バイトの%sに失敗しました\n", "%s: %u bytes %s failed\n"), name, size, action);
2020-03-22 21:19:43 +00:00
__osDisplayArena(&sZeldaArena);
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
} else if (gZeldaArenaLogSeverity >= LOG_SEVERITY_VERBOSE) {
PRINTF(T("%s: %u バイトの%sに成功しました\n", "%s: %u bytes %s succeeded\n"), name, size, action);
2020-03-17 04:31:30 +00:00
}
}
#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
2020-03-22 21:19:43 +00:00
void* ZeldaArena_Malloc(u32 size) {
void* ptr = __osMalloc(&sZeldaArena, size);
// TODO re-evaluate "secure" as a translation (in this file and others using "確保")
ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc", T("確保", "Secure"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#if OOT_DEBUG
void* ZeldaArena_MallocDebug(u32 size, const char* file, int line) {
void* ptr = __osMallocDebug(&sZeldaArena, size, file, line);
ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_DEBUG", T("確保", "Secure"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void* ZeldaArena_MallocR(u32 size) {
void* ptr = __osMallocR(&sZeldaArena, size);
ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_r", T("確保", "Secure"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#if OOT_DEBUG
void* ZeldaArena_MallocRDebug(u32 size, const char* file, int line) {
void* ptr = __osMallocRDebug(&sZeldaArena, size, file, line);
ZELDA_ARENA_CHECK_POINTER(ptr, size, "zelda_malloc_r_DEBUG", T("確保", "Secure"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#endif
2020-03-17 04:31:30 +00:00
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);
ZELDA_ARENA_CHECK_POINTER(ptr, newSize, "zelda_realloc", T("再確保", "Re-securing"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#if OOT_DEBUG
void* ZeldaArena_ReallocDebug(void* ptr, u32 newSize, const char* file, int line) {
2020-03-17 04:31:30 +00:00
ptr = __osReallocDebug(&sZeldaArena, ptr, newSize, file, line);
ZELDA_ARENA_CHECK_POINTER(ptr, newSize, "zelda_realloc_DEBUG", T("再確保", "Re-securing"));
2020-03-17 04:31:30 +00:00
return ptr;
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void ZeldaArena_Free(void* ptr) {
2020-03-17 04:31:30 +00:00
__osFree(&sZeldaArena, ptr);
}
#if OOT_DEBUG
void ZeldaArena_FreeDebug(void* ptr, const char* file, int line) {
2020-03-17 04:31:30 +00:00
__osFreeDebug(&sZeldaArena, ptr, file, line);
}
#endif
2020-03-17 04:31:30 +00:00
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 = num * size;
2020-03-22 21:19:43 +00:00
2020-03-17 04:31:30 +00:00
ret = __osMalloc(&sZeldaArena, n);
if (ret != NULL) {
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
ZELDA_ARENA_CHECK_POINTER(ret, n, "zelda_calloc", T("確保", "Secure"));
2020-03-17 04:31:30 +00:00
return ret;
}
#if OOT_DEBUG
2022-03-21 19:49:11 +00:00
void ZeldaArena_Display(void) {
PRINTF(T("ゼルダヒープ表示\n", "Zelda heap display\n"));
2020-03-17 04:31:30 +00:00
__osDisplayArena(&sZeldaArena);
}
#endif
2020-03-17 04:31:30 +00:00
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);
}
2022-03-21 19:49:11 +00:00
void ZeldaArena_Check(void) {
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) {
#if OOT_DEBUG
2020-03-17 04:31:30 +00:00
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
2020-03-17 04:31:30 +00:00
__osMallocInit(&sZeldaArena, start, size);
}
2022-03-21 19:49:11 +00:00
void ZeldaArena_Cleanup(void) {
#if OOT_DEBUG
2020-03-17 04:31:30 +00:00
gZeldaArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
2020-03-17 04:31:30 +00:00
__osMallocCleanup(&sZeldaArena);
}
s32 ZeldaArena_IsInitialized(void) {
return __osMallocIsInitialized(&sZeldaArena);
2020-03-17 04:31:30 +00:00
}