From c74ab070e60b3cdc1f8e3c43bcdb33c3490ca185 Mon Sep 17 00:00:00 2001 From: jaburns Date: Tue, 27 Oct 2020 13:26:58 -0600 Subject: [PATCH] Freeing display list allocations --- src/decomp/engine/geo_layout.c | 4 +- src/decomp/engine/graph_node.h | 2 +- src/decomp/game/mario.c | 4 +- src/decomp/game/rendering_graph_node.c | 4 +- src/decomp/memory.c | 59 ++++++++++++++++++++++++++ src/decomp/memory.h | 15 +++++++ src/decomp/shim.c | 35 +++------------ src/decomp/shim.h | 4 +- src/libsm64.c | 7 ++- src/load_anim_data.c | 37 +++++++++------- src/load_anim_data.h | 5 ++- src/memory.c | 30 ------------- src/memory.h | 10 ----- 13 files changed, 118 insertions(+), 98 deletions(-) create mode 100644 src/decomp/memory.c create mode 100644 src/decomp/memory.h delete mode 100644 src/memory.c delete mode 100644 src/memory.h diff --git a/src/decomp/engine/geo_layout.c b/src/decomp/engine/geo_layout.c index 7738c9f..ae40125 100644 --- a/src/decomp/engine/geo_layout.c +++ b/src/decomp/engine/geo_layout.c @@ -217,8 +217,8 @@ void geo_layout_cmd_node_root(void) { graphNode = init_graph_node_root(gGraphNodePool, NULL, 0, x, y, width, height); - // TODO: check type - gGeoViews = alloc_only_pool_alloc(gGraphNodePool, gGeoNumViews * sizeof(struct GraphNode *)); + // gGeoViews is unused in libsm64 + gGeoViews = NULL; // alloc_only_pool_alloc(gGraphNodePool, gGeoNumViews * sizeof(struct GraphNode *)); graphNode->views = gGeoViews; graphNode->numViews = gGeoNumViews; diff --git a/src/decomp/engine/graph_node.h b/src/decomp/engine/graph_node.h index c9f7fb1..925bbcc 100644 --- a/src/decomp/engine/graph_node.h +++ b/src/decomp/engine/graph_node.h @@ -5,7 +5,7 @@ #include "../include/PR/gbi.h" #include "../include/types.h" -#include "../../memory.h" +#include "../memory.h" #define GRAPH_RENDER_ACTIVE (1 << 0) #define GRAPH_RENDER_CHILDREN_FIRST (1 << 1) diff --git a/src/decomp/game/mario.c b/src/decomp/game/mario.c index 8dd197c..c72d286 100644 --- a/src/decomp/game/mario.c +++ b/src/decomp/game/mario.c @@ -67,7 +67,7 @@ s32 is_anim_past_end(struct MarioState *m) { s16 set_mario_animation(struct MarioState *m, s32 targetAnimID) { struct Object *o = m->marioObj; - hack_load_mario_animation_ex(m->animation, targetAnimID); + shim_load_mario_animation(m->animation, targetAnimID); struct Animation *targetAnim = m->animation->targetAnim; //if (load_patchable_table(m->animation, targetAnimID)) { // targetAnim->values = (void *) VIRTUAL_TO_PHYSICAL((u8 *) targetAnim + (uintptr_t) targetAnim->values); @@ -101,7 +101,7 @@ s16 set_mario_animation(struct MarioState *m, s32 targetAnimID) { s16 set_mario_anim_with_accel(struct MarioState *m, s32 targetAnimID, s32 accel) { struct Object *o = m->marioObj; - hack_load_mario_animation_ex(m->animation, targetAnimID); + shim_load_mario_animation(m->animation, targetAnimID); struct Animation *targetAnim = m->animation->targetAnim; //if (load_patchable_table(m->animation, targetAnimID)) { // targetAnim->values = (void *) VIRTUAL_TO_PHYSICAL((u8 *) targetAnim + (uintptr_t) targetAnim->values); diff --git a/src/decomp/game/rendering_graph_node.c b/src/decomp/game/rendering_graph_node.c index 09fd73d..dd0b7f1 100644 --- a/src/decomp/game/rendering_graph_node.c +++ b/src/decomp/game/rendering_graph_node.c @@ -1125,6 +1125,8 @@ void geo_process_root_hack_single_node(struct GraphNode *node) { gDisplayListHead = NULL; // Currently unused, but referenced + display_list_pool_reset(); + Mtx *initialMatrix; gDisplayListHeap = alloc_only_pool_init(); @@ -1162,5 +1164,5 @@ void geo_process_root_hack_single_node(struct GraphNode *node) gMarioObject->header.gfx.throwMatrix = NULL; - main_pool_free(gDisplayListHeap); + alloc_only_pool_free(gDisplayListHeap); } \ No newline at end of file diff --git a/src/decomp/memory.c b/src/decomp/memory.c new file mode 100644 index 0000000..270e96e --- /dev/null +++ b/src/decomp/memory.c @@ -0,0 +1,59 @@ +#include +#include + +#include "memory.h" + +// TODO don't handle every individual allocation with malloc, it sucks on windows + +struct AllocOnlyPool +{ + size_t allocatedCount; + void **allocatedBlocks; +}; + +struct AllocOnlyPool *s_display_list_pool; + +void memory_init(void) +{ + s_display_list_pool = alloc_only_pool_init(); +} + +void memory_terminate(void) +{ + alloc_only_pool_free( s_display_list_pool ); +} + +struct AllocOnlyPool *alloc_only_pool_init(void) +{ + struct AllocOnlyPool *newPool = malloc( sizeof( struct AllocOnlyPool )); + newPool->allocatedCount = 0; + newPool->allocatedBlocks = NULL; + return newPool; +} + +void *alloc_only_pool_alloc(struct AllocOnlyPool *pool, s32 size) +{ + pool->allocatedCount++; + pool->allocatedBlocks = realloc( pool->allocatedBlocks, pool->allocatedCount * sizeof( void * )); + pool->allocatedBlocks[ pool->allocatedCount - 1 ] = malloc( size ); + return pool->allocatedBlocks[ pool->allocatedCount - 1 ]; +} + +void alloc_only_pool_free(struct AllocOnlyPool *pool) +{ + for( int i = 0; i < pool->allocatedCount; ++i ) + free( pool->allocatedBlocks[i] ); + free( pool->allocatedBlocks ); + free( pool ); +} + +void display_list_pool_reset(void) +{ + alloc_only_pool_free( s_display_list_pool ); + s_display_list_pool = alloc_only_pool_init(); +} + +void *alloc_display_list(u32 size) +{ + return alloc_only_pool_alloc( s_display_list_pool, (s32)size ); +} \ No newline at end of file diff --git a/src/decomp/memory.h b/src/decomp/memory.h new file mode 100644 index 0000000..161624a --- /dev/null +++ b/src/decomp/memory.h @@ -0,0 +1,15 @@ +#pragma once + +#include "include/types.h" + +struct AllocOnlyPool; + +extern void memory_init(void); +extern void memory_terminate(void); + +extern struct AllocOnlyPool *alloc_only_pool_init(void); +extern void *alloc_only_pool_alloc(struct AllocOnlyPool *pool, s32 size); +extern void alloc_only_pool_free(struct AllocOnlyPool *pool); + +extern void display_list_pool_reset(void); +extern void *alloc_display_list(u32 size); \ No newline at end of file diff --git a/src/decomp/shim.c b/src/decomp/shim.c index 6810309..caea805 100644 --- a/src/decomp/shim.c +++ b/src/decomp/shim.c @@ -22,37 +22,12 @@ struct MarioState gMarioStateVal; struct MarioState *gMarioState = &gMarioStateVal; SM64DebugPrintFunctionPtr gDebugPrint = NULL; -void hack_load_mario_animation_ex(struct MarioAnimation *a, u32 index) +void shim_load_mario_animation(struct MarioAnimation *a, u32 index) { - if ((u32)a->currentAnimAddr == 1 + index) - return; - - a->currentAnimAddr = (u8*)(1 + index); - a->targetAnim = &gLibSm64MarioAnimations[index]; -} - -void hack_load_mario_animation(struct MarioAnimation *a, u32 index) -{ -// struct MarioAnimDmaRelatedThing *sp20 = a->animDmaTable; -// u8 *addr; -// u32 size; -// -// if (index < sp20->count) { -// addr = sp20->srcAddr + sp20->anim[index].offset; -// size = sp20->anim[index].size; -// -// if (a->currentAnimAddr != addr) { -// u32 a0 = sp20->anim[index].offset; -// u32 b0 = sp20->anim[index].size; -// -// memcpy( (u8*)a->targetAnim, (u8*)sp20 + a0, b0 ); -// a->currentAnimAddr = addr; -// -// struct Animation *targetAnim = a->targetAnim; -// targetAnim->values =(void*)( (u8 *)targetAnim + (uintptr_t)targetAnim->values ); -// targetAnim->index = (void*)( (u8 *)targetAnim + (uintptr_t)targetAnim->index ); -// } -// } + if ((u32)a->currentAnimAddr != 1 + index) { + a->currentAnimAddr = (u8*)(1 + index); + a->targetAnim = &g_libsm64_mario_animations[index]; + } } void *segmented_to_virtual(const void *addr) diff --git a/src/decomp/shim.h b/src/decomp/shim.h index 194da18..a776f60 100644 --- a/src/decomp/shim.h +++ b/src/decomp/shim.h @@ -69,8 +69,8 @@ extern SM64DebugPrintFunctionPtr gDebugPrint; } \ } while(0) -extern void hack_load_mario_animation_ex(struct MarioAnimation *a, u32 index); -extern void hack_load_mario_animation(struct MarioAnimation *a, u32 index); +extern void shim_load_mario_animation(struct MarioAnimation *a, u32 index); + extern void *segmented_to_virtual(const void *addr); extern void *virtual_to_segmented(u32 segment, const void *addr); extern void func_80320A4C(u8 bankIndex, u8 arg1); diff --git a/src/libsm64.c b/src/libsm64.c index 53a812d..0981a13 100644 --- a/src/libsm64.c +++ b/src/libsm64.c @@ -10,6 +10,7 @@ #include "decomp/engine/math_util.h" #include "decomp/include/sm64.h" #include "decomp/shim.h" +#include "decomp/memory.h" #include "decomp/game/mario.h" #include "decomp/game/object_stuff.h" #include "decomp/engine/surface_collision.h" @@ -87,6 +88,8 @@ SM64_LIB_FN void sm64_global_init( uint8_t *rom, uint8_t *outTexture, SM64DebugP load_mario_textures_from_rom( rom, outTexture ); load_mario_anims_from_rom( rom ); + memory_init(); + gMarioObject = hack_allocate_mario(); gCurrentArea = hack_build_area(); gCurrentObject = gMarioObject; @@ -176,9 +179,9 @@ SM64_LIB_FN void sm64_mario_tick( const struct SM64MarioInputs *inputs, struct S SM64_LIB_FN void sm64_global_terminate( void ) { - // TODO free other things - surfaces_unload_all(); + unload_mario_anims(); + memory_terminate(); } SM64_LIB_FN uint32_t sm64_load_surface_object( const struct SM64SurfaceObject *surfaceObject ) diff --git a/src/load_anim_data.c b/src/load_anim_data.c index 9db93a9..08c0c95 100644 --- a/src/load_anim_data.c +++ b/src/load_anim_data.c @@ -2,8 +2,7 @@ #include -struct Animation *gLibSm64MarioAnimations; -static struct Animation *s_marioAnimations; +struct Animation *g_libsm64_mario_animations; #define ANIM_DATA_ADDRESS 0x004EC000 @@ -37,18 +36,19 @@ void load_mario_anims_from_rom( uint8_t *rom ) uint8_t *read_ptr = rom + ANIM_DATA_ADDRESS; uint32_t num_entries = read_u32_be( read_ptr ); - s_marioAnimations = malloc( num_entries * sizeof( struct Animation )); + g_libsm64_mario_animations = malloc( num_entries * sizeof( struct Animation )); + struct Animation *anims = g_libsm64_mario_animations; for( int i = 0; i < num_entries; ++i ) { read_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i); - s_marioAnimations[i].flags = read_s16_be( read_ptr ); read_ptr += 2; - s_marioAnimations[i].animYTransDivisor = read_s16_be( read_ptr ); read_ptr += 2; - s_marioAnimations[i].startFrame = read_s16_be( read_ptr ); read_ptr += 2; - s_marioAnimations[i].loopStart = read_s16_be( read_ptr ); read_ptr += 2; - s_marioAnimations[i].loopEnd = read_s16_be( read_ptr ); read_ptr += 2; - s_marioAnimations[i].unusedBoneCount = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].flags = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].animYTransDivisor = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].startFrame = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].loopStart = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].loopEnd = read_s16_be( read_ptr ); read_ptr += 2; + anims[i].unusedBoneCount = read_s16_be( read_ptr ); read_ptr += 2; uint32_t values_offset = read_u32_be( read_ptr ); read_ptr += 4; uint32_t index_offset = read_u32_be( read_ptr ); read_ptr += 4; uint32_t end_offset = read_u32_be( read_ptr ); @@ -57,26 +57,31 @@ void load_mario_anims_from_rom( uint8_t *rom ) uint8_t *values_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i) + values_offset; uint8_t *end_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i) + end_offset; - s_marioAnimations[i].index = malloc( values_offset - index_offset ); - s_marioAnimations[i].values = malloc( end_offset - values_offset ); + anims[i].index = malloc( values_offset - index_offset ); + anims[i].values = malloc( end_offset - values_offset ); int j = 0; while( read_ptr < values_ptr ) { - s_marioAnimations[i].index[j++] = read_u16_be( read_ptr ); + anims[i].index[j++] = read_u16_be( read_ptr ); read_ptr += 2; } j = 0; while( read_ptr < end_ptr ) { - s_marioAnimations[i].values[j++] = read_u16_be( read_ptr ); + anims[i].values[j++] = read_u16_be( read_ptr ); read_ptr += 2; } } - gLibSm64MarioAnimations = s_marioAnimations; - - #undef GET_OFFSET + #undef GET_OFFSETg_libsm64_mario_animations #undef GET_SIZE +} + +void unload_mario_anims( void ) +{ + // TODO free index and values arrays + free( g_libsm64_mario_animations ); + g_libsm64_mario_animations = NULL; } \ No newline at end of file diff --git a/src/load_anim_data.h b/src/load_anim_data.h index d0d58a6..ec40c57 100644 --- a/src/load_anim_data.h +++ b/src/load_anim_data.h @@ -5,6 +5,7 @@ #include "decomp/include/types.h" -extern struct Animation *gLibSm64MarioAnimations; +extern struct Animation *g_libsm64_mario_animations; -extern void load_mario_anims_from_rom( uint8_t *rom ); \ No newline at end of file +extern void load_mario_anims_from_rom( uint8_t *rom ); +extern void unload_mario_anims( void ); \ No newline at end of file diff --git a/src/memory.c b/src/memory.c deleted file mode 100644 index a9d8ff7..0000000 --- a/src/memory.c +++ /dev/null @@ -1,30 +0,0 @@ -#include - -#include "memory.h" - -struct AllocOnlyPool -{ - int id; -}; - -struct AllocOnlyPool *alloc_only_pool_init(void) -{ - struct AllocOnlyPool *result = malloc( sizeof( struct AllocOnlyPool )); - result->id = 0x69; // value doesn't matter - return result; -} - -void *alloc_only_pool_alloc(struct AllocOnlyPool *pool, s32 size) -{ - return malloc(size); -} - -void *alloc_display_list(u32 size) -{ - return malloc(size); -} - -void main_pool_free(struct AllocOnlyPool *pool) -{ - // TODO lol -} \ No newline at end of file diff --git a/src/memory.h b/src/memory.h deleted file mode 100644 index 3ed8b0f..0000000 --- a/src/memory.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "decomp/include/types.h" - -struct AllocOnlyPool; - -extern struct AllocOnlyPool *alloc_only_pool_init(void); -extern void *alloc_only_pool_alloc(struct AllocOnlyPool *pool, s32 size); -extern void *alloc_display_list(u32 size); -extern void main_pool_free(struct AllocOnlyPool *pool); \ No newline at end of file