Fix surface_load memory leaks and improve performance (#38)

This commit is contained in:
PeachyPeach 2024-05-14 01:08:51 +02:00 committed by GitHub
parent 14cbb673fc
commit a6c1b2a3d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 22 deletions

View file

@ -31,18 +31,15 @@ SpatialPartitionCell gDynamicSurfacePartition[NUM_CELLS][NUM_CELLS];
/** /**
* Pools of data to contain either surface nodes or surfaces. * Pools of data to contain either surface nodes or surfaces.
*/ */
struct GrowingPool* sSurfaceNodePool = NULL; static struct GrowingArray *sSurfaceNodePool = NULL;
struct GrowingPool* sSurfacePool = NULL; static struct GrowingArray *sSurfacePool = NULL;
/** /**
* Allocate the part of the surface node pool to contain a surface node. * Allocate the part of the surface node pool to contain a surface node.
*/ */
static struct SurfaceNode *alloc_surface_node(void) { static struct SurfaceNode *alloc_surface_node(void) {
struct SurfaceNode* node = (struct SurfaceNode*)growing_pool_alloc(sSurfaceNodePool, sizeof(struct SurfaceNode)); sSurfaceNodePool->count = gSurfaceNodesAllocated++;
node->next = NULL; return growing_array_alloc(sSurfaceNodePool, sizeof(struct SurfaceNode));
gSurfaceNodesAllocated++;
return node;
} }
/** /**
@ -50,17 +47,8 @@ static struct SurfaceNode* alloc_surface_node(void) {
* initialize the surface. * initialize the surface.
*/ */
static struct Surface *alloc_surface(void) { static struct Surface *alloc_surface(void) {
struct Surface* surface = (struct Surface*)growing_pool_alloc(sSurfacePool, sizeof(struct Surface)); sSurfacePool->count = gSurfacesAllocated++;
return growing_array_alloc(sSurfacePool, sizeof(struct Surface));
surface->type = 0;
surface->force = 0;
surface->flags = 0;
surface->room = 0;
surface->object = NULL;
gSurfacesAllocated++;
return surface;
} }
/** /**
@ -481,8 +469,8 @@ void alloc_surface_pools(void) {
clear_static_surfaces(); clear_static_surfaces();
clear_dynamic_surfaces(); clear_dynamic_surfaces();
sSurfaceNodePool = growing_pool_init(sSurfaceNodePool, sizeof(struct SurfaceNode) * 1000); sSurfaceNodePool = growing_array_init(sSurfaceNodePool, 0x1000);
sSurfacePool = growing_pool_init(sSurfacePool, sizeof(struct Surface) * 1000); sSurfacePool = growing_array_init(sSurfacePool, 0x400);
gEnvironmentRegions = NULL; gEnvironmentRegions = NULL;
gSurfaceNodesAllocated = 0; gSurfaceNodesAllocated = 0;

View file

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include "memory.h" #include "memory.h"
#include "print.h"
#include "pc/debuglog.h" #include "pc/debuglog.h"
#define ALIGN16(val) (((val) + 0xF) & ~0xF) #define ALIGN16(val) (((val) + 0xF) & ~0xF)
@ -180,6 +181,63 @@ void growing_pool_free_pool(struct GrowingPool *pool) {
free(pool); free(pool);
} }
///////////////////
// growing array //
///////////////////
struct GrowingArray *growing_array_init(struct GrowingArray *array, u32 capacity) {
growing_array_free(&array);
array = calloc(1, sizeof(struct GrowingArray));
array->buffer = calloc(capacity, sizeof(void *));
array->capacity = capacity;
array->count = 0;
return array;
}
void *growing_array_alloc(struct GrowingArray *array, u32 size) {
if (array && array->buffer) {
// Increase capacity if needed
while (array->count >= array->capacity) {
u32 newCapacity = array->capacity * 2;
void **newBuffer = calloc(newCapacity, sizeof(void *));
memcpy(newBuffer, array->buffer, array->capacity * sizeof(void *));
free(array->buffer);
array->buffer = newBuffer;
array->capacity = newCapacity;
}
// Alloc element if needed
void **elem = &array->buffer[array->count++];
if (!*elem) {
*elem = malloc(size);
}
memset(*elem, 0, size);
return *elem;
}
return NULL;
}
void growing_array_free(struct GrowingArray **array) {
if (*array) {
for (u32 i = 0; i != (*array)->capacity; ++i) {
if ((*array)->buffer[i]) {
free((*array)->buffer[i]);
}
}
free((*array)->buffer);
free(*array);
*array = NULL;
}
}
void growing_array_debug_print(struct GrowingArray *array, const char *name, s32 x, s32 y) {
char text[256];
u32 allocated = 0; for (u32 i = 0; i != array->capacity; ++i) { allocated += (array->buffer[i] != NULL); }
snprintf(text, 256, "%-12s %5u/%5u/%5u", name, array->count, allocated, array->capacity);
print_text(x, y, text);
}
/////////////////// ///////////////////
// display lists // // display lists //
/////////////////// ///////////////////

View file

@ -39,6 +39,13 @@ struct GrowingPoolNode
struct GrowingPoolNode* prev; struct GrowingPoolNode* prev;
}; };
struct GrowingArray
{
void **buffer;
u32 count;
u32 capacity;
};
struct MarioAnimation; struct MarioAnimation;
struct Animation; struct Animation;
@ -63,6 +70,11 @@ struct GrowingPool* growing_pool_init(struct GrowingPool* pool, u32 nodeSize);
void* growing_pool_alloc(struct GrowingPool *pool, u32 size); void* growing_pool_alloc(struct GrowingPool *pool, u32 size);
void growing_pool_free_pool(struct GrowingPool *pool); void growing_pool_free_pool(struct GrowingPool *pool);
struct GrowingArray *growing_array_init(struct GrowingArray *array, u32 capacity);
void *growing_array_alloc(struct GrowingArray *array, u32 size);
void growing_array_free(struct GrowingArray **array);
void growing_array_debug_print(struct GrowingArray *array, const char *name, s32 x, s32 y);
void alloc_display_list_reset(void); void alloc_display_list_reset(void);
void *alloc_display_list(u32 size); void *alloc_display_list(u32 size);