2020-10-03 15:22:44 +00:00
|
|
|
#include "global.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
|
|
|
|
|
|
|
s32 gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
|
|
|
|
Arena gSystemArena;
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
|
2021-02-14 00:49:40 +00:00
|
|
|
if (ptr == NULL) {
|
2020-03-22 21:19:43 +00:00
|
|
|
if (gSystemArenaLogSeverity >= LOG_SEVERITY_ERROR) {
|
2020-04-16 21:36:12 +00:00
|
|
|
// "%s: %u bytes %s failed\n"
|
2020-03-17 04:31:30 +00:00
|
|
|
osSyncPrintf("%s: %u バイトの%sに失敗しました\n", name, size, action);
|
|
|
|
__osDisplayArena(&gSystemArena);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-22 21:19:43 +00:00
|
|
|
} else if (gSystemArenaLogSeverity >= LOG_SEVERITY_VERBOSE) {
|
2020-04-16 21:36:12 +00:00
|
|
|
// "%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* SystemArena_Malloc(u32 size) {
|
2021-02-14 00:49:40 +00:00
|
|
|
void* ptr = __osMalloc(&gSystemArena, size);
|
|
|
|
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, size, "malloc", "確保"); // "Secure"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_MallocDebug(u32 size, const char* file, s32 line) {
|
2021-02-14 00:49:40 +00:00
|
|
|
void* ptr = __osMallocDebug(&gSystemArena, size, file, line);
|
|
|
|
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, size, "malloc_DEBUG", "確保"); // "Secure"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_MallocR(u32 size) {
|
2021-02-14 00:49:40 +00:00
|
|
|
void* ptr = __osMallocR(&gSystemArena, size);
|
|
|
|
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, size, "malloc_r", "確保"); // "Secure"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_MallocRDebug(u32 size, const char* file, s32 line) {
|
2021-02-14 00:49:40 +00:00
|
|
|
void* ptr = __osMallocRDebug(&gSystemArena, size, file, line);
|
|
|
|
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, size, "malloc_r_DEBUG", "確保"); // "Secure"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_Realloc(void* ptr, u32 newSize) {
|
2020-03-17 04:31:30 +00:00
|
|
|
ptr = __osRealloc(&gSystemArena, ptr, newSize);
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, newSize, "realloc", "再確保"); // "Re-securing"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, s32 line) {
|
2020-03-17 04:31:30 +00:00
|
|
|
ptr = __osReallocDebug(&gSystemArena, ptr, newSize, file, line);
|
2021-09-28 23:53:56 +00:00
|
|
|
SystemArena_CheckPointer(ptr, newSize, "realloc_DEBUG", "再確保"); // "Re-securing"
|
2020-03-17 04:31:30 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void SystemArena_Free(void* ptr) {
|
2020-03-17 04:31:30 +00:00
|
|
|
__osFree(&gSystemArena, ptr);
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void SystemArena_FreeDebug(void* ptr, const char* file, s32 line) {
|
2020-03-17 04:31:30 +00:00
|
|
|
__osFreeDebug(&gSystemArena, ptr, file, line);
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* SystemArena_Calloc(u32 num, u32 size) {
|
2020-03-17 04:31:30 +00:00
|
|
|
void* ret;
|
2021-02-14 00:49:40 +00:00
|
|
|
u32 n = num * size;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
|
|
ret = __osMalloc(&gSystemArena, n);
|
2021-02-14 00:49:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
SystemArena_CheckPointer(ret, n, "calloc", "確保");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
void SystemArena_Display(void) {
|
2021-09-04 13:33:19 +00:00
|
|
|
osSyncPrintf("システムヒープ表示\n"); // "System heap display"
|
2020-03-17 04:31:30 +00:00
|
|
|
__osDisplayArena(&gSystemArena);
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) {
|
2020-03-17 04:31:30 +00:00
|
|
|
ArenaImpl_GetSizes(&gSystemArena, outMaxFree, outFree, outAlloc);
|
|
|
|
}
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
void SystemArena_Check(void) {
|
2020-03-17 04:31:30 +00:00
|
|
|
__osCheckArena(&gSystemArena);
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void SystemArena_Init(void* start, u32 size) {
|
2020-03-17 04:31:30 +00:00
|
|
|
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
|
|
|
|
__osMallocInit(&gSystemArena, start, size);
|
|
|
|
}
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
void SystemArena_Cleanup(void) {
|
2020-03-17 04:31:30 +00:00
|
|
|
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
|
|
|
|
__osMallocCleanup(&gSystemArena);
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:35:00 +00:00
|
|
|
u8 SystemArena_IsInitialized(void) {
|
|
|
|
return __osMallocIsInitialized(&gSystemArena);
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|