Replace Malloc With Pools
This commit introduces an object pool and replaces instances of malloc with pools where sensible.
This commit is contained in:
+19
-20
@@ -3,6 +3,7 @@
|
||||
#include "util.h"
|
||||
#include "audio_utils.h"
|
||||
#include "soundbank.h"
|
||||
#include "pool.h"
|
||||
|
||||
#include <tonc.h>
|
||||
#include <stdlib.h>
|
||||
@@ -15,27 +16,22 @@
|
||||
OBJ_ATTR obj_buffer[MAX_SPRITES];
|
||||
OBJ_AFFINE *obj_aff_buffer = (OBJ_AFFINE*)obj_buffer;
|
||||
|
||||
static Sprite *free_sprites[MAX_SPRITES] = {NULL};
|
||||
DECLARE_POOL_TYPE(SpriteObject)
|
||||
DEFINE_POOL_TYPE(SpriteObject, MAX_SPRITES)
|
||||
|
||||
DECLARE_POOL_TYPE(Sprite)
|
||||
DEFINE_POOL_TYPE(Sprite, MAX_SPRITES)
|
||||
|
||||
static bool free_affines[MAX_AFFINES] = {false};
|
||||
|
||||
// Sprite methods
|
||||
Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index)
|
||||
{
|
||||
Sprite *sprite = malloc(sizeof(Sprite));
|
||||
Sprite* sprite = POOL_GET(Sprite);
|
||||
|
||||
sprite->obj = NULL;
|
||||
sprite->aff = NULL;
|
||||
|
||||
if(!free_sprites[sprite_index])
|
||||
{
|
||||
free_sprites[sprite_index] = sprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
free(sprite);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a0 & ATTR0_AFF)
|
||||
{
|
||||
int aff_index = MAX_AFFINES;
|
||||
@@ -52,7 +48,6 @@ Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index)
|
||||
|
||||
if (aff_index == MAX_AFFINES)
|
||||
{
|
||||
free(sprite);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -62,26 +57,30 @@ Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index)
|
||||
sprite->aff = &obj_aff_buffer[aff_index];
|
||||
obj_set_attr(sprite->obj, a0, a1, ATTR2_PALBANK(pb) | tid);
|
||||
obj_aff_identity(&obj_aff_buffer[aff_index]);
|
||||
return sprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite->obj = &obj_buffer[sprite_index];
|
||||
obj_set_attr(sprite->obj, a0, a1, ATTR2_PALBANK(pb) | tid);
|
||||
return sprite;
|
||||
}
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
|
||||
void sprite_destroy(Sprite **sprite)
|
||||
{
|
||||
if (*sprite == NULL) return;
|
||||
|
||||
obj_hide((*sprite)->obj);
|
||||
free_sprites[(*sprite)->obj - obj_buffer] = NULL;
|
||||
|
||||
if ((*sprite)->aff != NULL)
|
||||
{
|
||||
free_affines[(*sprite)->aff - obj_aff_buffer] = false;
|
||||
}
|
||||
free(*sprite);
|
||||
|
||||
POOL_FREE(Sprite, *sprite);
|
||||
|
||||
*sprite = NULL;
|
||||
}
|
||||
|
||||
@@ -115,7 +114,7 @@ int sprite_get_pb(const Sprite *sprite)
|
||||
// SpriteObject methods
|
||||
SpriteObject* sprite_object_new()
|
||||
{
|
||||
SpriteObject* sprite_object = (SpriteObject*)malloc(sizeof(SpriteObject));
|
||||
SpriteObject* sprite_object = POOL_GET(SpriteObject);
|
||||
sprite_object->sprite = NULL;
|
||||
sprite_object_reset_transform(sprite_object);
|
||||
sprite_object->selected = false;
|
||||
@@ -127,8 +126,8 @@ SpriteObject* sprite_object_new()
|
||||
void sprite_object_destroy(SpriteObject** sprite_object)
|
||||
{
|
||||
if (*sprite_object == NULL) return;
|
||||
sprite_destroy(&((*sprite_object)->sprite));
|
||||
free(*sprite_object);
|
||||
sprite_destroy(&(*sprite_object)->sprite);
|
||||
POOL_FREE(SpriteObject, *sprite_object);
|
||||
*sprite_object = NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user