当前位置:网站首页>剖析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函数使用即可。
边栏推荐
- 用代码收集每天热点内容信息,并发送到自己的邮箱
- cmd命令
- 【BERT-多标签文本分类实战】之二——BERT的地位与名词术语解释
- typescript4-安装编译ts的工具包
- OA项目之待开会议&历史会议&所有会议
- Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
- typescript6 - simplify the steps to run ts
- Handler消息机制-Native层
- hcip 第14天学习笔记
- 【sleuth + zipkin 服务链路追踪】
猜你喜欢

40.【vector的运用】

Alibaba Cloud Cloud Server Firewall Settings

hcip实验

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

Burpsuite几种爆破方式

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

【Kotlin 中的类和继承】

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

SQL row-column conversion

RFID固定资产盘点系统给企业带来哪些便利?
随机推荐
test4
OA项目之待开会议&历史会议&所有会议
IDEA search plug-in has no results and the solution has been spinning in circles
mysql8的my.conf配置文件参考指引
经典毕业设计:基于SSM实现高校后勤报修系统
MongoDB - 千万级数据脚本过滤笔记
联想笔记本 如何更改Win10系统开机logo图标
Monkey and Banana
【sleuth + zipkin 服务链路追踪】
[GAN]老照片修复Bringing Old Photos Back to Life论文总结
SQL的ROUND函数用法及其实例
Handler消息机制-Native层
typescript8 - type annotations
Get all interface paths and names in the controller
求大佬解答,这种 sql 应该怎么写?
cmd命令
数据分发服务 (DDS) 内置主题
【Kotlin 中的类和继承】
typescript1-typescript是什么
redis的内存淘汰策略