Rewrite mempools to be scalable
Changelog: - Rewrite the Mempool Struct to be generic enough - Static alloc for components mempool and entities mempool - Component mempool now uses the component enum as index - Update all mempool functions to work with the new structscene_man
parent
b392e462b7
commit
e20b020829
167
mempool.c
167
mempool.c
|
@ -2,7 +2,10 @@
|
|||
#include "sc/queue/sc_queue.h"
|
||||
#include "sc/map/sc_map.h"
|
||||
|
||||
// BIG TODO: REWORK THE MEMPOOL FOR COMPONENT AS IT IS NOT SCALABLE
|
||||
// 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];
|
||||
|
||||
// Use hashmap as a Set
|
||||
// Use list will be used to check if an object exist
|
||||
|
@ -10,57 +13,48 @@
|
|||
// requires bound checking
|
||||
// It's just easier on the mind overall
|
||||
// If need to optimise memory, replace free_list with set and remove use_list
|
||||
static struct EntityMemPool
|
||||
typedef struct MemPool
|
||||
{
|
||||
Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
||||
void * const buffer;
|
||||
const unsigned long max_size;
|
||||
const unsigned long elem_size;
|
||||
struct sc_map_64 use_list;
|
||||
struct sc_queue_uint free_list;
|
||||
}entity_mem_pool;
|
||||
}MemPool_t;
|
||||
|
||||
static struct BBoxMemPool
|
||||
// Static allocate mempools
|
||||
static MemPool_t comp_mempools[N_COMPONENTS] =
|
||||
{
|
||||
CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
|
||||
struct sc_map_64 use_list;
|
||||
struct sc_queue_uint free_list;
|
||||
}bbox_mem_pool;
|
||||
|
||||
static struct CTransformMemPool
|
||||
{
|
||||
CBBox_t ctransform_buffer[MAX_COMP_POOL_SIZE];
|
||||
struct sc_map_64 use_list;
|
||||
struct sc_queue_uint free_list;
|
||||
}ctransform_mem_pool;
|
||||
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), {0}, {0}},
|
||||
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), {0}, {0}},
|
||||
};
|
||||
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), {0}, {0}};
|
||||
|
||||
static bool pool_inited = false;
|
||||
void init_memory_pools(void)
|
||||
{
|
||||
if (!pool_inited)
|
||||
{
|
||||
memset(bbox_mem_pool.bbox_buffer, 0, sizeof(bbox_mem_pool.bbox_buffer));
|
||||
memset(ctransform_mem_pool.ctransform_buffer, 0, sizeof(ctransform_mem_pool.ctransform_buffer));
|
||||
memset(entity_mem_pool.entity_buffer, 0, sizeof(entity_mem_pool.entity_buffer));
|
||||
|
||||
sc_queue_init(&bbox_mem_pool.free_list);
|
||||
for (int i=0;i<MAX_COMP_POOL_SIZE;++i)
|
||||
for (size_t i=0; i<N_COMPONENTS; ++i)
|
||||
{
|
||||
sc_queue_add_last(&(bbox_mem_pool.free_list), i);
|
||||
}
|
||||
sc_queue_init(&ctransform_mem_pool.free_list);
|
||||
for (int i=0;i<MAX_COMP_POOL_SIZE;++i)
|
||||
{
|
||||
sc_queue_add_last(&(ctransform_mem_pool.free_list), i);
|
||||
}
|
||||
sc_queue_init(&entity_mem_pool.free_list);
|
||||
for (int i=0;i<MAX_COMP_POOL_SIZE;++i)
|
||||
{
|
||||
entity_mem_pool.entity_buffer[i].m_id = i;
|
||||
sc_map_init_64(&entity_mem_pool.entity_buffer[i].components, 16 ,0);
|
||||
sc_queue_add_last(&(entity_mem_pool.free_list), 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);
|
||||
sc_queue_init(&comp_mempools[i].free_list);
|
||||
for (size_t j=0;j<MAX_COMP_POOL_SIZE;++j)
|
||||
{
|
||||
sc_queue_add_last(&comp_mempools[i].free_list, j);
|
||||
}
|
||||
}
|
||||
|
||||
sc_map_init_64(&bbox_mem_pool.use_list, MAX_COMP_POOL_SIZE ,0);
|
||||
sc_map_init_64(&ctransform_mem_pool.use_list, MAX_COMP_POOL_SIZE ,0);
|
||||
sc_map_init_64(&entity_mem_pool.use_list, MAX_COMP_POOL_SIZE ,0);
|
||||
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)
|
||||
{
|
||||
entity_buffer[i].m_id = i;
|
||||
sc_map_init_64(&entity_buffer[i].components, 16 ,0);
|
||||
sc_queue_add_last(&(ent_mempool.free_list), i);
|
||||
}
|
||||
pool_inited = true;
|
||||
}
|
||||
}
|
||||
|
@ -69,17 +63,17 @@ void free_memory_pools(void)
|
|||
{
|
||||
if (pool_inited)
|
||||
{
|
||||
sc_map_term_64(&bbox_mem_pool.use_list);
|
||||
sc_map_term_64(&ctransform_mem_pool.use_list);
|
||||
sc_map_term_64(&entity_mem_pool.use_list);
|
||||
|
||||
sc_queue_term(&bbox_mem_pool.free_list);
|
||||
sc_queue_term(&ctransform_mem_pool.free_list);
|
||||
sc_queue_term(&entity_mem_pool.free_list);
|
||||
for (size_t i=0; i<N_COMPONENTS; ++i)
|
||||
{
|
||||
sc_map_term_64(&comp_mempools[i].use_list);
|
||||
sc_queue_term(&comp_mempools[i].free_list);
|
||||
}
|
||||
sc_map_term_64(&ent_mempool.use_list);
|
||||
sc_queue_term(&ent_mempool.free_list);
|
||||
|
||||
for (int i=0;i<MAX_COMP_POOL_SIZE;++i)
|
||||
{
|
||||
sc_map_term_64(&entity_mem_pool.entity_buffer[i].components);
|
||||
sc_map_term_64(&entity_buffer[i].components);
|
||||
}
|
||||
pool_inited = false;
|
||||
}
|
||||
|
@ -87,11 +81,11 @@ void free_memory_pools(void)
|
|||
|
||||
Entity_t* new_entity_from_mempool(unsigned long *p_e_idx)
|
||||
{
|
||||
if(sc_queue_empty(&entity_mem_pool.free_list)) return NULL;
|
||||
unsigned long e_idx = sc_queue_del_first(&entity_mem_pool.free_list);
|
||||
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(&entity_mem_pool.use_list, e_idx, e_idx);
|
||||
Entity_t * ent = entity_mem_pool.entity_buffer + e_idx;
|
||||
sc_map_put_64(&ent_mempool.use_list, e_idx, e_idx);
|
||||
Entity_t * ent = entity_buffer + e_idx;
|
||||
sc_map_clear_64(&ent->components);
|
||||
ent->m_alive = true;
|
||||
ent->m_tag = NO_ENT_TAG;
|
||||
|
@ -100,41 +94,30 @@ Entity_t* new_entity_from_mempool(unsigned long *p_e_idx)
|
|||
|
||||
Entity_t * get_entity_wtih_id(unsigned long idx)
|
||||
{
|
||||
sc_map_get_64(&entity_mem_pool.use_list, idx);
|
||||
if (!sc_map_found(&entity_mem_pool.use_list)) return NULL;
|
||||
return entity_mem_pool.entity_buffer + idx;
|
||||
sc_map_get_64(&ent_mempool.use_list, idx);
|
||||
if (!sc_map_found(&ent_mempool.use_list)) return NULL;
|
||||
return entity_buffer + idx;
|
||||
}
|
||||
|
||||
void free_entity_to_mempool(unsigned long idx)
|
||||
{
|
||||
sc_map_del_64(&entity_mem_pool.use_list, idx);
|
||||
if (sc_map_found(&entity_mem_pool.use_list))
|
||||
sc_map_del_64(&ent_mempool.use_list, idx);
|
||||
if (sc_map_found(&ent_mempool.use_list))
|
||||
{
|
||||
sc_queue_add_first(&entity_mem_pool.free_list, idx);
|
||||
sc_queue_add_first(&ent_mempool.free_list, idx);
|
||||
}
|
||||
}
|
||||
|
||||
void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx)
|
||||
{
|
||||
void * comp = NULL;
|
||||
switch(comp_type)
|
||||
assert(comp_type < N_COMPONENTS);
|
||||
if(!sc_queue_empty(&comp_mempools[comp_type].free_list))
|
||||
{
|
||||
case CBBOX_COMP_T:
|
||||
if(sc_queue_empty(&bbox_mem_pool.free_list)) break;
|
||||
*idx = sc_queue_del_first(&bbox_mem_pool.free_list);
|
||||
sc_map_put_64(&bbox_mem_pool.use_list, *idx, *idx);
|
||||
comp = bbox_mem_pool.bbox_buffer + *idx;
|
||||
memset(comp, 0, sizeof(CBBox_t));
|
||||
break;
|
||||
case CTRANSFORM_COMP_T:
|
||||
if(sc_queue_empty(&ctransform_mem_pool.free_list)) break;
|
||||
*idx = sc_queue_del_first(&ctransform_mem_pool.free_list);
|
||||
sc_map_put_64(&ctransform_mem_pool.use_list, *idx, *idx);
|
||||
comp = ctransform_mem_pool.ctransform_buffer + *idx;
|
||||
memset(comp, 0, sizeof(CTransform_t));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
*idx = sc_queue_del_first(&comp_mempools[comp_type].free_list);
|
||||
sc_map_put_64(&comp_mempools[comp_type].use_list, *idx, *idx);
|
||||
comp = comp_mempools[comp_type].buffer + (*idx * comp_mempools[comp_type].elem_size);
|
||||
memset(comp, 0, comp_mempools[comp_type].elem_size);
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
@ -142,44 +125,22 @@ void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx)
|
|||
void* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx)
|
||||
{
|
||||
void * comp = NULL;
|
||||
switch(comp_type)
|
||||
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))
|
||||
{
|
||||
case CBBOX_COMP_T:
|
||||
sc_map_get_64(&bbox_mem_pool.use_list, idx);
|
||||
if (!sc_map_found(&bbox_mem_pool.use_list)) break;
|
||||
comp = bbox_mem_pool.bbox_buffer + idx;
|
||||
break;
|
||||
case CTRANSFORM_COMP_T:
|
||||
sc_map_get_64(&ctransform_mem_pool.use_list, idx);
|
||||
if (!sc_map_found(&ctransform_mem_pool.use_list)) break;
|
||||
comp = ctransform_mem_pool.ctransform_buffer + idx;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
comp = comp_mempools[comp_type].buffer + (idx * comp_mempools[comp_type].elem_size);
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
||||
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
|
||||
switch(comp_type)
|
||||
sc_map_del_64(&comp_mempools[comp_type].use_list, idx);
|
||||
if (sc_map_found(&comp_mempools[comp_type].use_list))
|
||||
{
|
||||
case CBBOX_COMP_T:
|
||||
sc_map_del_64(&bbox_mem_pool.use_list, idx);
|
||||
if (sc_map_found(&bbox_mem_pool.use_list))
|
||||
{
|
||||
sc_queue_add_first(&bbox_mem_pool.free_list, idx);
|
||||
}
|
||||
break;
|
||||
case CTRANSFORM_COMP_T:
|
||||
sc_map_del_64(&ctransform_mem_pool.use_list, idx);
|
||||
if (sc_map_found(&ctransform_mem_pool.use_list))
|
||||
{
|
||||
sc_queue_add_first(&ctransform_mem_pool.free_list, idx);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
sc_queue_add_first(&comp_mempools[comp_type].free_list, idx);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue