当前位置:网站首页>剖析SGI STL空间配置器(一 、辅助接口函数)
剖析SGI STL空间配置器(一 、辅助接口函数)
2022-07-30 07:38:00 【刚入门的代码spa技师】
剖析SGI STL空间配置器(一 、辅助接口函数)
SGI STL空间配置器有两个重要的辅助函数:
static size_t _S_round_up(size_t __bytes)
{
return (((__bytes) + (size_t) _ALIGN-1) & ~((size_t) _ALIGN - 1)); }
static size_t _S_freelist_index(size_t __bytes) {
return (((__bytes) + (size_t)_ALIGN-1)/(size_t)_ALIGN - 1);
}
_S_round_up函数分析
static size_t _S_round_up(size_t __bytes)
{
return (((__bytes) + (size_t) _ALIGN-1) & ~((size_t) _ALIGN - 1)); }
在申请小块内存时,为了便于管理内存块和加快内存的分配速度,SGI STL空间配置器通过_S_round_up函数对申请的小块内存大小提升至最邻近_ALIGN的倍数,_ALIGN值默认为8。参数__bytes是需要申请内存的大小。~((size_t) _ALIGN - 1))得到的值是~7,用二进制表示就是11111111 11111111 11111111 11111000,用一个数&上~7,就可以确保低三位必定为0,高三位以上的1都会保留,就可以确保得到的数必定是8的倍数。
我们需要的是提升到8的倍数,7的二进制是111,如果__bytes的低三位有1,就会满8进1,多余的1就会在和11111111 11111111 11111111 11111000时过滤掉,所以需要加上7。
复杂的位操作让我们搞不清这个函数要干什么,其实我们可以把这个表达式替换成±*/%组成的表达式,只是在计算机中位操作比四则运算效率要高,这个表达式等价于
static size_t _S_round_up(size_t __bytes)
{
return (__bytes + _ALIGN - 1) / _ALIGN * _ALIGN;
}
这个表达式就可以很清晰的表达这个函数想要的结果,举几个例子就可以明白这个函数的功能。
测试代码:
#include <iostream>
using namespace std;
enum{
_ALIGN =8 };
static size_t _S_round_up1(size_t __bytes)
{
return (((__bytes)+(size_t)_ALIGN - 1) & ~(_ALIGN - 1));
}
static size_t _S_round_up2(size_t __bytes)
{
return (__bytes + _ALIGN - 1) / _ALIGN * _ALIGN;
}
int main()
{
for (int i = 0; i <= 128; i++)
{
cout << i << " " << _S_round_up1(i) << " " << _S_round_up2(i) << endl;
}
return 0;
}

_S_freelist_index函数分析
static size_t _S_freelist_index(size_t __bytes) {
return (((__bytes) + (size_t)_ALIGN-1)/(size_t)_ALIGN - 1);
}
申请内存时,自由链表取下一个内存块,而自由链表维护的chunk块是按8字节对齐的,在申请时会对大小进行提升,在归还chunk块时也要归还到对应大小chunk块的下标。这个函数的功能就是用来定位__bytes大小的内存chunk块在自由链表对应的下标的。
边栏推荐
- typescript6 - simplify the steps to run ts
- 智能存储柜——解决您的存储需求
- C13—使用innosetup工具制作软件安装向导2022-07-23
- MagicDraw secondary development process
- 电脑文档误删除怎么恢复,恢复误删除电脑文档的方法
- Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing
- redis多节点部署实施指引
- Judging from the Internet:
- Selected as one of the "Top Ten Hard Core Technologies", explaining the technical points of Trusted Confidential Computing (TECC) in detail
- 【sleuth + zipkin 服务链路追踪】
猜你喜欢
随机推荐
input标签的tabindex属性 & a标签的tabindex属性
MagicDraw secondary development process
typescript6 - simplify the steps to run ts
蓝牙技术|了解蓝牙LE Audio的Auracast广播音频
【sleuth + zipkin 服务链路追踪】
leetcode经典问题——11.盛水最多的容器
SQL substring_index() usage - MySQL string interception
Why does typescript2-typescript add type support to js
动态规划专栏
JS中对事件流的理解
Link with Bracket Sequence II(杭电多校赛)
function (1)
AutoSAR EcuM系列02- Fixed EcuM的状态管理
cmd命令
linux安装mysql8参考指引
hcip第八天
tabindex attribute of input tag & tabindex attribute of a tag
C语言力扣第46题之全排列。回溯法
Fix datagrip connection sqlserver error: [08S01] The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption.
【Kotlin 中的类和继承】






![[GAN]老照片修复Bringing Old Photos Back to Life论文总结](/img/6e/80260ec72029ed6316763bf051fbb4.png)


