1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-01-16 05:26:59 +00:00
oot/src/code/__osMalloc.c

886 lines
27 KiB
C
Raw Normal View History

#include "global.h"
#include "fault.h"
#include "terminal.h"
2020-03-17 04:31:30 +00:00
#define FILL_ALLOC_BLOCK_FLAG (1 << 0)
#define FILL_FREE_BLOCK_FLAG (1 << 1)
#define CHECK_FREE_BLOCK_FLAG (1 << 2)
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
#define NODE_MAGIC (0x7373)
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
#define BLOCK_UNINIT_MAGIC (0xAB)
#define BLOCK_UNINIT_MAGIC_32 (0xABABABAB)
#define BLOCK_ALLOC_MAGIC (0xCD)
#define BLOCK_ALLOC_MAGIC_32 (0xCDCDCDCD)
#define BLOCK_FREE_MAGIC (0xEF)
#define BLOCK_FREE_MAGIC_32 (0xEFEFEFEF)
2020-03-17 04:31:30 +00:00
#define NODE_IS_VALID(node) (((node) != NULL) && ((node)->magic == NODE_MAGIC))
#if OOT_DEBUG
#define NODE_GET_NEXT(node) ArenaImpl_GetNextBlock(node)
#define NODE_GET_PREV(node) ArenaImpl_GetPrevBlock(node)
#define SET_DEBUG_INFO(node, file, line, arena) ArenaImpl_SetDebugInfo(node, file, line, arena)
#define FILL_ALLOC_BLOCK(arena, alloc, size) \
if ((arena)->flag & FILL_ALLOC_BLOCK_FLAG) \
memset(alloc, BLOCK_ALLOC_MAGIC, size)
#define FILL_FREE_BLOCK_HEADER(arena, node) \
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
memset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode))
#define FILL_FREE_BLOCK_CONTENTS(arena, node) \
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
memset((void*)((u32)(node) + sizeof(ArenaNode)), BLOCK_FREE_MAGIC, (node)->size)
#define CHECK_FREE_BLOCK(arena, node) \
if ((arena)->flag & CHECK_FREE_BLOCK_FLAG) \
__osMalloc_FreeBlockTest(arena, node)
#define CHECK_ALLOC_FAILURE(arena, ptr) (void)0
#else
#define NODE_GET_NEXT(node) (NODE_IS_VALID((node)->next) ? (node)->next : NULL)
#define NODE_GET_PREV(node) (NODE_IS_VALID((node)->prev) ? (node)->prev : NULL)
#define SET_DEBUG_INFO(node, file, line, arena) (void)0
#define FILL_ALLOC_BLOCK(arena, alloc, size) (void)0
#define FILL_FREE_BLOCK_HEADER(arena, node) (void)0
#define FILL_FREE_BLOCK_CONTENTS(arena, node) (void)0
#define CHECK_FREE_BLOCK(arena, node) (void)0
// Number of allocation failures across all arenas.
u32 gTotalAllocFailures = 0; // "Arena_failcnt"
#define CHECK_ALLOC_FAILURE(arena, ptr) \
do { \
if ((ptr) == NULL) { \
gTotalAllocFailures++; \
(arena)->allocFailures++; \
} \
} while (0)
#endif
2020-03-17 04:31:30 +00:00
OSMesg sArenaLockMsg;
#if OOT_DEBUG
2020-03-17 04:31:30 +00:00
u32 __osMalloc_FreeBlockTest_Enable;
u32 ArenaImpl_GetFillAllocBlock(Arena* arena) {
return (arena->flag & FILL_ALLOC_BLOCK_FLAG) != 0;
2020-03-17 04:31:30 +00:00
}
u32 ArenaImpl_GetFillFreeBlock(Arena* arena) {
return (arena->flag & FILL_FREE_BLOCK_FLAG) != 0;
2020-03-17 04:31:30 +00:00
}
u32 ArenaImpl_GetCheckFreeBlock(Arena* arena) {
return (arena->flag & CHECK_FREE_BLOCK_FLAG) != 0;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetFillAllocBlock(Arena* arena) {
arena->flag |= FILL_ALLOC_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetFillFreeBlock(Arena* arena) {
arena->flag |= FILL_FREE_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetCheckFreeBlock(Arena* arena) {
arena->flag |= CHECK_FREE_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetFillAllocBlock(Arena* arena) {
arena->flag &= ~FILL_ALLOC_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetFillFreeBlock(Arena* arena) {
arena->flag &= ~FILL_FREE_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) {
arena->flag &= ~CHECK_FREE_BLOCK_FLAG;
2020-03-17 04:31:30 +00:00
}
void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, int line, Arena* arena) {
2020-03-17 04:31:30 +00:00
node->filename = file;
node->line = line;
node->threadId = osGetThreadId(NULL);
node->arena = arena;
node->time = osGetTime();
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void ArenaImpl_LockInit(Arena* arena) {
osCreateMesgQueue(&arena->lockQueue, &sArenaLockMsg, 1);
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_Lock(Arena* arena) {
osSendMesg(&arena->lockQueue, NULL, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_Unlock(Arena* arena) {
osRecvMesg(&arena->lockQueue, NULL, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
}
#if OOT_DEBUG
2020-03-22 21:19:43 +00:00
ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) {
ArenaNode* next = node->next;
2020-03-17 04:31:30 +00:00
if (next != NULL && (next == NULL || (next->magic != NODE_MAGIC))) {
osSyncPrintf(VT_COL(RED, WHITE) T("緊急事態!メモリリーク発見! (block=%08x)\n",
"Emergency! Memory leak detected! (block=%08x)\n") VT_RST,
next);
next = NULL;
2020-03-17 04:31:30 +00:00
node->next = NULL;
}
return next;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) {
ArenaNode* prev = node->prev;
2020-03-17 04:31:30 +00:00
if (prev != NULL && (prev == NULL || (prev->magic != NODE_MAGIC))) {
osSyncPrintf(VT_COL(RED, WHITE) T("緊急事態!メモリリーク発見! (block=%08x)\n",
"Emergency! Memory leak detected! (block=%08x)\n") VT_RST,
prev);
prev = NULL;
2020-03-17 04:31:30 +00:00
node->prev = NULL;
}
return prev;
2020-03-17 04:31:30 +00:00
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
ArenaNode* last = NULL;
2020-03-17 04:31:30 +00:00
ArenaNode* iter;
if (arena != NULL && NODE_IS_VALID(arena->head)) {
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
last = iter;
iter = NODE_GET_NEXT(last);
2020-03-17 04:31:30 +00:00
}
}
return last;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void __osMallocInit(Arena* arena, void* start, u32 size) {
2020-03-17 04:31:30 +00:00
bzero(arena, sizeof(Arena));
ArenaImpl_LockInit(arena);
__osMallocAddBlock(arena, start, size);
arena->isInit = true;
}
2020-03-22 21:19:43 +00:00
void __osMallocAddBlock(Arena* arena, void* start, s32 size) {
2020-03-17 04:31:30 +00:00
s32 diff;
s32 size2;
ArenaNode* firstNode;
ArenaNode* lastNode;
2020-03-22 21:19:43 +00:00
if (start != NULL) {
2020-03-17 04:31:30 +00:00
firstNode = (ArenaNode*)ALIGN16((u32)start);
diff = (s32)firstNode - (s32)start;
size2 = (size - diff) & ~0xF;
2020-03-22 21:19:43 +00:00
if (size2 > (s32)sizeof(ArenaNode)) {
#if OOT_DEBUG
memset(firstNode, BLOCK_UNINIT_MAGIC, size2);
#endif
2020-03-17 04:31:30 +00:00
firstNode->next = NULL;
firstNode->prev = NULL;
firstNode->size = size2 - sizeof(ArenaNode);
firstNode->isFree = true;
firstNode->magic = NODE_MAGIC;
ArenaImpl_Lock(arena);
lastNode = ArenaImpl_GetLastBlock(arena);
if (lastNode == NULL) {
2020-03-17 04:31:30 +00:00
arena->head = firstNode;
arena->start = start;
2020-03-22 21:19:43 +00:00
} else {
2020-03-17 04:31:30 +00:00
firstNode->prev = lastNode;
lastNode->next = firstNode;
}
ArenaImpl_Unlock(arena);
}
}
}
#if OOT_DEBUG
2020-03-22 21:19:43 +00:00
void ArenaImpl_RemoveAllBlocks(Arena* arena) {
2020-03-17 04:31:30 +00:00
ArenaNode* iter;
ArenaNode* next;
ArenaImpl_Lock(arena);
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
next = NODE_GET_NEXT(iter);
memset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode));
2020-03-17 04:31:30 +00:00
iter = next;
}
ArenaImpl_Unlock(arena);
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void __osMallocCleanup(Arena* arena) {
#if OOT_DEBUG
2020-03-17 04:31:30 +00:00
ArenaImpl_RemoveAllBlocks(arena);
#endif
2020-03-17 04:31:30 +00:00
bzero(arena, sizeof(*arena));
}
u8 __osMallocIsInitialized(Arena* arena) {
2020-03-17 04:31:30 +00:00
return arena->isInit;
}
#if OOT_DEBUG
2020-03-22 21:19:43 +00:00
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) {
ArenaNode* node2 = node;
2020-03-17 04:31:30 +00:00
u32* start;
u32* end;
u32* iter;
2020-03-22 21:19:43 +00:00
if (__osMalloc_FreeBlockTest_Enable) {
2020-03-17 04:31:30 +00:00
start = (u32*)((u32)node + sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
end = (u32*)((u32)start + node2->size);
2020-03-17 04:31:30 +00:00
iter = start;
2020-03-22 21:19:43 +00:00
while (iter < end) {
if (*iter != BLOCK_UNINIT_MAGIC_32 && *iter != BLOCK_FREE_MAGIC_32) {
osSyncPrintf(VT_COL(RED, WHITE)
T("緊急事態!メモリリーク検出! (block=%08x s=%08x e=%08x p=%08x)\n",
"Emergency! Memory leak detected! (block=%08x s=%08x e=%08x p=%08x)\n") VT_RST,
node, start, end, iter);
2020-03-17 04:31:30 +00:00
__osDisplayArena(arena);
return;
}
iter++;
}
}
}
void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, int line) {
2020-03-22 21:19:43 +00:00
ArenaNode* iter;
2020-03-17 04:31:30 +00:00
u32 blockSize;
2020-03-22 21:19:43 +00:00
ArenaNode* newNode;
void* alloc = NULL;
2020-03-22 21:19:43 +00:00
ArenaNode* next;
2020-03-17 04:31:30 +00:00
iter = arena->head;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
2020-03-22 21:19:43 +00:00
if (next) {
2020-03-17 04:31:30 +00:00
next->prev = newNode;
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
2020-03-17 04:31:30 +00:00
break;
}
iter = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
}
return alloc;
2020-03-17 04:31:30 +00:00
}
void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line) {
void* alloc;
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
alloc = __osMalloc_NoLockDebug(arena, size, file, line);
2020-03-17 04:31:30 +00:00
ArenaImpl_Unlock(arena);
return alloc;
2020-03-17 04:31:30 +00:00
}
void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line) {
2020-03-22 21:19:43 +00:00
ArenaNode* iter;
ArenaNode* newNode;
u32 blockSize;
2020-03-22 21:19:43 +00:00
ArenaNode* next;
void* allocR = NULL;
2020-03-17 04:31:30 +00:00
size = ALIGN16(size);
ArenaImpl_Lock(arena);
2020-03-22 21:19:43 +00:00
iter = ArenaImpl_GetLastBlock(arena);
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
2020-03-17 04:31:30 +00:00
blockSize = ALIGN16(size) + sizeof(ArenaNode);
2020-03-22 21:19:43 +00:00
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + (iter->size - size));
newNode->next = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
2020-03-22 21:19:43 +00:00
if (next) {
2020-03-17 04:31:30 +00:00
next->prev = newNode;
2020-03-22 21:19:43 +00:00
}
iter = newNode;
2020-03-17 04:31:30 +00:00
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, allocR, size);
2020-03-17 04:31:30 +00:00
break;
}
iter = NODE_GET_PREV(iter);
2020-03-17 04:31:30 +00:00
}
ArenaImpl_Unlock(arena);
return allocR;
2020-03-17 04:31:30 +00:00
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void* __osMalloc_NoLock(Arena* arena, u32 size) {
ArenaNode* iter;
2020-03-17 04:31:30 +00:00
u32 blockSize;
2020-03-22 21:19:43 +00:00
ArenaNode* newNode;
void* alloc = NULL;
2020-03-22 21:19:43 +00:00
ArenaNode* next;
2020-03-17 04:31:30 +00:00
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
iter = arena->head;
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
2020-03-22 21:19:43 +00:00
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
2020-03-22 21:19:43 +00:00
if (next) {
2020-03-17 04:31:30 +00:00
next->prev = newNode;
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
2020-03-17 04:31:30 +00:00
break;
}
iter = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
}
CHECK_ALLOC_FAILURE(arena, alloc);
return alloc;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void* __osMalloc(Arena* arena, u32 size) {
void* alloc;
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
alloc = __osMalloc_NoLock(arena, size);
2020-03-17 04:31:30 +00:00
ArenaImpl_Unlock(arena);
return alloc;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void* __osMallocR(Arena* arena, u32 size) {
ArenaNode* iter;
ArenaNode* allocNode;
2020-03-22 21:19:43 +00:00
ArenaNode* newNode;
ArenaNode* next;
void* alloc = NULL;
u32 blockSize;
2020-03-17 04:31:30 +00:00
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
2020-03-22 21:19:43 +00:00
iter = ArenaImpl_GetLastBlock(arena);
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (blockSize < iter->size) {
allocNode = (ArenaNode*)((u32)iter + (iter->size - size));
allocNode->next = NODE_GET_NEXT(iter);
newNode = allocNode;
2020-03-17 04:31:30 +00:00
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
2020-03-22 21:19:43 +00:00
if (next) {
2020-03-17 04:31:30 +00:00
next->prev = newNode;
2020-03-22 21:19:43 +00:00
}
iter = newNode;
2020-03-17 04:31:30 +00:00
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
2020-03-17 04:31:30 +00:00
break;
}
iter = NODE_GET_PREV(iter);
2020-03-17 04:31:30 +00:00
}
CHECK_ALLOC_FAILURE(arena, alloc);
2020-03-17 04:31:30 +00:00
ArenaImpl_Unlock(arena);
return alloc;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void __osFree_NoLock(Arena* arena, void* ptr) {
2020-03-17 04:31:30 +00:00
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (node == NULL || node->magic != NODE_MAGIC) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n") VT_RST,
ptr);
return;
}
if (node->isFree) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n") VT_RST, ptr);
return;
}
#if OOT_DEBUG
if (arena != node->arena && arena != NULL) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n",
"__osFree:Tried to release in a different way than when it was secured (%08x:%08x)\n") VT_RST,
arena, node->arena);
return;
}
#endif
next = NODE_GET_NEXT(node);
prev = NODE_GET_PREV(node);
node->isFree = true;
SET_DEBUG_INFO(node, NULL, 0, arena);
FILL_FREE_BLOCK_CONTENTS(arena, node);
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
ArenaNode* newNext = NODE_GET_NEXT(next);
if (newNext != NULL) {
newNext->prev = node;
2020-03-17 04:31:30 +00:00
}
node->size += next->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, next);
node->next = newNext;
next = newNext;
}
2020-03-17 04:31:30 +00:00
if (prev != NULL && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) {
if (next) {
next->prev = prev;
2020-03-17 04:31:30 +00:00
}
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, node);
2020-03-17 04:31:30 +00:00
}
}
2020-03-22 21:19:43 +00:00
void __osFree(Arena* arena, void* ptr) {
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
__osFree_NoLock(arena, ptr);
ArenaImpl_Unlock(arena);
}
#if OOT_DEBUG
void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, int line) {
2020-03-17 04:31:30 +00:00
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
ArenaNode* newNext;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (node == NULL || node->magic != NODE_MAGIC) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:不正解放(%08x) [%s:%d ]\n", "__osFree: Unauthorized release (%08x) [%s:%d ]\n") VT_RST,
ptr, file, line);
return;
}
if (node->isFree) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:二重解放(%08x) [%s:%d ]\n", "__osFree: Double release (%08x) [%s:%d ]\n")
VT_RST,
ptr, file, line);
return;
}
if (arena != node->arena && arena != NULL) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n",
"__osFree:Tried to release in a different way than when it was secured (%08x:%08x)\n") VT_RST,
arena, node->arena);
return;
}
next = NODE_GET_NEXT(node);
prev = NODE_GET_PREV(node);
node->isFree = true;
SET_DEBUG_INFO(node, file, line, arena);
FILL_FREE_BLOCK_CONTENTS(arena, node);
newNext = node->next;
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
newNext = NODE_GET_NEXT(next);
if (newNext != NULL) {
newNext->prev = node;
2020-03-17 04:31:30 +00:00
}
node->size += next->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, next);
node->next = newNext;
next = newNext;
}
2020-03-17 04:31:30 +00:00
if (prev != NULL && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) {
if (next != NULL) {
next->prev = prev;
2020-03-17 04:31:30 +00:00
}
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, node);
2020-03-17 04:31:30 +00:00
}
}
void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line) {
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
__osFree_NoLockDebug(arena, ptr, file, line);
ArenaImpl_Unlock(arena);
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
2020-03-17 04:31:30 +00:00
ArenaNode* node;
void* newAlloc;
2020-03-17 04:31:30 +00:00
ArenaNode* next;
ArenaNode* newNext;
ArenaNode* overNext;
2020-03-17 04:31:30 +00:00
ArenaNode* newNext2;
ArenaNode* next2;
u32 sizeDiff;
2020-03-17 04:31:30 +00:00
ArenaNode* overNext2;
ArenaNode localCopy;
u32 blockSize;
s32 pad;
2020-03-17 04:31:30 +00:00
newSize = ALIGN16(newSize);
osSyncPrintf("__osRealloc(%08x, %d)\n", ptr, newSize);
ArenaImpl_Lock(arena);
if (ptr == NULL) {
ptr = __osMalloc_NoLock(arena, newSize);
} else if (newSize == 0) {
2020-03-17 04:31:30 +00:00
__osFree_NoLock(arena, ptr);
ptr = NULL;
2020-03-22 21:19:43 +00:00
} else {
2020-03-17 04:31:30 +00:00
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
if (newSize == node->size) {
osSyncPrintf(T("メモリブロックサイズが変わらないためなにもしません\n",
"Does nothing because the memory block size does not change\n"));
2020-03-22 21:19:43 +00:00
} else if (node->size < newSize) {
next = NODE_GET_NEXT(node);
2020-03-17 04:31:30 +00:00
sizeDiff = newSize - node->size;
2020-03-22 21:19:43 +00:00
if ((u32)next == ((u32)node + node->size + sizeof(ArenaNode)) && next->isFree && next->size >= sizeDiff) {
osSyncPrintf(T("現メモリブロックの後ろにフリーブロックがあるので結合します\n",
"Merge because there is a free block after the current memory block\n"));
2020-03-17 04:31:30 +00:00
next->size -= sizeDiff;
overNext = NODE_GET_NEXT(next);
2020-03-17 04:31:30 +00:00
newNext = (ArenaNode*)((u32)next + sizeDiff);
if (overNext != NULL) {
2020-03-17 04:31:30 +00:00
overNext->prev = newNext;
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
node->next = newNext;
node->size = newSize;
memmove(node->next, next, sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
} else {
osSyncPrintf(T("新たにメモリブロックを確保して内容を移動します\n",
"Allocate a new memory block and move the contents\n"));
2020-03-17 04:31:30 +00:00
newAlloc = __osMalloc_NoLock(arena, newSize);
if (newAlloc != NULL) {
2020-03-17 04:31:30 +00:00
bcopy(ptr, newAlloc, node->size);
__osFree_NoLock(arena, ptr);
}
ptr = newAlloc;
}
2020-03-22 21:19:43 +00:00
} else if (newSize < node->size) {
next2 = NODE_GET_NEXT(node);
if (next2 != NULL && next2->isFree) {
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
osSyncPrintf(T("現メモリブロックの後ろのフリーブロックを大きくしました\n",
"Increased free block behind current memory block\n"));
newNext2 = (ArenaNode*)((u32)node + blockSize);
localCopy = *next2;
*newNext2 = localCopy;
2020-03-17 04:31:30 +00:00
newNext2->size += node->size - newSize;
node->next = newNext2;
node->size = newSize;
overNext2 = NODE_GET_NEXT(newNext2);
if (overNext2 != NULL) {
2020-03-17 04:31:30 +00:00
overNext2->prev = newNext2;
2020-03-22 21:19:43 +00:00
}
} else if (newSize + sizeof(ArenaNode) < node->size) {
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
osSyncPrintf(T("現メモリブロックの後ろにフリーブロックがないので生成します\n",
"Generated because there is no free block after the current memory block\n"));
newNext2 = (ArenaNode*)((u32)node + blockSize);
newNext2->next = NODE_GET_NEXT(node);
newNext2->prev = node;
newNext2->size = node->size - blockSize;
newNext2->isFree = true;
newNext2->magic = NODE_MAGIC;
node->next = newNext2;
2020-03-17 04:31:30 +00:00
node->size = newSize;
overNext2 = NODE_GET_NEXT(newNext2);
if (overNext2 != NULL) {
overNext2->prev = newNext2;
2020-03-22 21:19:43 +00:00
}
} else {
osSyncPrintf(
T("フリーブロック生成するだけの空きがありません\n", "There is no room to generate free blocks\n"));
2020-03-17 04:31:30 +00:00
ptr = NULL;
}
}
CHECK_ALLOC_FAILURE(arena, ptr);
2020-03-17 04:31:30 +00:00
}
ArenaImpl_Unlock(arena);
2020-03-17 04:31:30 +00:00
return ptr;
}
#if OOT_DEBUG
void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line) {
2020-03-17 04:31:30 +00:00
return __osRealloc(arena, ptr, newSize);
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
2020-03-17 04:31:30 +00:00
ArenaNode* iter;
ArenaImpl_Lock(arena);
*outMaxFree = 0;
*outFree = 0;
*outAlloc = 0;
iter = arena->head;
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter->isFree) {
2020-03-17 04:31:30 +00:00
*outFree += iter->size;
2020-03-22 21:19:43 +00:00
if (*outMaxFree < iter->size) {
2020-03-17 04:31:30 +00:00
*outMaxFree = iter->size;
2020-03-22 21:19:43 +00:00
}
} else {
2020-03-17 04:31:30 +00:00
*outAlloc += iter->size;
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
iter = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
}
ArenaImpl_Unlock(arena);
}
#if OOT_DEBUG
2020-03-22 21:19:43 +00:00
void __osDisplayArena(Arena* arena) {
2020-03-17 04:31:30 +00:00
u32 freeSize;
u32 allocatedSize;
u32 maxFree;
ArenaNode* iter;
ArenaNode* next;
if (!__osMallocIsInitialized(arena)) {
osSyncPrintf(T("アリーナは初期化されていません\n", "Arena is not initialized\n"));
2020-03-17 04:31:30 +00:00
return;
}
ArenaImpl_Lock(arena);
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
osSyncPrintf(T("アリーナの内容 (0x%08x)\n", "Arena contents (0x%08x)\n"), arena);
osSyncPrintf(T("メモリブロック範囲 status サイズ [時刻 s ms us ns: TID:src:行]\n",
"Memory node range status size [time s ms us ns: TID:src:line]\n"));
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
if (iter != NULL && iter->magic == NODE_MAGIC) {
2020-03-17 04:31:30 +00:00
next = iter->next;
osSyncPrintf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size),
(next == NULL) ? '$' : (iter != next->prev ? '!' : ' '),
iter->isFree ? T("空き", "Free") : T("確保", "Secure"), iter->size);
2020-03-22 21:19:43 +00:00
if (!iter->isFree) {
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
Fix/cleanup/rephrase miscellaneous stuff (#983) * Add parens around params usage in VEC_SET macro * Remove unnecessary space character in a xml * Use defines instead of magic values in head/tail magic comments * Use `OS_USEC_TO_CYCLES` to make a time look better in `Graph_TaskSet00` * `0x25800` -> `sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH])` * `0x803DA800` -> `0x80400000 - frame buffer size` * Use `OS_VI_` defines instead of hex * Add empty line after some variable declarations * Remove unused `extern CutsceneData` in `z_bg_dy_yoseizo.c` * `Matrix_MtxFToYXZRotS` does not use `MTXMODE_` * Use `MTXMODE_` more * Remove `ASCII_TO_U32`, use `'IS64'` * Add explicit `!= NULL` in some ternaries * Use `INV_CONTENT`, `AMMO` macros more * Use `PLAYER_AP_` enum more to compare to `Player#heldItemActionParam` * Get rid of lowercase hex (outside libultra) * `gWindMill*` -> `gWindmill*` * Format and small fix enums in `z_boss_mo.h` * Use `CHECK_BTN_ANY` more * Fix xz/xy mistake in comment in tektite * Rephrase comments mentioning "the devs" in a more neutral way * Clean-up some objectively useless parens * Fix some negative values written as u16 instead of s16 in ichains * `SKJ_ACTON_` -> `SKJ_ACTION_` * Run formatter * Fix unk_ offset of `TransformUpdateIndex#unk_10` -> `unk_0E` * Remove comments using in-game text * Remove `U` suffix from integer literals * Revert "Remove `ASCII_TO_U32`, use `'IS64'`" This reverts commit c801337dde9fe5e8b7a7ecf85ad3629bf5b87aaf. * Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)` * Add empty line after decl x2 * Revert "Use `PLAYER_STR_*` to compare to `CUR_UPG_VALUE(UPG_STRENGTH)`" This reverts commit d80bdb32da449edc74e02b8ab3f5a2c532e74bdb. * Make `CUR_UPG_VALUE(UPG_STRENGTH)` compare to integers (eventually needs its own enum) * Only use `PLAYER_SHIELD_` enum with `Player#currentShield` * Only use `PLAYER_TUNIC_` enum with `Player#currentTunic`
2021-10-03 03:17:09 +00:00
iter->filename != NULL ? iter->filename : "**NULL**", iter->line);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
osSyncPrintf("\n");
2020-03-22 21:19:43 +00:00
if (iter->isFree) {
2020-03-17 04:31:30 +00:00
freeSize += iter->size;
2020-03-22 21:19:43 +00:00
if (maxFree < iter->size) {
2020-03-17 04:31:30 +00:00
maxFree = iter->size;
2020-03-22 21:19:43 +00:00
}
} else {
2020-03-17 04:31:30 +00:00
allocatedSize += iter->size;
}
2020-03-22 21:19:43 +00:00
} else {
2020-03-17 04:31:30 +00:00
osSyncPrintf("%08x Block Invalid\n", iter);
next = NULL;
2020-03-17 04:31:30 +00:00
}
iter = next;
2020-03-17 04:31:30 +00:00
}
osSyncPrintf(T("確保ブロックサイズの合計 0x%08x バイト\n", "Total reserved node size 0x%08x bytes\n"),
allocatedSize);
osSyncPrintf(T("空きブロックサイズの合計 0x%08x バイト\n", "Total free node size 0x%08x bytes\n"), freeSize);
osSyncPrintf(T("最大空きブロックサイズ 0x%08x バイト\n", "Maximum free node size 0x%08x bytes\n"), maxFree);
2020-03-17 04:31:30 +00:00
ArenaImpl_Unlock(arena);
}
#endif
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
void ArenaImpl_FaultClient(Arena* arena) {
2020-03-17 04:31:30 +00:00
u32 freeSize;
u32 allocatedSize;
u32 maxFree;
ArenaNode* iter;
ArenaNode* next;
Fault_Printf("ARENA INFO (0x%08x)\n", arena);
if (!__osMallocIsInitialized(arena)) {
Fault_Printf("Arena is uninitalized\n", arena);
2020-03-17 04:31:30 +00:00
return;
}
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
Fault_Printf("Memory Block Region status size\n");
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
if (iter != NULL && iter->magic == NODE_MAGIC) {
2020-03-17 04:31:30 +00:00
next = iter->next;
Fault_Printf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size),
(!next) ? '$' : (iter != next->prev ? '!' : ' '), iter->isFree ? "F" : "A", iter->size);
2020-03-22 21:19:43 +00:00
Fault_Printf("\n");
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (iter->isFree) {
2020-03-17 04:31:30 +00:00
freeSize += iter->size;
2020-03-22 21:19:43 +00:00
if (maxFree < iter->size) {
2020-03-17 04:31:30 +00:00
maxFree = iter->size;
2020-03-22 21:19:43 +00:00
}
} else {
2020-03-17 04:31:30 +00:00
allocatedSize += iter->size;
}
2020-03-22 21:19:43 +00:00
} else {
Fault_SetFontColor(0xF801);
Fault_Printf("%08x Block Invalid\n", iter);
next = NULL;
2020-03-17 04:31:30 +00:00
}
iter = next;
2020-03-17 04:31:30 +00:00
}
Fault_SetFontColor(0x7F1);
Fault_Printf("Total Alloc Block Size %08x\n", allocatedSize);
Fault_Printf("Total Free Block Size %08x\n", freeSize);
Fault_Printf("Largest Free Block Size %08x\n", maxFree);
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
u32 __osCheckArena(Arena* arena) {
ArenaNode* iter;
u32 error = 0;
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
osSyncPrintf(
T("アリーナの内容をチェックしています... (%08x)\n", "Checking the contents of the arena... (%08x)\n"),
arena);
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter && iter->magic == NODE_MAGIC) {
#if OOT_DEBUG
osSyncPrintf(VT_COL(RED, WHITE) T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n") VT_RST, iter,
iter->magic);
#else
osSyncPrintf(T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n"), iter, iter->magic);
#endif
2020-03-17 04:31:30 +00:00
error = 1;
break;
}
iter = NODE_GET_NEXT(iter);
2020-03-17 04:31:30 +00:00
}
if (error == 0) {
osSyncPrintf(T("アリーナはまだ、いけそうです\n", "The arena is still going well\n"));
2020-03-17 04:31:30 +00:00
}
ArenaImpl_Unlock(arena);
2020-03-17 04:31:30 +00:00
return error;
}
#if OOT_DEBUG
u8 ArenaImpl_GetAllocFailures(Arena* arena) {
return arena->allocFailures;
2020-03-17 04:31:30 +00:00
}
#endif