memory/mempool: rename from objectpool
This commit is contained in:
parent
fc54f3f8ce
commit
05168ac2d7
13 changed files with 37 additions and 41 deletions
|
@ -54,7 +54,7 @@ TASK(boss_damage_to_power, { BoxedBoss boss; }) {
|
|||
}
|
||||
|
||||
Boss *create_boss(const char *name, char *ani, cmplx pos) {
|
||||
Boss *boss = objpool_acquire(&stage_object_pools.bosses);
|
||||
Boss *boss = mempool_acquire(&stage_object_pools.bosses);
|
||||
|
||||
boss->name = name;
|
||||
boss->pos = pos;
|
||||
|
@ -1364,7 +1364,7 @@ void free_boss(Boss *boss) {
|
|||
ent_unregister(&boss->ent);
|
||||
boss_set_portrait(boss, NULL, NULL, NULL);
|
||||
aniplayer_free(&boss->ani);
|
||||
objpool_release(&stage_object_pools.bosses, boss);
|
||||
mempool_release(&stage_object_pools.bosses, boss);
|
||||
}
|
||||
|
||||
static void boss_schedule_next_attack(Boss *b, Attack *a) {
|
||||
|
|
|
@ -87,7 +87,7 @@ Enemy *create_enemy_p(EnemyList *enemies, cmplx pos, float hp, EnemyVisual visua
|
|||
log_fatal("Tried to spawn an enemy while in drawing code");
|
||||
}
|
||||
|
||||
Enemy *e = alist_append(enemies, (Enemy*)objpool_acquire(&stage_object_pools.enemies));
|
||||
Enemy *e = alist_append(enemies, (Enemy*)mempool_acquire(&stage_object_pools.enemies));
|
||||
e->moving = false;
|
||||
e->dir = 0;
|
||||
e->birthtime = global.frames;
|
||||
|
@ -165,7 +165,7 @@ static void *_delete_enemy(ListAnchor *enemies, List* enemy, void *arg) {
|
|||
|
||||
COEVENT_CANCEL_ARRAY(e->events);
|
||||
ent_unregister(&e->ent);
|
||||
objpool_release(&stage_object_pools.enemies, alist_unlink(enemies, enemy));
|
||||
mempool_release(&stage_object_pools.enemies, alist_unlink(enemies, enemy));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -130,7 +130,7 @@ Item *create_item(cmplx pos, cmplx v, ItemType type) {
|
|||
type = ITEM_SURGE;
|
||||
}
|
||||
|
||||
Item *i = objpool_acquire(&stage_object_pools.items);
|
||||
Item *i = mempool_acquire(&stage_object_pools.items);
|
||||
alist_append(&global.items, i);
|
||||
|
||||
i->pos = pos;
|
||||
|
@ -150,7 +150,7 @@ Item *create_item(cmplx pos, cmplx v, ItemType type) {
|
|||
|
||||
void delete_item(Item *item) {
|
||||
ent_unregister(&item->ent);
|
||||
objpool_release(&stage_object_pools.items, alist_unlink(&global.items, item));
|
||||
mempool_release(&stage_object_pools.items, alist_unlink(&global.items, item));
|
||||
}
|
||||
|
||||
Item *create_clear_item(cmplx pos, uint clear_flags) {
|
||||
|
|
|
@ -55,7 +55,7 @@ void lasers_shutdown(void) {
|
|||
}
|
||||
|
||||
Laser *create_laser(cmplx pos, float time, float deathtime, const Color *color, LaserRule rule) {
|
||||
Laser *l = objpool_acquire(&stage_object_pools.lasers);
|
||||
Laser *l = mempool_acquire(&stage_object_pools.lasers);
|
||||
alist_push(&global.lasers, l);
|
||||
|
||||
l->birthtime = global.frames;
|
||||
|
@ -113,7 +113,7 @@ void laserline_set_posdir(Laser *l, cmplx pos, cmplx dir) {
|
|||
static void *_delete_laser(ListAnchor *lasers, List *laser, void *arg) {
|
||||
Laser *l = (Laser*)laser;
|
||||
ent_unregister(&l->ent);
|
||||
objpool_release(&stage_object_pools.lasers, alist_unlink(lasers, laser));
|
||||
mempool_release(&stage_object_pools.lasers, alist_unlink(lasers, laser));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,18 +6,16 @@
|
|||
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
||||
*/
|
||||
|
||||
#include "objectpool.h"
|
||||
#include "mempool.h"
|
||||
|
||||
#include "memory/arena.h"
|
||||
|
||||
void objpool_init(
|
||||
ObjectPool *pool,
|
||||
void mempool_init(
|
||||
MemPool *pool,
|
||||
const char *tag,
|
||||
MemArena *arena,
|
||||
size_t obj_size,
|
||||
size_t obj_align
|
||||
) {
|
||||
*pool = (ObjectPool) {
|
||||
*pool = (MemPool) {
|
||||
.arena = arena,
|
||||
.obj_size = obj_size,
|
||||
.obj_align = obj_align,
|
||||
|
@ -25,8 +23,8 @@ void objpool_init(
|
|||
};
|
||||
}
|
||||
|
||||
void *objpool_acquire(ObjectPool *pool) {
|
||||
ObjHeader *obj = pool->free_objects;
|
||||
void *mempool_acquire(MemPool *pool) {
|
||||
MemPoolObjectHeader *obj = pool->free_objects;
|
||||
|
||||
if(obj) {
|
||||
assert(pool->num_used < pool->num_allocated);
|
||||
|
@ -44,8 +42,8 @@ void *objpool_acquire(ObjectPool *pool) {
|
|||
return obj;
|
||||
}
|
||||
|
||||
void objpool_release(ObjectPool *pool, void *object) {
|
||||
ObjHeader *obj = object;
|
||||
void mempool_release(MemPool *pool, void *object) {
|
||||
MemPoolObjectHeader *obj = object;
|
||||
obj->next = pool->free_objects;
|
||||
pool->free_objects = obj;
|
||||
|
|
@ -9,18 +9,17 @@
|
|||
#pragma once
|
||||
#include "taisei.h"
|
||||
|
||||
#include "memory/arena.h"
|
||||
#include "arena.h"
|
||||
|
||||
typedef struct ObjectPool ObjectPool;
|
||||
typedef struct ObjectPoolStats ObjectPoolStats;
|
||||
typedef struct MemPool MemPool;
|
||||
|
||||
typedef struct ObjHeader {
|
||||
alignas(alignof(max_align_t)) struct ObjHeader *next;
|
||||
} ObjHeader;
|
||||
typedef struct MemPoolObjectHeader {
|
||||
alignas(alignof(max_align_t)) struct MemPoolObjectHeader *next;
|
||||
} MemPoolObjectHeader;
|
||||
|
||||
struct ObjectPool {
|
||||
struct MemPool {
|
||||
MemArena *arena;
|
||||
ObjHeader *free_objects;
|
||||
MemPoolObjectHeader *free_objects;
|
||||
const char *tag;
|
||||
size_t obj_size;
|
||||
size_t obj_align;
|
||||
|
@ -28,16 +27,16 @@ struct ObjectPool {
|
|||
int num_used;
|
||||
};
|
||||
|
||||
void objpool_init(
|
||||
ObjectPool *pool,
|
||||
void mempool_init(
|
||||
MemPool *pool,
|
||||
const char *tag,
|
||||
MemArena *arena,
|
||||
size_t obj_size,
|
||||
size_t obj_align
|
||||
) attr_nonnull(1, 2, 3);
|
||||
|
||||
void *objpool_acquire(ObjectPool *pool)
|
||||
void *mempool_acquire(MemPool *pool)
|
||||
attr_returns_allocated attr_hot attr_nonnull(1);
|
||||
|
||||
void objpool_release(ObjectPool *pool, void *object)
|
||||
void mempool_release(MemPool *pool, void *object)
|
||||
attr_hot attr_nonnull(1, 2);
|
|
@ -3,4 +3,5 @@ memory_src = files(
|
|||
'allocator.c',
|
||||
'arena.c',
|
||||
'memory.c',
|
||||
'mempool.c',
|
||||
)
|
||||
|
|
|
@ -77,7 +77,6 @@ taisei_src = files(
|
|||
'list.c',
|
||||
'log.c',
|
||||
'move.c',
|
||||
'objectpool.c',
|
||||
'player.c',
|
||||
'plrmodes.c',
|
||||
'portrait.c',
|
||||
|
|
|
@ -245,7 +245,7 @@ static Projectile* _create_projectile(ProjArgs *args) {
|
|||
log_fatal("Tried to spawn a projectile while in drawing code");
|
||||
}
|
||||
|
||||
Projectile *p = objpool_acquire(&stage_object_pools.projectiles);
|
||||
Projectile *p = mempool_acquire(&stage_object_pools.projectiles);
|
||||
|
||||
p->birthtime = global.frames;
|
||||
p->pos = p->pos0 = p->prevpos = args->pos;
|
||||
|
@ -326,7 +326,7 @@ static void delete_projectile(ProjectileList *projlist, Projectile *p, ProjColli
|
|||
signal_event_with_collision_result(p, &p->events.killed, col);
|
||||
COEVENT_CANCEL_ARRAY(p->events);
|
||||
ent_unregister(&p->ent);
|
||||
objpool_release(&stage_object_pools.projectiles, alist_unlink(projlist, p));
|
||||
mempool_release(&stage_object_pools.projectiles, alist_unlink(projlist, p));
|
||||
}
|
||||
|
||||
static void *foreach_delete_projectile(ListAnchor *projlist, List *proj, void *arg) {
|
||||
|
|
|
@ -1198,7 +1198,7 @@ static void stage_draw_hud_objpool_stats(float x, float y, float width) {
|
|||
y += lineskip * 1.5;
|
||||
|
||||
for(int i = 0; i < ARRAY_SIZE(*objpools); ++i) {
|
||||
ObjectPool *p = &(*objpools)[i];
|
||||
MemPool *p = &(*objpools)[i];
|
||||
|
||||
snprintf(buf, sizeof(buf), "%u | %7u", p->num_used, p->num_allocated);
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ void stage_objpools_init(void) {
|
|||
}
|
||||
|
||||
#define OBJECT_POOL(type, field) \
|
||||
objpool_init( \
|
||||
mempool_init( \
|
||||
&stage_object_pools.field, \
|
||||
#type, \
|
||||
&stgobjs.arena, \
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#pragma once
|
||||
#include "taisei.h"
|
||||
|
||||
#include "objectpool.h"
|
||||
#include "memory/mempool.h"
|
||||
#include "aniplayer.h" // IWYU pragma: export
|
||||
#include "projectile.h" // IWYU pragma: export
|
||||
#include "item.h" // IWYU pragma: export
|
||||
|
@ -28,7 +28,7 @@
|
|||
|
||||
typedef struct StageObjectPools {
|
||||
#define OBJECT_POOL(type, field) \
|
||||
ObjectPool field;
|
||||
MemPool field;
|
||||
|
||||
OBJECT_POOLS
|
||||
#undef OBJECT_POOL
|
||||
|
@ -37,7 +37,7 @@ typedef struct StageObjectPools {
|
|||
extern StageObjectPools stage_object_pools;
|
||||
|
||||
#define STAGE_OBJPOOLS_AS_ARRAYPTR \
|
||||
(ObjectPool (*)[sizeof(stage_object_pools) / sizeof(ObjectPool)])&stage_object_pools
|
||||
(MemPool (*)[sizeof(stage_object_pools) / sizeof(MemPool)])&stage_object_pools
|
||||
|
||||
// Can be called many times to reinitialize the pools while reusing allocated arena memory.
|
||||
void stage_objpools_init(void);
|
||||
|
|
|
@ -9,13 +9,12 @@
|
|||
#include "stagetext.h"
|
||||
|
||||
#include "global.h"
|
||||
#include "objectpool.h"
|
||||
#include "stageobjects.h"
|
||||
|
||||
static StageText *textlist = NULL;
|
||||
|
||||
StageText* stagetext_add(const char *text, cmplx pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime) {
|
||||
StageText *t = objpool_acquire(&stage_object_pools.stagetext);
|
||||
StageText *t = mempool_acquire(&stage_object_pools.stagetext);
|
||||
list_append(&textlist, t);
|
||||
|
||||
if(text != NULL) {
|
||||
|
@ -48,7 +47,7 @@ StageText* stagetext_add_numeric(int n, cmplx pos, Alignment align, Font *font,
|
|||
}
|
||||
|
||||
static void* stagetext_delete(List **dest, List *txt, void *arg) {
|
||||
objpool_release(&stage_object_pools.stagetext, list_unlink(dest, txt));
|
||||
mempool_release(&stage_object_pools.stagetext, list_unlink(dest, txt));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue