您的位置:首页 > 编程语言 > C语言/C++

C++ allocator

2015-12-08 18:35 411 查看
说一说C++里的allocator。我们知道,C++ STL里定义了很多的容器(containers),每一个容器的第二个模板参数都是allocator类型。比方说在VC10里,vector类的模板声明为:

  template<class _Ty, class _Ax = allocator<_Ty> >

  class vector

但是,基本上很少有人会自定义一个allocator。一来,默认的allocator已经够用了;二来,确实不知道该怎么用。一般来说,我们没有必要重新定义一个allocator。自定义的方式主要是为了提高内存分配相关操作的性能。而STL提供的方式性能已经足够好了。事实上,在windows平台上,new的底层实现是基于C语言的malloc函数;malloc函数家族又是基于Windows HeapCreate、HeapAlloc、HeapFree等相关API来实现的(具体可以参考%VSInstallFolder%\VC\crt\src目录中的heapinit.c、malloc.c和new.cpp等相关函数)。

先撇开性能的问题不说,我们看一看如何实现一个自己的allocator。

在C++ 2003标准文档里,关于allocator的说明其实并不多,大概就20.1.5 Allocator requirements和20.4.1 The default allocator两处主要位置。虽然内容不多,但是足够我们写出一个自己的allocator。

根据Allocator requirements我们需要提供一些typedefs:

1: template <typename T>

2: class CHxAllocator

3: {

4: public:

5:     // typedefs...

6:     typedef T                   value_type;

7:     typedef value_type*         pointer;

8:     typedef value_type&         reference;

9:     typedef value_type const*   const_pointer;

10:     typedef value_type const&   const_reference;

11:     typedef size_t              size_type;

12:     typedef ptrdiff_t           difference_type;

13: 

14:     // rebind...

15:     template <typename _other> struct rebind { typedef CHxAllocator<_other> other; };

16: };


在这里有一个比较不太容易理解的东西:rebind。C++标准里这么描述rebind的:

The member class template rebind in the table above is effectively a typedef template: if the name Allocator is bound to SomeAllocator<T>, then
Allocator::rebind<U>::other is the same type as SomeAllocator<U>.

啥意思?可以用一个简单的例子来说明下:

学校都学过数据结构,比方说栈、单向列表、树。我们就拿栈和列表来对比,看看有什么大不一样的地方。撇开数据结构上的差异,从allocator的角度来看,我们可以发现:堆栈是存贮元素本身的,但是列表实际上不是直接存储元素本身的。要维护一个列表,我们至少还需要一个所谓的next的指针。因此,虽然是一个保存int的列表list<int>,但是列表存储的对象并不是int本身,而是一个数据结构,它保存了int并且还包含指向前后元素的指针。那么,list<int, allocator<int>>如何知道分配这个内部数据结构呢?毕竟allocator<int>只知道分配int类型的空间。这就是rebind要解决的问题。通过allocator<int>::rebind<_Node>()你就可以创建出用于分配_Node类型空间的分配器了。

接下来要提供其他的接口。根据The default allocator的描述,我们要提供如下一些接口:
pointer address(reference val) const
const_pointer address(const_reference val) const

返回val的地址
pointer allocate(size_type cnt, CHxAllocator<void>::const_pointer pHint = 0)分配空间。类似malloc。pHint可以无视,主要是给类库使用,用于提高性能。
void deallocate(pointer p, size_type n)释放空间,类似free。
size_type max_size() const throw()可分配的最大数量。
void construct(pointer p, const_reference val)在地址p所指向的空间,使用val进行填充。需要使用到palcement new,以便保证调用到构造函数。
void destroy(pointer p)析构p指向的内存块中内容。一般通过显示调研析构函数来执行。
allocator() throw ()

allocator(const_reference) throw ()

template <typename _other> allocator(CHxAllocator <_other> const&) throw()

~CHxAllocator() throw()
各种构造函数和析构函数
如何实现上面这些函数,你只要照抄标准库中的实现就可以了。如果你想要用c的malloc和free来实现,也可以这么写:

1: pointer allocate(size_type cnt, CHxAllocator<void>::const_pointer pHint = 0)

2: {

3:     UNREFERENCED_PARAMETER(pHint);

4: 

5:     if (cnt <= 0)

6:     {

7:         return 0 ;

8:     }

9: 

10:     void* pMem = nullptr ;

11:     if (max_size() < cnt || (pMem = malloc(cnt * sizeof(value_type))) == NULL)

12:     {

13:         throw std::bad_alloc(0);

14:     }

15: 

16:     return static_cast <pointer>(pMem);

17: }

18: 

19: void deallocate(pointer p, size_type)

20: {

21:     free(p);

22: }
23: 

24: void construct(pointer p, const_reference val)

25: {

26:     :: new ((void *)p) T(val);

27: }

28: 

29: void destroy(pointer p)

30: {

31:     p->~T();

32: }


基本上,我们就简单实现了一个自己的allocator。另外,除了这些最主要的接口函数,你还需要实现比较操作符==和!=,但是这些函根据标准文档,都直接返回true和false。

开头已经说了,重写allocator的主要目的是为了提高性。那怎样才能提高性能呢?直接使用Windows的HeapXXXX堆内存API?其实,你自己用一下就会发现,性能提升并不明显。因为通过new,再通过malloc,最后通过HeapAlloc不比直接调用HeapAlloc多几句话。如何实现一个高性能的allocator,需要借助memory pool的想法。另外,侯捷的stl源码剖析里分析了SGI STL利用类似想法实现的一个alloc。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: