STL — construct


本文原创

SGI STL 中的分配器使用的不是 std::allocator,该分配器定义在头文件 defalloc.h 中,只是简单地封装了 ::operator::new 和 ::operator::delete,在 SGI STL 中,该分配器从不使用,SGI STL 的内存分配策略将内存申请释放与构造函数析构函数的调用分离开来,分配器 malloc_alloc 和 alloc 只负责内存的分配和释放,而构造函数的调用由接口 construct 负责,析构函数的调用由接口 destroy 负责,头文件 stl_construct.h 中定义了这些接口函数。construct 相关定义如下:

template <class _T1, class _T2>
inline void _Construct(_T1* __p, const _T2& __value)
{
    new ((void*) __p) _T1(__value);
}

template <class _T1>
inline void _Construct(_T1* __p)
{
    new ((void*) __p) _T1();
}

template <class _T1, class _T2>
inline void construct(_T1* __p, const _T2& __value)
{
    _Construct(__p, __value);
}

template <class _T1>
inline void construct(_T1* __p)
{
    _Construct(__p);
}

construct 只是对 _Construct 调用做了一层封装,在 _Construct 内使用了 placement new,在指定的内存位置 (void *)p 上构建新对象,该构建过程不需开辟新内存,只需调用对象的构造函数即可。

destroy相关定义如下:

template <class _Tp>
inline void _Destroy(_Tp* __pointer)
{
    __pointer->~_Tp();
}

template <class _ForwardIterator>
void __destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type)
{
    for ( ; __first != __last; ++__first)
        destroy(&*__first);
}

template <class _ForwardIterator> 
inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}

template <class _ForwardIterator, class _Tp>
inline void __destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*)
{
    typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
    __destroy_aux(__first, __last, _Trivial_destructor());
}

template <class _ForwardIterator>
inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
{
    __destroy(__first, __last, __value_type(__first));
}

inline void _Destroy(char*, char*) {}
inline void _Destroy(int*, int*) {}
inline void _Destroy(long*, long*) {}
inline void _Destroy(float*, float*) {}
inline void _Destroy(double*, double*) {}
inline void _Destroy(wchar_t*, wchar_t*) {}

template <class _Tp>
inline void destroy(_Tp* __pointer)
{
    _Destroy(__pointer);
}

template <class _ForwardIterator>
inline void destroy(_ForwardIterator __first, _ForwardIterator __last)
{
    _Destroy(__first, __last);
}

同样,destroy 也是封装了 _Destroy 的调用,_Destroy 使用 __value_type 获取迭代器指向对象的类型,调用的 __destroy 分析该类似是否有 trivial destructor,若有则调用什么也不做版本的 __destroy_aux,否则调用的 __destroy_aux 中对每个迭代器解引用后调用 _Destroy,后者中显式调用对象的析构函数。

此外针对内置具体类型的指针定义了各个非模板重载函数版本,从而在一开始调用 _Destroy(__first, __last); 的时候优先进行匹配,从而无需进行 __value_type 类型判断,节省了编译开销。

Leave a comment

邮箱地址不会被公开。 必填项已用*标注