__pool<true>的初始化
__pool<true>的初始化工作同样包括2个部分,对象构造和初始化。
356 explicit __pool()
357 : _M_bin(NULL), _M_bin_size(1), _M_thread_freelist(NULL)
358 { }
359
360 explicit __pool(const __pool_base::_Tune& __tune)
361 : __pool_base(__tune), _M_bin(NULL), _M_bin_size(1),
362 _M_thread_freelist(NULL)
363 { }
构造函数基本和__pool<false>一样,除了多出一个_M_thread_freelist成员变量。注意_M_bin_size仍是被初始化为1。
初始化函数_M_initialize是让我觉得头疼的函数之一,不仅仅是因为它很长(159行),而且因为它代码缩进混乱,变量命名有歧义等等。所以我不得不对代码进行一些“修整”工作,以便顺利的阅读,不过下面的示例代码我还是保持原样。
<mt_allocator.cc>
421 void
422 __pool<true>::_M_initialize()
函数_M_initialize的原型。前面有部分代码和__pool<false>::_M_initialize一样,所以不用详细解释。
423 {
424 // _M_force_new must not change after the first allocate(),
425 // which in turn calls this method, so if it's false, it's false
426 // forever and we don't need to return here ever again.
427 if (_M_options._M_force_new)
428 {
429 _M_init = true;
430 return;
431 }
如果_M_force_new为true,则不需要额外的初始化工作,因为所有的内存操作都通过new和delete来完成。
433 // Create the bins.
434 // Calculate the number of bins required based on _M_max_bytes.
435 // _M_bin_size is statically-initialized to one.
436 size_t __bin_size = _M_options._M_min_bin;
437 while (_M_options._M_max_bytes > __bin_size)
438 {
439 __bin_size <<= 1;
440 ++_M_bin_size;
441 }
计算bin的个数,存放在_M_bin_size里。
443 // Setup the bin map for quick lookup of the relevant bin.