Improve and fix up mempool

Changelog:
- Fix incorrect queue size init in mempool
- Replace map with boolean array for use list which is much simpler
- Remove unused component mempool
scene_man
En Yi 2022-12-30 17:51:05 +08:00
parent f7057bd56c
commit 5b3da5c94f
1 changed files with 22 additions and 25 deletions

View File

@ -1,12 +1,11 @@
#include "mempool.h"
#include "sc/queue/sc_queue.h"
#include "sc/map/sc_map.h"
#include <stdlib.h>
// Static allocate buffers
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
//static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE];
static CJump_t cjump_buffer[1]; // Only player is expected to have this
static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this
@ -21,20 +20,19 @@ typedef struct MemPool
void * const buffer;
const unsigned long max_size;
const unsigned long elem_size;
struct sc_map_64 use_list;
bool *use_list;
struct sc_queue_uint free_list;
}MemPool_t;
// Static allocate mempools
static MemPool_t comp_mempools[N_COMPONENTS] =
{
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), {0}, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), {0}, {0}},
//{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), {0}, {0}},
{cjump_buffer, 1, sizeof(CJump_t), {0}, {0}},
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), {0}, {0}},
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}},
{cjump_buffer, 1, sizeof(CJump_t), NULL, {0}},
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}},
};
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), {0}, {0}};
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}};
static bool pool_inited = false;
void init_memory_pools(void)
@ -44,9 +42,10 @@ void init_memory_pools(void)
for (size_t i=0; i<N_COMPONENTS; ++i)
{
memset(comp_mempools[i].buffer, 0, comp_mempools[i].elem_size*comp_mempools[i].max_size);
sc_map_init_64(&comp_mempools[i].use_list, MAX_COMP_POOL_SIZE ,0);
comp_mempools[i].use_list = (bool *)calloc(comp_mempools[i].max_size, sizeof(bool));
assert(comp_mempools[i].use_list != NULL);
sc_queue_init(&comp_mempools[i].free_list);
for (size_t j=0;j<MAX_COMP_POOL_SIZE;++j)
for (size_t j=0;j<comp_mempools[i].max_size;++j)
{
sc_queue_add_last(&comp_mempools[i].free_list, j);
}
@ -54,8 +53,8 @@ void init_memory_pools(void)
memset(ent_mempool.buffer, 0, ent_mempool.elem_size*ent_mempool.max_size);
sc_queue_init(&ent_mempool.free_list);
sc_map_init_64(&ent_mempool.use_list, MAX_COMP_POOL_SIZE ,0);
for (size_t i=0;i<MAX_COMP_POOL_SIZE;++i)
ent_mempool.use_list = (bool *)calloc(ent_mempool.max_size, sizeof(bool));
for (size_t i=0;i<ent_mempool.max_size;++i)
{
entity_buffer[i].m_id = i;
sc_map_init_64(&entity_buffer[i].components, 16 ,0);
@ -71,10 +70,10 @@ void free_memory_pools(void)
{
for (size_t i=0; i<N_COMPONENTS; ++i)
{
sc_map_term_64(&comp_mempools[i].use_list);
free(comp_mempools[i].use_list);
sc_queue_term(&comp_mempools[i].free_list);
}
sc_map_term_64(&ent_mempool.use_list);
free(ent_mempool.use_list);
sc_queue_term(&ent_mempool.free_list);
for (int i=0;i<MAX_COMP_POOL_SIZE;++i)
@ -90,7 +89,7 @@ Entity_t* new_entity_from_mempool(unsigned long *p_e_idx)
if(sc_queue_empty(&ent_mempool.free_list)) return NULL;
unsigned long e_idx = sc_queue_del_first(&ent_mempool.free_list);
*p_e_idx = e_idx;
sc_map_put_64(&ent_mempool.use_list, e_idx, e_idx);
ent_mempool.use_list[e_idx] = true;
Entity_t * ent = entity_buffer + e_idx;
sc_map_clear_64(&ent->components);
ent->m_alive = true;
@ -100,16 +99,15 @@ Entity_t* new_entity_from_mempool(unsigned long *p_e_idx)
Entity_t * get_entity_wtih_id(unsigned long idx)
{
sc_map_get_64(&ent_mempool.use_list, idx);
if (!sc_map_found(&ent_mempool.use_list)) return NULL;
if (!ent_mempool.use_list[idx]) return NULL;
return entity_buffer + idx;
}
void free_entity_to_mempool(unsigned long idx)
{
sc_map_del_64(&ent_mempool.use_list, idx);
if (sc_map_found(&ent_mempool.use_list))
if (ent_mempool.use_list[idx])
{
ent_mempool.use_list[idx] = false;
sc_queue_add_first(&ent_mempool.free_list, idx);
}
}
@ -121,7 +119,7 @@ void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx)
if(!sc_queue_empty(&comp_mempools[comp_type].free_list))
{
*idx = sc_queue_del_first(&comp_mempools[comp_type].free_list);
sc_map_put_64(&comp_mempools[comp_type].use_list, *idx, *idx);
comp_mempools[comp_type].use_list[*idx] = true;
comp = comp_mempools[comp_type].buffer + (*idx * comp_mempools[comp_type].elem_size);
memset(comp, 0, comp_mempools[comp_type].elem_size);
}
@ -132,8 +130,7 @@ void* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx)
{
void * comp = NULL;
assert(comp_type < N_COMPONENTS);
sc_map_get_64(&comp_mempools[comp_type].use_list, idx);
if (sc_map_found(&comp_mempools[comp_type].use_list))
if (comp_mempools[comp_type].use_list[idx])
{
comp = comp_mempools[comp_type].buffer + (idx * comp_mempools[comp_type].elem_size);
}
@ -144,9 +141,9 @@ void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx)
{
assert(comp_type < N_COMPONENTS);
// This just free the component from the memory pool
sc_map_del_64(&comp_mempools[comp_type].use_list, idx);
if (sc_map_found(&comp_mempools[comp_type].use_list))
if (comp_mempools[comp_type].use_list[idx])
{
comp_mempools[comp_type].use_list[idx] = false;
sc_queue_add_first(&comp_mempools[comp_type].free_list, idx);
}
}