47#define MIN_ALLOC_BLOCK_NODE_COUNT (1024)
48#define MAX_ALLOC_BLOCK_NODE_COUNT (1024 << 2)
49#define MIN_ALLOC_LENGTH (1024)
50#define MAX_ALLOC_LENGTH (1024 << 2)
62 return size + (4 - size % 4);
82 char*
start = (
char*)malloc(size_count);
87 char* block_ptr =
start;
91 if (!block_ptr)
return;
99 if (block_ptr !=
start) {
146 return should_return;
190 static uint32_t _pool_size =
sizeof(
xmem_pool);
201 space = (
char*)malloc(size_count);
209 if (!pool)
return (
char*)0;
232 start_block = wrapper;
239 while (start_block) {
248 wrapper->
start = space;
253 end_block->
next = wrapper;
271 printf(
"----- POOL OF SIZE [%.4d] -----\n", pool->
block_size);
272 printf(
" + id: %d\n", pool_id++);
275#pragma GCC diagnostic push
276#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
277 printf(
" + spaces: [0x%.8X, 0x%.8X)\n",
278 (
unsigned int)pool->
start,
279 (
unsigned int)pool->
end);
280#pragma GCC diagnostic pop
285 printf(
" + free blocks: -\n");
303 printf(
"A new pool of [%d] is created!",
block_size);
328 next_block = block->
next;
335 next_pool = pool->
next;
342 static uint32_t pool_element_size =
sizeof(
xmem_pool);
358 memcpy(&temp_pool, pool, pool_element_size);
359 memcpy(pool, new_pool, pool_element_size);
360 memcpy(new_pool, &temp_pool, pool_element_size);
362 pool->
next = new_pool;
368 space = block->
start;
390 block->
start = pointer;
xmempool - A Simple Memory Pool Implementation
struct xmem_pool_block * next
xmem_pool_block * free_blocks_tail
xmem_pool_block * free_blocks
uint32_t next_alloc_length
uint32_t size_count_with_4(uint32_t size)
static uint32_t _xmem_pool_next_alloc_block_node_count
void xmem_clean_up()
Clean up global resources used by xmempool.
void xmem_print_info(xmem_pool_handle _pool)
Print information about the memory pool.
static void _alloc_block_nodes()
Allocate memory for new block nodes.
static int _xmem_page_size
static void _recover_block_node(xmem_pool_block *node)
char * xmem_alloc(xmem_pool_handle handle)
Allocate a block from the memory pool.
static int _xmem_count_free_blocks(xmem_pool *pool)
static xmem_pool_block * _free_block_ptr
static uint32_t _block_size
#define MAX_ALLOC_BLOCK_NODE_COUNT
int xmem_free(xmem_pool_handle handle, char *pointer)
Free a block back to the memory pool.
void xmem_destroy_pool(xmem_pool_handle handle)
Destroy a memory pool and free all associated resources.
#define MIN_ALLOC_BLOCK_NODE_COUNT
static xmem_pool_block * _get_next_block_node()
Get the next available block node.
xmem_pool_handle _create_pool(uint32_t block_size, uint32_t block_count)
Create a new memory pool.
static xmem_pool_block * _free_block_ptr_end
xmem_pool_handle xmem_create_pool(unsigned int block_size)
Memory pool management APIs.
char * xmem_pool_handle
Handle type for memory pools.