userpro / memorypool Goto Github PK
View Code? Open in Web Editor NEW一个极简内存池实现
License: MIT License
一个极简内存池实现
License: MIT License
使用自动扩展后,merge_free_chunk会报错,while ((char *)p0 >= mm->start && p0->is_free),从调试来看,内存访问都是无法获取的状态
使用windows的wchar_t分配内存后释放存在问题,本来我是分配的2G,释放之后只有几百个字节可用
if (mp->auto_extend) {
// 超过总内存限制
if (mp->alloc_mempool_size + total_needed_size > mp->max_mempool_size) {
goto err_out;
}
// 问题1:
mem_size_t add_mem_sz = mp->max_mempool_size - mp->mempool_size;
// 如果空间足够则按 mempool_size 新增, 不足则分配剩下所有内存
add_mem_sz = add_mem_sz >= mp->mempool_size ? mp->mempool_size
: add_mem_sz;
if (!extend_memory_list(mp, add_mem_sz)) goto err_out;
// 更新实际分配内存
mp->alloc_mempool_size += add_mem_sz;
goto FIND_FREE_CHUNK;
}
是否保证线程安全
大哥 help 代码有毒
为什么要将list分成三个区,chunk结构体区,实际数据区,指向chunk的指针区。感觉第三个部分多余了吧。
你好:
关注和使用你的项目一段时间了,感谢你的分享,给我们带来了便利!
但是,我们在使用的过程中,通过get_mempool_usage方法获取使用率,一直能正确获取,但是有时会突然返回一个很大的值,由于是个异常,不太好抓,但是真的出现了!我找了一段时间没找到原因,不知道你是否有时间看看!(我用在嵌入式Linux上)
初始化内存池时,此时内存池作为一个大的空闲块,其尾部也应该有指针指向首地址,此次没有相应操作
像这样:
*(mp_chunk**)((char*)ck + ck->size - MP_CHUNK_POINTER) = ck;
#define MP_INIT_MEMORY_STRUCT(mm, mempool_sz) \
do { \
mm->mempool_size = mempool_sz; \
mm->alloc_mem = 0; \
mm->alloc_prog_mem = 0; \
mm->free_list = (_MP_Chunk*) mm->start; \
mm->free_list->is_free = 1; \
mm->free_list->alloc_mem = mempool_sz; \
mm->free_list->prev = NULL; \
mm->free_list->next = NULL; \
mm->alloc_list = NULL; \
} while (0)
如果释放的内存为第一块申请出来的内存,在使用自动扩展后,在释放时:merge_free_chunk函数中,Chunk *p0 = *(Chunk **)((char *)c - CHUNKEND), *p1 = c;
while ((char *)p0 >= mm->start && p0->is_free)
{
.........
}
c操作任然会获取到错误的地址。
建议增加每个管理数据段的结束地址校验,比如
while ((char *)p0 >= mm->start && *(char )p0 < mm->end && p0->is_free)
end在每次调用系统malloc的时候进行记录
static int merge_free_chunk(MemoryPool* mp, _MP_Memory* mm, _MP_Chunk* c) {
_MP_Chunk *p0 = c, *p1 = c;
while (p0->is_free) {
p1 = p0;
// bug:向前面搜索邻接内存块貌似有错?p0是作为内存块_MP_Chunk首地址,而不是分配给用户的内存空间的首地址,因此不应该减去MP_CHUNKHEADER
// ++if ((char*) p0 - MP_CHUNKHEADER <= mm->start) break;
// --if ((char*) p0 - MP_CHUNKEND - MP_CHUNKHEADER <= mm->start) break;
p0 = *(_MP_Chunk**) ((char*) p0 - MP_CHUNKEND);
}
p0 = (_MP_Chunk*) ((char*) p1 + p1->alloc_mem);
while ((char*) p0 < mm->start + mp->mempool_size && p0->is_free) {
MP_DLINKLIST_DEL(mm->free_list, p0);
p1->alloc_mem += p0->alloc_mem;
p0 = (_MP_Chunk*) ((char*) p0 + p0->alloc_mem);
}
*(_MP_Chunk**) ((char*) p1 + p1->alloc_mem - MP_CHUNKEND) = p1;
#ifdef _Z_MEMORYPOOL_THREAD_
MP_UNLOCK(mp);
#endif
return 0;
}
MemoryPool* MemoryPoolInit(mem_size_t maxmempoolsize, mem_size_t mempoolsize) {
if (mempoolsize > maxmempoolsize) {
// printf("[MemoryPool_Init] MemPool Init ERROR! Mempoolsize is too big!
// \n");
return NULL;
}
MemoryPool* mp = (MemoryPool*) malloc(sizeof(MemoryPool));
if (!mp) return NULL;
mp->last_id = 0;
if (mempoolsize < maxmempoolsize) mp->auto_extend = 1;
mp->max_mem_pool_size = maxmempoolsize;
mp->total_mem_pool_size = mp->mem_pool_size = mempoolsize;
#ifdef _Z_MEMORYPOOL_THREAD_
pthread_mutex_init(&mp->lock, NULL);
#endif
char* s = (char*) malloc(sizeof(_MP_Memory) +
sizeof(char) * mp->mem_pool_size);
if (!s) return NULL; //HERE!!!!!!!!!!!!!!!!!!!!, leads mem leak as mp is not free yet :-)
......
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.