A memory pool implemented by C.
X Memo Pool is a lightweight and efficient memory management solution designed to optimize memory allocation and deallocation in C programs. It provides a pool-based approach to memory management, which can significantly improve performance in scenarios where frequent allocations of fixed-size blocks are required.
Note: Sharing a single pool across multiple threads is not recommended for thread safety.
- Fast allocation and deallocation of fixed-size memory blocks
- Automatic pool expansion when the current pool is exhausted
- Zero-initialization of allocated blocks
- Easy-to-use API with creation, allocation, freeing, and destruction functions
- Debugging support with pool information printing
To start using X Memo Pool, first create a pool handler for your data structure:
xmem_pool_handle xmem_create_pool(uint32_t block_size);
Example:
typedef struct {
int id;
char str[16];
} my_struct;
xmem_pool_handle pool = xmem_create_pool(sizeof(my_struct));
if (!pool) {
printf("Failed to create memory pool\n");
}
To allocate a block from the pool:
char* xmem_alloc(xmem_pool_handle handle);
Example:
my_struct* data = (my_struct*)xmem_alloc(pool);
if (!data) {
printf("Failed to allocate memory from pool\n");
}
To return a block to the pool:
int xmem_free(xmem_pool_handle handle, char* pointer);
Example:
int result = xmem_free(pool, (char*)data);
if (!result) {
printf("Failed to free memory\n");
}
To destroy the entire pool and free all associated resources:
void xmem_destroy_pool(xmem_pool_handle pool);
Example:
xmem_destroy_pool(pool);
For more detailed information about the API, please refer to the xmempool.h header file.
To run performance tests:
-
Install dependencies:
npm install
-
Run the performance script:
node perf.js
The performance report will be generated in the ./perf/report
directory.
Contributions to X Memo Pool are welcome! Feel free to submit pull requests or open issues for bugs, feature requests, or improvements.
X Memo Pool is open-source software. Please refer to the LICENSE file for detailed licensing information.