当前位置:网站首页>剖析SGI STL空间配置器(_S_refill内存块填充函数)
剖析SGI STL空间配置器(_S_refill内存块填充函数)
2022-07-30 07:38:00 【刚入门的代码spa技师】
剖析SGI STL空间配置器(_S_refill内存块填充函数)
通过allocate函数申请内存块时,会在对应自由链表下取内存块,当该位置下没有内存块链时,就需要调用_S_refill,填充该位置下的内存块,比如刚开始时,每个自由链表_S_free_list的结点都为nullptr,就需要填充内存块。
_S_refill的函数定义如下:
template <bool __threads, int __inst>
void* __default_alloc_template<__threads, __inst>::_S_refill(size_t __n)
{
int __nobjs = 20;
char* __chunk = _S_chunk_alloc(__n, __nobjs);
_Obj* __STL_VOLATILE* __my_free_list;
_Obj* __result;
_Obj* __current_obj;
_Obj* __next_obj;
int __i;
if (1 == __nobjs) return(__chunk);
__my_free_list = _S_free_list + _S_freelist_index(__n);
/* Build free list in chunk */
__result = (_Obj*)__chunk;
*__my_free_list = __next_obj = (_Obj*)(__chunk + __n);
for (__i = 1; ; __i++) {
__current_obj = __next_obj;
__next_obj = (_Obj*)((char*)__next_obj + __n);
if (__nobjs - 1 == __i) {
__current_obj -> _M_free_list_link = 0;
break;
} else {
__current_obj -> _M_free_list_link = __next_obj;
}
}
return(__result);
}
参数__n是要填充chunk块的大小,要填充chunk块,内存来源就需要继续向下申请了,char* __chunk = _S_chunk_alloc(__n, __nobjs);的功能就是。__chunk是返回的内存块首地址,__nobjs以引用的形式传进_S_chunk_alloc,_S_chunk_alloc执行结束__nobjs的值就是申请的__n大小的chunk块的块数了。
申请完成后,_S_refill函数还需要把申请到的chunk块串起来。
举个例子,参数__n为8,通过_S_chunk_alloc申请到了5块的内存,也就是40字节,后面的过程可以用下面这张图来简单概括:
最后把申请到的内存块首地址返回给allocate函数使用即可。
边栏推荐
猜你喜欢

立创EDA——PCB的走线(五)

Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing

Mybitatis related configuration files

42.【vector简单列题】

typescript2-typescript为什么给js添加类型支持

潜心打磨,主动求变——这群技术排头兵,如何做好底层开发这件事?
![[Mini Program Column] Summarize the development specifications of uniapp to develop small programs](/img/7b/110d324eba00652e4987bc623a5bc6.png)
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs

孙洪鹤讲教材:原点+众筹+产品,逆向营销实战操作方案

手把手教学OneOS FOTA升级

开创ETC生态建设新格局 JASMINER新一批X4服务器陆续发出
随机推荐
typescript4 - installs a toolkit for compiling ts
SQL窗口函数
The blockbuster IP that has been popular in the world for 25 years, the new work has become a script paradise
基于JSP实现校园二手交易平台
mysql8的my.conf配置文件参考指引
npm指令
Portable small fan PD power chip
实现定时器
开创ETC生态建设新格局 JASMINER新一批X4服务器陆续发出
typescript6-简化运行ts的步骤
LeetCode:647. 回文子串
File类
ipset restore命令维护set,但原已存在的条目未删除掉
hcip实验
代币(双代币)系统研究
SQL substring_index() usage - MySQL string interception
2022牛客暑期多校训练营3(ACFGJ)
【Flask框架②】——第一个Flask项目
「活动推荐」探索未来:数字科技
RT-Thread-GD32的SPI在切换同一总线不同挂载设备时会出现切换后乱发送数据的问题