当前位置:网站首页>剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)
剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)
2022-08-06 13:00:00 【刚入门的代码spa技师】
剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)
deallocate内存回收函数
deallocate函数的定义:
static void deallocate(void* __p, size_t __n)
{
if (__n > (size_t) _MAX_BYTES)
malloc_alloc::deallocate(__p, __n);
else {
_Obj* __STL_VOLATILE* __my_free_list
= _S_free_list + _S_freelist_index(__n);
_Obj* __q = (_Obj*)__p;
// acquire lock
# ifndef _NOTHREADS
/*REFERENCED*/
_Lock __lock_instance;
# endif /* _NOTHREADS */
__q -> _M_free_list_link = *__my_free_list;
*__my_free_list = __q;
// lock is released here
}
}
deallocate函数需要接收两个参数:一个是回收内存的起始地址,另一个是回收内存的大小。
如果回收内存的大小大于128字节,调用的是malloc_alloc::deallocate(__p, __n);,该函数所做的工作就只有free一个动作:
如果释放的内存小于128字节,只需把这块chunk块以头插的方式,插入对应自由链表_S_free_list的结点即可。在多线程环境下,对自由链表的操作是需要加锁的。
reallocate内存扩充/缩小函数
reallocate函数定义:
template <bool threads, int inst>
void*
__default_alloc_template<threads, inst>::reallocate(void* __p,
size_t __old_sz,
size_t __new_sz)
{
void* __result;
size_t __copy_sz;
if (__old_sz > (size_t) _MAX_BYTES && __new_sz > (size_t) _MAX_BYTES) {
return(realloc(__p, __new_sz));
}
if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return(__p);
__result = allocate(__new_sz);
__copy_sz = __new_sz > __old_sz? __old_sz : __new_sz;
memcpy(__result, __p, __copy_sz);
deallocate(__p, __old_sz);
return(__result);
}
如果新内存和旧内存的大小都大于128字节,直接调用库函数里的realloc函数。
如果新内存和旧内存提升后的大小需要的都是同样大小的chunk块,则不需要做任何改变。
否则就是申请一块新内存,比较新旧内存的大小,把小内存的内容往大内存拷,再释放旧内存,把新内存地址返回即可。
边栏推荐
- Chemical composition of Q295HR steel plate
- 【 TypeScript will learn will be 】 you must know all about TypeScript
- 40 degrees high temperature, how to find the coolest place indoors through SOLIDWORKS?
- 博学谷学习记录】超强总结,用心分享 | mongodb基础用法
- 安全第七天课后练习
- AVS视频编码标准的演变:20年来的创新与发展
- Security on the sixth day practice after class
- 如何使用 Kubernetes Hooks 跟踪容器生命周期
- 【SSL集训DAY1】B【动态规划】
- Validate date format
猜你喜欢
随机推荐
40 degrees high temperature, how to find the coolest place indoors through SOLIDWORKS?
如何使用 Kubernetes Hooks 跟踪容器生命周期
Q295HR钢板化学成分
阿里巴巴2022全新出品亿级并发设计速成宝典(系统拆分,缓存,MQ,读写分离,分布分表,ES扩容应有尽有)
链表 | 双指针法 | 删除链表的倒数第 N 个结点 | leecode刷题笔记
哈希表 | 快乐数 | leecode刷题笔记
明德扬FmcAd9144 产品说明书
解析隐式类型转换操作operator double() const,带你了解隐式转换危害有多大
Shardingsphere 强制主库查询
Learned Valley Learning Records] Super summary, share with heart | Basic usage of mongodb
“恰好装满求最值”背包问题的初始化解析
Kubernetes daily troubleshooting
一文带你弄懂 CDN 的技术原理!
The most complete technical guide for 3D format conversion tool HOOPS Exchange (1): 4 major functional characteristics and typical usage scenarios
Dry goods | Those ingenious pooling operations, I burst after watching!
Rtklib-rinex文件的读取(rinex.c)-序言
Wang Shuang Assembly Language Chapter 6: Programs Containing Multiple Segments
SQL Injection Review Summary
Kubernetes 集群 Ingress 网关
密码学系列之:PEM和PKCS7,PKCS8,PKCS12









