__mt_alloc源码分析(12)

__pool<true>的销

终于,我们来到了最后一站,本节完后,整个mt allocator源码分析也就结束了。和__pool<false>一样,_M_destroy函数其实从未被调用过,但是为了保持设计的完整性,这个函数还是有意义的。

 

<mt_allocator.cc>

209    void

210    __pool<true>::_M_destroy() throw()

 

_M_destroy函数原型。

 

211    {

212      if (_M_init && !_M_options._M_force_new)

213        {

214     if (__gthread_active_p())

 

判断是否支持多线程。

 

215       {

216         for (size_t __n = 0; __n < _M_bin_size; ++__n)

217           {

218         _Bin_record& __bin = _M_bin[__n];

219         while (__bin._M_address)

220           {

221             _Block_address* __tmp = __bin._M_address->_M_next;

222             ::operator delete(__bin._M_address->_M_initial);

223             __bin._M_address = __tmp;

 

归还每个binOS申请过的内存块,这个信息是在_M_address里存储的。虽然某些用户使用的内存块可能在线程间传递,但是从OS直接申请的内存块却是从不会混淆的。

 

224           }

225         ::operator delete(__bin._M_first);

226         ::operator delete(__bin._M_free);

227         ::operator delete(__bin._M_used);

228         ::operator delete(__bin._M_mutex);

 

释放bin的辅助数据。

 

229           }

230       }

231     else

 

在没有多线程支持的时候,采取的操作。

 

232       {

233         for (size_t __n = 0; __n < _M_bin_size; ++__n)

234           {

235         _Bin_record& __bin = _M_bin[__n];

236         while (__bin._M_address)

237           {

238             _Block_address* __tmp = __bin._M_address->_M_next;

239             ::operator delete(__bin._M_address->_M_initial);

240             __bin._M_address = __tmp;

241           }

242         ::operator delete(__bin._M_first);

 

同样是释放从OS申请的内存块,只是bin的辅助数据这时要少些。

 

243           }

244       }

245     ::operator delete(_M_bin);

246     ::operator delete(_M_binmap);

 

释放bin数组和_M_binmap映射数组。

 

247        }

248    }

 

至此,对mt allocator的源码分析全部完成了。其实这篇文章并没有覆盖mt allocator所有的源码,因为我觉得没有必要把每个角落都交代得清清楚楚,有时让读者自己思考一下,会更有效果。由于本人水平有限,文章中难免出现谬误之处,欢迎广大读者批评和指正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值