diff options
| author | Roman971 <romanlasnier@hotmail.com> | 2020-03-22 22:19:43 +0100 |
|---|---|---|
| committer | Roman971 <romanlasnier@hotmail.com> | 2020-03-22 22:20:03 +0100 |
| commit | 8cfe7cce9f8d8abea794e67510879e751d373a87 (patch) | |
| tree | c6c46f34d3575f6dc02b98b2b5f5ad88d2b2c64e /src/code/__osMalloc.c | |
| parent | 251aea64ab3e6e8cd6f1d9ed34f514e656724777 (diff) | |
Format all src C files
Diffstat (limited to 'src/code/__osMalloc.c')
| -rw-r--r-- | src/code/__osMalloc.c | 581 |
1 files changed, 249 insertions, 332 deletions
diff --git a/src/code/__osMalloc.c b/src/code/__osMalloc.c index 34d602f0a..d5501c663 100644 --- a/src/code/__osMalloc.c +++ b/src/code/__osMalloc.c @@ -1,63 +1,53 @@ #include <global.h> #include <vt.h> -#define FILL_ALLOCBLOCK (1 << 0) -#define FILL_FREEBLOCK (1 << 1) -#define CHECK_FREE_BLOCK (1 << 2) +#define FILL_ALLOCBLOCK (1 << 0) +#define FILL_FREEBLOCK (1 << 1) +#define CHECK_FREE_BLOCK (1 << 2) -#define NODE_MAGIC (0x7373) +#define NODE_MAGIC (0x7373) -#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) +#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) OSMesg sArenaLockMsg; u32 __osMalloc_FreeBlockTest_Enable; -bool ArenaImpl_GetFillAllocBlock(Arena* arena) -{ +bool ArenaImpl_GetFillAllocBlock(Arena* arena) { return (arena->flag & FILL_ALLOCBLOCK) != 0; } -bool ArenaImpl_GetFillFreeBlock(Arena* arena) -{ +bool ArenaImpl_GetFillFreeBlock(Arena* arena) { return (arena->flag & FILL_FREEBLOCK) != 0; } -bool ArenaImpl_GetCheckFreeBlock(Arena* arena) -{ +bool ArenaImpl_GetCheckFreeBlock(Arena* arena) { return (arena->flag & CHECK_FREE_BLOCK) != 0; } -void ArenaImpl_SetFillAllocBlock(Arena* arena) -{ +void ArenaImpl_SetFillAllocBlock(Arena* arena) { arena->flag |= FILL_ALLOCBLOCK; } -void ArenaImpl_SetFillFreeBlock(Arena* arena) -{ +void ArenaImpl_SetFillFreeBlock(Arena* arena) { arena->flag |= FILL_FREEBLOCK; } -void ArenaImpl_SetCheckFreeBlock(Arena* arena) -{ +void ArenaImpl_SetCheckFreeBlock(Arena* arena) { arena->flag |= CHECK_FREE_BLOCK; } -void ArenaImpl_UnsetFillAllocBlock(Arena* arena) -{ +void ArenaImpl_UnsetFillAllocBlock(Arena* arena) { arena->flag &= ~FILL_ALLOCBLOCK; } -void ArenaImpl_UnsetFillFreeBlock(Arena* arena) -{ +void ArenaImpl_UnsetFillFreeBlock(Arena* arena) { arena->flag &= ~FILL_FREEBLOCK; } -void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) -{ +void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) { arena->flag &= ~CHECK_FREE_BLOCK; } -void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, s32 line, Arena* arena) -{ +void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, s32 line, Arena* arena) { node->filename = file; node->line = line; node->threadId = osGetThreadId(NULL); @@ -65,28 +55,23 @@ void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, s32 line, Arena* node->time = osGetTime(); } -void ArenaImpl_LockInit(Arena* arena) -{ +void ArenaImpl_LockInit(Arena* arena) { osCreateMesgQueue(&arena->lock, &sArenaLockMsg, 1); } -void ArenaImpl_Lock(Arena* arena) -{ +void ArenaImpl_Lock(Arena* arena) { osSendMesg(&arena->lock, NULL, OS_MESG_BLOCK); } -void ArenaImpl_Unlock(Arena* arena) -{ +void ArenaImpl_Unlock(Arena* arena) { osRecvMesg(&arena->lock, NULL, OS_MESG_BLOCK); } -ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) -{ +ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) { ArenaNode* ret; ret = node->next; - if (ret && (!ret || (ret->magic != NODE_MAGIC))) - { + if (ret && (!ret || (ret->magic != NODE_MAGIC))) { osSyncPrintf(VT_COL(RED, WHITE) "緊急事態!メモリリーク発見! (block=%08x)\n" VT_RST, ret); ret = NULL; node->next = NULL; @@ -95,13 +80,11 @@ ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) return ret; } -ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) -{ +ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) { ArenaNode* ret; ret = node->prev; - if (ret && (!ret || (ret->magic != NODE_MAGIC))) - { + if (ret && (!ret || (ret->magic != NODE_MAGIC))) { osSyncPrintf(VT_COL(RED, WHITE) "緊急事態!メモリリーク発見! (block=%08x)\n" VT_RST, ret); ret = NULL; node->prev = NULL; @@ -110,16 +93,13 @@ ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) return ret; } -ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) -{ +ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) { ArenaNode* ret = NULL; ArenaNode* iter; - if (arena && arena->head && arena->head->magic == NODE_MAGIC) - { + if (arena && arena->head && arena->head->magic == NODE_MAGIC) { iter = arena->head; - while (iter) - { + while (iter) { ret = iter; iter = ArenaImpl_GetNextBlock(iter); } @@ -128,29 +108,25 @@ ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) return ret; } -void __osMallocInit(Arena* arena, void* start, u32 size) -{ +void __osMallocInit(Arena* arena, void* start, u32 size) { bzero(arena, sizeof(Arena)); ArenaImpl_LockInit(arena); __osMallocAddBlock(arena, start, size); arena->isInit = true; } -void __osMallocAddBlock(Arena* arena, void* start, s32 size) -{ +void __osMallocAddBlock(Arena* arena, void* start, s32 size) { s32 diff; s32 size2; ArenaNode* firstNode; ArenaNode* lastNode; - - if (start) - { + + if (start) { firstNode = (ArenaNode*)ALIGN16((u32)start); diff = (s32)firstNode - (s32)start; size2 = (size - diff) & ~0xF; - if (size2 > (s32)sizeof(ArenaNode)) - { - func_80106860(firstNode, BLOCK_UNINIT_MAGIC, size2); //memset + if (size2 > (s32)sizeof(ArenaNode)) { + func_80106860(firstNode, BLOCK_UNINIT_MAGIC, size2); // memset firstNode->next = NULL; firstNode->prev = NULL; firstNode->size = size2 - sizeof(ArenaNode); @@ -158,13 +134,10 @@ void __osMallocAddBlock(Arena* arena, void* start, s32 size) firstNode->magic = NODE_MAGIC; ArenaImpl_Lock(arena); lastNode = ArenaImpl_GetLastBlock(arena); - if (!lastNode) - { + if (!lastNode) { arena->head = firstNode; arena->start = start; - } - else - { + } else { firstNode->prev = lastNode; lastNode->next = firstNode; } @@ -173,53 +146,47 @@ void __osMallocAddBlock(Arena* arena, void* start, s32 size) } } -void ArenaImpl_RemoveAllBlocks(Arena* arena) -{ +void ArenaImpl_RemoveAllBlocks(Arena* arena) { ArenaNode* iter; ArenaNode* next; ArenaImpl_Lock(arena); iter = arena->head; - while (iter) - { + while (iter) { next = ArenaImpl_GetNextBlock(iter); - func_80106860(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode)); //memset + func_80106860(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode)); // memset iter = next; } ArenaImpl_Unlock(arena); } -void __osMallocCleanup(Arena* arena) -{ +void __osMallocCleanup(Arena* arena) { ArenaImpl_RemoveAllBlocks(arena); bzero(arena, sizeof(*arena)); } -u8 __osMallocIsInitalized(Arena* arena) -{ +u8 __osMallocIsInitalized(Arena* arena) { return arena->isInit; } -void __osMalloc_FreeBlockTest(Arena *arena, ArenaNode *node) -{ - ArenaNode *node2; +void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) { + ArenaNode* node2; u32* start; u32* end; u32* iter; node2 = node; - if (__osMalloc_FreeBlockTest_Enable) - { + if (__osMalloc_FreeBlockTest_Enable) { start = (u32*)((u32)node + sizeof(ArenaNode)); - end = (u32*)((u32) start + node2->size); + end = (u32*)((u32)start + node2->size); iter = start; - 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); + 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); __osDisplayArena(arena); return; } @@ -228,29 +195,26 @@ void __osMalloc_FreeBlockTest(Arena *arena, ArenaNode *node) } } -void* __osMalloc_NoLockDebug(Arena *arena, u32 size, const char *file, s32 line) -{ - ArenaNode *iter; +void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, s32 line) { + ArenaNode* iter; u32 blockSize; - ArenaNode *newNode; - void *ret; - ArenaNode *next; + ArenaNode* newNode; + void* ret; + ArenaNode* next; ret = NULL; iter = arena->head; size = ALIGN16(size); blockSize = ALIGN16(size) + sizeof(ArenaNode); - while (iter) - { - if (iter->isFree && iter->size >= size) - { - if (arena->flag & CHECK_FREE_BLOCK) + while (iter) { + if (iter->isFree && iter->size >= size) { + if (arena->flag & CHECK_FREE_BLOCK) { __osMalloc_FreeBlockTest(arena, iter); + } - if (blockSize < iter->size) - { - newNode = (ArenaNode *)((u32)iter + blockSize); + if (blockSize < iter->size) { + newNode = (ArenaNode*)((u32)iter + blockSize); newNode->next = ArenaImpl_GetNextBlock(iter); newNode->prev = iter; newNode->size = iter->size - blockSize; @@ -260,15 +224,17 @@ void* __osMalloc_NoLockDebug(Arena *arena, u32 size, const char *file, s32 line) iter->next = newNode; iter->size = size; next = ArenaImpl_GetNextBlock(newNode); - if (next) + if (next) { next->prev = newNode; + } } iter->isFree = false; ArenaImpl_SetDebugInfo(iter, file, line, arena); - ret = (void *)((u32)iter + sizeof(ArenaNode)); - if (arena->flag & FILL_ALLOCBLOCK) + ret = (void*)((u32)iter + sizeof(ArenaNode)); + if (arena->flag & FILL_ALLOCBLOCK) { func_80106860(ret, BLOCK_ALLOC_MAGIC, size); + } break; } @@ -279,8 +245,7 @@ void* __osMalloc_NoLockDebug(Arena *arena, u32 size, const char *file, s32 line) return ret; } -void* __osMallocDebug(Arena* arena, u32 size, const char* file, s32 line) -{ +void* __osMallocDebug(Arena* arena, u32 size, const char* file, s32 line) { void* ret; ArenaImpl_Lock(arena); ret = __osMalloc_NoLockDebug(arena, size, file, line); @@ -288,29 +253,27 @@ void* __osMallocDebug(Arena* arena, u32 size, const char* file, s32 line) return ret; } -void* __osMallocRDebug(Arena *arena, u32 size, const char *file, s32 line) -{ - ArenaNode *iter; - ArenaNode *newNode; +void* __osMallocRDebug(Arena* arena, u32 size, const char* file, s32 line) { + ArenaNode* iter; + ArenaNode* newNode; u32 blockSize; - ArenaNode *next; - void *ret; + ArenaNode* next; + void* ret; ret = NULL; size = ALIGN16(size); ArenaImpl_Lock(arena); - iter = ArenaImpl_GetLastBlock(arena);; - while (iter) - { - if (iter->isFree && iter->size >= size) - { - if (arena->flag & CHECK_FREE_BLOCK) + iter = ArenaImpl_GetLastBlock(arena); + + while (iter) { + if (iter->isFree && iter->size >= size) { + if (arena->flag & CHECK_FREE_BLOCK) { __osMalloc_FreeBlockTest(arena, iter); + } blockSize = ALIGN16(size) + sizeof(ArenaNode); - if (blockSize < iter->size) - { - newNode = (ArenaNode *)((u32)iter + (iter->size - size)); + if (blockSize < iter->size) { + newNode = (ArenaNode*)((u32)iter + (iter->size - size)); newNode->next = ArenaImpl_GetNextBlock(iter); newNode->prev = iter; newNode->size = size; @@ -319,17 +282,19 @@ void* __osMallocRDebug(Arena *arena, u32 size, const char *file, s32 line) iter->next = newNode; iter->size -= blockSize; next = ArenaImpl_GetNextBlock(newNode); - if (next) + if (next) { next->prev = newNode; + } iter = newNode; } iter->isFree = false; ArenaImpl_SetDebugInfo(iter, file, line, arena); - ret = (void *)((u32)iter + sizeof(ArenaNode)); - if (arena->flag & FILL_ALLOCBLOCK) + ret = (void*)((u32)iter + sizeof(ArenaNode)); + if (arena->flag & FILL_ALLOCBLOCK) { func_80106860(ret, BLOCK_ALLOC_MAGIC, size); + } break; } @@ -341,30 +306,27 @@ void* __osMallocRDebug(Arena *arena, u32 size, const char *file, s32 line) return ret; } -void* __osMalloc_NoLock(Arena *arena, u32 size) -{ - ArenaNode *iter; +void* __osMalloc_NoLock(Arena* arena, u32 size) { + ArenaNode* iter; u32 blockSize; - ArenaNode *newNode; - void *ret; - ArenaNode *next; + ArenaNode* newNode; + void* ret; + ArenaNode* next; ret = NULL; iter = arena->head; size = ALIGN16(size); blockSize = ALIGN16(size) + sizeof(ArenaNode); - while (iter) - { + while (iter) { - if (iter->isFree && iter->size >= size) - { - if (arena->flag & CHECK_FREE_BLOCK) + if (iter->isFree && iter->size >= size) { + if (arena->flag & CHECK_FREE_BLOCK) { __osMalloc_FreeBlockTest(arena, iter); + } - if (blockSize < iter->size) - { - newNode = (ArenaNode *)((u32)iter + blockSize); + if (blockSize < iter->size) { + newNode = (ArenaNode*)((u32)iter + blockSize); newNode->next = ArenaImpl_GetNextBlock(iter); newNode->prev = iter; newNode->size = iter->size - blockSize; @@ -374,15 +336,17 @@ void* __osMalloc_NoLock(Arena *arena, u32 size) iter->next = newNode; iter->size = size; next = ArenaImpl_GetNextBlock(newNode); - if (next) + if (next) { next->prev = newNode; + } } iter->isFree = false; ArenaImpl_SetDebugInfo(iter, NULL, 0, arena); - ret = (void *)((u32)iter + sizeof(ArenaNode)); - if (arena->flag & FILL_ALLOCBLOCK) + ret = (void*)((u32)iter + sizeof(ArenaNode)); + if (arena->flag & FILL_ALLOCBLOCK) { func_80106860(ret, BLOCK_ALLOC_MAGIC, size); + } break; } @@ -393,8 +357,7 @@ void* __osMalloc_NoLock(Arena *arena, u32 size) return ret; } -void* __osMalloc(Arena* arena, u32 size) -{ +void* __osMalloc(Arena* arena, u32 size) { void* ret; ArenaImpl_Lock(arena); ret = __osMalloc_NoLock(arena, size); @@ -402,29 +365,27 @@ void* __osMalloc(Arena* arena, u32 size) return ret; } -void* __osMallocR(Arena *arena, u32 size) -{ - ArenaNode *iter; - ArenaNode *newNode; +void* __osMallocR(Arena* arena, u32 size) { + ArenaNode* iter; + ArenaNode* newNode; u32 blockSize; - ArenaNode *next; - void *ret; + ArenaNode* next; + void* ret; ret = NULL; size = ALIGN16(size); ArenaImpl_Lock(arena); - iter = ArenaImpl_GetLastBlock(arena);; - while (iter) - { - if (iter->isFree && iter->size >= size) - { - if (arena->flag & CHECK_FREE_BLOCK) + iter = ArenaImpl_GetLastBlock(arena); + + while (iter) { + if (iter->isFree && iter->size >= size) { + if (arena->flag & CHECK_FREE_BLOCK) { __osMalloc_FreeBlockTest(arena, iter); + } blockSize = ALIGN16(size) + sizeof(ArenaNode); - if (blockSize < iter->size) - { - newNode = (ArenaNode *)((u32)iter + (iter->size - size)); + if (blockSize < iter->size) { + newNode = (ArenaNode*)((u32)iter + (iter->size - size)); newNode->next = ArenaImpl_GetNextBlock(iter); newNode->prev = iter; newNode->size = size; @@ -433,17 +394,19 @@ void* __osMallocR(Arena *arena, u32 size) iter->next = newNode; iter->size -= blockSize; next = ArenaImpl_GetNextBlock(newNode); - if (next) + if (next) { next->prev = newNode; - + } + iter = newNode; } iter->isFree = false; ArenaImpl_SetDebugInfo(iter, NULL, 0, arena); - ret = (void *)((u32)iter + sizeof(ArenaNode)); - if (arena->flag & FILL_ALLOCBLOCK) + ret = (void*)((u32)iter + sizeof(ArenaNode)); + if (arena->flag & FILL_ALLOCBLOCK) { func_80106860(ret, BLOCK_ALLOC_MAGIC, size); + } break; } @@ -455,30 +418,27 @@ void* __osMallocR(Arena *arena, u32 size) return ret; } -void __osFree_NoLock(Arena* arena, void* ptr) -{ +void __osFree_NoLock(Arena* arena, void* ptr) { ArenaNode* node; ArenaNode* next; ArenaNode* prev; ArenaNode* newNext; - if (ptr) - { + if (ptr) { 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) + 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) + 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); + 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; } @@ -486,68 +446,66 @@ void __osFree_NoLock(Arena* arena, void* ptr) prev = ArenaImpl_GetPrevBlock(node); node->isFree = true; ArenaImpl_SetDebugInfo(node, NULL, 0, arena); - if (arena->flag & FILL_FREEBLOCK) - { + 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) - { + if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) { newNext = ArenaImpl_GetNextBlock(next); - if (newNext) + if (newNext) { newNext->prev = node; - + } + node->size += next->size + sizeof(ArenaNode); - if (arena->flag & FILL_FREEBLOCK) + if (arena->flag & FILL_FREEBLOCK) { func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode)); + } node->next = newNext; next = newNext; } - if (prev && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) - { - if (next) + if (prev && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) { + if (next) { next->prev = prev; + } prev->next = next; prev->size += node->size + sizeof(ArenaNode); - if (arena->flag & FILL_FREEBLOCK) + if (arena->flag & FILL_FREEBLOCK) { func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode)); + } } } } -void __osFree(Arena* arena, void* ptr) -{ +void __osFree(Arena* arena, void* ptr) { ArenaImpl_Lock(arena); __osFree_NoLock(arena, ptr); ArenaImpl_Unlock(arena); } -void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) -{ +void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) { ArenaNode* node; ArenaNode* next; ArenaNode* prev; ArenaNode* newNext; - if (ptr) - { + if (ptr) { 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) + 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) + 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); + 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; } @@ -555,46 +513,45 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) prev = ArenaImpl_GetPrevBlock(node); node->isFree = true; ArenaImpl_SetDebugInfo(node, file, line, arena); - if (arena->flag & FILL_FREEBLOCK) - { + 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) - { + if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) { newNext = ArenaImpl_GetNextBlock(next); - if (newNext) + if (newNext) { newNext->prev = node; - + } + node->size += next->size + sizeof(ArenaNode); - if (arena->flag & FILL_FREEBLOCK) + if (arena->flag & FILL_FREEBLOCK) { func_80106860(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode)); + } node->next = newNext; next = newNext; } - if (prev && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) - { - if (next) + if (prev && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) { + if (next) { next->prev = prev; + } prev->next = next; prev->size += node->size + sizeof(ArenaNode); - if (arena->flag & FILL_FREEBLOCK) + if (arena->flag & FILL_FREEBLOCK) { func_80106860(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode)); + } } } } -void __osFreeDebug(Arena* arena, void* ptr, const char* file, s32 line) -{ +void __osFreeDebug(Arena* arena, void* ptr, const char* file, s32 line) { ArenaImpl_Lock(arena); __osFree_NoLockDebug(arena, ptr, file, line); ArenaImpl_Unlock(arena); } -void* __osRealloc(Arena* arena, void* ptr, u32 newSize) -{ +void* __osRealloc(Arena* arena, void* ptr, u32 newSize) { void* newAlloc; ArenaNode* node; ArenaNode* next; @@ -611,60 +568,46 @@ void* __osRealloc(Arena* arena, void* ptr, u32 newSize) newSize = ALIGN16(newSize); osSyncPrintf("__osRealloc(%08x, %d)\n", ptr, newSize); ArenaImpl_Lock(arena); - if (!ptr) - { + if (!ptr) { ptr = __osMalloc_NoLock(arena, newSize); - } - else if (!newSize) - { + } else if (!newSize) { __osFree_NoLock(arena, ptr); ptr = NULL; - } - else - { + } else { node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode)); - if (newSize == node->size) - { - //Does nothing because the memory block size does not change + if (newSize == node->size) { + // Does nothing because the memory block size does not change osSyncPrintf("メモリブロックサイズが変わらないためなにもしません\n"); - } - else if (node->size < newSize) - { + } else if (node->size < newSize) { next = ArenaImpl_GetNextBlock(node); sizeDiff = newSize - node->size; - 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 + 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 osSyncPrintf("現メモリブロックの後ろにフリーブロックがあるので結合します\n"); next->size -= sizeDiff; overNext = ArenaImpl_GetNextBlock(next); newNext = (ArenaNode*)((u32)next + sizeDiff); - if (overNext) + if (overNext) { overNext->prev = newNext; + } node->next = newNext; node->size = newSize; - func_801068B0(newNext, next, sizeof(ArenaNode)); //memcpy - } - else - { - //Allocate a new memory block and move the contents + func_801068B0(newNext, next, sizeof(ArenaNode)); // memcpy + } else { + // Allocate a new memory block and move the contents osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n"); newAlloc = __osMalloc_NoLock(arena, newSize); - if (newAlloc) - { + if (newAlloc) { bcopy(ptr, newAlloc, node->size); __osFree_NoLock(arena, ptr); } ptr = newAlloc; } - } - else if (newSize < node->size) - { + } else if (newSize < node->size) { next2 = ArenaImpl_GetNextBlock(node); - if (next2 && next2->isFree) - { + if (next2 && next2->isFree) { blockSize = ALIGN16(newSize) + sizeof(ArenaNode); - //Increased free block behind current memory block + // Increased free block behind current memory block osSyncPrintf("現メモリブロックの後ろのフリーブロックを大きくしました\n"); newNext2 = (ArenaNode*)((u32)node + blockSize); localCopy = *next2; @@ -673,13 +616,12 @@ void* __osRealloc(Arena* arena, void* ptr, u32 newSize) node->next = newNext2; node->size = newSize; overNext2 = ArenaImpl_GetNextBlock(newNext2); - if (overNext2) + if (overNext2) { overNext2->prev = newNext2; - } - else if (newSize + sizeof(ArenaNode) < node->size) - { + } + } else if (newSize + sizeof(ArenaNode) < node->size) { blockSize = ALIGN16(newSize) + sizeof(ArenaNode); - //Generated because there is no free block after the current memory block + // Generated because there is no free block after the current memory block osSyncPrintf("現メモリブロックの後ろにフリーブロックがないので生成します\n"); newNext2 = (ArenaNode*)((u32)node + blockSize); newNext2->next = ArenaImpl_GetNextBlock(node); @@ -690,12 +632,11 @@ void* __osRealloc(Arena* arena, void* ptr, u32 newSize) node->next = newNext2; node->size = newSize; overNext2 = ArenaImpl_GetNextBlock(newNext2); - if (overNext2) + if (overNext2) { overNext2->prev = newNext2; - } - else - { - //There is no room to generate free blocks + } + } else { + // There is no room to generate free blocks osSyncPrintf("フリーブロック生成するだけの空きがありません\n"); ptr = NULL; } @@ -706,13 +647,11 @@ void* __osRealloc(Arena* arena, void* ptr, u32 newSize) return ptr; } -void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, s32 line) -{ +void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, s32 line) { return __osRealloc(arena, ptr, newSize); } -void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) -{ +void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) { ArenaNode* iter; ArenaImpl_Lock(arena); @@ -722,16 +661,15 @@ void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAll *outAlloc = 0; iter = arena->head; - while (iter) - { - if (iter->isFree) - { + while (iter) { + if (iter->isFree) { *outFree += iter->size; - if (*outMaxFree < iter->size) + if (*outMaxFree < iter->size) { *outMaxFree = iter->size; - } - else + } + } else { *outAlloc += iter->size; + } iter = ArenaImpl_GetNextBlock(iter); } @@ -739,17 +677,15 @@ void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAll ArenaImpl_Unlock(arena); } -void __osDisplayArena(Arena* arena) -{ +void __osDisplayArena(Arena* arena) { u32 freeSize; u32 allocatedSize; u32 maxFree; ArenaNode* iter; ArenaNode* next; - if (!__osMallocIsInitalized(arena)) - { - //Arena is not initalized + if (!__osMallocIsInitalized(arena)) { + // Arena is not initalized osSyncPrintf("アリーナは初期化されていません\n"); return; } @@ -760,40 +696,36 @@ void __osDisplayArena(Arena* arena) freeSize = 0; allocatedSize = 0; - //Arena contents (0x%08x) + // Arena contents (0x%08x) osSyncPrintf("アリーナの内容 (0x%08x)\n", arena); - //Memory node range status size [time s ms us ns: TID: src: line] + // Memory node range status size [time s ms us ns: TID: src: line] osSyncPrintf("メモリブロック範囲 status サイズ [時刻 s ms us ns: TID:src:行]\n"); iter = arena->head; - while (iter) - { - if (iter && iter->magic == NODE_MAGIC) - { + while (iter) { + if (iter && iter->magic == NODE_MAGIC) { next = iter->next; osSyncPrintf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size), - (!next) ? '$' : (iter != next->prev ? '!' : ' '), - iter->isFree ? "空き" : "確保", //? "Free" : "Secure" - iter->size); - - if (!iter->isFree) - osSyncPrintf(" [%016llu:%2d:%s:%d]", (iter->time*64ll)/3ull, iter->threadId, iter->filename ? iter->filename : "**NULL**", iter->line); + (!next) ? '$' : (iter != next->prev ? '!' : ' '), + iter->isFree ? "空き" : "確保", //? "Free" : "Secure" + iter->size); + + if (!iter->isFree) { + osSyncPrintf(" [%016llu:%2d:%s:%d]", (iter->time * 64ll) / 3ull, iter->threadId, + iter->filename ? iter->filename : "**NULL**", iter->line); + } osSyncPrintf("\n"); - if (iter->isFree) - { + if (iter->isFree) { freeSize += iter->size; - if (maxFree < iter->size) + if (maxFree < iter->size) { maxFree = iter->size; - } - else - { + } + } else { allocatedSize += iter->size; } - } - else - { + } else { osSyncPrintf("%08x Block Invalid\n", iter); next = NULL; } @@ -801,15 +733,14 @@ void __osDisplayArena(Arena* arena) iter = next; } - 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 + 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 ArenaImpl_Unlock(arena); } -void ArenaImpl_FaultClient(Arena* arena) -{ +void ArenaImpl_FaultClient(Arena* arena) { u32 freeSize; u32 allocatedSize; u32 maxFree; @@ -817,8 +748,7 @@ void ArenaImpl_FaultClient(Arena* arena) ArenaNode* next; FaultDrawer_Printf("ARENA INFO (0x%08x)\n", arena); - if (!__osMallocIsInitalized(arena)) - { + if (!__osMallocIsInitalized(arena)) { FaultDrawer_Printf("Arena is uninitalized\n", arena); return; } @@ -830,31 +760,23 @@ void ArenaImpl_FaultClient(Arena* arena) FaultDrawer_Printf("Memory Block Region status size\n"); iter = arena->head; - while (iter) - { - if (iter && iter->magic == NODE_MAGIC) - { + while (iter) { + if (iter && iter->magic == NODE_MAGIC) { next = iter->next; FaultDrawer_Printf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size), - (!next) ? '$' : (iter != next->prev ? '!' : ' '), - iter->isFree ? "F" : "A", - iter->size); - + (!next) ? '$' : (iter != next->prev ? '!' : ' '), iter->isFree ? "F" : "A", iter->size); + FaultDrawer_Printf("\n"); - if (iter->isFree) - { + if (iter->isFree) { freeSize += iter->size; - if (maxFree < iter->size) + if (maxFree < iter->size) { maxFree = iter->size; - } - else - { + } + } else { allocatedSize += iter->size; } - } - else - { + } else { FaultDrawer_SetFontColor(0xF801); FaultDrawer_Printf("%08x Block Invalid\n", iter); next = NULL; @@ -869,37 +791,32 @@ void ArenaImpl_FaultClient(Arena* arena) FaultDrawer_Printf("Largest Free Block Size %08x\n", maxFree); } -u32 __osCheckArena(Arena* arena) -{ - ArenaNode *iter; +u32 __osCheckArena(Arena* arena) { + ArenaNode* iter; u32 error; error = 0; ArenaImpl_Lock(arena); - //Checking the contents of the arena. . . (%08x) + // Checking the contents of the arena. . . (%08x) osSyncPrintf("アリーナの内容をチェックしています... (%08x)\n", arena); iter = arena->head; - while (iter) - { - if (iter && iter->magic == NODE_MAGIC) - { - //Oops!! (%08x %08x) + while (iter) { + if (iter && iter->magic == NODE_MAGIC) { + // Oops!! (%08x %08x) osSyncPrintf(VT_COL(RED, WHITE) "おおっと!! (%08x %08x)\n" VT_RST, iter, iter->magic); error = 1; break; } iter = ArenaImpl_GetNextBlock(iter); } - if (!error) - { - //The arena is still going well + if (!error) { + // The arena is still going well osSyncPrintf("アリーナはまだ、いけそうです\n"); } ArenaImpl_Unlock(arena); return error; } -u8 func_800FF334(Arena* arena) -{ +u8 func_800FF334(Arena* arena) { return arena->unk_20; } |
