From bbdf51bc338ebc4803dfea3e9569c5ec526b98c5 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 13 May 2023 01:49:54 -0700 Subject: [PATCH] Remove the main pool --- data/dynos_warps.cpp | 2 - src/engine/level_script.c | 11 --- src/game/main.c | 8 --- src/game/memory.c | 139 -------------------------------------- src/game/memory.h | 8 --- src/pc/pc_main.c | 5 -- 6 files changed, 173 deletions(-) diff --git a/data/dynos_warps.cpp b/data/dynos_warps.cpp index 5c317831..5bde12f4 100644 --- a/data/dynos_warps.cpp +++ b/data/dynos_warps.cpp @@ -246,7 +246,6 @@ static void *DynOS_Warp_UpdateWarp(void *aCmd, bool aIsLevelInitDone) { clear_objects(); clear_area_graph_nodes(); clear_areas(); - main_pool_pop_state(); // Reset Mario's state gMarioState->healCounter = 0; @@ -414,7 +413,6 @@ static void *DynOS_Warp_UpdateExit(void *aCmd, bool aIsLevelInitDone) { clear_objects(); clear_area_graph_nodes(); clear_areas(); - main_pool_pop_state(); // Reset Mario's state gMarioState->healCounter = 0; diff --git a/src/engine/level_script.c b/src/engine/level_script.c index a5e79334..47bd8d31 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -141,7 +141,6 @@ static void area_check_red_coin_or_secret(void *arg, bool isMacroObject) { } static void level_cmd_load_and_execute(void) { - main_pool_push_state(); load_segment(CMD_GET(s16, 2), CMD_GET(void *, 4), CMD_GET(void *, 8), MEMORY_POOL_LEFT); *sStackTop++ = (uintptr_t) NEXT_CMD; @@ -154,9 +153,6 @@ static void level_cmd_load_and_execute(void) { static void level_cmd_exit_and_execute(void) { void *targetAddr = CMD_GET(void *, 12); - main_pool_pop_state(); - main_pool_push_state(); - load_segment(CMD_GET(s16, 2), CMD_GET(void *, 4), CMD_GET(void *, 8), MEMORY_POOL_LEFT); @@ -165,8 +161,6 @@ static void level_cmd_exit_and_execute(void) { } static void level_cmd_exit(void) { - main_pool_pop_state(); - sStackTop = sStackBase; sStackBase = (uintptr_t *) *(--sStackTop); sCurrentCmd = (struct LevelCommand *) *(--sStackTop); @@ -307,12 +301,10 @@ static void level_cmd_set_register(void) { } static void level_cmd_push_pool_state(void) { - main_pool_push_state(); sCurrentCmd = CMD_NEXT; } static void level_cmd_pop_pool_state(void) { - main_pool_pop_state(); sCurrentCmd = CMD_NEXT; } @@ -358,7 +350,6 @@ static void level_cmd_init_level(void) { init_graph_node_start(NULL, (struct GraphNodeStart *) &gObjParentGraphNode); clear_objects(); clear_areas(); - main_pool_push_state(); smlua_model_util_clear(); gSkipInterpolationTitleScreen = false; @@ -377,8 +368,6 @@ static void level_cmd_clear_level(void) { sLevelOwnedGraphNodes[i] = false; } } - main_pool_pop_state(); - sCurrentCmd = CMD_NEXT; } diff --git a/src/game/main.c b/src/game/main.c index 24f497a3..fd77f719 100644 --- a/src/game/main.c +++ b/src/game/main.c @@ -142,13 +142,6 @@ void setup_mesg_queues(void) { osSetEventMesg(OS_EVENT_PRENMI, &gIntrMesgQueue, (OSMesg) MESG_NMI_REQUEST); } -void alloc_pool(void) { - void *start = (void *) SEG_POOL_START; - void *end = (void *) (SEG_POOL_START + POOL_SIZE); - - main_pool_init(start, end); -} - void create_thread(OSThread *thread, OSId id, void (*entry)(void *), void *arg, void *sp, OSPri pri) { thread->next = NULL; thread->queue = NULL; @@ -343,7 +336,6 @@ void handle_dp_complete(void) { void thread3_main(UNUSED void *arg) { setup_mesg_queues(); - alloc_pool(); load_engine_code_segment(); create_thread(&gSoundThread, 4, thread4_sound, NULL, gThread4Stack + 0x2000, 20); diff --git a/src/game/memory.c b/src/game/memory.c index 1cd1ee8b..766f4172 100644 --- a/src/game/memory.c +++ b/src/game/memory.c @@ -78,145 +78,6 @@ void *virtual_to_segmented(UNUSED u32 segment, const void *addr) { void move_segment_table_to_dmem(void) { } -/** - * Initialize the main memory pool. This pool is conceptually a pair of stacks - * that grow inward from the left and right. It therefore only supports - * freeing the object that was most recently allocated from a side. - */ -void main_pool_init(void *start, void *end) { - sPoolStart = (u8 *) ALIGN16((uintptr_t) start) + 16; - sPoolEnd = (u8 *) ALIGN16((uintptr_t) end - 15) - 16; - sPoolFreeSpace = sPoolEnd - sPoolStart; - - sPoolListHeadL = (struct MainPoolBlock *) (sPoolStart - 16); - sPoolListHeadR = (struct MainPoolBlock *) sPoolEnd; - sPoolListHeadL->prev = NULL; - sPoolListHeadL->next = NULL; - sPoolListHeadR->prev = NULL; - sPoolListHeadR->next = NULL; -} - -/** - * Allocate a block of memory from the pool of given size, and from the - * specified side of the pool (MEMORY_POOL_LEFT or MEMORY_POOL_RIGHT). - * If there is not enough space, return NULL. - */ -void *main_pool_alloc(u32 size, u32 side) { - struct MainPoolBlock *newListHead; - void *addr = NULL; - - size = ALIGN16(size) + 16; - if (size != 0 && sPoolFreeSpace >= size) { - sPoolFreeSpace -= size; - if (side == MEMORY_POOL_LEFT) { - newListHead = (struct MainPoolBlock *) ((u8 *) sPoolListHeadL + size); - sPoolListHeadL->next = newListHead; - newListHead->prev = sPoolListHeadL; - newListHead->next = NULL; - addr = (u8 *) sPoolListHeadL + 16; - sPoolListHeadL = newListHead; - } else { - newListHead = (struct MainPoolBlock *) ((u8 *) sPoolListHeadR - size); - sPoolListHeadR->prev = newListHead; - newListHead->next = sPoolListHeadR; - newListHead->prev = NULL; - sPoolListHeadR = newListHead; - addr = (u8 *) sPoolListHeadR + 16; - } - } - if (addr == NULL) { - LOG_ERROR("Main pool failed to allocate memory of size 0x%X on side %d.", size, side); - } - return addr; -} - -/** - * Free a block of memory that was allocated from the pool. The block must be - * the most recently allocated block from its end of the pool, otherwise all - * newer blocks are freed as well. - * Return the amount of free space left in the pool. - */ -u32 main_pool_free(void *addr) { - struct MainPoolBlock *block = (struct MainPoolBlock *) ((u8 *) addr - 16); - struct MainPoolBlock *oldListHead = (struct MainPoolBlock *) ((u8 *) addr - 16); - - if (oldListHead < sPoolListHeadL) { - while (oldListHead->next != NULL) { - oldListHead = oldListHead->next; - } - sPoolListHeadL = block; - sPoolListHeadL->next = NULL; - sPoolFreeSpace += (uintptr_t) oldListHead - (uintptr_t) sPoolListHeadL; - } else { - while (oldListHead->prev != NULL) { - oldListHead = oldListHead->prev; - } - sPoolListHeadR = block->next; - sPoolListHeadR->prev = NULL; - sPoolFreeSpace += (uintptr_t) sPoolListHeadR - (uintptr_t) oldListHead; - } - return sPoolFreeSpace; -} - -/** - * Resize a block of memory that was allocated from the left side of the pool. - * If the block is increasing in size, it must be the most recently allocated - * block from the left side. - * The block does not move. - */ -void *main_pool_realloc(void *addr, u32 size) { - void *newAddr = NULL; - struct MainPoolBlock *block = (struct MainPoolBlock *) ((u8 *) addr - 16); - - if (block->next == sPoolListHeadL) { - main_pool_free(addr); - newAddr = main_pool_alloc(size, MEMORY_POOL_LEFT); - } - if (addr == NULL) { - LOG_ERROR("Main pool failed to reallocate memory of size 0x%X at %p!", size, addr); - } - return newAddr; -} - -/** - * Return the size of the largest block that can currently be allocated from the - * pool. - */ -u32 main_pool_available(void) { - return sPoolFreeSpace - 16; -} - -/** - * Push pool state, to be restored later. Return the amount of free space left - * in the pool. - */ -u32 main_pool_push_state(void) { - struct MainPoolState *prevState = gMainPoolState; - u32 freeSpace = sPoolFreeSpace; - struct MainPoolBlock *lhead = sPoolListHeadL; - struct MainPoolBlock *rhead = sPoolListHeadR; - - gMainPoolState = main_pool_alloc(sizeof(*gMainPoolState), MEMORY_POOL_LEFT); - gMainPoolState->freeSpace = freeSpace; - gMainPoolState->listHeadL = lhead; - gMainPoolState->listHeadR = rhead; - gMainPoolState->prev = prevState; - return sPoolFreeSpace; -} - -/** - * Restore pool state from a previous call to main_pool_push_state. Return the - * amount of free space left in the pool. - */ -u32 main_pool_pop_state(void) { - if (!gMainPoolState) { return sPoolFreeSpace; } - sPoolFreeSpace = gMainPoolState->freeSpace; - sPoolListHeadL = gMainPoolState->listHeadL; - sPoolListHeadR = gMainPoolState->listHeadR; - gMainPoolState = gMainPoolState->prev; - return sPoolFreeSpace; -} - static struct MarioAnimDmaRelatedThing* func_802789F0(u8* srcAddr) { u32 count = 0; memcpy(&count, srcAddr, sizeof(u32)); diff --git a/src/game/memory.h b/src/game/memory.h index 8bb321f0..5086d3fb 100644 --- a/src/game/memory.h +++ b/src/game/memory.h @@ -49,14 +49,6 @@ void *segmented_to_virtual(const void *addr); void *virtual_to_segmented(u32 segment, const void *addr); void move_segment_table_to_dmem(void); -void main_pool_init(void *start, void *end); -void *main_pool_alloc(u32 size, u32 side); -u32 main_pool_free(void *addr); -void *main_pool_realloc(void *addr, u32 size); -u32 main_pool_available(void); -u32 main_pool_push_state(void); -u32 main_pool_pop_state(void); - #define load_segment(...) #define load_to_fixed_pool_addr(...) #define load_segment_decompress(...) diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index af0b3476..f08379ac 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -318,11 +318,6 @@ void main_func(void) { else if (gCLIOpts.FullScreen == 2) configWindow.fullscreen = false; - const size_t poolsize = gCLIOpts.PoolSize ? gCLIOpts.PoolSize : DEFAULT_POOL_SIZE; - u64 *pool = calloc(poolsize, 1); - if (!pool) sys_fatal("Could not alloc %u bytes for main pool.\n", poolsize); - main_pool_init(pool, pool + poolsize / sizeof(pool[0])); - #if defined(WAPI_SDL1) || defined(WAPI_SDL2) wm_api = &gfx_sdl; #elif defined(WAPI_DXGI)