SomeGameEngineV2/engine/memory.h

71 lines
1.9 KiB
C

#ifndef ENGINE_MEMORY_H
#define ENGINE_MEMORY_H
#include <stdint.h>
#define CC_NO_SHORT_NAMES
#include "base.h"
typedef struct memoryPool {
//struct idxSet* free_set;
void * const buffer;
const unsigned long capacity;
const unsigned long elem_size;
cc_oset( uint32_t ) free_set;
} memoryPool;
void init_memory_system(void);
void free_memory_system(void);
void* new_component_generic(unsigned int comp_type, unsigned int* idx);
void* get_component_generic(unsigned int comp_type, unsigned int idx);
void free_component_generic(unsigned int comp_type, unsigned int idx);
void print_memory_info();
extern struct memoryImpl {
memoryPool* comp_mempools;
uint32_t n_components;
} mem_impl;
#define DEFINE_COMPONENT_HEADERS(name, comp_id, type) \
static const unsigned int CID_##name = comp_id; \
static inline type* new_component_##name(unsigned int* idx) { \
return (type*)new_component_generic(comp_id, idx); \
}\
static inline type* get_component_##name(unsigned int idx) { \
return (type*)get_component_generic(comp_id, idx); \
}\
static inline void free_component_##name(unsigned int idx) { \
free_component_generic(comp_id, idx); \
}\
#define DEFINE_COMP_MEMPOOL_BUF(type, cap) \
static type type##_buf[cap]; \
const unsigned long type##_CNT = cap; \
#define BEGIN_DEFINE_COMP_MEMPOOL \
static memoryPool _memPool[] = {
#define ADD_COMP_MEMPOOL(name, type) \
[CID_##name] = (memoryPool){\
.buffer = type##_buf,\
.capacity = type##_CNT,\
.elem_size = sizeof(type),\
0\
}, \
#define END_DEFINE_COMP_MEMPOOL\
}; \
struct memoryImpl mem_impl = { \
.comp_mempools = _memPool, \
.n_components = USER_N_COMPONENTS, \
};\
#define STATIC_ASSERT_COMP_POOL \
static_assert( \
(sizeof(_memPool) / sizeof(_memPool[0])) == USER_N_COMPONENTS, "Insufficnet component pool definition" \
); \
#endif // ENGINE_MEMORY_H