2020-10-03 15:22:44 +00:00
|
|
|
|
#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;
|
|
|
|
|
|
2021-11-26 22:37:33 +00:00
|
|
|
|
u32 ArenaImpl_GetFillAllocBlock(Arena* arena) {
|
2020-03-17 04:31:30 +00:00
|
|
|
|
return (arena->flag & FILL_ALLOCBLOCK) != 0;
|
|
|
|
|
}
|
2021-11-26 22:37:33 +00:00
|
|
|
|
u32 ArenaImpl_GetFillFreeBlock(Arena* arena) {
|
2020-03-17 04:31:30 +00:00
|
|
|
|
return (arena->flag & FILL_FREEBLOCK) != 0;
|
|
|
|
|
}
|
2021-11-26 22:37:33 +00:00
|
|
|
|
u32 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) {
|
2022-04-09 00:20:23 +00:00
|
|
|
|
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) {
|
2022-04-09 00:20:23 +00:00
|
|
|
|
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) {
|
2022-04-09 00:20:23 +00:00
|
|
|
|
osRecvMesg(&arena->lockQueue, NULL, OS_MESG_BLOCK);
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
|
ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
ArenaNode* next = node->next;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
2021-02-14 00:49:40 +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;
|
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return next;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
|
ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
ArenaNode* prev = node->prev;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
2021-02-14 00:49:40 +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;
|
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return prev;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
|
ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
ArenaNode* last = NULL;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaNode* iter;
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (arena != NULL && arena->head != NULL && arena->head->magic == NODE_MAGIC) {
|
2020-03-17 04:31:30 +00:00
|
|
|
|
iter = arena->head;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
while (iter != NULL) {
|
|
|
|
|
last = iter;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
iter = ArenaImpl_GetNextBlock(iter);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-14 00:49:40 +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
|
|
|
|
|
2021-02-14 00:49:40 +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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (size2 > (s32)sizeof(ArenaNode)) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(firstNode, BLOCK_UNINIT_MAGIC, size2);
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
2020-03-17 04:31:30 +00:00
|
|
|
|
iter = arena->head;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
while (iter != NULL) {
|
2020-03-17 04:31:30 +00:00
|
|
|
|
next = ArenaImpl_GetNextBlock(iter);
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode));
|
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) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2020-03-18 00:09:21 +00:00
|
|
|
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
alloc = (void*)((u32)iter + sizeof(ArenaNode));
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_ALLOCBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
void* alloc;
|
|
|
|
|
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaImpl_Lock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
alloc = __osMalloc_NoLockDebug(arena, size, file, line);
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaImpl_Unlock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
u32 blockSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
|
ArenaNode* next;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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
|
|
|
|
}
|
2020-03-18 00:09:21 +00:00
|
|
|
|
iter = newNode;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iter->isFree = false;
|
|
|
|
|
ArenaImpl_SetDebugInfo(iter, file, line, arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
allocR = (void*)((u32)iter + sizeof(ArenaNode));
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_ALLOCBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2020-03-18 00:09:21 +00:00
|
|
|
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
while (iter != NULL) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
alloc = (void*)((u32)iter + sizeof(ArenaNode));
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_ALLOCBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return alloc;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
|
void* __osMalloc(Arena* arena, u32 size) {
|
2021-02-14 00:49:40 +00:00
|
|
|
|
void* alloc;
|
|
|
|
|
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaImpl_Lock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
alloc = __osMalloc_NoLock(arena, size);
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaImpl_Unlock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
u32 blockSize;
|
2020-03-22 21:19:43 +00:00
|
|
|
|
ArenaNode* next;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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
|
|
|
|
}
|
2020-03-18 00:09:21 +00:00
|
|
|
|
iter = newNode;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iter->isFree = false;
|
|
|
|
|
ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
alloc = (void*)((u32)iter + sizeof(ArenaNode));
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_ALLOCBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
|
|
|
|
|
if (node == NULL || node->magic != NODE_MAGIC) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
// "__osFree: Unauthorized release (%08x)"
|
|
|
|
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x)\n" VT_RST, ptr);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (node->isFree) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x)\n" VT_RST, ptr); // "__osFree: Double release (%08x)"
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (arena != node->arena && arena != NULL) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
// "__osFree:Tried to release in a different way than when it was secured (%08x:%08x)"
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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) {
|
2022-06-16 00:15:44 +00:00
|
|
|
|
__osMemset((void*)((u32)node + sizeof(ArenaNode)), BLOCK_FREE_MAGIC, node->size);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
node->size += next->size + sizeof(ArenaNode);
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_FREEBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
node->next = newNext;
|
|
|
|
|
next = newNext;
|
|
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
2021-02-14 00:49:40 +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
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
prev->next = next;
|
|
|
|
|
prev->size += node->size + sizeof(ArenaNode);
|
|
|
|
|
if (arena->flag & FILL_FREEBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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;
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
|
|
|
|
|
if (node == NULL || node->magic != NODE_MAGIC) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
// "__osFree: Unauthorized release (%08x)"
|
|
|
|
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (node->isFree) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
// "__osFree: Double release (%08x)"
|
|
|
|
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (arena != node->arena && arena != NULL) {
|
2021-09-28 23:53:56 +00:00
|
|
|
|
// "__osFree:Tried to release in a different way than when it was secured (%08x:%08x)"
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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) {
|
2022-06-16 00:15:44 +00:00
|
|
|
|
__osMemset((void*)((u32)node + sizeof(ArenaNode)), BLOCK_FREE_MAGIC, node->size);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
|
node->size += next->size + sizeof(ArenaNode);
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (arena->flag & FILL_FREEBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
node->next = newNext;
|
|
|
|
|
next = newNext;
|
|
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
2021-02-14 00:49:40 +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
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
prev->next = next;
|
|
|
|
|
prev->size += node->size + sizeof(ArenaNode);
|
|
|
|
|
if (arena->flag & FILL_FREEBLOCK) {
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemset(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) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
void* newAlloc;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaNode* node;
|
|
|
|
|
ArenaNode* next;
|
|
|
|
|
ArenaNode* newNext;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
ArenaNode* overNext;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaNode* newNext2;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
ArenaNode* next2;
|
|
|
|
|
u32 sizeDiff;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
ArenaNode* overNext2;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
if (ptr == NULL) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
ptr = __osMalloc_NoLock(arena, newSize);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
} 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) {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "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) {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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;
|
2022-05-01 22:06:35 +00:00
|
|
|
|
__osMemmove(newNext, next, sizeof(ArenaNode));
|
2020-03-22 21:19:43 +00:00
|
|
|
|
} else {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "Allocate a new memory block and move the contents"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n");
|
|
|
|
|
newAlloc = __osMalloc_NoLock(arena, newSize);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (next2 != NULL && next2->isFree) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "Increased free block behind current memory block"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
osSyncPrintf("現メモリブロックの後ろのフリーブロックを大きくしました\n");
|
2020-03-18 00:09:21 +00:00
|
|
|
|
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);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "Generated because there is no free block after the current memory block"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
osSyncPrintf("現メモリブロックの後ろにフリーブロックがないので生成します\n");
|
2020-03-18 00:09:21 +00:00
|
|
|
|
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;
|
2020-03-18 00:09:21 +00:00
|
|
|
|
overNext2 = ArenaImpl_GetNextBlock(newNext2);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (overNext2 != NULL) {
|
2020-03-18 00:09:21 +00:00
|
|
|
|
overNext2->prev = newNext2;
|
2020-03-22 21:19:43 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "There is no room to generate free blocks"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
osSyncPrintf("フリーブロック生成するだけの空きがありません\n");
|
|
|
|
|
ptr = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ArenaImpl_Unlock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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)) {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
osSyncPrintf("アリーナは初期化されていません\n"); // "Arena is not initalized"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArenaImpl_Lock(arena);
|
|
|
|
|
|
|
|
|
|
maxFree = 0;
|
|
|
|
|
freeSize = 0;
|
|
|
|
|
allocatedSize = 0;
|
|
|
|
|
|
2021-09-04 13:33:19 +00:00
|
|
|
|
osSyncPrintf("アリーナの内容 (0x%08x)\n", arena); // "Arena contents (0x%08x)"
|
|
|
|
|
// "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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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),
|
2021-02-14 00:49:40 +00:00
|
|
|
|
(next == NULL) ? '$' : (iter != next->prev ? '!' : ' '),
|
2020-03-22 21:19:43 +00:00
|
|
|
|
iter->isFree ? "空き" : "確保", //? "Free" : "Secure"
|
|
|
|
|
iter->size);
|
|
|
|
|
|
|
|
|
|
if (!iter->isFree) {
|
2020-04-08 16:36:15 +00:00
|
|
|
|
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
|
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);
|
2020-03-18 00:09:21 +00:00
|
|
|
|
next = NULL;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
2020-03-18 00:09:21 +00:00
|
|
|
|
iter = next;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "Total reserved node size 0x%08x bytes"
|
|
|
|
|
osSyncPrintf("確保ブロックサイズの合計 0x%08x バイト\n", allocatedSize);
|
|
|
|
|
// "Total free node size 0x%08x bytes"
|
|
|
|
|
osSyncPrintf("空きブロックサイズの合計 0x%08x バイト\n", freeSize);
|
|
|
|
|
// "Maximum free node size 0x%08x bytes"
|
|
|
|
|
osSyncPrintf("最大空きブロックサイズ 0x%08x バイト\n", maxFree);
|
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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
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);
|
2020-03-18 00:09:21 +00:00
|
|
|
|
next = NULL;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
2020-03-18 00:09:21 +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;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
u32 error = 0;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
|
|
|
|
|
ArenaImpl_Lock(arena);
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "Checking the contents of the arena. . . (%08x)"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
osSyncPrintf("アリーナの内容をチェックしています... (%08x)\n", arena);
|
|
|
|
|
iter = arena->head;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
while (iter != NULL) {
|
2020-03-22 21:19:43 +00:00
|
|
|
|
if (iter && iter->magic == NODE_MAGIC) {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
// "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);
|
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
|
if (error == 0) {
|
2021-09-04 13:33:19 +00:00
|
|
|
|
osSyncPrintf("アリーナはまだ、いけそうです\n"); // "The arena is still going well"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
ArenaImpl_Unlock(arena);
|
2021-02-14 00:49:40 +00:00
|
|
|
|
|
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;
|
|
|
|
|
}
|