首先需要把你的需要取得内存块转换成8的倍数这样转换 (m+ 8 - 1)&~(8-1)这样的话就可以转化了,
然后直接找打自由链表的节点需要这个函数freelist_index这个函数可以计算你的第几个自由链表;
然后直接找到自由链表中的现在可以使用的头结点的地址这里用my_free_list = freelist + index;
然后找到之后就开始分配了,看看你的自由联是不是还有可以分配的内存,
如果有就直接移动你的freelist中保存的可以分配的该自由链表的地址的信息,就是这样操作
object* volatile* my_free_list = freelist + index(n);
object* result = *my_free_list;
*my_free_list = result->next;
return result;这里就直接返回了result了;
这里有一个我必须得说;
就是object这个union,这个union使用的是union object{object* next,char client_data[0];}
这里使用union是有用意的;
这个union在后端的话用的是union中的object*next,而在客端用的是client_data[0];
注意到这里union的size只有4bytes
注意这个union可以说同一时间只有一个东西,但是最厉害的就是这个东西是个指针;
真是太聪明的设计,很细节的东西,虽然只节省了4byte这里是32位机;
现在继续说上面的result如果返回的是0的话说明你的自由链表已经用完了所有的chunk,那么现在需要的就是重新申请一个chunk
但是chunk_alloc中申请的只是你的需要的内存然后并没有加入你的自由链表中;而如果需要加入到自由链表中就需要这个函数refill这个函数;
先说一下chunk_alloc这个函数其实我感觉memory pool里面最核心的代码就是chunk_alloc了
首先这个你申请的为 size_t m 然后内存池里面的start_free 和end_free这两个变量很好理解;自由区链表的内存都是来自于内存池;而内存池就需要start_free和end_free这两个东西表示剩下的bytes很好理解;
然后bytes_left = end_free - start_free;然后就得到了byte_left剩下的bytes了,然后比较bytes_left和m*20的大小;如果bytes_left > m*20说明你的剩下的足够分配了;但是这里还得说一下就是这个m*20这个为啥是个20呢,因为这里需要一次性能多分配就多分配;下一次就不用分配了;减少了时间复杂度吧;
然后还有一种情况就是你分配的刚好只能分配一个以上的chunk的大小;说到这里就有一个东西需要注意一个参数很重要就是int& node 注意这里是reference这里的node的使用是说明你申请的chunk的块数,但是在refill调用chunk_alloc的时候默认设置为20了,刚才为啥是20,原因就在这里了;
现在呢不够申请20个chunk了,那就能申请几个就申请几个呗;
node = bytes_left / m;
total_bytes = node * m;
result = start_free;
start_free += total_types;
return result;
这里就返回了一个以上少于20个chunk;
但是还有就是一个都不够呢;这里的设计也是很聪明的;
就是先扫一下start_free到end_free还剩多少还够给哪个自由链表添加chunk,然后找到那个freelist+n然后直接在前面把这个;内存块加载进这个自由链表;
这里的m都是已经8的倍数化了啊!
当然现在就是你的需要做的就是寻找你的m的倍数的chunk,找到如果m倍数的chunk存在的话,直接把他拿到内存池里面来;
for(;m < MAX_SIZE;m+=ALIGN)
{
my_free_list = freelist + free_list_index(m);
result = *my_free_list;
if(result){
start_free = result;
end_free = result + m;
*my_free_list = result ->next;
return chunk_alloc();//这里递归调用
}
}
然后是山穷水尽实在没有了自由链表中也找不到了这里就给内存池分配一片空间;
但是这个空间是size_t bytes = 2 * total_sizes + (heap_size >>4)这里我有个问题就是这个heap_size真的有用处吗;
还不如size_t bytes = 3 * total_sizes;直接申请3个;
这里直接给内存池扩增为你的需求的2倍,可见内存的增长幅度也不是很大;
void* p = malloc(bytes);
if(p) {
start_free = p;
end_free = p+bytes;
chunk_alloc();
}
这里还有一种情况就是如果没有申请到呢;
先把end_free = 0;end_free置为0再说;
然后利用一级内存池配置器配置
start_free = __malloc_alloc_template::allocate(bytes);
这里每次如果内存池有一个不够就保存当前需要的内存池的大小这里就是bytes heap_sizes = bytes;
end_free = start_free + bytes;
然后chunk_alloc();递归调用chunk_alloc直接分配内存;
char* __default_alloc_template<threads, n>::chunk_alloc(size_t N, int& node){
size_t end_storage = end_free - start_free;
size_t total_alloc = N * node;
char* result = nullptr;
object *curobj = nullptr;
object* volatile * my_free_list;
if (end_storage >= total_alloc){
result = start_free;
start_free += total_alloc;
return static_cast<char*>(result);
}
else if (end_storage >= N){
node = end_storage / N;//the left can be used by how many chunks
total_alloc = node * N;//here distribution the left memory
result = start_free;
start_free += total_alloc;
return static_cast<char*>(result);
}
else{//here to deal to cannot distibution one chunk
if(end_storage > 0){
my_free_list = freelist + index(end_storage);
((object*)(start_free))->next = *my_free_list;
*my_free_list = (object*)(start_free);
}
size_t get_bytes = 2 * total_alloc + ROUND_UP(heap_size >> 4);
start_free = static_cast<char*>(malloc(get_bytes));
object* p = nullptr;
if (start_free == NULL){//here it cannot to distibution anything
for (int i = N; i < MAX_BYTE; i += ALIGN){
my_free_list = freelist + index(i);
p = *my_free_list;
if (p){
(*my_free_list) = (p->next);
start_free = (char*)p;
end_free = start_free + i;
return chunk_alloc(N, node);
}
}
end_free = 0;
start_free = static_cast<char*>(malloc_alloc::allocate(get_bytes));
}
heap_size += get_bytes;
end_free = start_free + get_bytes;
return chunk_alloc(N, node);
}
}template<bool threads,int n>
void* __default_alloc_template<threads, n>::refill(size_t N){
int node = 20;//here node = 20 but I donot think it's reasonable;
char* pchunk= chunk_alloc(N, node);//here is to use the chunk alloc to get more than N bytes
object* volatile* my_free_list;
object* result = nullptr;
if (1 == node){
return (void*)(pchunk);//here illustrate the only one chunk
}
else{
my_free_list = freelist + index(N);
result = (object*)(pchunk);
//here It's customed memory
//here now the the chunk which result point to must be used now
// so *freelist pointer the second chunk
object* next_object = (object*)(pchunk + N);
*my_free_list = next_object;
object* cur_obj = nullptr;
for (int i = 1;; ++i){
cur_obj = next_object;
next_object = (object*)((char*)(cur_obj) + N);
if (node - 1 - i){
cur_obj = nullptr;
break;
}
else{
cur_obj->next = next_object;
}
}
}
return (void*)(result);
}看完了 我只想说一句话 ,真是STL的这个内存池真的很精髓,我基本上该分析的东西基本都分析到了,如果那里不懂的,可以随时向我提问;我乐意解答,只要我能帮到的;

本文深入剖析STL内存池的实现原理,包括内存分配、回收等关键环节,并重点介绍了chunk_alloc与refill函数的工作机制。

648

被折叠的 条评论
为什么被折叠?



