summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcadmic <cadmic24@gmail.com>2024-09-04 02:10:14 -0700
committerGitHub <noreply@github.com>2024-09-04 11:10:14 +0200
commitc6d7cc7697c67989b1f018ae2bc934f6677c09b7 (patch)
treecadb909a8c2adde978320b8505fc1bda48e3342c
parentaf24970d8919e4e6bb8356fd5672c938582dfcb8 (diff)
[ntsc-1.2] Match __osMalloc.c and code_800FC620.c (new/delete) (#2106)
* Match __osMalloc * Match src/code/code_800FC620.c (new/delete) * Wrap versions-specific files in ifdefs to fix compilation * Fix bss * Remove {FAULT,RAND,OSMALLOC}_VERSION in favor of PLATFORM_N64 * Fix __osMalloc data splits, add unused strings * __osMalloc.h -> osMalloc.h * Fix merge
-rw-r--r--Makefile3
-rw-r--r--include/fault.h15
-rw-r--r--include/functions.h24
-rw-r--r--include/padmgr.h2
-rw-r--r--include/rand.h3
-rw-r--r--include/variables.h4
-rw-r--r--include/versions.h16
-rw-r--r--include/z64.h27
-rw-r--r--osMalloc.h72
-rw-r--r--spec10
-rw-r--r--src/boot/sprintf.c5
-rw-r--r--src/code/__osMalloc_gc.c (renamed from src/code/__osMalloc.c)48
-rw-r--r--src/code/__osMalloc_n64.c474
-rw-r--r--src/code/code_800FC620.c65
-rw-r--r--src/code/debug_malloc.c3
-rw-r--r--src/code/fault_gc.c10
-rw-r--r--src/code/fault_gc_drawer.c11
-rw-r--r--src/code/fault_n64.c11
-rw-r--r--src/code/game.c1
-rw-r--r--src/code/rand.c13
-rw-r--r--src/code/system_malloc.c13
-rw-r--r--src/code/z_malloc.c3
-rw-r--r--tools/disasm/ntsc-1.2/files_code.csv6
-rw-r--r--tools/disasm/ntsc-1.2/functions.txt2
24 files changed, 682 insertions, 159 deletions
diff --git a/Makefile b/Makefile
index f32eb17a2..1a79bef8f 100644
--- a/Makefile
+++ b/Makefile
@@ -425,7 +425,8 @@ $(BUILD_DIR)/src/boot/sleep.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/boot/sprintf.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/boot/stackcheck.o: OPTFLAGS := -O2
-$(BUILD_DIR)/src/code/__osMalloc.o: OPTFLAGS := -O2
+$(BUILD_DIR)/src/code/__osMalloc_n64.o: OPTFLAGS := -O2
+$(BUILD_DIR)/src/code/__osMalloc_gc.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/code_800FC620.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/fp_math.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/rand.o: OPTFLAGS := -O2
diff --git a/include/fault.h b/include/fault.h
index 4ecb17aea..ac63a234d 100644
--- a/include/fault.h
+++ b/include/fault.h
@@ -4,9 +4,8 @@
#include "ultra64.h"
#include "attributes.h"
#include "padmgr.h"
-#include "versions.h"
-#if FAULT_VERSION == FAULT_GC
+#if PLATFORM_GC
// These are the same as the 3-bit ansi color codes
#define FAULT_COLOR_BLACK 0
#define FAULT_COLOR_RED 1
@@ -34,7 +33,7 @@ typedef struct FaultClient {
/* 0x0C */ void* arg1;
} FaultClient; // size = 0x10
-#if FAULT_VERSION == FAULT_GC
+#if PLATFORM_GC
typedef struct FaultAddrConvClient {
/* 0x00 */ struct FaultAddrConvClient* next;
/* 0x04 */ void* callback;
@@ -56,7 +55,7 @@ NORETURN void Fault_AddHungupAndCrash(const char* file, int line);
void Fault_AddClient(FaultClient* client, void* callback, void* arg0, void* arg1);
void Fault_RemoveClient(FaultClient* client);
-#if FAULT_VERSION == FAULT_GC
+#if PLATFORM_GC
void Fault_AddAddrConvClient(FaultAddrConvClient* client, void* callback, void* arg);
void Fault_RemoveAddrConvClient(FaultAddrConvClient* client);
#endif
@@ -71,13 +70,13 @@ void Fault_SetCursor(s32 x, s32 y);
s32 Fault_Printf(const char* fmt, ...);
void Fault_DrawText(s32 x, s32 y, const char* fmt, ...);
-#if FAULT_VERSION == FAULT_N64
+#if PLATFORM_N64
// Not implemented. Silently noop-ing is fine, these are not essential for functionality.
#define Fault_SetFontColor(color) (void)0
#define Fault_SetCharPad(padW, padH) (void)0
-#elif FAULT_VERSION == FAULT_GC
+#else
void Fault_InitDrawer(void);
void Fault_SetForeColor(u16 color);
@@ -88,13 +87,13 @@ s32 Fault_VPrintf(const char* fmt, va_list args);
#endif
-#if FAULT_VERSION == FAULT_N64
+#if PLATFORM_N64
extern vs32 gFaultMsgId;
#define FAULT_MSG_ID gFaultMsgId
-#elif FAULT_VERSION == FAULT_GC
+#else
typedef struct FaultMgr {
/* 0x000 */ OSThread thread;
diff --git a/include/functions.h b/include/functions.h
index 7ca8a2ae2..6ad09a447 100644
--- a/include/functions.h
+++ b/include/functions.h
@@ -862,7 +862,7 @@ void ZeldaArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
void ZeldaArena_Check(void);
void ZeldaArena_Init(void* start, u32 size);
void ZeldaArena_Cleanup(void);
-u8 ZeldaArena_IsInitialized(void);
+s32 ZeldaArena_IsInitialized(void);
#if OOT_DEBUG
void ZeldaArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action);
void* ZeldaArena_MallocDebug(u32 size, const char* file, int line);
@@ -1323,7 +1323,7 @@ void DebugArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
void DebugArena_Check(void);
void DebugArena_Init(void* start, u32 size);
void DebugArena_Cleanup(void);
-u8 DebugArena_IsInitialized(void);
+s32 DebugArena_IsInitialized(void);
#if OOT_DEBUG
void DebugArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action);
void* DebugArena_MallocDebug(u32 size, const char* file, int line);
@@ -1593,7 +1593,7 @@ void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc);
void SystemArena_Check(void);
void SystemArena_Init(void* start, u32 size);
void SystemArena_Cleanup(void);
-u8 SystemArena_IsInitialized(void);
+s32 SystemArena_IsInitialized(void);
#if OOT_DEBUG
void* SystemArena_MallocDebug(u32 size, const char* file, int line);
void* SystemArena_MallocRDebug(u32 size, const char* file, int line);
@@ -1601,24 +1601,6 @@ void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, int lin
void SystemArena_FreeDebug(void* ptr, const char* file, int line);
void SystemArena_Display(void);
#endif
-
-void __osMallocInit(Arena* arena, void* start, u32 size);
-void __osMallocAddBlock(Arena* arena, void* start, s32 size);
-void __osMallocCleanup(Arena* arena);
-u8 __osMallocIsInitialized(Arena* arena);
-void* __osMalloc(Arena* arena, u32 size);
-void* __osMallocR(Arena* arena, u32 size);
-void __osFree(Arena* arena, void* ptr);
-void* __osRealloc(Arena* arena, void* ptr, u32 newSize);
-void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc);
-u32 __osCheckArena(Arena* arena);
-#if OOT_DEBUG
-void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line);
-void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line);
-void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line);
-void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line);
-void __osDisplayArena(Arena* arena);
-#endif
s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args);
s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...);
void Sleep_Cycles(OSTime cycles);
diff --git a/include/padmgr.h b/include/padmgr.h
index efd245c7d..7078c9007 100644
--- a/include/padmgr.h
+++ b/include/padmgr.h
@@ -53,7 +53,7 @@ void PadMgr_Init(PadMgr* padMgr, OSMesgQueue* serialEventQueue, IrqMgr* irqMgr,
// This function cannot be prototyped here in all configurations because it is called incorrectly in fault_gc.c
// (see bug in `Fault_PadCallback`)
-#if FAULT_VERSION == FAULT_N64 || defined(AVOID_UB)
+#if PLATFORM_N64 || defined(AVOID_UB)
void PadMgr_RequestPadData(PadMgr* padmgr, Input* inputs, s32 gameRequest);
#endif
diff --git a/include/rand.h b/include/rand.h
index a1a6a7c16..79ced24ea 100644
--- a/include/rand.h
+++ b/include/rand.h
@@ -2,7 +2,6 @@
#define RAND_H
#include "ultra64.h"
-#include "versions.h"
u32 Rand_Next(void);
void Rand_Seed(u32 seed);
@@ -11,7 +10,7 @@ void Rand_Seed_Variable(u32* rndNum, u32 seed);
u32 Rand_Next_Variable(u32* rndNum);
f32 Rand_ZeroOne_Variable(u32* rndNum);
-#if RAND_VERSION == RAND_GC
+#if PLATFORM_GC
f32 Rand_Centered(void);
f32 Rand_Centered_Variable(u32* rndNum);
#endif
diff --git a/include/variables.h b/include/variables.h
index 739a76818..fa09f0f54 100644
--- a/include/variables.h
+++ b/include/variables.h
@@ -2,6 +2,7 @@
#define VARIABLES_H
#include "z64.h"
+#include "osMalloc.h"
#include "segment_symbols.h"
extern Mtx D_01000000;
@@ -234,7 +235,6 @@ extern ActiveSequence gActiveSeqs[4];
extern AudioContext gAudioCtx;
extern AudioCustomUpdateFunction gAudioCustomUpdateFunction;
-extern u32 __osMalloc_FreeBlockTest_Enable;
extern Arena gSystemArena;
extern OSPifRam __osContPifRam;
extern u8 __osContLastCmd;
@@ -248,6 +248,4 @@ extern u64 gGfxSPTaskStack[SP_DRAM_STACK_SIZE64]; // 0x400 bytes
extern GfxPool gGfxPools[2]; // 0x24820 bytes
extern u8 gAudioHeap[0x38000]; // 0x38000 bytes
-extern u32 gTotalAllocFailures;
-
#endif
diff --git a/include/versions.h b/include/versions.h
index 1112ac01f..b135ae390 100644
--- a/include/versions.h
+++ b/include/versions.h
@@ -16,20 +16,4 @@
#define GC_EU_MQ 12
#define GC_JP_CE 13
-#define FAULT_N64 1 // in OoT N64
-#define FAULT_GC 2 // in OoT GC
-#if PLATFORM_N64
-#define FAULT_VERSION FAULT_N64
-#else
-#define FAULT_VERSION FAULT_GC
-#endif
-
-#define RAND_N64 1 // in OoT N64
-#define RAND_GC 2 // in OoT GC
-#if PLATFORM_N64
-#define RAND_VERSION RAND_N64
-#else
-#define RAND_VERSION RAND_GC
-#endif
-
#endif
diff --git a/include/z64.h b/include/z64.h
index 6b6cfb284..02cdafb49 100644
--- a/include/z64.h
+++ b/include/z64.h
@@ -439,33 +439,6 @@ typedef struct Yaz0Header {
/* 0x0C */ u32 uncompDataOffset; // only used in mio0
} Yaz0Header; // size = 0x10
-struct ArenaNode;
-
-typedef struct Arena {
- /* 0x00 */ struct ArenaNode* head;
- /* 0x04 */ void* start;
- /* 0x08 */ OSMesgQueue lockQueue;
- /* 0x20 */ u8 allocFailures; // only used in non-debug builds
- /* 0x21 */ u8 isInit;
- /* 0x22 */ u8 flag;
-} Arena; // size = 0x24
-
-typedef struct ArenaNode {
- /* 0x00 */ s16 magic;
- /* 0x02 */ s16 isFree;
- /* 0x04 */ u32 size;
- /* 0x08 */ struct ArenaNode* next;
- /* 0x0C */ struct ArenaNode* prev;
-#if OOT_DEBUG // TODO: This debug info is also present in N64 retail builds
- /* 0x10 */ const char* filename;
- /* 0x14 */ int line;
- /* 0x18 */ OSId threadId;
- /* 0x1C */ Arena* arena;
- /* 0x20 */ OSTime time;
- /* 0x28 */ u8 unk_28[0x30-0x28]; // probably padding
-#endif
-} ArenaNode; // size = 0x30
-
/* Relocation entry field getters */
#define RELOC_SECTION(reloc) ((reloc) >> 30)
#define RELOC_OFFSET(reloc) ((reloc) & 0xFFFFFF)
diff --git a/osMalloc.h b/osMalloc.h
new file mode 100644
index 000000000..5fb8dd8e4
--- /dev/null
+++ b/osMalloc.h
@@ -0,0 +1,72 @@
+#ifndef OSMALLOC_H
+#define OSMALLOC_H
+
+#include "ultra64.h"
+
+struct ArenaNode;
+
+typedef struct Arena {
+ /* 0x00 */ struct ArenaNode* head;
+ /* 0x04 */ void* start;
+#if PLATFORM_N64
+ /* 0x08 */ u32 size;
+ /* 0x0C */ u8 allocFailures;
+#else
+ /* 0x08 */ OSMesgQueue lockQueue;
+ /* 0x20 */ u8 allocFailures; // only used in non-debug builds
+ /* 0x21 */ u8 isInit;
+ /* 0x22 */ u8 flag;
+#endif
+} Arena; // size = 0x10 (N64), size = 0x24 (GC)
+
+typedef struct ArenaNode {
+ /* 0x00 */ s16 magic;
+ /* 0x02 */ s16 isFree;
+ /* 0x04 */ u32 size;
+ /* 0x08 */ struct ArenaNode* next;
+ /* 0x0C */ struct ArenaNode* prev;
+#if PLATFORM_N64 || OOT_DEBUG
+ /* 0x10 */ const char* filename;
+ /* 0x14 */ int line;
+ /* 0x18 */ OSId threadId;
+ /* 0x1C */ Arena* arena;
+ /* 0x20 */ OSTime time;
+ /* 0x28 */ u8 unk_28[0x30-0x28]; // probably padding
+#endif
+} ArenaNode; // size = 0x30 (N64 and GC debug), size = 0x10 (GC retail)
+
+#if PLATFORM_N64
+#define DECLARE_INTERRUPT_MASK OSIntMask __mask;
+#define CLEAR_INTERRUPTS() __mask = osSetIntMask(OS_IM_NONE)
+#define DISABLE_INTERRUPTS() __mask = osSetIntMask(OS_IM_NONE)
+#define RESTORE_INTERRUPTS() osSetIntMask(__mask)
+#else
+#define DECLARE_INTERRUPT_MASK
+#define CLEAR_INTERRUPTS() (void)0
+#define DISABLE_INTERRUPTS() (void)0
+#define RESTORE_INTERRUPTS() (void)0
+#endif
+
+void __osMallocInit(Arena* arena, void* start, s32 size);
+void __osMallocCleanup(Arena* arena);
+s32 __osMallocIsInitialized(Arena* arena);
+void* __osMalloc(Arena* arena, u32 size);
+void* __osMallocR(Arena* arena, u32 size);
+void __osFree(Arena* arena, void* ptr);
+void* __osRealloc(Arena* arena, void* ptr, u32 newSize);
+void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc);
+s32 __osCheckArena(Arena* arena);
+
+#if OOT_DEBUG
+void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line);
+void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line);
+void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line);
+void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line);
+void __osDisplayArena(Arena* arena);
+
+extern u32 __osMalloc_FreeBlockTest_Enable;
+#else
+extern u32 gTotalAllocFailures;
+#endif
+
+#endif
diff --git a/spec b/spec
index f86bbe7a4..9dfd4b20c 100644
--- a/spec
+++ b/spec
@@ -547,9 +547,9 @@ beginseg
#if OOT_DEBUG
include "$(BUILD_DIR)/src/code/debug_malloc.o"
#endif
-#if FAULT_VERSION == FAULT_N64
+#if PLATFORM_N64
include "$(BUILD_DIR)/src/code/fault_n64.o"
-#elif FAULT_VERSION == FAULT_GC
+#else
include "$(BUILD_DIR)/src/code/fault_gc.o"
include "$(BUILD_DIR)/src/code/fault_gc_drawer.o"
#endif
@@ -604,7 +604,11 @@ beginseg
include "$(BUILD_DIR)/src/code/fp.o"
include "$(BUILD_DIR)/src/code/system_malloc.o"
include "$(BUILD_DIR)/src/code/rand.o"
- include "$(BUILD_DIR)/src/code/__osMalloc.o"
+#if PLATFORM_N64
+ include "$(BUILD_DIR)/src/code/__osMalloc_n64.o"
+#else
+ include "$(BUILD_DIR)/src/code/__osMalloc_gc.o"
+#endif
#if !OOT_DEBUG
include "$(BUILD_DIR)/src/boot/sprintf.o"
#endif
diff --git a/src/boot/sprintf.c b/src/boot/sprintf.c
index cb80fe39a..d4af36356 100644
--- a/src/boot/sprintf.c
+++ b/src/boot/sprintf.c
@@ -3,6 +3,11 @@
#include "string.h"
#include "ultra64/xstdio.h"
+#if PLATFORM_N64
+// Generated by CVS "$Id$" keyword
+char sSprintfFileInfo[] = "$Id: sprintf.c,v 1.5 1997/03/19 02:28:53 hayakawa Exp $";
+#endif
+
void* proutSprintf(void* dst, const char* fmt, size_t size) {
return (char*)memcpy(dst, fmt, size) + size;
}
diff --git a/src/code/__osMalloc.c b/src/code/__osMalloc_gc.c
index 598e16a98..45e19532f 100644
--- a/src/code/__osMalloc.c
+++ b/src/code/__osMalloc_gc.c
@@ -1,19 +1,22 @@
#include "global.h"
#include "fault.h"
+#include "osMalloc.h"
#include "terminal.h"
+#if PLATFORM_GC
+
#define FILL_ALLOC_BLOCK_FLAG (1 << 0)
#define FILL_FREE_BLOCK_FLAG (1 << 1)
#define CHECK_FREE_BLOCK_FLAG (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
#define NODE_IS_VALID(node) (((node) != NULL) && ((node)->magic == NODE_MAGIC))
@@ -24,6 +27,8 @@
#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)
@@ -48,6 +53,7 @@
#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
@@ -68,6 +74,8 @@ u32 gTotalAllocFailures = 0; // "Arena_failcnt"
OSMesg sArenaLockMsg;
+void __osMallocAddBlock(Arena* arena, void* start, s32 size);
+
#if OOT_DEBUG
u32 __osMalloc_FreeBlockTest_Enable;
@@ -164,7 +172,7 @@ ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
return last;
}
-void __osMallocInit(Arena* arena, void* start, u32 size) {
+void __osMallocInit(Arena* arena, void* start, s32 size) {
bzero(arena, sizeof(Arena));
ArenaImpl_LockInit(arena);
__osMallocAddBlock(arena, start, size);
@@ -183,9 +191,7 @@ void __osMallocAddBlock(Arena* arena, void* start, s32 size) {
size2 = (size - diff) & ~0xF;
if (size2 > (s32)sizeof(ArenaNode)) {
-#if OOT_DEBUG
- memset(firstNode, BLOCK_UNINIT_MAGIC, size2);
-#endif
+ FILL_UNINIT_BLOCK(arena, firstNode, size2);
firstNode->next = NULL;
firstNode->prev = NULL;
firstNode->size = size2 - sizeof(ArenaNode);
@@ -230,7 +236,7 @@ void __osMallocCleanup(Arena* arena) {
bzero(arena, sizeof(*arena));
}
-u8 __osMallocIsInitialized(Arena* arena) {
+s32 __osMallocIsInitialized(Arena* arena) {
return arena->isInit;
}
@@ -267,9 +273,9 @@ void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, int line)
void* alloc = NULL;
ArenaNode* next;
- iter = arena->head;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
+ iter = arena->head;
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
@@ -357,6 +363,7 @@ void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line) {
iter = NODE_GET_PREV(iter);
}
+
ArenaImpl_Unlock(arena);
return allocR;
@@ -375,9 +382,9 @@ void* __osMalloc_NoLock(Arena* arena, u32 size) {
iter = arena->head;
while (iter != NULL) {
-
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
+
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
@@ -398,6 +405,7 @@ void* __osMalloc_NoLock(Arena* arena, u32 size) {
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
+
break;
}
@@ -480,7 +488,7 @@ void __osFree_NoLock(Arena* arena, void* ptr) {
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
- if (node == NULL || node->magic != NODE_MAGIC) {
+ if (!NODE_IS_VALID(node)) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n") VT_RST,
ptr);
return;
@@ -546,7 +554,7 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, int line) {
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
- if (node == NULL || node->magic != NODE_MAGIC) {
+ if (!NODE_IS_VALID(node)) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:不正解放(%08x) [%s:%d ]\n", "__osFree: Unauthorized release (%08x) [%s:%d ]\n") VT_RST,
ptr, file, line);
@@ -573,7 +581,6 @@ void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, int line) {
FILL_FREE_BLOCK_CONTENTS(arena, node);
- newNext = node->next;
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
newNext = NODE_GET_NEXT(next);
if (newNext != NULL) {
@@ -848,7 +855,7 @@ void ArenaImpl_FaultClient(Arena* arena) {
Fault_Printf("Largest Free Block Size %08x\n", maxFree);
}
-u32 __osCheckArena(Arena* arena) {
+s32 __osCheckArena(Arena* arena) {
ArenaNode* iter;
u32 error = 0;
@@ -858,7 +865,8 @@ u32 __osCheckArena(Arena* arena) {
arena);
iter = arena->head;
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
osSyncPrintf(VT_COL(RED, WHITE) T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n") VT_RST, iter,
iter->magic);
@@ -883,3 +891,5 @@ u8 ArenaImpl_GetAllocFailures(Arena* arena) {
return arena->allocFailures;
}
#endif
+
+#endif
diff --git a/src/code/__osMalloc_n64.c b/src/code/__osMalloc_n64.c
new file mode 100644
index 000000000..bd9c358af
--- /dev/null
+++ b/src/code/__osMalloc_n64.c
@@ -0,0 +1,474 @@
+#include "global.h"
+#include "fault.h"
+#include "osMalloc.h"
+
+#if PLATFORM_N64
+
+#define NODE_MAGIC 0x7373
+
+#define NODE_IS_VALID(node) ((node)->magic == NODE_MAGIC)
+
+#define NODE_GET_NEXT(node) ((node)->next)
+#define NODE_GET_PREV(node) ((node)->prev)
+
+#define SET_DEBUG_INFO(node, f, l, a) \
+ { \
+ (node)->filename = (f); \
+ (node)->line = (l); \
+ (node)->threadId = osGetThreadId(NULL); \
+ (node)->arena = (a); \
+ (node)->time = osGetTime(); \
+ } \
+ (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)
+
+void __osMallocInit(Arena* arena, void* start, s32 size) {
+ ArenaNode* firstNode = (ArenaNode*)ALIGN16((u32)start);
+
+ size -= (u8*)firstNode - (u8*)start;
+ size &= ~0xF;
+
+ firstNode->next = NULL;
+ firstNode->prev = NULL;
+ firstNode->size = size - sizeof(ArenaNode);
+ firstNode->isFree = true;
+ firstNode->magic = NODE_MAGIC;
+
+ arena->head = firstNode;
+ arena->start = start;
+ arena->size = size;
+}
+
+void __osMallocCleanup(Arena* arena) {
+ bzero(arena, sizeof(*arena));
+}
+
+s32 __osMallocIsInitialized(Arena* arena) {
+ return arena->start != NULL;
+}
+
+void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line) {
+ ArenaNode* iter;
+ u32 blockSize;
+ ArenaNode* newNode;
+ void* alloc = NULL;
+ ArenaNode* next;
+
+ size = ALIGN16(size);
+ blockSize = ALIGN16(size) + sizeof(ArenaNode);
+ iter = arena->head;
+
+ while (iter != NULL) {
+ if (iter->isFree && iter->size >= size) {
+ CHECK_FREE_BLOCK(arena, iter);
+
+ if (blockSize < iter->size) {
+ newNode = (ArenaNode*)((u32)iter + blockSize);
+ newNode->next = NODE_GET_NEXT(iter);
+ newNode->prev = iter;
+ newNode->size = iter->size - blockSize;
+ newNode->isFree = true;
+ newNode->magic = NODE_MAGIC;
+
+ iter->next = newNode;
+ iter->size = size;
+ next = NODE_GET_NEXT(newNode);
+ if (next) {
+ next->prev = newNode;
+ }
+ }
+
+ iter->isFree = false;
+ SET_DEBUG_INFO(iter, file, line, arena);
+ alloc = (void*)((u32)iter + sizeof(ArenaNode));
+ FILL_ALLOC_BLOCK(arena, alloc, size);
+
+ break;
+ }
+
+ iter = NODE_GET_NEXT(iter);
+ }
+
+ CHECK_ALLOC_FAILURE(arena, alloc);
+
+ return alloc;
+}
+
+void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line) {
+ ArenaNode* iter;
+ ArenaNode* newNode;
+ u32 blockSize;
+ u32 nodeSize;
+ ArenaNode* next;
+ void* allocR = NULL;
+ ArenaNode* next2;
+
+ size = ALIGN16(size);
+
+ iter = arena->head;
+ next2 = NODE_GET_NEXT(iter);
+ while (next2 != NULL) {
+ iter = next2;
+ next2 = NODE_GET_NEXT(next2);
+ }
+
+ while (iter != NULL) {
+ if (iter->isFree && iter->size >= size) {
+ CHECK_FREE_BLOCK(arena, iter);
+
+ blockSize = ALIGN16(size) + sizeof(ArenaNode);
+ nodeSize = iter->size;
+ if (blockSize < nodeSize) {
+ newNode = (ArenaNode*)((u32)iter + (iter->size - size));
+ newNode->next = NODE_GET_NEXT(iter);
+ newNode->prev = iter;
+ newNode->size = size;
+ newNode->magic = NODE_MAGIC;
+
+ iter->next = newNode;
+ iter->size -= blockSize;
+ next = NODE_GET_NEXT(newNode);
+ if (next) {
+ next->prev = newNode;
+ }
+ iter = newNode;
+ }
+
+ iter->isFree = false;
+ SET_DEBUG_INFO(iter, file, line, arena);
+ allocR = (void*)((u32)iter + sizeof(ArenaNode));
+ FILL_ALLOC_BLOCK(arena, allocR, size);
+
+ break;
+ }
+
+ iter = NODE_GET_PREV(iter);
+ }
+
+ CHECK_ALLOC_FAILURE(arena, allocR);
+
+ return allocR;
+}
+
+void* __osMalloc(Arena* arena, u32 size) {
+ ArenaNode* iter;
+ u32 blockSize;
+ ArenaNode* newNode;
+ void* alloc = NULL;
+ ArenaNode* next;
+
+ size = ALIGN16(size);
+ blockSize = ALIGN16(size) + sizeof(ArenaNode);
+ iter = arena->head;
+
+ while (iter != NULL) {
+ if (iter->isFree && iter->size >= size) {
+ CHECK_FREE_BLOCK(arena, iter);
+
+ if (blockSize < iter->size) {
+ newNode = (ArenaNode*)((u32)iter + blockSize);
+ newNode->next = NODE_GET_NEXT(iter);
+ newNode->prev = iter;
+ newNode->size = iter->size - blockSize;
+ newNode->isFree = true;
+ newNode->magic = NODE_MAGIC;
+
+ iter->next = newNode;
+ iter->size = size;
+ next = NODE_GET_NEXT(newNode);
+ if (next) {
+ next->prev = newNode;
+ }
+ }
+
+ iter->isFree = false;
+ SET_DEBUG_INFO(iter, NULL, 0, arena);
+ alloc = (void*)((u32)iter + sizeof(ArenaNode));
+ FILL_ALLOC_BLOCK(arena, alloc, size);
+
+ break;
+ }
+
+ iter = NODE_GET_NEXT(iter);
+ }
+
+ CHECK_ALLOC_FAILURE(arena, alloc);
+
+ return alloc;
+}
+
+void* __osMallocR(Arena* arena, u32 size) {
+ ArenaNode* iter;
+ ArenaNode* newNode;
+ u32 blockSize;
+ u32 nodeSize;
+ ArenaNode* next;
+ void* allocR = NULL;
+ ArenaNode* next2;
+
+ size = ALIGN16(size);
+
+ iter = arena->head;
+ next2 = NODE_GET_NEXT(iter);
+ while (next2 != NULL) {
+ iter = next2;
+ next2 = NODE_GET_NEXT(next2);
+ }
+
+ while (iter != NULL) {
+ if (iter->isFree && iter->size >= size) {
+ CHECK_FREE_BLOCK(arena, iter);
+
+ blockSize = ALIGN16(size) + sizeof(ArenaNode);
+ nodeSize = iter->size;
+ if (blockSize < nodeSize) {
+ newNode = (ArenaNode*)((u32)iter + (iter->size - size));
+ newNode->next = NODE_GET_NEXT(iter);
+ newNode->prev = iter;
+ newNode->size = size;
+ newNode->magic = NODE_MAGIC;
+
+ iter->next = newNode;
+ iter->size -= blockSize;
+ next = NODE_GET_NEXT(newNode);
+ if (next) {
+ next->prev = newNode;
+ }
+ iter = newNode;
+ }
+
+ iter->isFree = false;
+ SET_DEBUG_INFO(iter, NULL, 0, arena);
+ allocR = (void*)((u32)iter + sizeof(ArenaNode));
+ FILL_ALLOC_BLOCK(arena, allocR, size);
+
+ break;
+ }
+ iter = NODE_GET_PREV(iter);
+ }
+
+ CHECK_ALLOC_FAILURE(arena, allocR);
+
+ return allocR;
+}
+
+void __osFree(Arena* arena, void* ptr) {
+ ArenaNode* node;
+ ArenaNode* next;
+ ArenaNode* prev;
+
+ if (ptr == NULL) {
+ return;
+ }
+
+ node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
+ if (!NODE_IS_VALID(node)) {
+ (void)T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n");
+ osSetIntMask(OS_IM_ALL);
+ return;
+ }
+ if (node->isFree) {
+ (void)T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n");
+ osSetIntMask(OS_IM_ALL);
+ return;
+ }
+ if (arena != node->arena && arena != NULL) {
+ (void)T("__osFree:arena(%08x)が__osMallocのarena(%08x)と一致しない\n",
+ "__osFree:arena(%08x) and __osMalloc:arena(%08x) do not match\n");
+ }
+
+ node->isFree = true;
+ SET_DEBUG_INFO(node, NULL, 0, arena);
+
+ if (node->next != NULL) {
+ next = node->next;
+ if (next->isFree) {
+ if (next->next != NULL) {
+ next->next->prev = node;
+ }
+
+ node->size += next->size + sizeof(ArenaNode);
+ node->next = next->next;
+ }
+ }
+
+ if (node->prev != NULL) {
+ prev = node->prev;
+ if (prev->isFree) {
+ prev->size += node->size + sizeof(ArenaNode);
+ prev->next = NODE_GET_NEXT(node);
+
+ if (node->next != NULL) {
+ node->next->prev = prev;
+ }
+ }
+ }
+}
+
+void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line) {
+ ArenaNode* node;
+ ArenaNode* next;
+ ArenaNode* prev;
+
+ if (ptr == NULL) {
+ return;
+ }
+
+ node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
+ if (!NODE_IS_VALID(node)) {
+ (void)T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n");
+ osSetIntMask(OS_IM_ALL);
+ return;
+ }
+ if (node->isFree) {
+ (void)T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n");
+ osSetIntMask(OS_IM_ALL);
+ return;
+ }
+ if (arena != node->arena && arena != NULL) {
+ (void)T("__osFree:arena(%08x)が__osMallocのarena(%08x)と一致しない\n",
+ "__osFree:arena(%08x) and __osMalloc:arena(%08x) do not match\n");
+ }
+
+ node->isFree = true;
+ SET_DEBUG_INFO(node, file, line, arena);
+
+ if (node->next != NULL) {
+ next = node->next;
+ if (next->isFree) {
+ if (next->next != NULL) {
+ next->next->prev = node;
+ }
+
+ node->size += next->size + sizeof(ArenaNode);
+ node->next = next->next;
+ }
+ }
+
+ if (node->prev != NULL) {
+ prev = node->prev;
+ if (prev->isFree) {
+ prev->size += node->size + sizeof(ArenaNode);
+ prev->next = NODE_GET_NEXT(node);
+
+ if (node->next != NULL) {
+ node->next->prev = prev;
+ }
+ }
+ }
+}
+
+void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
+ ArenaNode* node;
+ void* newAlloc;
+ ArenaNode* next;
+ ArenaNode* newNext;
+ u32 sizeDiff;
+
+ (void)"__osRealloc(%08x, %d)\n";
+ osSetIntMask(OS_IM_ALL);
+
+ if (ptr == NULL) {
+ ptr = __osMalloc(arena, newSize);
+ } else if (newSize == 0) {
+ __osFree(arena, ptr);
+ ptr = NULL;
+ } else {
+ newSize = ALIGN16(newSize);
+ node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
+ if (newSize == node->size) {
+ // Do nothing
+ } else if (node->size < newSize) {
+ next = NODE_GET_NEXT(node);
+ sizeDiff = newSize - node->size;
+ if (next != NULL && next->isFree && next->size >= sizeDiff) {
+ next->size -= sizeDiff;
+ newNext = (ArenaNode*)((u32)next + sizeDiff);
+ if (NODE_GET_NEXT(next) != NULL) {
+ NODE_GET_NEXT(next)->prev = newNext;
+ }
+ node->next = newNext;
+ node->size = newSize;
+ memmove(node->next, next, sizeof(ArenaNode));
+ } else {
+ newAlloc = __osMalloc(arena, newSize);
+ if (newAlloc != NULL) {
+ memcpy(ptr, newAlloc, node->size);
+ __osFree(arena, ptr);
+ }
+ ptr = newAlloc;
+ }
+ } else if (newSize < node->size) {
+ (void)T("メモリブロックの縮小機能はまだインプリメントしていません\n",
+ "Memory block shrinking functionality is not yet implemented\n");
+ }
+ }
+
+ CHECK_ALLOC_FAILURE(arena, ptr);
+
+ return ptr;
+}
+
+void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line) {
+ return __osRealloc(arena, ptr, newSize);
+}
+
+void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
+ ArenaNode* iter;
+
+ *outMaxFree = 0;
+ *outFree = 0;
+ *outAlloc = 0;
+
+ iter = arena->head;
+ while (iter != NULL) {
+ if (iter->isFree) {
+ *outFree += iter->size;
+ if (*outMaxFree < iter->size) {
+ *outMaxFree = iter->size;
+ }
+ } else {
+ *outAlloc += iter->size;
+ }
+
+ iter = NODE_GET_NEXT(iter);
+ }
+}
+
+s32 __osCheckArena(Arena* arena) {
+ ArenaNode* iter;
+
+ (void)T("アリーナの内容をチェックしています... (%08x)\n", "Checking the arena contents... (%08x)\n");
+ iter = arena->head;
+ while (iter != NULL) {
+ if (!NODE_IS_VALID(iter)) {
+ (void)T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n");
+ return 1;
+ }
+ iter = NODE_GET_NEXT(iter);
+ }
+
+ (void)T("アリーナはまだ、いけそうです\n", "The arena is still going well\n");
+ return 0;
+}
+
+u8 ArenaImpl_GetAllocFailures(Arena* arena) {
+ return arena->allocFailures;
+}
+
+#endif
diff --git a/src/code/code_800FC620.c b/src/code/code_800FC620.c
index eafbe5080..4d7b7b4ef 100644
--- a/src/code/code_800FC620.c
+++ b/src/code/code_800FC620.c
@@ -1,4 +1,5 @@
#include "global.h"
+#include "osMalloc.h"
typedef void (*arg3_800FC868)(void*);
typedef void (*arg3_800FC8D8)(void*, u32);
@@ -26,43 +27,64 @@ char D_80134488[0x18] = {
// possibly some kind of new() function
void* func_800FC800(u32 size) {
+ DECLARE_INTERRUPT_MASK
+ void* ptr;
+
+ DISABLE_INTERRUPTS();
+
if (size == 0) {
size = 1;
}
#if OOT_DEBUG
- return __osMallocDebug(&gSystemArena, size, sNew, 0);
+ ptr = __osMallocDebug(&gSystemArena, size, sNew, 0);
#else
- return __osMalloc(&gSystemArena, size);
+ ptr = __osMalloc(&gSystemArena, size);
#endif
+
+ RESTORE_INTERRUPTS();
+ return ptr;
}
// possibly some kind of delete() function
void func_800FC83C(void* ptr) {
+ DECLARE_INTERRUPT_MASK
+
+ DISABLE_INTERRUPTS();
if (ptr != NULL) {
__osFree(&gSystemArena, ptr);
}
+ RESTORE_INTERRUPTS();
}
void func_800FC868(void* blk, u32 nBlk, u32 blkSize, arg3_800FC868 arg3) {
+ DECLARE_INTERRUPT_MASK
u32 pos;
- for (pos = (u32)blk; pos < (u32)blk + (nBlk * blkSize); pos = (u32)pos + (blkSize & ~0)) {
+ DISABLE_INTERRUPTS();
+ for (pos = ((u32)blk & ~0); pos < (u32)blk + (nBlk * blkSize); pos = (u32)pos + (blkSize & ~0)) {
arg3((void*)pos);
}
+ RESTORE_INTERRUPTS();
}
void func_800FC8D8(void* blk, u32 nBlk, s32 blkSize, arg3_800FC8D8 arg3) {
+ DECLARE_INTERRUPT_MASK
u32 pos;
- for (pos = (u32)blk; pos < (u32)blk + (nBlk * blkSize); pos = (u32)pos + (blkSize & ~0)) {
+ DISABLE_INTERRUPTS();
+ for (pos = ((u32)blk & ~0); pos < (u32)blk + (nBlk * blkSize); pos = (u32)pos + (blkSize & ~0)) {
arg3((void*)pos, 2);
}
+ RESTORE_INTERRUPTS();
}
void* func_800FC948(void* blk, u32 nBlk, u32 blkSize, arg3_800FC948 arg3) {
+ DECLARE_INTERRUPT_MASK
u32 pos;
+ DISABLE_INTERRUPTS();
+
if (blk == NULL) {
blk = func_800FC800(nBlk * blkSize);
}
@@ -74,29 +96,35 @@ void* func_800FC948(void* blk, u32 nBlk, u32 blkSize, arg3_800FC948 arg3) {
pos = (u32)pos + (blkSize & ~0);
}
}
+
+ RESTORE_INTERRUPTS();
return blk;
}
void func_800FCA18(void* blk, u32 nBlk, u32 blkSize, arg3_800FCA18 arg3, s32 arg4) {
+ DECLARE_INTERRUPT_MASK
u32 pos;
u32 end;
- if (blk == NULL) {
- return;
- }
- if (arg3 != NULL) {
- end = (u32)blk;
- pos = (u32)end + (nBlk * blkSize);
+ DISABLE_INTERRUPTS();
- while (pos > end) {
- pos -= (s32)(blkSize & ~0);
- arg3((void*)pos, 2);
+ if (blk != NULL) {
+ if (arg3 != NULL) {
+ end = (u32)blk;
+ pos = (u32)end + (nBlk * blkSize);
+
+ while (pos > end) {
+ pos -= (s32)(blkSize & ~0);
+ arg3((void*)pos, 2);
+ }
}
- }
- if (arg4 != 0) {
- func_800FC83C(blk);
+ if (arg4 != 0) {
+ func_800FC83C(blk);
+ }
}
+
+ RESTORE_INTERRUPTS();
}
void func_800FCB34(void) {
@@ -120,6 +148,11 @@ void func_800FCB34(void) {
}
void SystemHeap_Init(void* start, u32 size) {
+#if PLATFORM_N64
+ __osMallocInit(&gSystemArena, start, size);
+#else
SystemArena_Init(start, size);
+#endif
+
func_800FCB34();
}
diff --git a/src/code/debug_malloc.c b/src/code/debug_malloc.c
index 3e31f7845..5be04b23c 100644
--- a/src/code/debug_malloc.c
+++ b/src/code/debug_malloc.c
@@ -1,4 +1,5 @@
#include "global.h"
+#include "osMalloc.h"
#define LOG_SEVERITY_NOLOG 0
#define LOG_SEVERITY_ERROR 2
@@ -125,6 +126,6 @@ void DebugArena_Cleanup(void) {
__osMallocCleanup(&sDebugArena);
}
-u8 DebugArena_IsInitialized(void) {
+s32 DebugArena_IsInitialized(void) {
return __osMallocIsInitialized(&sDebugArena);
}
diff --git a/src/code/fault_gc.c b/src/code/fault_gc.c
index 587f7cb24..0402573ad 100644
--- a/src/code/fault_gc.c
+++ b/src/code/fault_gc.c
@@ -40,15 +40,11 @@
* DPad-Up may be pressed to enable sending fault pages over osSyncPrintf as well as displaying them on-screen.
* DPad-Down disables sending fault pages over osSyncPrintf.
*/
+#if PLATFORM_GC
+
#pragma increment_block_number "gc-eu:240 gc-eu-mq:240 gc-eu-mq-dbg:224 gc-jp:240 gc-jp-ce:240 gc-jp-mq:240 gc-us:240" \
"gc-us-mq:240"
-// Include versions.h first and redefine FAULT_VERSION
-// This allows this file to compile even when versions.h uses FAULT_N64
-#include "versions.h"
-#undef FAULT_VERSION
-#define FAULT_VERSION FAULT_GC
-
#include "global.h"
#include "alloca.h"
#include "fault.h"
@@ -1336,3 +1332,5 @@ NORETURN void Fault_AddHungupAndCrash(const char* file, int line) {
sprintf(msg, "HungUp %s:%d", file, line);
Fault_AddHungupAndCrashImpl(msg, NULL);
}
+
+#endif
diff --git a/src/code/fault_gc_drawer.c b/src/code/fault_gc_drawer.c
index dbdcba7e7..5cf088fca 100644
--- a/src/code/fault_gc_drawer.c
+++ b/src/code/fault_gc_drawer.c
@@ -4,17 +4,12 @@
* Implements routines for drawing text with a fixed font directly to a framebuffer, used in displaying
* the crash screen implemented by fault.c
*/
-
-// Include versions.h first and redefine FAULT_VERSION
-// This allows this file to compile even when versions.h uses FAULT_N64
-#include "versions.h"
-#undef FAULT_VERSION
-#define FAULT_VERSION FAULT_GC
-
#include "global.h"
#include "fault.h"
#include "terminal.h"
+#if PLATFORM_GC
+
typedef struct FaultDrawer {
/* 0x00 */ u16* fb;
/* 0x04 */ u16 w;
@@ -348,3 +343,5 @@ void Fault_InitDrawer(void) {
bcopy(&sFaultDrawerDefault, &sFaultDrawer, sizeof(FaultDrawer));
sFaultDrawer.fb = (u16*)(PHYS_TO_K0(osMemSize) - sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH]));
}
+
+#endif
diff --git a/src/code/fault_n64.c b/src/code/fault_n64.c
index da9d7bf6f..5a8dd9d1d 100644
--- a/src/code/fault_n64.c
+++ b/src/code/fault_n64.c
@@ -1,14 +1,11 @@
-// Include versions.h first and redefine FAULT_VERSION
-// This allows this file to compile even when versions.h uses FAULT_GC
-#include "versions.h"
-#undef FAULT_VERSION
-#define FAULT_VERSION FAULT_N64
-
#include "global.h"
#include "fault.h"
+#include "osMalloc.h"
#include "stack.h"
#include "terminal.h"
+#if PLATFORM_N64
+
typedef struct FaultMgr {
OSThread thread;
char unk_1B0[0x400];
@@ -846,3 +843,5 @@ void Fault_AddHungupAndCrash(const char* file, int line) {
sprintf(msg, "HungUp %s:%d", file, line);
Fault_AddHungupAndCrashImpl(msg, NULL);
}
+
+#endif
diff --git a/src/code/game.c b/src/code/game.c
index ffb8bc847..07007f6a7 100644
--- a/src/code/game.c
+++ b/src/code/game.c
@@ -1,5 +1,6 @@
#include "global.h"
#include "fault.h"
+#include "osMalloc.h"
#include "terminal.h"
#if PLATFORM_N64
#include "n64dd.h"
diff --git a/src/code/rand.c b/src/code/rand.c
index ce2beb9e3..917b1ecfc 100644
--- a/src/code/rand.c
+++ b/src/code/rand.c
@@ -42,7 +42,6 @@
* @note Original name: qrand.c
*/
#include "rand.h"
-#include "versions.h"
#define RAND_MULTIPLIER 1664525
#define RAND_INCREMENT 1013904223
@@ -54,7 +53,7 @@
*/
static u32 sRandInt = 1;
-#if RAND_VERSION == RAND_GC
+#if PLATFORM_GC
/**
* Space to store a value to be re-interpreted as a float.
*
@@ -69,7 +68,7 @@ static fu sRandFloat;
* @note Original name: qrand
*/
u32 Rand_Next(void) {
-#if RAND_VERSION == RAND_N64
+#if PLATFORM_N64
u32 next = sRandInt * RAND_MULTIPLIER + RAND_INCREMENT;
sRandInt = next;
@@ -98,7 +97,7 @@ void Rand_Seed(u32 seed) {
* @note Original name: fqrand
*/
f32 Rand_ZeroOne(void) {
-#if RAND_VERSION == RAND_N64
+#if PLATFORM_N64
fu v;
f32 vf;
@@ -115,7 +114,7 @@ f32 Rand_ZeroOne(void) {
#endif
}
-#if RAND_VERSION == RAND_GC
+#if PLATFORM_GC
/**
* Returns a pseudo-random floating-point number between -0.5f and 0.5f by the same manner in which Rand_ZeroOne
* generates its result.
@@ -164,7 +163,7 @@ u32 Rand_Next_Variable(u32* rndNum) {
* @note Original name: fqrand_r
*/
f32 Rand_ZeroOne_Variable(u32* rndNum) {
-#if RAND_VERSION == RAND_N64
+#if PLATFORM_N64
fu v;
f32 vf;
u32 next = Rand_Next_Variable(rndNum);
@@ -180,7 +179,7 @@ f32 Rand_ZeroOne_Variable(u32* rndNum) {
#endif
}
-#if RAND_VERSION == RAND_GC
+#if PLATFORM_GC
/**
* Generates the next pseudo-random floating-point number between -0.5f and 0.5f from the provided rndNum.
*
diff --git a/src/code/system_malloc.c b/src/code/system_malloc.c
index 8385a883c..de6a56b30 100644
--- a/src/code/system_malloc.c
+++ b/src/code/system_malloc.c
@@ -1,4 +1,5 @@
#include "global.h"
+#include "osMalloc.h"
#define LOG_SEVERITY_NOLOG 0
#define LOG_SEVERITY_ERROR 2
@@ -6,16 +7,6 @@
Arena gSystemArena;
-#if PLATFORM_N64
-#define DECLARE_INTERRUPT_MASK OSIntMask __mask;
-#define DISABLE_INTERRUPTS() __mask = osSetIntMask(OS_IM_NONE)
-#define RESTORE_INTERRUPTS() osSetIntMask(__mask)
-#else
-#define DECLARE_INTERRUPT_MASK
-#define DISABLE_INTERRUPTS() (void)0
-#define RESTORE_INTERRUPTS() (void)0
-#endif
-
#if OOT_DEBUG
s32 gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
@@ -176,6 +167,6 @@ void SystemArena_Cleanup(void) {
__osMallocCleanup(&gSystemArena);
}
-u8 SystemArena_IsInitialized(void) {
+s32 SystemArena_IsInitialized(void) {
return __osMallocIsInitialized(&gSystemArena);
}
diff --git a/src/code/z_malloc.c b/src/code/z_malloc.c
index 979de62fe..5b1c5637f 100644
--- a/src/code/z_malloc.c
+++ b/src/code/z_malloc.c
@@ -1,4 +1,5 @@
#include "global.h"
+#include "osMalloc.h"
#define LOG_SEVERITY_NOLOG 0
#define LOG_SEVERITY_ERROR 2
@@ -124,6 +125,6 @@ void ZeldaArena_Cleanup(void) {
__osMallocCleanup(&sZeldaArena);
}
-u8 ZeldaArena_IsInitialized(void) {
+s32 ZeldaArena_IsInitialized(void) {
return __osMallocIsInitialized(&sZeldaArena);
}
diff --git a/tools/disasm/ntsc-1.2/files_code.csv b/tools/disasm/ntsc-1.2/files_code.csv
index 8aec80c33..dd1ac65e8 100644
--- a/tools/disasm/ntsc-1.2/files_code.csv
+++ b/tools/disasm/ntsc-1.2/files_code.csv
@@ -132,7 +132,7 @@ BC670,800CDD50,src/code/fp_math
BCA20,800CE100,src/code/fp
BCB80,800CE260,src/code/system_malloc
BCDF0,800CE4D0,src/code/rand
-BCEF0,800CE5D0,src/code/__osMalloc
+BCEF0,800CE5D0,src/code/__osMalloc_n64
BD8A0,800CEF80,src/boot/sprintf
BD970,800CF050,src/code/printutils
BD9D0,800CF0B0,src/code/jpegutils
@@ -292,7 +292,8 @@ F3AD0,801051B0,src/code/gfxprint
F4380,80105A60,src/code/loadfragment2_n64
F4390,80105A70,src/code/code_800FC620
F43A0,80105A80,src/code/rand
-F43B0,80105A90,src/code/__osMalloc
+F43B0,80105A90,src/code/__osMalloc_n64
+F43C0,80105AA0,src/boot/sprintf
F4400,80105AE0,src/libultra/gu/sins
F4C00,801062E0,src/libultra/io/siacs
F4C10,801062F0,src/libultra/io/controller
@@ -379,6 +380,7 @@ F9190,8010A870,src/code/gfxprint
F91D0,8010A8B0,src/code/rcp_utils
F9320,8010AA00,src/code/loadfragment2_n64
F94C0,8010ABA0,src/code/fp_math
+F94E0,8010ABC0,src/code/__osMalloc_n64
F9690,8010AD70,src/libultra/gu/sinf
F96E0,8010ADC0,src/libultra/gu/perspective
F96F0,8010ADD0,src/libultra/gu/lookathil
diff --git a/tools/disasm/ntsc-1.2/functions.txt b/tools/disasm/ntsc-1.2/functions.txt
index 457c53d6e..a3c4c7520 100644
--- a/tools/disasm/ntsc-1.2/functions.txt
+++ b/tools/disasm/ntsc-1.2/functions.txt
@@ -2931,7 +2931,7 @@ __osRealloc = 0x800CED54; // type:func
__osReallocDebug = 0x800CEEA8; // type:func
ArenaImpl_GetSizes = 0x800CEEC8; // type:func
__osCheckArena = 0x800CEF3C; // type:func
-func_800FF334 = 0x800CEF78; // type:func
+ArenaImpl_GetAllocFailures = 0x800CEF78; // type:func
proutSprintf = 0x800CEF80; // type:func
vsprintf = 0x800CEFA4; // type:func
sprintf = 0x800CEFF4; // type:func