Replace ringbuffer with sc_queue

It is more flexible and uses less heap.
main
En Yi 2024-08-24 15:30:32 +08:00
parent cb9508a4b6
commit bbe3a22cc5
3 changed files with 26 additions and 74 deletions

View File

@ -3,7 +3,7 @@
#define MAX_SCENES_TO_RENDER 16
#define MAX_RENDER_LAYERS 4
#define MAX_ENTITIES 2048
#define MAX_ENTITIES 2047
#define MAX_TEXTURES 16
#define MAX_SPRITES 128
#define MAX_SOUNDS 32

View File

@ -1,50 +1,9 @@
#include "mempool.h"
//#include "sc/queue/sc_queue.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
static void cb_init(ULongCircBuffer_t* cb, size_t capacity)
{
cb->buffer = (unsigned long*)malloc(capacity * sizeof(unsigned long));
assert(cb->buffer != NULL);
cb->buffer_end = cb->buffer + capacity;
cb->capacity = capacity;
cb->count = 0;
cb->head = cb->buffer;
cb->tail = cb->buffer;
}
static void cb_free(ULongCircBuffer_t* cb)
{
free(cb->buffer);
// clear out other fields too, just to be safe
}
static bool cb_pop_front(ULongCircBuffer_t* cb, unsigned long* item)
{
if (cb->count == 0) return false;
*item = *cb->tail;
cb->tail++;
if(cb->tail == cb->buffer_end) cb->tail = cb->buffer;
cb->count--;
return true;
}
static bool cb_push_back(ULongCircBuffer_t* cb, unsigned long item)
{
if(cb->count == cb->capacity) return false;
*(cb->head) = item;
cb->head++;
if(cb->head == cb->buffer_end) cb->head = cb->buffer;
cb->count++;
return true;
}
// Static allocate buffers
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
static MemPool_t ent_mempool = {
@ -65,23 +24,21 @@ void init_memory_pools(void)
memset(comp_mempools[i].buffer, 0, comp_mempools[i].elem_size * comp_mempools[i].max_size);
comp_mempools[i].use_list = (bool*)calloc(comp_mempools[i].max_size, sizeof(bool));
assert(comp_mempools[i].use_list != NULL);
cb_init(&comp_mempools[i].free_list, comp_mempools[i].max_size);
sc_queue_init(&comp_mempools[i].free_list);
for (size_t j = 0; j < comp_mempools[i].max_size; ++j)
{
comp_mempools[i].free_list.buffer[j] = j;
sc_queue_add_last(&comp_mempools[i].free_list, j);
}
comp_mempools[i].free_list.count = comp_mempools[i].max_size;
}
memset(ent_mempool.buffer, 0, ent_mempool.elem_size*ent_mempool.max_size);
cb_init(&ent_mempool.free_list, ent_mempool.max_size);
sc_queue_init(&ent_mempool.free_list);
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;
ent_mempool.free_list.buffer[i] = i;
sc_queue_add_last(&ent_mempool.free_list, i);
}
ent_mempool.free_list.count = ent_mempool.max_size;
pool_inited = true;
}
}
@ -93,10 +50,10 @@ void free_memory_pools(void)
for (size_t i = 0; i < N_COMPONENTS; ++i)
{
free(comp_mempools[i].use_list);
cb_free(&comp_mempools[i].free_list);
sc_queue_term(&comp_mempools[i].free_list);
}
free(ent_mempool.use_list);
cb_free(&ent_mempool.free_list);
sc_queue_term(&ent_mempool.free_list);
pool_inited = false;
}
@ -104,8 +61,9 @@ void free_memory_pools(void)
Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
{
unsigned long e_idx;
if (!cb_pop_front(&ent_mempool.free_list, &e_idx)) return NULL;
if (sc_queue_empty(&ent_mempool.free_list)) return NULL;
unsigned long e_idx = sc_queue_del_first(&ent_mempool.free_list);
*e_idx_ptr = e_idx;
ent_mempool.use_list[e_idx] = true;
@ -130,20 +88,21 @@ void free_entity_to_mempool(unsigned long idx)
if (ent_mempool.use_list[idx])
{
ent_mempool.use_list[idx] = false;
cb_push_back(&ent_mempool.free_list, idx);
sc_queue_add_first(&ent_mempool.free_list, idx);
}
}
void* new_component_from_mempool(unsigned int comp_type, unsigned long* idx)
{
void* comp = NULL;
assert(comp_type < N_COMPONENTS);
if (cb_pop_front(&comp_mempools[comp_type].free_list, idx))
{
if (sc_queue_empty(&comp_mempools[comp_type].free_list)) return NULL;
*idx = sc_queue_del_first(&comp_mempools[comp_type].free_list);
comp_mempools[comp_type].use_list[*idx] = true;
comp = comp_mempools[comp_type].buffer + (*idx * comp_mempools[comp_type].elem_size);
void* comp = comp_mempools[comp_type].buffer + (*idx * comp_mempools[comp_type].elem_size);
memset(comp, 0, comp_mempools[comp_type].elem_size);
}
return comp;
}
@ -165,23 +124,23 @@ void free_component_to_mempool(unsigned int comp_type, unsigned long idx)
if (comp_mempools[comp_type].use_list[idx])
{
comp_mempools[comp_type].use_list[idx] = false;
cb_push_back(&comp_mempools[comp_type].free_list, idx);
sc_queue_add_first(&comp_mempools[comp_type].free_list, idx);
}
}
void print_mempool_stats(char* buffer)
{
buffer += sprintf(buffer, "Entity free: %u\n", ent_mempool.free_list.count);
buffer += sprintf(buffer, "Entity free: %lu\n", sc_queue_size(&ent_mempool.free_list));
for (size_t i = 0; i < N_COMPONENTS; ++i)
{
buffer += sprintf(
buffer, "%lu: %u/%u\n",
i, comp_mempools[i].free_list.count, comp_mempools[i].free_list.capacity
buffer, "%lu: %lu/%lu\n",
i, sc_queue_size(&comp_mempools[i].free_list), comp_mempools[i].free_list.cap
);
}
}
uint32_t get_num_of_free_entities(void)
{
return ent_mempool.free_list.count;
return sc_queue_size(&ent_mempool.free_list);
}

View File

@ -1,23 +1,16 @@
#ifndef __MEMPOOL_H
#define __MEMPOOL_H
#include "EC.h"
#include "sc/queue/sc_queue.h"
void init_memory_pools(void);
void free_memory_pools(void);
typedef struct ULongCircBuffer {
unsigned long* buffer; // data buffer
unsigned long* buffer_end; // end of data buffer
uint32_t capacity; // maximum number of items in the buffer
uint32_t count; // number of items in the buffer
unsigned long* head; // pointer to head
unsigned long* tail; // pointer to tail
}ULongCircBuffer_t;
typedef struct MemPool {
void * const buffer;
const unsigned long max_size;
const unsigned long elem_size;
bool *use_list;
ULongCircBuffer_t free_list;
struct sc_queue_32 free_list;
} MemPool_t;
// Game needs to implement this somewhere