16 if (size < MIN_MEMORY_POOL_SIZE) {
18 size = MIN_MEMORY_POOL_SIZE;
23 log_msg(ERROR,
"Memory",
"Failed to allocate memory for the pool base structure");
27 pool->memory = malloc(size);
29 log_msg(ERROR,
"Memory",
"Failed to allocate memory for the pool");
34 pool->pool_size = size;
37 pool->first->active = 0;
38 pool->first->next = NULL;
55 if (!current->active && current->size >= size) {
58 const size_t remaining = current->size - size;
59 if (remaining > MIN_MEMORY_BLOCK_SIZE) {
65 new_block->active = 0;
66 new_block->next = current->next;
70 current->next = new_block;
72 log_msg(WARNING,
"Memory",
"No more space left in the block, using the whole block");
77 return (
void*) (current + 1);
79 current = current->next;
82 log_msg(ERROR,
"Memory",
"No free block found for allocation");
96 log_msg(ERROR,
"Memory",
"Pointer is NULL");
101 if (block < pool->first || block >= (
memory_block_t*) ((
char*) pool->first + pool->pool_size)) {
102 log_msg(ERROR,
"Memory",
"Pointer is not in the memory pool");
109 while (current && current->next) {
110 if (!current->active && !current->next->active) {
113 current->next = current->next->next;
115 current = current->next;
122 log_msg(ERROR,
"Memory",
"Pool is NULL");
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Header file for logging functionality of the game.
memory_pool_t * init_memory_pool(size_t size)
Initialize a memory pool of the given size.
void * memory_pool_alloc(memory_pool_t *pool, size_t size)
Allocates memory on the given memory pool.
void shutdown_memory_pool(memory_pool_t *pool)
Shuts down the memory pool.
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.
Exposes functions for working with the memory management.