mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-01-30 15:00:13 -05:00
Clean __osMalloc.c
(#4351)
* Remove #ifs * Add `NODE_IS_VALID` * Fix current macros * `NODE_GET_NEXT` & `NODE_GET_PREV` * Add macros * Use macros and general cleanup * Fix build (hopefully) * Address review
This commit is contained in:
parent
f12a2bbbb7
commit
ffc132a01b
@ -2267,7 +2267,7 @@ void __osMallocInit(Arena* arena, void* start, size_t size);
|
|||||||
void __osMallocAddBlock(Arena* arena, void* start, ptrdiff_t size);
|
void __osMallocAddBlock(Arena* arena, void* start, ptrdiff_t size);
|
||||||
void ArenaImpl_RemoveAllBlocks(Arena* arena);
|
void ArenaImpl_RemoveAllBlocks(Arena* arena);
|
||||||
void __osMallocCleanup(Arena* arena);
|
void __osMallocCleanup(Arena* arena);
|
||||||
u8 __osMallocIsInitalized(Arena* arena);
|
s32 __osMallocIsInitialized(Arena* arena);
|
||||||
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node);
|
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node);
|
||||||
void* __osMalloc_NoLockDebug(Arena* arena, size_t size, const char* file, s32 line);
|
void* __osMalloc_NoLockDebug(Arena* arena, size_t size, const char* file, s32 line);
|
||||||
void* __osMallocDebug(Arena* arena, size_t size, const char* file, s32 line);
|
void* __osMallocDebug(Arena* arena, size_t size, const char* file, s32 line);
|
||||||
@ -2284,7 +2284,7 @@ void* __osReallocDebug(Arena* arena, void* ptr, size_t newSize, const char* file
|
|||||||
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc);
|
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc);
|
||||||
void __osDisplayArena(Arena* arena);
|
void __osDisplayArena(Arena* arena);
|
||||||
void ArenaImpl_FaultClient(Arena* arena);
|
void ArenaImpl_FaultClient(Arena* arena);
|
||||||
u32 __osCheckArena(Arena* arena);
|
s32 __osCheckArena(Arena* arena);
|
||||||
u8 func_800FF334(Arena* arena);
|
u8 func_800FF334(Arena* arena);
|
||||||
s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args);
|
s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args);
|
||||||
s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...);
|
s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...);
|
||||||
@ -2408,7 +2408,7 @@ OSThread* __osGetCurrFaultedThread(void);
|
|||||||
u32* osViGetCurrentFramebuffer(void);
|
u32* osViGetCurrentFramebuffer(void);
|
||||||
s32 __osSpSetPc(void* pc);
|
s32 __osSpSetPc(void* pc);
|
||||||
f32 absf(f32);
|
f32 absf(f32);
|
||||||
void* func_801068B0(void* dst, void* src, size_t size);
|
void* oot_memmove(void* dest, const void* src, size_t len);
|
||||||
void Message_UpdateOcarinaGame(PlayState* play);
|
void Message_UpdateOcarinaGame(PlayState* play);
|
||||||
u8 Message_ShouldAdvance(PlayState* play);
|
u8 Message_ShouldAdvance(PlayState* play);
|
||||||
u8 Message_ShouldAdvanceSilent(PlayState* play);
|
u8 Message_ShouldAdvanceSilent(PlayState* play);
|
||||||
|
@ -2038,13 +2038,13 @@ typedef struct ArenaNode {
|
|||||||
/* 0x04 */ size_t size;
|
/* 0x04 */ size_t size;
|
||||||
/* 0x08 */ struct ArenaNode* next;
|
/* 0x08 */ struct ArenaNode* next;
|
||||||
/* 0x0C */ struct ArenaNode* prev;
|
/* 0x0C */ struct ArenaNode* prev;
|
||||||
///* 0x10 */ const char* filename;
|
// /* 0x10 */ const char* filename;
|
||||||
///* 0x14 */ s32 line;
|
// /* 0x14 */ s32 line;
|
||||||
///* 0x18 */ OSId threadId;
|
// /* 0x18 */ OSId threadId;
|
||||||
///* 0x1C */ Arena* arena;
|
// /* 0x1C */ Arena* arena;
|
||||||
///* 0x20 */ OSTime time;
|
// /* 0x20 */ OSTime time;
|
||||||
///* 0x28 */ u8 unk_28[0x30-0x28]; // probably padding
|
// /* 0x28 */ u8 unk_28[0x30-0x28]; // probably padding
|
||||||
} ArenaNode; // size = 0x10
|
} ArenaNode; // size = 0x30
|
||||||
|
|
||||||
typedef struct OverlayRelocationSection {
|
typedef struct OverlayRelocationSection {
|
||||||
/* 0x00 */ u32 textSize;
|
/* 0x00 */ u32 textSize;
|
||||||
|
@ -3,61 +3,132 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define FILL_ALLOCBLOCK (1 << 0)
|
// SOH [General] This file corresponds to decomp's "__osMalloc_gc.c", there's currently no file corresponding to decomp's "__osMalloc_n64.c"
|
||||||
#define FILL_FREEBLOCK (1 << 1)
|
|
||||||
#define CHECK_FREE_BLOCK (1 << 2)
|
|
||||||
|
|
||||||
#define NODE_MAGIC (0x7373)
|
// #region SOH [General] We currently don't set OOT_DEBUG when building so set it here manually
|
||||||
|
#define OOT_DEBUG 1
|
||||||
|
// #endregion
|
||||||
|
|
||||||
#define BLOCK_UNINIT_MAGIC (0xAB)
|
// #region SOH [General] We renamed OoT's memmove to prevent conflicts with the libc version
|
||||||
#define BLOCK_UNINIT_MAGIC_32 (0xABABABAB)
|
#define memmove oot_memmove
|
||||||
#define BLOCK_ALLOC_MAGIC (0xCD)
|
// #endregion
|
||||||
#define BLOCK_ALLOC_MAGIC_32 (0xCDCDCDCD)
|
|
||||||
#define BLOCK_FREE_MAGIC (0xEF)
|
#define FILL_ALLOC_BLOCK_FLAG (1 << 0)
|
||||||
#define BLOCK_FREE_MAGIC_32 (0xEFEFEFEF)
|
#define FILL_FREE_BLOCK_FLAG (1 << 1)
|
||||||
|
#define CHECK_FREE_BLOCK_FLAG (1 << 2)
|
||||||
|
|
||||||
|
#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 NODE_IS_VALID(node) (((node) != NULL) && ((node)->magic == NODE_MAGIC))
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
|
|
||||||
|
#define NODE_GET_NEXT(node) ArenaImpl_GetNextBlock(node)
|
||||||
|
#define NODE_GET_PREV(node) ArenaImpl_GetPrevBlock(node)
|
||||||
|
|
||||||
|
#define SET_DEBUG_INFO(node, file, line, arena) ArenaImpl_SetDebugInfo(node, file, line, arena)
|
||||||
|
|
||||||
|
#define FILL_UNINIT_BLOCK(arena, node, size) memset(node, BLOCK_UNINIT_MAGIC, size)
|
||||||
|
|
||||||
|
#define FILL_ALLOC_BLOCK(arena, alloc, size) \
|
||||||
|
if ((arena)->flag & FILL_ALLOC_BLOCK_FLAG) \
|
||||||
|
memset(alloc, BLOCK_ALLOC_MAGIC, size)
|
||||||
|
|
||||||
|
#define FILL_FREE_BLOCK_HEADER(arena, node) \
|
||||||
|
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
|
||||||
|
memset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode))
|
||||||
|
|
||||||
|
#define FILL_FREE_BLOCK_CONTENTS(arena, node) \
|
||||||
|
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
|
||||||
|
memset((void*)((uintptr_t)(node) + sizeof(ArenaNode)), BLOCK_FREE_MAGIC, (node)->size)
|
||||||
|
|
||||||
|
#define CHECK_FREE_BLOCK(arena, node) \
|
||||||
|
if ((arena)->flag & CHECK_FREE_BLOCK_FLAG) \
|
||||||
|
__osMalloc_FreeBlockTest(arena, node)
|
||||||
|
|
||||||
|
#define CHECK_ALLOC_FAILURE(arena, ptr) (void)0
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define NODE_GET_NEXT(node) (NODE_IS_VALID((node)->next) ? (node)->next : NULL)
|
||||||
|
#define NODE_GET_PREV(node) (NODE_IS_VALID((node)->prev) ? (node)->prev : NULL)
|
||||||
|
|
||||||
|
#define SET_DEBUG_INFO(node, file, line, arena) (void)0
|
||||||
|
#define FILL_UNINIT_BLOCK(arena, node, size) (void)0
|
||||||
|
#define FILL_ALLOC_BLOCK(arena, alloc, size) (void)0
|
||||||
|
#define FILL_FREE_BLOCK_HEADER(arena, node) (void)0
|
||||||
|
#define FILL_FREE_BLOCK_CONTENTS(arena, node) (void)0
|
||||||
|
#define CHECK_FREE_BLOCK(arena, node) (void)0
|
||||||
|
|
||||||
|
// Number of allocation failures across all arenas.
|
||||||
|
u32 gTotalAllocFailures = 0; // "Arena_failcnt"
|
||||||
|
|
||||||
|
#define CHECK_ALLOC_FAILURE(arena, ptr) \
|
||||||
|
do { \
|
||||||
|
if ((ptr) == NULL) { \
|
||||||
|
gTotalAllocFailures++; \
|
||||||
|
(arena)->allocFailures++; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
OSMesg sArenaLockMsg;
|
OSMesg sArenaLockMsg;
|
||||||
|
|
||||||
|
void __osMallocAddBlock(Arena* arena, void* start, ptrdiff_t size);
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
u32 __osMalloc_FreeBlockTest_Enable;
|
u32 __osMalloc_FreeBlockTest_Enable;
|
||||||
|
|
||||||
u32 ArenaImpl_GetFillAllocBlock(Arena* arena) {
|
u32 ArenaImpl_GetFillAllocBlock(Arena* arena) {
|
||||||
return (arena->flag & FILL_ALLOCBLOCK) != 0;
|
return (arena->flag & FILL_ALLOC_BLOCK_FLAG) != 0;
|
||||||
}
|
}
|
||||||
u32 ArenaImpl_GetFillFreeBlock(Arena* arena) {
|
u32 ArenaImpl_GetFillFreeBlock(Arena* arena) {
|
||||||
return (arena->flag & FILL_FREEBLOCK) != 0;
|
return (arena->flag & FILL_FREE_BLOCK_FLAG) != 0;
|
||||||
}
|
}
|
||||||
u32 ArenaImpl_GetCheckFreeBlock(Arena* arena) {
|
u32 ArenaImpl_GetCheckFreeBlock(Arena* arena) {
|
||||||
return (arena->flag & CHECK_FREE_BLOCK) != 0;
|
return (arena->flag & CHECK_FREE_BLOCK_FLAG) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArenaImpl_SetFillAllocBlock(Arena* arena) {
|
void ArenaImpl_SetFillAllocBlock(Arena* arena) {
|
||||||
arena->flag |= FILL_ALLOCBLOCK;
|
arena->flag |= FILL_ALLOC_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
void ArenaImpl_SetFillFreeBlock(Arena* arena) {
|
void ArenaImpl_SetFillFreeBlock(Arena* arena) {
|
||||||
arena->flag |= FILL_FREEBLOCK;
|
arena->flag |= FILL_FREE_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
void ArenaImpl_SetCheckFreeBlock(Arena* arena) {
|
void ArenaImpl_SetCheckFreeBlock(Arena* arena) {
|
||||||
arena->flag |= CHECK_FREE_BLOCK;
|
arena->flag |= CHECK_FREE_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArenaImpl_UnsetFillAllocBlock(Arena* arena) {
|
void ArenaImpl_UnsetFillAllocBlock(Arena* arena) {
|
||||||
arena->flag &= ~FILL_ALLOCBLOCK;
|
arena->flag &= ~FILL_ALLOC_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
void ArenaImpl_UnsetFillFreeBlock(Arena* arena) {
|
void ArenaImpl_UnsetFillFreeBlock(Arena* arena) {
|
||||||
arena->flag &= ~FILL_FREEBLOCK;
|
arena->flag &= ~FILL_FREE_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) {
|
void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) {
|
||||||
arena->flag &= ~CHECK_FREE_BLOCK;
|
arena->flag &= ~CHECK_FREE_BLOCK_FLAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, int line, Arena* arena) {
|
||||||
void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, s32 line, Arena* arena) {
|
// Upstream TODO: Figure out why uncommenting this crashes
|
||||||
|
/*
|
||||||
node->filename = file;
|
node->filename = file;
|
||||||
node->line = line;
|
node->line = line;
|
||||||
node->threadId = osGetThreadId(NULL);
|
node->threadId = osGetThreadId(NULL);
|
||||||
node->arena = arena;
|
node->arena = arena;
|
||||||
node->time = osGetTime();
|
node->time = osGetTime();
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void ArenaImpl_LockInit(Arena* arena) {
|
void ArenaImpl_LockInit(Arena* arena) {
|
||||||
osCreateMesgQueue(&arena->lock, &sArenaLockMsg, 1);
|
osCreateMesgQueue(&arena->lock, &sArenaLockMsg, 1);
|
||||||
}
|
}
|
||||||
@ -70,6 +141,7 @@ void ArenaImpl_Unlock(Arena* arena) {
|
|||||||
osRecvMesg(&arena->lock, NULL, OS_MESG_BLOCK);
|
osRecvMesg(&arena->lock, NULL, OS_MESG_BLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) {
|
ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) {
|
||||||
ArenaNode* next = node->next;
|
ArenaNode* next = node->next;
|
||||||
|
|
||||||
@ -91,16 +163,17 @@ ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) {
|
|||||||
}
|
}
|
||||||
return prev;
|
return prev;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
|
ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
|
||||||
ArenaNode* last = NULL;
|
ArenaNode* last = NULL;
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
|
|
||||||
if (arena != NULL && arena->head != NULL && arena->head->magic == NODE_MAGIC) {
|
if (arena != NULL && NODE_IS_VALID(arena->head)) {
|
||||||
iter = arena->head;
|
iter = arena->head;
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
last = iter;
|
last = iter;
|
||||||
iter = ArenaImpl_GetNextBlock(iter);
|
iter = NODE_GET_NEXT(last);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return last;
|
return last;
|
||||||
@ -125,7 +198,7 @@ void __osMallocAddBlock(Arena* arena, void* start, ptrdiff_t size) {
|
|||||||
size2 = (size - diff) & ~0xF;
|
size2 = (size - diff) & ~0xF;
|
||||||
|
|
||||||
if (size2 > (ptrdiff_t)sizeof(ArenaNode)) {
|
if (size2 > (ptrdiff_t)sizeof(ArenaNode)) {
|
||||||
memset(firstNode, BLOCK_UNINIT_MAGIC, size2); // memset
|
FILL_UNINIT_BLOCK(arena, firstNode, size2);
|
||||||
firstNode->next = NULL;
|
firstNode->next = NULL;
|
||||||
firstNode->prev = NULL;
|
firstNode->prev = NULL;
|
||||||
firstNode->size = size2 - sizeof(ArenaNode);
|
firstNode->size = size2 - sizeof(ArenaNode);
|
||||||
@ -145,6 +218,7 @@ void __osMallocAddBlock(Arena* arena, void* start, ptrdiff_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
void ArenaImpl_RemoveAllBlocks(Arena* arena) {
|
void ArenaImpl_RemoveAllBlocks(Arena* arena) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
@ -153,23 +227,27 @@ void ArenaImpl_RemoveAllBlocks(Arena* arena) {
|
|||||||
|
|
||||||
iter = arena->head;
|
iter = arena->head;
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
next = ArenaImpl_GetNextBlock(iter);
|
next = NODE_GET_NEXT(iter);
|
||||||
memset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode)); // memset
|
memset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode));
|
||||||
iter = next;
|
iter = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void __osMallocCleanup(Arena* arena) {
|
void __osMallocCleanup(Arena* arena) {
|
||||||
|
#if OOT_DEBUG
|
||||||
ArenaImpl_RemoveAllBlocks(arena);
|
ArenaImpl_RemoveAllBlocks(arena);
|
||||||
|
#endif
|
||||||
memset(arena, 0, sizeof(*arena));
|
memset(arena, 0, sizeof(*arena));
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 __osMallocIsInitalized(Arena* arena) {
|
s32 __osMallocIsInitialized(Arena* arena) {
|
||||||
return arena->isInit;
|
return arena->isInit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) {
|
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) {
|
||||||
ArenaNode* node2 = node;
|
ArenaNode* node2 = node;
|
||||||
u32* start;
|
u32* start;
|
||||||
@ -194,26 +272,24 @@ void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* __osMalloc_NoLockDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
void* __osMalloc_NoLockDebug(Arena* arena, size_t size, const char* file, int line) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
u32 blockSize;
|
u32 blockSize;
|
||||||
ArenaNode* newNode;
|
ArenaNode* newNode;
|
||||||
void* alloc = NULL;
|
void* alloc = NULL;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
|
|
||||||
iter = arena->head;
|
|
||||||
size = ALIGN16(size);
|
size = ALIGN16(size);
|
||||||
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
||||||
|
iter = arena->head;
|
||||||
|
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
if (iter->isFree && iter->size >= size) {
|
if (iter->isFree && iter->size >= size) {
|
||||||
if (arena->flag & CHECK_FREE_BLOCK) {
|
CHECK_FREE_BLOCK(arena, iter);
|
||||||
__osMalloc_FreeBlockTest(arena, iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (blockSize < iter->size) {
|
if (blockSize < iter->size) {
|
||||||
newNode = (ArenaNode*)((uintptr_t)iter + blockSize);
|
newNode = (ArenaNode*)((uintptr_t)iter + blockSize);
|
||||||
newNode->next = ArenaImpl_GetNextBlock(iter);
|
newNode->next = NODE_GET_NEXT(iter);
|
||||||
newNode->prev = iter;
|
newNode->prev = iter;
|
||||||
newNode->size = iter->size - blockSize;
|
newNode->size = iter->size - blockSize;
|
||||||
newNode->isFree = true;
|
newNode->isFree = true;
|
||||||
@ -221,29 +297,27 @@ void* __osMalloc_NoLockDebug(Arena* arena, size_t size, const char* file, s32 li
|
|||||||
|
|
||||||
iter->next = newNode;
|
iter->next = newNode;
|
||||||
iter->size = size;
|
iter->size = size;
|
||||||
next = ArenaImpl_GetNextBlock(newNode);
|
next = NODE_GET_NEXT(newNode);
|
||||||
if (next) {
|
if (next) {
|
||||||
next->prev = newNode;
|
next->prev = newNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iter->isFree = false;
|
iter->isFree = false;
|
||||||
//ArenaImpl_SetDebugInfo(iter, file, line, arena);
|
SET_DEBUG_INFO(iter, file, line, arena);
|
||||||
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
||||||
if (arena->flag & FILL_ALLOCBLOCK) {
|
FILL_ALLOC_BLOCK(arena, alloc, size);
|
||||||
memset(alloc, BLOCK_ALLOC_MAGIC, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = ArenaImpl_GetNextBlock(iter);
|
iter = NODE_GET_NEXT(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
return alloc;
|
return alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* __osMallocDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
void* __osMallocDebug(Arena* arena, size_t size, const char* file, int line) {
|
||||||
void* alloc;
|
void* alloc;
|
||||||
|
|
||||||
ArenaImpl_Lock(arena);
|
ArenaImpl_Lock(arena);
|
||||||
@ -253,7 +327,7 @@ void* __osMallocDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
|||||||
return alloc;
|
return alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* __osMallocRDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
void* __osMallocRDebug(Arena* arena, size_t size, const char* file, int line) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
ArenaNode* newNode;
|
ArenaNode* newNode;
|
||||||
u32 blockSize;
|
u32 blockSize;
|
||||||
@ -266,21 +340,19 @@ void* __osMallocRDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
|||||||
|
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
if (iter->isFree && iter->size >= size) {
|
if (iter->isFree && iter->size >= size) {
|
||||||
if (arena->flag & CHECK_FREE_BLOCK) {
|
CHECK_FREE_BLOCK(arena, iter);
|
||||||
__osMalloc_FreeBlockTest(arena, iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
||||||
if (blockSize < iter->size) {
|
if (blockSize < iter->size) {
|
||||||
newNode = (ArenaNode*)((uintptr_t)iter + (iter->size - size));
|
newNode = (ArenaNode*)((uintptr_t)iter + (iter->size - size));
|
||||||
newNode->next = ArenaImpl_GetNextBlock(iter);
|
newNode->next = NODE_GET_NEXT(iter);
|
||||||
newNode->prev = iter;
|
newNode->prev = iter;
|
||||||
newNode->size = size;
|
newNode->size = size;
|
||||||
newNode->magic = NODE_MAGIC;
|
newNode->magic = NODE_MAGIC;
|
||||||
|
|
||||||
iter->next = newNode;
|
iter->next = newNode;
|
||||||
iter->size -= blockSize;
|
iter->size -= blockSize;
|
||||||
next = ArenaImpl_GetNextBlock(newNode);
|
next = NODE_GET_NEXT(newNode);
|
||||||
if (next) {
|
if (next) {
|
||||||
next->prev = newNode;
|
next->prev = newNode;
|
||||||
}
|
}
|
||||||
@ -288,21 +360,21 @@ void* __osMallocRDebug(Arena* arena, size_t size, const char* file, s32 line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
iter->isFree = false;
|
iter->isFree = false;
|
||||||
//ArenaImpl_SetDebugInfo(iter, file, line, arena);
|
SET_DEBUG_INFO(iter, file, line, arena);
|
||||||
allocR = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
allocR = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
||||||
if (arena->flag & FILL_ALLOCBLOCK) {
|
FILL_ALLOC_BLOCK(arena, allocR, size);
|
||||||
memset(allocR, BLOCK_ALLOC_MAGIC, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = ArenaImpl_GetPrevBlock(iter);
|
iter = NODE_GET_PREV(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
|
|
||||||
return allocR;
|
return allocR;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void* __osMalloc_NoLock(Arena* arena, size_t size) {
|
void* __osMalloc_NoLock(Arena* arena, size_t size) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
@ -311,20 +383,17 @@ void* __osMalloc_NoLock(Arena* arena, size_t size) {
|
|||||||
void* alloc = NULL;
|
void* alloc = NULL;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
|
|
||||||
iter = arena->head;
|
|
||||||
size = ALIGN16(size);
|
size = ALIGN16(size);
|
||||||
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
||||||
|
iter = arena->head;
|
||||||
|
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
|
|
||||||
if (iter->isFree && iter->size >= size) {
|
if (iter->isFree && iter->size >= size) {
|
||||||
if (arena->flag & CHECK_FREE_BLOCK) {
|
CHECK_FREE_BLOCK(arena, iter);
|
||||||
__osMalloc_FreeBlockTest(arena, iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (blockSize < iter->size) {
|
if (blockSize < iter->size) {
|
||||||
newNode = (ArenaNode*)((uintptr_t)iter + blockSize);
|
newNode = (ArenaNode*)((uintptr_t)iter + blockSize);
|
||||||
newNode->next = ArenaImpl_GetNextBlock(iter);
|
newNode->next = NODE_GET_NEXT(iter);
|
||||||
newNode->prev = iter;
|
newNode->prev = iter;
|
||||||
newNode->size = iter->size - blockSize;
|
newNode->size = iter->size - blockSize;
|
||||||
newNode->isFree = true;
|
newNode->isFree = true;
|
||||||
@ -332,24 +401,25 @@ void* __osMalloc_NoLock(Arena* arena, size_t size) {
|
|||||||
|
|
||||||
iter->next = newNode;
|
iter->next = newNode;
|
||||||
iter->size = size;
|
iter->size = size;
|
||||||
next = ArenaImpl_GetNextBlock(newNode);
|
next = NODE_GET_NEXT(newNode);
|
||||||
if (next) {
|
if (next) {
|
||||||
next->prev = newNode;
|
next->prev = newNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iter->isFree = false;
|
iter->isFree = false;
|
||||||
//ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
|
SET_DEBUG_INFO(iter, NULL, 0, arena);
|
||||||
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
||||||
if (arena->flag & FILL_ALLOCBLOCK) {
|
FILL_ALLOC_BLOCK(arena, alloc, size);
|
||||||
memset(alloc, BLOCK_ALLOC_MAGIC, size);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = ArenaImpl_GetNextBlock(iter);
|
iter = NODE_GET_NEXT(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHECK_ALLOC_FAILURE(arena, alloc);
|
||||||
|
|
||||||
return alloc;
|
return alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,32 +435,33 @@ void* __osMalloc(Arena* arena, size_t size) {
|
|||||||
|
|
||||||
void* __osMallocR(Arena* arena, size_t size) {
|
void* __osMallocR(Arena* arena, size_t size) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
|
ArenaNode* allocNode;
|
||||||
ArenaNode* newNode;
|
ArenaNode* newNode;
|
||||||
u32 blockSize;
|
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
void* alloc = NULL;
|
void* alloc = NULL;
|
||||||
|
u32 blockSize;
|
||||||
|
|
||||||
size = ALIGN16(size);
|
size = ALIGN16(size);
|
||||||
|
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
||||||
ArenaImpl_Lock(arena);
|
ArenaImpl_Lock(arena);
|
||||||
iter = ArenaImpl_GetLastBlock(arena);
|
iter = ArenaImpl_GetLastBlock(arena);
|
||||||
|
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
if (iter->isFree && iter->size >= size) {
|
if (iter->isFree && iter->size >= size) {
|
||||||
if (arena->flag & CHECK_FREE_BLOCK) {
|
CHECK_FREE_BLOCK(arena, iter);
|
||||||
__osMalloc_FreeBlockTest(arena, iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
blockSize = ALIGN16(size) + sizeof(ArenaNode);
|
|
||||||
if (blockSize < iter->size) {
|
if (blockSize < iter->size) {
|
||||||
newNode = (ArenaNode*)((uintptr_t)iter + (iter->size - size));
|
allocNode = (ArenaNode*)((uintptr_t)iter + (iter->size - size));
|
||||||
newNode->next = ArenaImpl_GetNextBlock(iter);
|
allocNode->next = NODE_GET_NEXT(iter);
|
||||||
|
|
||||||
|
newNode = allocNode;
|
||||||
newNode->prev = iter;
|
newNode->prev = iter;
|
||||||
newNode->size = size;
|
newNode->size = size;
|
||||||
newNode->magic = NODE_MAGIC;
|
newNode->magic = NODE_MAGIC;
|
||||||
|
|
||||||
iter->next = newNode;
|
iter->next = newNode;
|
||||||
iter->size -= blockSize;
|
iter->size -= blockSize;
|
||||||
next = ArenaImpl_GetNextBlock(newNode);
|
next = NODE_GET_NEXT(newNode);
|
||||||
if (next) {
|
if (next) {
|
||||||
next->prev = newNode;
|
next->prev = newNode;
|
||||||
}
|
}
|
||||||
@ -398,15 +469,16 @@ void* __osMallocR(Arena* arena, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
iter->isFree = false;
|
iter->isFree = false;
|
||||||
//ArenaImpl_SetDebugInfo(iter, NULL, 0, arena);
|
SET_DEBUG_INFO(iter, NULL, 0, arena);
|
||||||
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
alloc = (void*)((uintptr_t)iter + sizeof(ArenaNode));
|
||||||
if (arena->flag & FILL_ALLOCBLOCK) {
|
FILL_ALLOC_BLOCK(arena, alloc, size);
|
||||||
memset(alloc, BLOCK_ALLOC_MAGIC, size);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
iter = ArenaImpl_GetPrevBlock(iter);
|
iter = NODE_GET_PREV(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHECK_ALLOC_FAILURE(arena, alloc);
|
||||||
|
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
|
|
||||||
return alloc;
|
return alloc;
|
||||||
@ -416,14 +488,13 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
|
|||||||
ArenaNode* node;
|
ArenaNode* node;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
ArenaNode* prev;
|
ArenaNode* prev;
|
||||||
ArenaNode* newNext;
|
|
||||||
|
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
node = (ArenaNode*)((uintptr_t)ptr - sizeof(ArenaNode));
|
node = (ArenaNode*)((uintptr_t)ptr - sizeof(ArenaNode));
|
||||||
if (node == NULL || node->magic != NODE_MAGIC) {
|
if (!NODE_IS_VALID(node)) {
|
||||||
// "__osFree: Unauthorized release (%08x)"
|
// "__osFree: Unauthorized release (%08x)"
|
||||||
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x)\n" VT_RST, ptr);
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x)\n" VT_RST, ptr);
|
||||||
return;
|
return;
|
||||||
@ -432,34 +503,32 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
|
|||||||
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x)\n" VT_RST, ptr); // "__osFree: Double release (%08x)"
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x)\n" VT_RST, ptr); // "__osFree: Double release (%08x)"
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if 0
|
#if OOT_DEBUG
|
||||||
|
/*
|
||||||
if (arena != node->arena && arena != NULL) {
|
if (arena != node->arena && arena != NULL) {
|
||||||
// "__osFree:Tried to release in a different way than when it was secured (%08x:%08x)"
|
// "__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,
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n" VT_RST, arena,
|
||||||
node->arena);
|
node->arena);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
#endif
|
#endif
|
||||||
next = ArenaImpl_GetNextBlock(node);
|
|
||||||
prev = ArenaImpl_GetPrevBlock(node);
|
next = NODE_GET_NEXT(node);
|
||||||
|
prev = NODE_GET_PREV(node);
|
||||||
node->isFree = true;
|
node->isFree = true;
|
||||||
//ArenaImpl_SetDebugInfo(node, NULL, 0, arena);
|
SET_DEBUG_INFO(node, NULL, 0, arena);
|
||||||
|
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
FILL_FREE_BLOCK_CONTENTS(arena, node);
|
||||||
memset((uintptr_t)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
|
|
||||||
}
|
|
||||||
|
|
||||||
newNext = next;
|
|
||||||
if ((uintptr_t)next == (uintptr_t)node + sizeof(ArenaNode) + node->size && next->isFree) {
|
if ((uintptr_t)next == (uintptr_t)node + sizeof(ArenaNode) + node->size && next->isFree) {
|
||||||
newNext = ArenaImpl_GetNextBlock(next);
|
ArenaNode* newNext = NODE_GET_NEXT(next);
|
||||||
if (newNext != NULL) {
|
if (newNext != NULL) {
|
||||||
newNext->prev = node;
|
newNext->prev = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
node->size += next->size + sizeof(ArenaNode);
|
node->size += next->size + sizeof(ArenaNode);
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
FILL_FREE_BLOCK_HEADER(arena, next);
|
||||||
memset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
|
|
||||||
}
|
|
||||||
node->next = newNext;
|
node->next = newNext;
|
||||||
next = newNext;
|
next = newNext;
|
||||||
}
|
}
|
||||||
@ -470,9 +539,7 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
|
|||||||
}
|
}
|
||||||
prev->next = next;
|
prev->next = next;
|
||||||
prev->size += node->size + sizeof(ArenaNode);
|
prev->size += node->size + sizeof(ArenaNode);
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
FILL_FREE_BLOCK_HEADER(arena, node);
|
||||||
memset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -482,7 +549,8 @@ void __osFree(Arena* arena, void* ptr) {
|
|||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
|
#if OOT_DEBUG
|
||||||
|
void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, int line) {
|
||||||
ArenaNode* node;
|
ArenaNode* node;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
ArenaNode* prev;
|
ArenaNode* prev;
|
||||||
@ -493,7 +561,7 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
node = (ArenaNode*)((uintptr_t)ptr - sizeof(ArenaNode));
|
node = (ArenaNode*)((uintptr_t)ptr - sizeof(ArenaNode));
|
||||||
if (node == NULL || node->magic != NODE_MAGIC) {
|
if (!NODE_IS_VALID(node)) {
|
||||||
// "__osFree: Unauthorized release (%08x)"
|
// "__osFree: Unauthorized release (%08x)"
|
||||||
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:不正解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
||||||
return;
|
return;
|
||||||
@ -503,34 +571,32 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
|
|||||||
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:二重解放(%08x) [%s:%d ]\n" VT_RST, ptr, file, line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if 0
|
|
||||||
|
/*
|
||||||
if (arena != node->arena && arena != NULL) {
|
if (arena != node->arena && arena != NULL) {
|
||||||
// "__osFree:Tried to release in a different way than when it was secured (%08x:%08x)"
|
// "__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,
|
osSyncPrintf(VT_COL(RED, WHITE) "__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n" VT_RST, arena,
|
||||||
node->arena);
|
node->arena);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
*/
|
||||||
next = ArenaImpl_GetNextBlock(node);
|
|
||||||
prev = ArenaImpl_GetPrevBlock(node);
|
|
||||||
node->isFree = true;
|
|
||||||
//ArenaImpl_SetDebugInfo(node, file, line, arena);
|
|
||||||
|
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
next = NODE_GET_NEXT(node);
|
||||||
memset((uintptr_t)node + sizeof(ArenaNode), BLOCK_FREE_MAGIC, node->size);
|
prev = NODE_GET_PREV(node);
|
||||||
}
|
node->isFree = true;
|
||||||
|
SET_DEBUG_INFO(node, file, line, arena);
|
||||||
|
|
||||||
|
FILL_FREE_BLOCK_CONTENTS(arena, node);
|
||||||
|
|
||||||
newNext = node->next;
|
newNext = node->next;
|
||||||
if ((uintptr_t)next == (uintptr_t)node + sizeof(ArenaNode) + node->size && next->isFree) {
|
if ((uintptr_t)next == (uintptr_t)node + sizeof(ArenaNode) + node->size && next->isFree) {
|
||||||
newNext = ArenaImpl_GetNextBlock(next);
|
newNext = NODE_GET_NEXT(next);
|
||||||
if (newNext != NULL) {
|
if (newNext != NULL) {
|
||||||
newNext->prev = node;
|
newNext->prev = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
node->size += next->size + sizeof(ArenaNode);
|
node->size += next->size + sizeof(ArenaNode);
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
FILL_FREE_BLOCK_HEADER(arena, next);
|
||||||
memset(next, BLOCK_FREE_MAGIC, sizeof(ArenaNode));
|
|
||||||
}
|
|
||||||
node->next = newNext;
|
node->next = newNext;
|
||||||
next = newNext;
|
next = newNext;
|
||||||
}
|
}
|
||||||
@ -541,21 +607,20 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, s32 line) {
|
|||||||
}
|
}
|
||||||
prev->next = next;
|
prev->next = next;
|
||||||
prev->size += node->size + sizeof(ArenaNode);
|
prev->size += node->size + sizeof(ArenaNode);
|
||||||
if (arena->flag & FILL_FREEBLOCK) {
|
FILL_FREE_BLOCK_HEADER(arena, node);
|
||||||
memset(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, int line) {
|
||||||
ArenaImpl_Lock(arena);
|
ArenaImpl_Lock(arena);
|
||||||
__osFree_NoLockDebug(arena, ptr, file, line);
|
__osFree_NoLockDebug(arena, ptr, file, line);
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
||||||
void* newAlloc;
|
|
||||||
ArenaNode* node;
|
ArenaNode* node;
|
||||||
|
void* newAlloc;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
ArenaNode* newNext;
|
ArenaNode* newNext;
|
||||||
ArenaNode* overNext;
|
ArenaNode* overNext;
|
||||||
@ -582,20 +647,20 @@ void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
|||||||
// "Does nothing because the memory block size does not change"
|
// "Does nothing because the memory block size does not change"
|
||||||
osSyncPrintf("メモリブロックサイズが変わらないためなにもしません\n");
|
osSyncPrintf("メモリブロックサイズが変わらないためなにもしません\n");
|
||||||
} else if (node->size < newSize) {
|
} else if (node->size < newSize) {
|
||||||
next = ArenaImpl_GetNextBlock(node);
|
next = NODE_GET_NEXT(node);
|
||||||
sizeDiff = newSize - node->size;
|
sizeDiff = newSize - node->size;
|
||||||
if ((uintptr_t)next == ((uintptr_t)node + node->size + sizeof(ArenaNode)) && next->isFree && next->size >= sizeDiff) {
|
if ((uintptr_t)next == ((uintptr_t)node + node->size + sizeof(ArenaNode)) && next->isFree && next->size >= sizeDiff) {
|
||||||
// "Merge because there is a free block after the current memory block"
|
// "Merge because there is a free block after the current memory block"
|
||||||
osSyncPrintf("現メモリブロックの後ろにフリーブロックがあるので結合します\n");
|
osSyncPrintf("現メモリブロックの後ろにフリーブロックがあるので結合します\n");
|
||||||
next->size -= sizeDiff;
|
next->size -= sizeDiff;
|
||||||
overNext = ArenaImpl_GetNextBlock(next);
|
overNext = NODE_GET_NEXT(next);
|
||||||
newNext = (ArenaNode*)((uintptr_t)next + sizeDiff);
|
newNext = (ArenaNode*)((uintptr_t)next + sizeDiff);
|
||||||
if (overNext != NULL) {
|
if (overNext != NULL) {
|
||||||
overNext->prev = newNext;
|
overNext->prev = newNext;
|
||||||
}
|
}
|
||||||
node->next = newNext;
|
node->next = newNext;
|
||||||
node->size = newSize;
|
node->size = newSize;
|
||||||
func_801068B0(newNext, next, sizeof(ArenaNode)); // memcpy
|
memmove(node->next, next, sizeof(ArenaNode));
|
||||||
} else {
|
} else {
|
||||||
// "Allocate a new memory block and move the contents"
|
// "Allocate a new memory block and move the contents"
|
||||||
osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n");
|
osSyncPrintf("新たにメモリブロックを確保して内容を移動します\n");
|
||||||
@ -607,7 +672,7 @@ void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
|||||||
ptr = newAlloc;
|
ptr = newAlloc;
|
||||||
}
|
}
|
||||||
} else if (newSize < node->size) {
|
} else if (newSize < node->size) {
|
||||||
next2 = ArenaImpl_GetNextBlock(node);
|
next2 = NODE_GET_NEXT(node);
|
||||||
if (next2 != NULL && next2->isFree) {
|
if (next2 != NULL && next2->isFree) {
|
||||||
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
|
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
|
||||||
// "Increased free block behind current memory block"
|
// "Increased free block behind current memory block"
|
||||||
@ -618,7 +683,7 @@ void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
|||||||
newNext2->size += node->size - newSize;
|
newNext2->size += node->size - newSize;
|
||||||
node->next = newNext2;
|
node->next = newNext2;
|
||||||
node->size = newSize;
|
node->size = newSize;
|
||||||
overNext2 = ArenaImpl_GetNextBlock(newNext2);
|
overNext2 = NODE_GET_NEXT(newNext2);
|
||||||
if (overNext2 != NULL) {
|
if (overNext2 != NULL) {
|
||||||
overNext2->prev = newNext2;
|
overNext2->prev = newNext2;
|
||||||
}
|
}
|
||||||
@ -627,14 +692,14 @@ void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
|||||||
// "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");
|
osSyncPrintf("現メモリブロックの後ろにフリーブロックがないので生成します\n");
|
||||||
newNext2 = (ArenaNode*)((uintptr_t)node + blockSize);
|
newNext2 = (ArenaNode*)((uintptr_t)node + blockSize);
|
||||||
newNext2->next = ArenaImpl_GetNextBlock(node);
|
newNext2->next = NODE_GET_NEXT(node);
|
||||||
newNext2->prev = node;
|
newNext2->prev = node;
|
||||||
newNext2->size = node->size - blockSize;
|
newNext2->size = node->size - blockSize;
|
||||||
newNext2->isFree = true;
|
newNext2->isFree = true;
|
||||||
newNext2->magic = NODE_MAGIC;
|
newNext2->magic = NODE_MAGIC;
|
||||||
node->next = newNext2;
|
node->next = newNext2;
|
||||||
node->size = newSize;
|
node->size = newSize;
|
||||||
overNext2 = ArenaImpl_GetNextBlock(newNext2);
|
overNext2 = NODE_GET_NEXT(newNext2);
|
||||||
if (overNext2 != NULL) {
|
if (overNext2 != NULL) {
|
||||||
overNext2->prev = newNext2;
|
overNext2->prev = newNext2;
|
||||||
}
|
}
|
||||||
@ -644,15 +709,19 @@ void* __osRealloc(Arena* arena, void* ptr, size_t newSize) {
|
|||||||
ptr = NULL;
|
ptr = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHECK_ALLOC_FAILURE(arena, ptr);
|
||||||
}
|
}
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* __osReallocDebug(Arena* arena, void* ptr, size_t newSize, const char* file, s32 line) {
|
#if OOT_DEBUG
|
||||||
|
void* __osReallocDebug(Arena* arena, void* ptr, size_t newSize, const char* file, int line) {
|
||||||
return __osRealloc(arena, ptr, newSize);
|
return __osRealloc(arena, ptr, newSize);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
|
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
@ -674,12 +743,13 @@ void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAll
|
|||||||
*outAlloc += iter->size;
|
*outAlloc += iter->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = ArenaImpl_GetNextBlock(iter);
|
iter = NODE_GET_NEXT(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if OOT_DEBUG
|
||||||
void __osDisplayArena(Arena* arena) {
|
void __osDisplayArena(Arena* arena) {
|
||||||
size_t freeSize;
|
size_t freeSize;
|
||||||
size_t allocatedSize;
|
size_t allocatedSize;
|
||||||
@ -687,7 +757,7 @@ void __osDisplayArena(Arena* arena) {
|
|||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
|
|
||||||
if (!__osMallocIsInitalized(arena)) {
|
if (!__osMallocIsInitialized(arena)) {
|
||||||
osSyncPrintf("アリーナは初期化されていません\n"); // "Arena is not initalized"
|
osSyncPrintf("アリーナは初期化されていません\n"); // "Arena is not initalized"
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -710,12 +780,14 @@ void __osDisplayArena(Arena* arena) {
|
|||||||
(next == NULL) ? '$' : (iter != next->prev ? '!' : ' '),
|
(next == NULL) ? '$' : (iter != next->prev ? '!' : ' '),
|
||||||
iter->isFree ? "空き" : "確保", //? "Free" : "Secure"
|
iter->isFree ? "空き" : "確保", //? "Free" : "Secure"
|
||||||
iter->size);
|
iter->size);
|
||||||
#if 0
|
|
||||||
|
/*
|
||||||
if (!iter->isFree) {
|
if (!iter->isFree) {
|
||||||
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
|
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
|
||||||
iter->filename != NULL ? iter->filename : "**NULL**", iter->line);
|
iter->filename != NULL ? iter->filename : "**NULL**", iter->line);
|
||||||
}
|
}
|
||||||
#endif
|
*/
|
||||||
|
|
||||||
osSyncPrintf("\n");
|
osSyncPrintf("\n");
|
||||||
|
|
||||||
if (iter->isFree) {
|
if (iter->isFree) {
|
||||||
@ -742,6 +814,7 @@ void __osDisplayArena(Arena* arena) {
|
|||||||
|
|
||||||
ArenaImpl_Unlock(arena);
|
ArenaImpl_Unlock(arena);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void ArenaImpl_FaultClient(Arena* arena) {
|
void ArenaImpl_FaultClient(Arena* arena) {
|
||||||
size_t freeSize;
|
size_t freeSize;
|
||||||
@ -751,7 +824,7 @@ void ArenaImpl_FaultClient(Arena* arena) {
|
|||||||
ArenaNode* next;
|
ArenaNode* next;
|
||||||
|
|
||||||
FaultDrawer_Printf("ARENA INFO (0x%08x)\n", arena);
|
FaultDrawer_Printf("ARENA INFO (0x%08x)\n", arena);
|
||||||
if (!__osMallocIsInitalized(arena)) {
|
if (!__osMallocIsInitialized(arena)) {
|
||||||
FaultDrawer_Printf("Arena is uninitalized\n", arena);
|
FaultDrawer_Printf("Arena is uninitalized\n", arena);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -793,7 +866,7 @@ void ArenaImpl_FaultClient(Arena* arena) {
|
|||||||
FaultDrawer_Printf("Largest Free Block Size %08x\n", maxFree);
|
FaultDrawer_Printf("Largest Free Block Size %08x\n", maxFree);
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 __osCheckArena(Arena* arena) {
|
s32 __osCheckArena(Arena* arena) {
|
||||||
ArenaNode* iter;
|
ArenaNode* iter;
|
||||||
u32 error = 0;
|
u32 error = 0;
|
||||||
|
|
||||||
@ -802,13 +875,20 @@ u32 __osCheckArena(Arena* arena) {
|
|||||||
osSyncPrintf("アリーナの内容をチェックしています... (%08x)\n", arena);
|
osSyncPrintf("アリーナの内容をチェックしています... (%08x)\n", arena);
|
||||||
iter = arena->head;
|
iter = arena->head;
|
||||||
while (iter != NULL) {
|
while (iter != NULL) {
|
||||||
if (iter && iter->magic == NODE_MAGIC) {
|
//! @bug: Probably intended to be `!NODE_IS_VALID(iter)`
|
||||||
|
if (NODE_IS_VALID(iter)) {
|
||||||
|
#if OOT_DEBUG
|
||||||
// "Oops!! (%08x %08x)"
|
// "Oops!! (%08x %08x)"
|
||||||
osSyncPrintf(VT_COL(RED, WHITE) "おおっと!! (%08x %08x)\n" VT_RST, iter, iter->magic);
|
osSyncPrintf(VT_COL(RED, WHITE) "おおっと!! (%08x %08x)\n" VT_RST, iter,
|
||||||
|
iter->magic);
|
||||||
|
#else
|
||||||
|
// "Oops!! (%08x %08x)"
|
||||||
|
osSyncPrintf("おおっと!! (%08x %08x)\n", iter, iter->magic);
|
||||||
|
#endif
|
||||||
error = 1;
|
error = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
iter = ArenaImpl_GetNextBlock(iter);
|
iter = NODE_GET_NEXT(iter);
|
||||||
}
|
}
|
||||||
if (error == 0) {
|
if (error == 0) {
|
||||||
osSyncPrintf("アリーナはまだ、いけそうです\n"); // "The arena is still going well"
|
osSyncPrintf("アリーナはまだ、いけそうです\n"); // "The arena is still going well"
|
||||||
@ -818,6 +898,8 @@ u32 __osCheckArena(Arena* arena) {
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 func_800FF334(Arena* arena) {
|
#if OOT_DEBUG
|
||||||
|
u8 ArenaImpl_GetAllocFailures(Arena* arena) {
|
||||||
return arena->unk_20;
|
return arena->unk_20;
|
||||||
}
|
}
|
||||||
|
#endif
|
@ -1,24 +1,33 @@
|
|||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
|
||||||
// memmove used in __osMalloc.c
|
/**
|
||||||
void* func_801068B0(void* dst, void* src, size_t size) {
|
* memmove: copies `len` bytes from memory starting at `src` to memory starting at `dest`.
|
||||||
u8* spC = dst;
|
*
|
||||||
u8* sp8 = src;
|
* Unlike memcpy(), the regions of memory may overlap.
|
||||||
register s32 a3;
|
*
|
||||||
|
* @param dest address of start of buffer to write to
|
||||||
|
* @param src address of start of buffer to read from
|
||||||
|
* @param len number of bytes to copy.
|
||||||
|
*
|
||||||
|
* @return dest
|
||||||
|
*/
|
||||||
|
void* oot_memmove(void* dest, const void* src, size_t len) {
|
||||||
|
char* d = dest;
|
||||||
|
const char* s = src;
|
||||||
|
|
||||||
if (spC == sp8) {
|
if (d == s) {
|
||||||
return dst;
|
return dest;
|
||||||
}
|
}
|
||||||
if (spC < sp8) {
|
if (d < s) {
|
||||||
for (a3 = size--; a3 != 0; a3 = size--) {
|
while (len--) {
|
||||||
*spC++ = *sp8++;
|
*d++ = *s++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
spC += size - 1;
|
d += len - 1;
|
||||||
sp8 += size - 1;
|
s += len - 1;
|
||||||
for (a3 = size--; a3 != 0; a3 = size--) {
|
while (len--) {
|
||||||
*spC-- = *sp8--;
|
*d-- = *s--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dst;
|
return dest;
|
||||||
}
|
}
|
||||||
|
@ -109,5 +109,5 @@ void DebugArena_Cleanup(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u8 DebugArena_IsInitalized(void) {
|
u8 DebugArena_IsInitalized(void) {
|
||||||
return __osMallocIsInitalized(&sDebugArena);
|
return __osMallocIsInitialized(&sDebugArena);
|
||||||
}
|
}
|
||||||
|
@ -107,5 +107,5 @@ void SystemArena_Cleanup(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u8 SystemArena_IsInitalized(void) {
|
u8 SystemArena_IsInitalized(void) {
|
||||||
return __osMallocIsInitalized(&gSystemArena);
|
return __osMallocIsInitialized(&gSystemArena);
|
||||||
}
|
}
|
||||||
|
@ -106,5 +106,5 @@ void ZeldaArena_Cleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u8 ZeldaArena_IsInitalized() {
|
u8 ZeldaArena_IsInitalized() {
|
||||||
return __osMallocIsInitalized(&sZeldaArena);
|
return __osMallocIsInitialized(&sZeldaArena);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user