内存配置器alloc

来源:互联网 发布:ubuntu 开机自启动 编辑:程序博客网 时间:2024/06/05 13:22
   虽然STL的内存配置器在我们的实际应用中几乎不用涉及,但它却在STL的各种容器背后默默做了大量的工作,STL内存配置器为容器分配并管理内存。统一的内存管理使得STL库的可用性、可移植行、以及效率都有了很大的提升,因此STL内存配置器是STL库中最重要的组成部分之一。
 
1、Constructor和Destroy
 
我们所习惯的C++内存空间分配动作如下:
  1. class Foo { ... };
  2. Foo* pObj = new Foo;
  3. delete pObj; 
其中new操作符的动作可以分为两部分:1. 为对象分配内存空间(alloc::allocate 函数);2. 调用对象的构造函数(alloc::construct 函数)
delete操作符的动作也可以分为两部分:1. 调用对象的析构函数 (alloc::deallocate 函数);2. 释放对象占用的内存空间(alloc::destroy 函数)
 
 在STL库的空间配置器中,new和delete的这两个过程都是分开的,这还要归功于C++语言的placement new操作符(具体请参考C++Primer)。
 
 
construct源码
  1. template <class _T1, class _T2>
  2. inline void _Construct(_T1* __p, const _T2& __value) {
  3.   new ((void*) __p) _T1(__value);         // 把初始值设定到指定的空间上,placement new操作符(参考C++ Primer)
  4.                                                                  // 调用T1::T1(value)
  5. }
  6. template <class _T1>
  7. inline void _Construct(_T1* __p) {
  8.   new ((void*) __p) _T1();                       // 把初始值设定到指定的空间上,placement new操作符,调用T1::T1()
  9. }
  10. template <class _T1, class _T2>
  11. inline void construct(_T1* __p, const _T2& __value) {        // construct的第一个版本
  12.   _Construct(__p, __value);
  13. }
  14. template <class _T1>
  15. inline void construct(_T1* __p) {                           // construct的第二个版本
  16.   _Construct(__p);
  17. }
destroy源码
  1. template <class _Tp>
  2. inline void _Destroy(_Tp* __pointer) {
  3.   __pointer->~_Tp();                            // 显式调用Tp::~Tp()
  4. }
  5. template <class _ForwardIterator>
  6. inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) {
  7.   __destroy(__first, __last, value_type(__first));
  8. }
 
  1. // 以下这些基本数据类型在销毁时都不需要调用对象的析构函数
  2. inline void _Destroy(char*, char*) {}        // char* 类型的特化
  3. inline void _Destroy(int*, int*) {}            // int* 类型的特化
  4. inline void _Destroy(long*, long*) {}        // long* 类型的特化
  5. inline void _Destroy(float*, float*) {}        // float*类型的特化
  6. inline void _Destroy(double*, double*) {}    // double*类型的特化
  7. #ifdef __STL_HAS_WCHAR_T
  8. inline void _Destroy(wchar_t*, wchar_t*) {}    // wchar_t* 类型的特化
  9. #endif /* __STL_HAS_WCHAR_T */
  10. template <class _ForwardIterator, class _Tp>    // 泛化
  11. inline void 
  12. __destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*)
  13. {
  14.   typedef typename __type_traits<_Tp>::has_trivial_destructor  _Trivial_destructor;
  15.   __destroy_aux(__first, __last, _Trivial_destructor());    // _Trivial_destructor() 会返回Tp类型的对象是否存在non-trivial destructor
  16. }
  17. template <class _ForwardIterator>
  18. void
  19. __destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type)
  20. {
  21.   for ( ; __first != __last; ++__first)        // 如果该类型的对象存在non-trivial destructor,则调用destroy的第一个版本来析构对象
  22.     destroy(&*__first);
  23. }
  24. template <class _ForwardIterator> 
  25. inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}    // 如果该类型的对象不存在non-trivial destructor,什么也不用做
  26. template <class _Tp>
  27. inline void destroy(_Tp* __pointer) {        // destroy的第一个版本,析构单个Tp对象
  28.   _Destroy(__pointer);
  29. }
  30. template <class _ForwardIterator>
  31. inline void destroy(_ForwardIterator __first, _ForwardIterator __last) {        // destroy的第二个版本,析构first,last之间的所有Tp对象
  32.   _Destroy(__first, __last);
  33. }
 
 
 
两个问题:
1. C++并不支持通过value_type来判断指针所指对象的类型,STL中通过什么来判断?
2. C++也不支持判断一个对象是否存在析构函数,STL如何来实现判断?
 
第一个问题参考头文件<stl_iterator.h>
  1. template <class interator>
  2. inline typename iterator_traits<interator>::value_type* 
  3. value_type(const interator&)
  4. {
  5.     return static_cast<typename iterator_triats<interator>::value_type*>(0);
  6. }
