Freeing display list allocations

This commit is contained in:
jaburns 2020-10-27 13:26:58 -06:00
parent 71e918a01e
commit c74ab070e6
13 changed files with 118 additions and 98 deletions

View file

@ -217,8 +217,8 @@ void geo_layout_cmd_node_root(void) {
graphNode = init_graph_node_root(gGraphNodePool, NULL, 0, x, y, width, height); graphNode = init_graph_node_root(gGraphNodePool, NULL, 0, x, y, width, height);
// TODO: check type // gGeoViews is unused in libsm64
gGeoViews = alloc_only_pool_alloc(gGraphNodePool, gGeoNumViews * sizeof(struct GraphNode *)); gGeoViews = NULL; // alloc_only_pool_alloc(gGraphNodePool, gGeoNumViews * sizeof(struct GraphNode *));
graphNode->views = gGeoViews; graphNode->views = gGeoViews;
graphNode->numViews = gGeoNumViews; graphNode->numViews = gGeoNumViews;

View file

@ -5,7 +5,7 @@
#include "../include/PR/gbi.h" #include "../include/PR/gbi.h"
#include "../include/types.h" #include "../include/types.h"
#include "../../memory.h" #include "../memory.h"
#define GRAPH_RENDER_ACTIVE (1 << 0) #define GRAPH_RENDER_ACTIVE (1 << 0)
#define GRAPH_RENDER_CHILDREN_FIRST (1 << 1) #define GRAPH_RENDER_CHILDREN_FIRST (1 << 1)

View file

@ -67,7 +67,7 @@ s32 is_anim_past_end(struct MarioState *m) {
s16 set_mario_animation(struct MarioState *m, s32 targetAnimID) { s16 set_mario_animation(struct MarioState *m, s32 targetAnimID) {
struct Object *o = m->marioObj; 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; struct Animation *targetAnim = m->animation->targetAnim;
//if (load_patchable_table(m->animation, targetAnimID)) { //if (load_patchable_table(m->animation, targetAnimID)) {
// targetAnim->values = (void *) VIRTUAL_TO_PHYSICAL((u8 *) targetAnim + (uintptr_t) targetAnim->values); // 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) { s16 set_mario_anim_with_accel(struct MarioState *m, s32 targetAnimID, s32 accel) {
struct Object *o = m->marioObj; 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; struct Animation *targetAnim = m->animation->targetAnim;
//if (load_patchable_table(m->animation, targetAnimID)) { //if (load_patchable_table(m->animation, targetAnimID)) {
// targetAnim->values = (void *) VIRTUAL_TO_PHYSICAL((u8 *) targetAnim + (uintptr_t) targetAnim->values); // targetAnim->values = (void *) VIRTUAL_TO_PHYSICAL((u8 *) targetAnim + (uintptr_t) targetAnim->values);

View file

@ -1125,6 +1125,8 @@ void geo_process_root_hack_single_node(struct GraphNode *node)
{ {
gDisplayListHead = NULL; // Currently unused, but referenced gDisplayListHead = NULL; // Currently unused, but referenced
display_list_pool_reset();
Mtx *initialMatrix; Mtx *initialMatrix;
gDisplayListHeap = alloc_only_pool_init(); gDisplayListHeap = alloc_only_pool_init();
@ -1162,5 +1164,5 @@ void geo_process_root_hack_single_node(struct GraphNode *node)
gMarioObject->header.gfx.throwMatrix = NULL; gMarioObject->header.gfx.throwMatrix = NULL;
main_pool_free(gDisplayListHeap); alloc_only_pool_free(gDisplayListHeap);
} }

59
src/decomp/memory.c Normal file
View file

@ -0,0 +1,59 @@
#include <stdlib.h>
#include <stdbool.h>
#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 );
}

15
src/decomp/memory.h Normal file
View file

@ -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);

View file

@ -22,37 +22,12 @@ struct MarioState gMarioStateVal;
struct MarioState *gMarioState = &gMarioStateVal; struct MarioState *gMarioState = &gMarioStateVal;
SM64DebugPrintFunctionPtr gDebugPrint = NULL; 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) if ((u32)a->currentAnimAddr != 1 + index) {
return; a->currentAnimAddr = (u8*)(1 + index);
a->targetAnim = &g_libsm64_mario_animations[index];
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 );
// }
// }
} }
void *segmented_to_virtual(const void *addr) void *segmented_to_virtual(const void *addr)

View file

