xmempool 内存池

时间:2018-01-04 05:43:08
【文件属性】:

文件名称:xmempool 内存池

文件大小:14KB

文件格式:ZIP

更新时间:2018-01-04 05:43:08

内存池 C语言

C语言版简单内存池的实现。 X Memo Pool A memory pool implemented by C. Usage Create Pool 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 which typedefed at xmempool.h. typedef void* xmem_pool_handler; 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"); Allocate Memory You can get a space of a certain size via function: void* xmem_alloc(xmem_pool_handle handle); For an example: stct* my_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. Recover Memory 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, void* 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"); Destroy the Whole Pool void xmem_destroy_pool(xmem_pool_handle pool); Caution! This function will destroy the whole pool, freeing all your memory allocated by that pool, even if you haven't xmem_free yet. xmem_destroy_pool(pool1); xmem_destroy_pool(pool2);


【文件预览】:
xmempool-master
----test.c(1KB)
----compile_commands.json(112B)
----LICENSE(18KB)
----README.md(2KB)
----Makefile(1KB)
----xmempool.h(1004B)
----.ycm_extra_conf.py(3KB)
----.gitignore(271B)
----xmempool.c(9KB)

网友评论