根本目的就是定义了一个value_type类型的空指针,作用是为了函数重载
 
第二个问题请参看文章:《神奇的__type_traits》
 
2、空间的分配与释放

 SGI使用双层配置器,如果分配的空间大于128bytes则采用第一级配置器,直接调用malloc(),free();如果分配的空间小于128bytes则采用复杂一点的memory pool方式。在SGI中一级配置器的实现依靠的是模板类__malloc_alloc_template,二级配置器依靠模板类__default_alloc_template

 SGI STL中的几种配置器
1.
 
1、__malloc_alloc_template是一个基础配置器,直接调用malloc和free来分配和释放空间,如果空间不够会调用handler处理过后再重新分配。__malloc_alloc_template配置器被作为一级配置器来使用。
  1. template <int inst>
  2. class __malloc_alloc_template {
  3. private:
  4.     static void *oom_malloc(size_t);                // out of memory的情况下会调用此函数分配
  5.     static void *oom_realloc(void *, size_t);    // out of memory的情况下会调用此函数重新分配
  6.     static void (* __malloc_alloc_oom_handler)();   // out of memory情况下会如果这个handler存在就会被调用
  7. public:
  8.     static void * allocate(size_t n)
  9.     {
  10.         void *result = malloc(n);                          // 调用 普通的malloc函数直接分配
  11.         if (0 == result) result = oom_malloc(n);    // 如果分配失败才调用oom_malloc
  12.         return result;
  13.     }
  14.     static void deallocate(void *p, size_t /* n */)
  15.     {
  16.         free(p);        // 直接调用free函数释放
  17.     }
  18.     static void * reallocate(void *p, size_t /* old_sz */size_t new_sz)
  19.     {
  20.         void * result = realloc(p, new_sz);        // 调用普通的realloc函数重新分配
  21.         if (0 == result) result = oom_realloc(p, new_sz);  // 如果失败就调用oom_realloc函数重新分配
  22.         return result;
  23.     }
  24.     static void (* set_malloc_handler(void (*f)()))()        // 为out of memory的情况设置handler
  25.     {
  26.         void (* old)() = __malloc_alloc_oom_handler;
  27.         __malloc_alloc_oom_handler = f;
  28.         return(old);
  29.     }
  30. };
  31. template <int inst>
  32. void * __malloc_alloc_template<inst>::oom_malloc(size_t n)
  33. {
  34.     void (* my_malloc_handler)();
  35.     void *result;
  36.     for (;;) {
  37.         my_malloc_handler = __malloc_alloc_oom_handler;
  38.         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
  39.         (*my_malloc_handler)();        // 如果存在handler就先调用handler回收能够回收的内存
  40.         result = malloc(n);                // 然后再尝试malloc
  41.         if (result) return(result);
  42.     }
  43. }
  44. template <int inst>
  45. void * __malloc_alloc_template<inst>::oom_realloc(void *p, size_t n)
  46. {
  47.     void (* my_malloc_handler)();
  48.     void *result;
  49.     for (;;) {
  50.         my_malloc_handler = __malloc_alloc_oom_handler;
  51.         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
  52.         (*my_malloc_handler)();        // 如果存在handler就先调用handler回收能够回收的内存
  53.         result = realloc(p, n);            // 然后再尝试realloc
  54.         if (result) return(result);
  55.     }
  56. }
  57. typedef __malloc_alloc_template<0> malloc_alloc;    // STL的一级内存配置器
2.、__default_alloc_template也是一个基础配置器,和__malloc_alloc_template相比多了一下几个功能:
 
A. 多线程的支持,包括资源的加锁解锁
B. 分配空间的大小按8字节对齐
C. 如果分配的空间大于128字节,直接调用一级配置器的malloc_alloc::alloc函数来分配
D. 维护了16个自由空间的链表(free list),每个链表都链接了一串相等大小的自由空间,各个链表节点的自由空间大小不同,但都是8的倍数(8、16、24......96、104、112、120、128刚好16级)
E. 如果分配的空间小于128字节,找到一个链表,使得该链表节点的自由空间大小大于请求分配的空间而又与之大小最接近,从该链表中取出一个节点分配
F. 维护了一个memory pool,如果所有自由空间列表为空就会从记忆池中申请空间,记忆池尽量返回可用的空间。
 
__default_alloc_template 通常被作为二级配置器来使用,由于该类的代码非常繁杂,这里就不列出来了。有兴趣可以阅读STL的源代码。
  1. template <bool threads, int inst>
  2. class __default_alloc_template {
  3.     ...
  4. };
  5. typedef __default_alloc_template<__NODE_ALLOCATOR_THREADS, 0> alloc;
  6. typedef __default_alloc_template<false, 0> single_client_alloc;
原创粉丝点击