1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-12-02 15:55:59 +00:00
oot/src/code/__osMalloc.c

819 lines
25 KiB
C
Raw Normal View History

#include "global.h"
#include "vt.h"
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
#define FILL_ALLOCBLOCK (1 << 0)
#define FILL_FREEBLOCK (1 << 1)
#define CHECK_FREE_BLOCK (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
OSMesg sArenaLockMsg;
u32 __osMalloc_FreeBlockTest_Enable;
2020-03-22 21:19:43 +00:00
bool ArenaImpl_GetFillAllocBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
return (arena->flag & FILL_ALLOCBLOCK) != 0;
}
2020-03-22 21:19:43 +00:00
bool ArenaImpl_GetFillFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
return (arena->flag & FILL_FREEBLOCK) != 0;
}
2020-03-22 21:19:43 +00:00
bool ArenaImpl_GetCheckFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
return (arena->flag & CHECK_FREE_BLOCK) != 0;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetFillAllocBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag |= FILL_ALLOCBLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetFillFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag |= FILL_FREEBLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetCheckFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag |= CHECK_FREE_BLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetFillAllocBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag &= ~FILL_ALLOCBLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetFillFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag &= ~FILL_FREEBLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
arena->flag &= ~CHECK_FREE_BLOCK;
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, s32 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();
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_LockInit(Arena* arena) {
2020-03-17 04:31:30 +00:00
osCreateMesgQueue(&arena->lock, &sArenaLockMsg, 1);
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_Lock(Arena* arena) {
2020-03-17 04:31:30 +00:00
osSendMesg(&arena->lock, NULL, OS_MESG_BLOCK);
}
2020-03-22 21:19:43 +00:00
void ArenaImpl_Unlock(Arena* arena) {
2020-03-17 04:31:30 +00:00
osRecvMesg(&arena->lock, NULL, OS_MESG_BLOCK);
}
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) "緊急事態!メモリリーク発見! (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) "緊急事態!メモリリーク発見! (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
}
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 && arena->head != NULL && arena->head->magic == NODE_MAGIC) {
2020-03-17 04:31:30 +00:00
iter = arena->head;
while (iter != NULL) {
last = iter;
2020-03-17 04:31:30 +00:00
iter = ArenaImpl_GetNextBlock(iter);
}
}
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)) {
func_80106860(firstNode, BLOCK_UNINIT_MAGIC, size2); // memset
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);
}
}
}
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) {
2020-03-17 04:31:30 +00:00
next = ArenaImpl_GetNextBlock(iter);
2020-03-22 21:19:43 +00:00
func_80106860(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode)); // memset
2020-03-17 04:31:30 +00:00
iter = next;
}
ArenaImpl_Unlock(arena);
}
2020-03-22 21:19:43 +00:00
void __osMallocCleanup(Arena* arena) {
2020-03-17 04:31:30 +00:00
ArenaImpl_RemoveAllBlocks(arena);
bzero(arena, sizeof(*arena));
}
2020-03-22 21:19:43 +00:00
u8 __osMallocIsInitalized(Arena* arena) {
2020-03-17 04:31:30 +00:00
return arena->isInit;
}
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) "緊急事態!メモリリーク検出! (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++;
}
}
}
2020-03-22 21:19:43 +00:00
void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, s32 line) {
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) {
if (arena->flag & CHECK_FREE_BLOCK) {
2020-03-17 04:31:30 +00:00
__osMalloc_FreeBlockTest(arena, iter);
2020-03-22 21:19:43 +00:00
}
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);
2020-03-17 04:31:30 +00:00
newNode->next = ArenaImpl_GetNextBlock(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = ArenaImpl_GetNextBlock(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;
ArenaImpl_SetDebugInfo(iter, file, line, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
break;
}
iter = ArenaImpl_GetNextBlock(iter);
}
return alloc;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
void* __osMallocDebug(Arena* arena, u32 size, const char* file, s32 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
}
2020-03-22 21:19:43 +00:00
void* __osMallocRDebug(Arena* arena, u32 size, const char* file, s32 line) {
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) {
if (arena->flag & CHECK_FREE_BLOCK) {
2020-03-17 04:31:30 +00:00
__osMalloc_FreeBlockTest(arena, iter);
2020-03-22 21:19:43 +00:00
}
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));
2020-03-17 04:31:30 +00:00
newNode->next = ArenaImpl_GetNextBlock(iter);
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = ArenaImpl_GetNextBlock(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;
ArenaImpl_SetDebugInfo(iter, file, line, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(allocR, BLOCK_ALLOC_MAGIC, size);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
break;
}
iter = ArenaImpl_GetPrevBlock(iter);
}
ArenaImpl_Unlock(arena);
return allocR;
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
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) {
if (arena->flag & CHECK_FREE_BLOCK) {
2020-03-17 04:31:30 +00:00
__osMalloc_FreeBlockTest(arena, iter);
2020-03-22 21:19:43 +00:00
}
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);
2020-03-17 04:31:30 +00:00
newNode->next = ArenaImpl_GetNextBlock(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = ArenaImpl_GetNextBlock(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;
ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
break;
}
iter = ArenaImpl_GetNextBlock(iter);
}
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* newNode;
u32 blockSize;
2020-03-22 21:19:43 +00:00
ArenaNode* next;
void* alloc = 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) {
if (arena->flag & CHECK_FREE_BLOCK) {
2020-03-17 04:31:30 +00:00
__osMalloc_FreeBlockTest(arena, iter);
2020-03-22 21:19:43 +00:00
}
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));
2020-03-17 04:31:30 +00:00
newNode->next = ArenaImpl_GetNextBlock(iter);
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = ArenaImpl_GetNextBlock(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;
ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_ALLOCBLOCK) {
func_80106860(alloc, BLOCK_ALLOC_MAGIC, size);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
break;
}
iter = ArenaImpl_GetPrevBlock(iter);
}
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;
ArenaNode* newNext;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (node == NULL || node->magic != NODE_MAGIC) {
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x)\n" VT_RST,
ptr); // __osFree: Unauthorized release (%08x)
return;
}
if (node->isFree) {
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x)\n" VT_RST, ptr); // __osFree: Double release (%08x)
return;
}
if (arena != node->arena && arena != NULL) {
// __osFree:Tried to release in a different way than when it was secured (%08x:%08x)
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n" VT_RST, arena,
node->arena);
return;
}
next = ArenaImpl_GetNextBlock(node);
prev = ArenaImpl_GetPrevBlock(node);
node->isFree = true;
ArenaImpl_SetDebugInfo(node, NULL, 0, arena);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
}
newNext = next;
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
newNext = ArenaImpl_GetNextBlock(next);
if (newNext != NULL) {
newNext->prev = node;
2020-03-17 04:31:30 +00:00
}
node->size += next->size + sizeof(ArenaNode);
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
2020-03-17 04:31:30 +00:00
}
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);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
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);
}
2020-03-22 21:19:43 +00:00
void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 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) {
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x) [%s:%d ]\n" VT_RST, ptr, file,
line); // __osFree: Unauthorized release (%08x)
return;
}
if (node->isFree) {
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x) [%s:%d ]\n" VT_RST, ptr, file,
line); // __osFree: Double release (%08x)
return;
}
if (arena != node->arena && arena != NULL) {
// __osFree:Tried to release in a different way than when it was secured (%08x:%08x)
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n" VT_RST, arena,
node->arena);
return;
}
next = ArenaImpl_GetNextBlock(node);
prev = ArenaImpl_GetPrevBlock(node);
node->isFree = true;
ArenaImpl_SetDebugInfo(node, file, line, arena);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860((u32)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
}
newNext = node->next;
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
newNext = ArenaImpl_GetNextBlock(next);
if (newNext != NULL) {
newNext->prev = node;
2020-03-17 04:31:30 +00:00
}
node->size += next->size + sizeof(ArenaNode);
2020-03-22 21:19:43 +00:00
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
2020-03-17 04:31:30 +00:00
}
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);
if (arena->flag & FILL_FREEBLOCK) {
func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
2020-03-17 04:31:30 +00:00
}
}
}
2020-03-22 21:19:43 +00:00
void __osFreeDebug(Arena* arena, void* ptr, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
ArenaImpl_Lock(arena);
__osFree_NoLockDebug(arena, ptr, file, line);
ArenaImpl_Unlock(arena);
}
2020-03-22 21:19:43 +00:00
void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
void* newAlloc;
2020-03-17 04:31:30 +00:00
ArenaNode* node;
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) {
// Does nothing because the memory block size does not change
2020-03-17 04:31:30 +00:00
osSyncPrintf("メモリブロックサイズが変わらないためなにもしません\n");
2020-03-22 21:19:43 +00:00
} else if (node->size < newSize) {
2020-03-17 04:31:30 +00:00
next = ArenaImpl_GetNextBlock(node);
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) {
// Merge because there is a free block after the current memory block
2020-03-17 04:31:30 +00:00
osSyncPrintf("現メモリブロックの後ろにフリーブロックがあるので結合します\n");
next->size -= sizeDiff;
overNext = ArenaImpl_GetNextBlock(next);
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;
2020-03-22 21:19:43 +00:00
func_801068B0(newNext, next, sizeof(ArenaNode)); // memcpy
} else {
// Allocate a new memory block and move the contents
2020-03-17 04:31:30 +00:00
osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n");
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) {
2020-03-17 04:31:30 +00:00
next2 = ArenaImpl_GetNextBlock(node);
if (next2 != NULL && next2->isFree) {
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
2020-03-22 21:19:43 +00:00
// Increased free block behind current memory block
2020-03-17 04:31:30 +00:00
osSyncPrintf("現メモリブロックの後ろのフリーブロックを大きくしました\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 = ArenaImpl_GetNextBlock(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);
2020-03-22 21:19:43 +00:00
// Generated because there is no free block after the current memory block
2020-03-17 04:31:30 +00:00
osSyncPrintf("現メモリブロックの後ろにフリーブロックがないので生成します\n");
newNext2 = (ArenaNode*)((u32)node + blockSize);
newNext2->next = ArenaImpl_GetNextBlock(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 = ArenaImpl_GetNextBlock(newNext2);
if (overNext2 != NULL) {
overNext2->prev = newNext2;
2020-03-22 21:19:43 +00:00
}
} else {
// There is no room to generate free blocks
2020-03-17 04:31:30 +00:00
osSyncPrintf("フリーブロック生成するだけの空きがありません\n");
ptr = NULL;
}
}
}
ArenaImpl_Unlock(arena);
2020-03-17 04:31:30 +00:00
return ptr;
}
2020-03-22 21:19:43 +00:00
void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, s32 line) {
2020-03-17 04:31:30 +00:00
return __osRealloc(arena, ptr, newSize);
}
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 = ArenaImpl_GetNextBlock(iter);
}
ArenaImpl_Unlock(arena);
}
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;
2020-03-22 21:19:43 +00:00
if (!__osMallocIsInitalized(arena)) {
// Arena is not initalized
2020-03-17 04:31:30 +00:00
osSyncPrintf("アリーナは初期化されていません\n");
return;
}
ArenaImpl_Lock(arena);
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
2020-03-22 21:19:43 +00:00
// Arena contents (0x%08x)
2020-03-17 04:31:30 +00:00
osSyncPrintf("アリーナの内容 (0x%08x)\n", arena);
2020-03-22 21:19:43 +00:00
// Memory node range status size [time s ms us ns: TID: src: line]
2020-03-17 04:31:30 +00:00
osSyncPrintf("メモリブロック範囲 status サイズ [時刻 s ms us ns: TID:src:行]\n");
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 ? '!' : ' '),
2020-03-22 21:19:43 +00:00
iter->isFree ? "空き" : "確保", //? "Free" : "Secure"
iter->size);
if (!iter->isFree) {
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
2020-03-22 21:19:43 +00:00
iter->filename ? iter->filename : "**NULL**", iter->line);
}
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
}
2020-03-22 21:19:43 +00:00
osSyncPrintf("確保ブロックサイズの合計 0x%08x バイト\n", allocatedSize); // Total reserved node size 0x%08x bytes
osSyncPrintf("空きブロックサイズの合計 0x%08x バイト\n", freeSize); // Total free node size 0x%08x bytes
osSyncPrintf("最大空きブロックサイズ 0x%08x バイト\n", maxFree); // Maximum free node size 0x%08x bytes
2020-03-17 04:31:30 +00:00
ArenaImpl_Unlock(arena);
}
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;
FaultDrawer_Printf("ARENA INFO (0x%08x)\n", arena);
2020-03-22 21:19:43 +00:00
if (!__osMallocIsInitalized(arena)) {
2020-03-17 04:31:30 +00:00
FaultDrawer_Printf("Arena is uninitalized\n", arena);
return;
}
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
FaultDrawer_Printf("Memory Block Region status size\n");
iter = arena->head;
while (iter != NULL) {
if (iter != NULL && iter->magic == NODE_MAGIC) {
2020-03-17 04:31:30 +00:00
next = iter->next;
FaultDrawer_Printf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size),
2020-03-22 21:19:43 +00:00
(!next) ? '$' : (iter != next->prev ? '!' : ' '), iter->isFree ? "F" : "A", iter->size);
2020-03-17 04:31:30 +00:00
FaultDrawer_Printf("\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
FaultDrawer_SetFontColor(0xF801);
FaultDrawer_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
}
FaultDrawer_SetFontColor(0x7F1);
FaultDrawer_Printf("Total Alloc Block Size %08x\n", allocatedSize);
FaultDrawer_Printf("Total Free Block Size %08x\n", freeSize);
FaultDrawer_Printf("Largest Free Block Size %08x\n", maxFree);
}
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);
2020-03-22 21:19:43 +00:00
// Checking the contents of the arena. . (%08x)
2020-03-17 04:31:30 +00:00
osSyncPrintf("アリーナの内容をチェックしています... (%08x)\n", arena);
iter = arena->head;
while (iter != NULL) {
2020-03-22 21:19:43 +00:00
if (iter && iter->magic == NODE_MAGIC) {
// Oops!! (%08x %08x)
2020-03-17 04:31:30 +00:00
osSyncPrintf(VT_COL(RED, WHITE) "おおっと!! (%08x %08x)\n" VT_RST, iter, iter->magic);
error = 1;
break;
}
iter = ArenaImpl_GetNextBlock(iter);
}
if (error == 0) {
2020-03-22 21:19:43 +00:00
// The arena is still going well
2020-03-17 04:31:30 +00:00
osSyncPrintf("アリーナはまだ、いけそうです\n");
}
ArenaImpl_Unlock(arena);
2020-03-17 04:31:30 +00:00
return error;
}
2020-03-22 21:19:43 +00:00
u8 func_800FF334(Arena* arena) {
2020-03-17 04:31:30 +00:00
return arena->unk_20;
}