A memory pool implemented by C.
Sharing a same pool in multi-thread is not recommanded.
At first you should create a pool handler for your data (or structure).
xmem_pool_handler xmem_create_pool(unsigned int block_size);
xmem_pool_hander
is only a pointer whichtypedef
ed at xmempool.h.typedef char* xmem_pool_handler;Notice: first I used
typedef void* xmem_pool_handler
, but for compatible, I changed it tochar*
.
For an example:
typedef struct stct {
int id;
char str[16];
} stct;
xmem_pool_handler pool1 = xmem_create_pool(sizeof(stct));
xmem_pool_handler pool2 = xmem_create_pool(sizeof(int));
if(!pool1 || !pool2) printf("Can't alloc more space\n");
You can get a space of a certain size via function:
char* xmem_alloc(xmem_pool_handle handle);
For an example:
stct* my_stct = (stct*)xmem_alloc(pool1);
if(!my_stct) printf("Can't alloc more space.\n");
And then you will get a whole empty space that fits stct
.
After your scope you want to recover your memory, don't use free()
. There's a function below:
int xmem_free(xmem_pool_handle handle, char* pointer);
The return value:
0
: invalid pool handler or the pointer not belongs to this pool, can't be recovered.1
: succeeded.
For an example:
int result = xmem_free(pool1, my_stct);
if(!result) printf("Cannot be recovered.\n");
void xmem_destroy_pool(xmem_pool_handle pool);
Caution! This function will destroy the whole pool,
free
ing all your memory allocated by that pool, even if you haven'txmem_free
yet.
xmem_destroy_pool(pool1);
xmem_destroy_pool(pool2);
You can refer to xmempool.h for any more functions.
$ npm install
Install dependencies via command above and then type follow command:
$ node perf.js
The report will be generated at ./perf/report
.
Here's some my report.
You're welcome to pull requests!
γιηΆζθ¦ΊεΎδΈζιΊΌε―θ½ζδΊΊζι注ζγ