@ -69,8 +69,8 @@ extern SM64DebugPrintFunctionPtr gDebugPrint;
} \ } \
} while(0) } while(0)
extern void hack_load_mario_animation_ex(struct MarioAnimation *a, u32 index); extern void shim_load_mario_animation(struct MarioAnimation *a, u32 index);
extern void hack_load_mario_animation(struct MarioAnimation *a, u32 index);
extern void *segmented_to_virtual(const void *addr); extern void *segmented_to_virtual(const void *addr);
extern void *virtual_to_segmented(u32 segment, const void *addr); extern void *virtual_to_segmented(u32 segment, const void *addr);
extern void func_80320A4C(u8 bankIndex, u8 arg1); extern void func_80320A4C(u8 bankIndex, u8 arg1);

View file

@ -10,6 +10,7 @@
#include "decomp/engine/math_util.h" #include "decomp/engine/math_util.h"
#include "decomp/include/sm64.h" #include "decomp/include/sm64.h"
#include "decomp/shim.h" #include "decomp/shim.h"
#include "decomp/memory.h"
#include "decomp/game/mario.h" #include "decomp/game/mario.h"
#include "decomp/game/object_stuff.h" #include "decomp/game/object_stuff.h"
#include "decomp/engine/surface_collision.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_textures_from_rom( rom, outTexture );
load_mario_anims_from_rom( rom ); load_mario_anims_from_rom( rom );
memory_init();
gMarioObject = hack_allocate_mario(); gMarioObject = hack_allocate_mario();
gCurrentArea = hack_build_area(); gCurrentArea = hack_build_area();
gCurrentObject = gMarioObject; 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 ) SM64_LIB_FN void sm64_global_terminate( void )
{ {
// TODO free other things
surfaces_unload_all(); surfaces_unload_all();
unload_mario_anims();
memory_terminate();
} }
SM64_LIB_FN uint32_t sm64_load_surface_object( const struct SM64SurfaceObject *surfaceObject ) SM64_LIB_FN uint32_t sm64_load_surface_object( const struct SM64SurfaceObject *surfaceObject )

View file

@ -2,8 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
struct Animation *gLibSm64MarioAnimations; struct Animation *g_libsm64_mario_animations;
static struct Animation *s_marioAnimations;
#define ANIM_DATA_ADDRESS 0x004EC000 #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; uint8_t *read_ptr = rom + ANIM_DATA_ADDRESS;
uint32_t num_entries = read_u32_be( read_ptr ); 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 ) for( int i = 0; i < num_entries; ++i )
{ {
read_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i); read_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i);
s_marioAnimations[i].flags = read_s16_be( read_ptr ); read_ptr += 2; anims[i].flags = read_s16_be( read_ptr ); read_ptr += 2;
s_marioAnimations[i].animYTransDivisor = read_s16_be( read_ptr ); read_ptr += 2; anims[i].animYTransDivisor = read_s16_be( read_ptr ); read_ptr += 2;
s_marioAnimations[i].startFrame = read_s16_be( read_ptr ); read_ptr += 2; anims[i].startFrame = read_s16_be( read_ptr ); read_ptr += 2;
s_marioAnimations[i].loopStart = read_s16_be( read_ptr ); read_ptr += 2; anims[i].loopStart = read_s16_be( read_ptr ); read_ptr += 2;
s_marioAnimations[i].loopEnd = read_s16_be( read_ptr ); read_ptr += 2; anims[i].loopEnd = read_s16_be( read_ptr ); read_ptr += 2;
s_marioAnimations[i].unusedBoneCount = 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 values_offset = read_u32_be( read_ptr ); read_ptr += 4;
uint32_t index_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 ); 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 *values_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i) + values_offset;
uint8_t *end_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i) + end_offset; uint8_t *end_ptr = rom + ANIM_DATA_ADDRESS + GET_OFFSET(i) + end_offset;
s_marioAnimations[i].index = malloc( values_offset - index_offset ); anims[i].index = malloc( values_offset - index_offset );
s_marioAnimations[i].values = malloc( end_offset - values_offset ); anims[i].values = malloc( end_offset - values_offset );
int j = 0; int j = 0;
while( read_ptr < values_ptr ) 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; read_ptr += 2;
} }
j = 0; j = 0;
while( read_ptr < end_ptr ) 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; read_ptr += 2;
} }
} }
gLibSm64MarioAnimations = s_marioAnimations; #undef GET_OFFSETg_libsm64_mario_animations
#undef GET_OFFSET
#undef GET_SIZE #undef GET_SIZE
}
void unload_mario_anims( void )
{
// TODO free index and values arrays
free( g_libsm64_mario_animations );
g_libsm64_mario_animations = NULL;
} }

View file

@ -5,6 +5,7 @@
#include "decomp/include/types.h" #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 ); extern void load_mario_anims_from_rom( uint8_t *rom );
extern void unload_mario_anims( void );

View file

@ -1,30 +0,0 @@
#include <stdlib.h>
#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
}

View file

@ -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);