1.以end_free - start_free来判断内存池的水量.
2.如果水量充足.就直接调出20个区块返回给free_list.如果不足20,但是能提供一个以上.就拔出这些.这时,其nobjs就被修改为实际的供应的区块数.nobjs传的是引用 .如果一块也不能供应 ,此时就必须利用malloc()从heap中配置内存,再加上一个越来越大的附加量.
3.万一山穷水尽,整个system heap空间都不够了,malloc()行动失败, chunk_alloc()就四处寻找有无"尚有未用区块.且区块足够大之 free_list.找到就挖出.找不到就调用一级适配器,那里有out-of-memory处理机制.
- //内存池
-
//从内存池中取出空间给 free list 使用,是chunk_alloc()的工作
-
template <bool threads, int inst>
-
char * _default_alloc_template<threads, inst>::chunk_alloc(size_t size,int & nobjs)
-
{
-
char *result;
-
size_t total_bytes = size*nobjs;
-
size_t byte_left = end_free - start_free;//内存池剩余的空间
-
if (bytes_left >= tetal_bytes)//内存池剩余空间完全满足需求量
-
{
-
result = start_free;
-
start += total_bytes;
-
return result;
-
}
-
else if (bytes_left >= size)//内存池剩余空间不能完全满足需求量,但是能够供应一个以上的区块
-
{
-
nobjs = byte_left/size;
-
total_bytes = size*nobjs;
-
result = start_free;
-
start_free += total_bytes;
-
return result;
-
}
-
else//一个都无法提供
-
{
-
size_t bytes_to_get = 2*total_bytes + ROUND_UP(heap_size >> 4);
-
-
//这部分是充分利用剩余的零头
-
if (bytes_left > 0)
-
{
-
obj * volatile * my_free_list = free_list + FREELIST_INDEX(bytes_left);
-
((obj*)start_free) -> free_list_link = *my_free_list;
-
*my_free_list = (obj *)start_free;
-
}
-
-
//配置heap空间,用来补充内存池
-
star_free = (char *)malloc(bytes_to_get);
-
if (0 == start_free)//heap空间不足,malloc失败
-
{
-
int i;
-
obj * volatile * my_free_list,p;
-
for (i = size; i <= _MAX_BYTES; i += _ALIGN)
-
{
-
my_free_list = free_list + FREELIST_INDEX(i);
-
p = *my_free_list;
-
if ( 0 != p)
-
{
-
*my_free_list = p -> free_list_link;
-
start_free = (char *)p;
-
end_free = start_free +i;
-
return chunk_alloc(size,nobjs);//递归调用,修正nobjs
-
}
-
}
-
end_free = 0;
-
start_free = (char *)malloc_alloc::allocate(byte_to_get);
-
}
-
-
heap_size += bytes_to_get;
-
end_free = start_free + bytes_to_get;
-
return chunk_alloc(size,nobjs);//递归调用,修正nobjs
-
}
-
}
阅读(1764) | 评论(0) | 转发(0) |