|
xmempool 1.0
A Simple Memory Pool Implementation
|
#include "xmempool.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>Go to the source code of this file.
Data Structures | |
| struct | xmem_pool_block |
| xmempool - A Simple Memory Pool Implementation More... | |
| struct | xmem_pool |
Macros | |
| #define | MIN_ALLOC_BLOCK_NODE_COUNT (1024) |
| #define | MAX_ALLOC_BLOCK_NODE_COUNT (1024 << 2) |
| #define | MIN_ALLOC_LENGTH (1024) |
| #define | MAX_ALLOC_LENGTH (1024 << 2) |
Functions | |
| uint32_t | size_count_with_4 (uint32_t size) |
| static void | _alloc_block_nodes () |
| Allocate memory for new block nodes. | |
| static xmem_pool_block * | _get_next_block_node () |
| Get the next available block node. | |
| static void | _recover_block_node (xmem_pool_block *node) |
| static int | _xmem_count_free_blocks (xmem_pool *pool) |
| xmem_pool_handle | _create_pool (uint32_t block_size, uint32_t block_count) |
| Create a new memory pool. | |
| void | xmem_print_info (xmem_pool_handle _pool) |
| Print information about the memory pool. | |
| xmem_pool_handle | xmem_create_pool (unsigned int block_size) |
| void | xmem_destroy_pool (xmem_pool_handle handle) |
| Destroy a memory pool and free all associated resources. | |
| char * | xmem_alloc (xmem_pool_handle handle) |
| Allocate a block from the memory pool. | |
| int | xmem_free (xmem_pool_handle handle, char *pointer) |
| Free a block back to the memory pool. | |
| void | xmem_init () |
| void | xmem_clean_up () |
| Clean up global resources used by xmempool. | |
Variables | |
| static uint32_t | _xmem_pool_next_alloc_block_node_count |
| static xmem_pool_block * | _free_block_ptr = 0 |
| static xmem_pool_block * | _free_block_ptr_end = 0 |
| static int | _xmem_page_size = 0 |
| static uint32_t | _block_size = sizeof(xmem_pool_block) |
| #define MAX_ALLOC_BLOCK_NODE_COUNT (1024 << 2) |
Definition at line 48 of file xmempool.c.
| #define MAX_ALLOC_LENGTH (1024 << 2) |
Definition at line 50 of file xmempool.c.
| #define MIN_ALLOC_BLOCK_NODE_COUNT (1024) |
Definition at line 47 of file xmempool.c.
| #define MIN_ALLOC_LENGTH (1024) |
Definition at line 49 of file xmempool.c.
|
inlinestatic |
Allocate memory for new block nodes.
This function allocates a chunk of memory for new block nodes when the current pool is exhausted. It doubles the allocation size each time, up to a maximum defined by MAX_ALLOC_BLOCK_NODE_COUNT. The newly allocated nodes are then added to the free block list.
Definition at line 74 of file xmempool.c.
| xmem_pool_handle _create_pool | ( | uint32_t | block_size, |
| uint32_t | block_count ) |
Create a new memory pool.
This function creates a new memory pool with the specified block size and count. It allocates memory for the pool structure and the actual memory blocks, then initializes the free block list.
| block_size | Size of each memory block in the pool |
| block_count | Number of blocks to allocate initially |
Definition at line 189 of file xmempool.c.
|
inlinestatic |
Get the next available block node.
This function returns the next available block node from the free list. If the free list is empty, it calls _alloc_block_nodes() to allocate more.
Definition at line 127 of file xmempool.c.
|
inlinestatic |
Definition at line 149 of file xmempool.c.
|
inlinestatic |
Definition at line 162 of file xmempool.c.
| uint32_t size_count_with_4 | ( | uint32_t | size | ) |
Definition at line 58 of file xmempool.c.
| char * xmem_alloc | ( | xmem_pool_handle | handle | ) |
Allocate a block from the memory pool.
This function returns a pointer to a free block from the pool. If the current pool is full, it automatically creates and chains a new pool. The allocated block is always zero-initialized.
| handle | Handle to the pool to allocate from |
Definition at line 341 of file xmempool.c.
| void xmem_clean_up | ( | ) |
Clean up global resources used by xmempool.
This function frees all global resources used by the xmempool system, particularly the memory used for managing block nodes. It should be called when the xmempool system is no longer needed.
Definition at line 408 of file xmempool.c.
| xmem_pool_handle xmem_create_pool | ( | unsigned int | block_size | ) |
Definition at line 292 of file xmempool.c.
| void xmem_destroy_pool | ( | xmem_pool_handle | pool | ) |
Destroy a memory pool and free all associated resources.
This function frees all memory associated with the pool, including all chained pools and their blocks. It also recovers all block nodes used for managing free blocks.
| pool | Handle to the pool to be destroyed |
Definition at line 311 of file xmempool.c.
| int xmem_free | ( | xmem_pool_handle | handle, |
| char * | pointer ) |
Free a block back to the memory pool.
This function returns a block to the free list of the first pool in the chain. It does not actually free memory, but makes the block available for future allocations.
| handle | Handle to the pool |
| pointer | Pointer to the block to be freed |
Definition at line 379 of file xmempool.c.
| void xmem_init | ( | ) |
Definition at line 404 of file xmempool.c.
| void xmem_print_info | ( | xmem_pool_handle | pool | ) |
Print information about the memory pool.
This function prints detailed information about each pool in the chain, including its size, block count, memory range, and number of free blocks.
| pool | Handle to the pool |
Definition at line 264 of file xmempool.c.
|
static |
Definition at line 56 of file xmempool.c.
|
static |
Definition at line 53 of file xmempool.c.
|
static |
Definition at line 54 of file xmempool.c.
|
static |
Definition at line 55 of file xmempool.c.
|
static |
Definition at line 51 of file xmempool.c.