STL map的自定义内存分配器

15
这个问题是关于在插入std::map时构建自定义分配器实例的。以下是一个用于std::map的自定义分配器,以及一个使用它的小程序:
#include <stddef.h>
#include <stdio.h>
#include <map>
#include <typeinfo>

class MyPool {
public:
  void * GetNext() {
    return malloc(24);
  }
  void Free(void *ptr) {
    free(ptr);
  }
};

template<typename T>
class MyPoolAlloc {
public:
  static MyPool *pMyPool;

  typedef size_t     size_type;
  typedef ptrdiff_t  difference_type;
  typedef T*         pointer;
  typedef const T*   const_pointer;
  typedef T&         reference;
  typedef const T&   const_reference;
  typedef T          value_type;

  template<typename X>
  struct rebind
  { typedef MyPoolAlloc<X> other; };

  MyPoolAlloc() throw() {
    printf("-------Alloc--CONSTRUCTOR--------%08x %32s\n", this, typeid(T).name());
  }

  MyPoolAlloc(const MyPoolAlloc&) throw()  {
    printf(" Copy Constructor ---------------%08x %32s\n", this, typeid(T).name());
  }

  template<typename X>
  MyPoolAlloc(const MyPoolAlloc<X>&) throw() {
    printf(" Construct T Alloc from X Alloc--%08x %32s %32s\n", this, typeid(T).name(), typeid(X).name());
  }

  ~MyPoolAlloc() throw() {
    printf(" Destructor ---------------------%08x %32s\n", this, typeid(T).name());
  };

  pointer address(reference __x) const { return &__x; }

  const_pointer address(const_reference __x) const { return &__x; }

  pointer allocate(size_type __n, const void * hint = 0) {
    if (__n != 1)
      perror("MyPoolAlloc::allocate: __n is not 1.\n");
    if (NULL == pMyPool) {
      pMyPool = new MyPool();
      printf("======>Creating a new pool object.\n");
    }
    return reinterpret_cast<T*>(pMyPool->GetNext());
  }

  //__p is not permitted to be a null pointer
  void deallocate(pointer __p, size_type __n) {
    pMyPool->Free(reinterpret_cast<void *>(__p));
  }

  size_type max_size() const throw() {
    return size_t(-1) / sizeof(T);
  }

  void construct(pointer __p, const T& __val) {
    printf("+++++++ %08x %s.\n", __p, typeid(T).name());
    ::new(__p) T(__val);
  }

  void destroy(pointer __p) {
    printf("-+-+-+- %08x.\n", __p);
    __p->~T();
  }
};

template<typename T>
inline bool operator==(const MyPoolAlloc<T>&, const MyPoolAlloc<T>&) {
  return true;
}

template<typename T>
inline bool operator!=(const MyPoolAlloc<T>&, const MyPoolAlloc<T>&) {
  return false;
}

template<typename T>
MyPool* MyPoolAlloc<T>::pMyPool = NULL;

int main(int argc, char *argv[]) {

  std::map<int, int, std::less<int>, MyPoolAlloc<std::pair<const int,int> > > m;
  //random insertions in the map
  m.insert(std::pair<int,int>(1,2));
  m[5] = 7;
  m[8] = 11;
  printf("======>End of map insertions.\n");
  return 0;
}

下面是程序的输出结果:
-------Alloc--CONSTRUCTOR--------bffcdaa6                     St4pairIKiiE
从X Alloc构造T Alloc--bffcda77  St13_Rb_tree_nodeISt4pairIKiiEE                     St4pairIKiiE
复制构造函数---------------bffcdad8  St13_Rb_tree_nodeISt4pairIKiiEE
析构函数---------------------bffcda77  St13_Rb_tree_nodeISt4pairIKiiEE
析构函数---------------------bffcdaa6                     St4pairIKiiE
======>创建一个新池对象。
从X Alloc构造T Alloc--bffcd9df                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d028 St4pairIKiiE。
析构函数---------------------bffcd9df                     St4pairIKiiE
从X Alloc构造T Alloc--bffcd95f                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d048 St4pairIKiiE。
析构函数---------------------bffcd95f                     St4pairIKiiE
从X Alloc构造T Alloc--bffcd95f                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d068 St4pairIKiiE。
析构函数---------------------bffcd95f                     St4pairIKiiE
======>结束映射插入。
从X Alloc构造T Alloc--bffcda23                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d068。
析构函数---------------------bffcda23                     St4pairIKiiE
从X Alloc构造T Alloc--bffcda43                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d048。
析构函数---------------------bffcda43                     St4pairIKiiE
从X Alloc构造T Alloc--bffcda43                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d028。
析构函数---------------------bffcda43                     St4pairIKiiE
析构函数---------------------bffcdad8  St13_Rb_tree_nodeISt4pairIKiiEE
输出结果的最后两列显示,每次向映射中插入一个元素时都会构造一个std::pair <const int,int>的分配器。为什么要这样做?有没有办法抑制这种行为?
谢谢!
注:此代码在带有g++版本4.1.2的x86机器上测试。如果您希望在64位机器上运行它,您至少必须更改return malloc(24)一行。更改为return malloc(48)应该可以解决问题。
3个回答

1

这是因为分配器是针对std::pair<const int, int>,但实际上需要分配一个更复杂的数据结构,其中它是成员之一。虽然我希望实际所需的分配器被构建和缓存,但每次重新构建它并不违法。这是一个实现细节,如果不改变你的实现,你无法避免。实际创建的分配器类型是St13_Rb_tree_nodeISt4pairIKiiEE(mangled name)。


2
在C++03中,实现允许假设分配器是无状态的(空的)。这样,创建副本几乎是免费的。在您的情况下,您可能可以使复制构造函数和赋值运算符复制内部指针,而不是每次创建新的池。 - Bo Persson
@Bo - 这个问题的主题不是复制构造函数!相反,它是创建一个St4pairIKiiE的分配器构造函数,该构造函数来自于一个St13_Rb_tree_nodeISt4pairIKiiEE的分配器。 - Prasoon Tiwari
1
@Prasoon 为什么要费心呢?如果你去掉跟踪语句,编译器会优化掉构造和析构过程,所以不会有额外的开销。 - Alan Stokes
1
@Prasoon 这是一个特性。它与可怕的 rebind 成员模板有关。正如 Scrubbins 所说,您为一种类型拥有一个分配器,而 map 需要另一种类型的分配器,因此它必须通过 rebind 构造一个 via rebind - Alan Stokes
1
@Prasoon:“虽然构造函数调用将被优化,但相应的内存分配调用可能不会。” 什么内存分配?容器没有在堆上分配您的分配器;它们在堆栈上。而且您的分配器在构造函数中也没有在堆上分配内存。那么...你在谈论什么分配? - Nicol Bolas
显示剩余7条评论

1
在 MyPool.h(单例)中:
class MyPool
{
...
public:
  static MyPool & GetInstance( void );
private:
  MyPool(void);
}

在MyPool.cpp文件中:
MyPool & MyPool::GetInstance( void )
{
  static MyPool retval;
  return retval;
}

在fooStdAllocator.h文件中:
#pragma once

#include "MyPool.h"

#pragma push_macro( "new" )
#undef new
#include <new>

template <class T1> class fooStdAllocator;

// Description:
// Specialize for void
template <> class fooStdAllocator<void>
{
public:
  typedef void * pointer;
  typedef const void* const_pointer;
  typedef void value_type;
  template <class U1> struct rebind { typedef fooStdAllocator<U1> other; };
};

template <class T1> class fooStdAllocator
{
public:
  // Description:
  // Typedefs
  typedef T1 value_type;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
  typedef T1* pointer;
  typedef const T1* const_pointer;
  typedef T1& reference;
  typedef const T1& const_reference;

  // Description:
  // The rebind member allows a container to construct an allocator for some arbitrary type out of
  // the allocator type provided as a template parameter.
  template <class U1> struct rebind { typedef fooStdAllocator<U1> other; };

  // Description:
  // Constructors
  fooStdAllocator( void ) : pool(MyPool::GetInstance()) {};
  fooStdAllocator( const fooStdAllocator& other ) : pool(MyPool::GetInstance()) {};
  template <class U1> fooStdAllocator(const fooStdAllocator<U1>&) : pool(MyPool::GetInstance()) {};

  // Description:
  // Destructor
  ~fooStdAllocator( void ) {};

  // Description:
  // Returns the address of r as a pointer type. This function and the following function are used
  // to convert references to pointers.
  pointer address(reference r) const { return &r; };
  const_pointer address(const_reference r) const { return &r; };

  // Description:
  // Allocate storage for n values of T1.
  pointer allocate( size_type n, fooStdAllocator<void>::const_pointer hint = 0 )
  {
    // I would never do it that way:
    //pointer return_value = reinterpret_cast<pointer>( pool.GetNext() );
    // I would prefer to use the got size to allocate:
    pointer return_value = reinterpret_cast<pointer>( pool.GetNext(n) );

    if ( return_value == 0 )
      throw std::bad_alloc();
    return return_value;
  };

  // Description:
  // Deallocate storage obtained by a call to allocate.
  void deallocate(pointer p, size_type n)
  {
    pool.Free(p);
  };

  // Description:
  // Return the largest possible storage available through a call to allocate.
  size_type max_size() const
  {
    size_type return_value = 0xFFFFFFFF;
    return_value /= sizeof(T1);
    return return_value;
  };

  // Description:
  // Construct an object of type T1 at the location of ptr
  void construct(pointer ptr)
  {
    ::new (reinterpret_cast<void*>(ptr)) T1;
  };

  // Description:
  // Construct an object of type T1 at the location of ptr, using the value of U1 in the call to the
  // constructor for T1.
  template <class U1> void construct(pointer ptr, const U1& val)
  {
    ::new (reinterpret_cast<void*>(ptr)) T1(val);
  };

  // Description:
  // Construct an object of type T1 at the location of ptr, using the value of T1 in the call to the
  // constructor for T1.
  void construct(pointer ptr, const T1& val)
  {
    ::new (reinterpret_cast<void*>(ptr)) T1(val);
  };

  // Description:
  // Call the destructor on the value pointed to by p
  void destroy(pointer p)
  {
    p->T1::~T1();
  };
private:
  MyPool &pool;
};

// Return true if allocators b and a can be safely interchanged. "Safely interchanged" means that b could be
// used to deallocate storage obtained through a and vice versa.
template <class T1, class T2> bool operator == ( const fooStdAllocator<T1>& a, const fooStdAllocator<T2>& b)
{
  return true;
};
// Return false if allocators b and a can be safely interchanged. "Safely interchanged" means that b could be
// used to deallocate storage obtained through a and vice versa.
template <class T1, class T2> bool operator != ( const fooStdAllocator<T1>& a, const fooStdAllocator<T2>& b)
{
  return false;
};
#pragma pop_macro( "new" )

您可以按照以下方式使用它:
std::map<keyT,valueT,std::less<keyT>,fooStdAllocator> your_map;

@PrasoonTiwari: 这篇文章讲得非常好。 - Naszta
@PrasoonTiwari:reinterpret_cast:可能是真的(也许对于new运算符),但我为你复制了一个经过测试的工作代码。 :) - Naszta
1
@PrasoonTiwari:如果你想要单一实例,你应该创建单例。我已经为你修改了代码。 - Naszta
1
分配方法不应该使用pool.GetNext(n)而是应该使用pool.GetNext(n * sizeof T1)吗?map似乎为每个节点调用allocate(1) - xan
据我所知,GetNext() 函数确实知道 sizeof(T1)。如果它不知道,你是对的。顺便说一下:GetNext<T1>(n) 是最好的选择... - Naszta
显示剩余5条评论

0

我最近有一个项目,让我研究了一下C++容器的自定义内存分配器。那时我看到了这篇文章。它足够好,可以复制一组代码,只需要进行最小的更改即可使其工作。

我将微调Naszta的代码示例,该示例由Prasoon Tiwari起草。第一个更改是将MyPool更改为模板类。很抱歉我的示例将包括名称更改。以下是模板:

#pragma once
#include <cstdint>

//class MyPool
template  <class T1>
class FooPool
{
public:
    typedef T1 value_type;
    typedef T1* pointer;
    typedef const T1* const_pointer;
    typedef T1& reference;
    typedef const T1& const_reference;

    static FooPool& GetInstance()
    {
        static FooPool myPool;

        return myPool;
    }

    pointer GetNext( size_t szItemCount )
    {
        pointer pObjects = nullptr;

        if ( szItemCount > 0 )
        {
            size_t blockSize = szItemCount * sizeof( T1 );
            uint8_t* pBytes = new uint8_t[blockSize];
            memset( pBytes, 0, blockSize );

            pObjects = reinterpret_cast<pointer>(pBytes);
        }

        return pObjects;
    }

    bool Free( pointer pObjects )
    {
        uint8_t* pBytes = reinterpret_cast<uint8_t*>(pObjects);
        delete[] pBytes;

        return true;
    }

private:
    // hide constructor to enforce singleton usage
    FooPool() = default;
    
    // this constructor will show the type of objects in this pool
    //FooPool()
    //{
    //    OutputDebugStringA( "FooPool object type: ");
    //    OutputDebugStringA( typeid(T1).name() );
    //    OutputDebugStringA( "  aka  " );
    //    OutputDebugStringA( typeid(T1).raw_name() );
    //    OutputDebugStringA( "\n" );
    //}
};

这里的重大变化是FooPool知道它正在创建的对象类型。这被认为使其在如何进行内存分配方面更具灵活性。您的编译器将向您显示必须在FooStdAllocator构造函数中进行的其他一些调整。

现在在FooStdAllocator中声明为:

template <class T1>
class FooStdAllocator
{
private:
    FooPool<T1>& m_FooPool;
...
}

最后一个更改与FooStdAllocator的分配调用有关。 这已更改为:
pointer allocate( size_type n, FooStdAllocator<void>::const_pointer hint = nullptr )
{
    //pointer return_value = reinterpret_cast<pointer>(pool.GetNext( n * sizeof( T1 ) ));
    // FooPool is now templated so it now knows the size of T1
    pointer return_value = m_FooPool.GetNext( n );

    if ( return_value == nullptr )
    {
        throw std::bad_alloc();
    }

    return return_value;
}

这包括xan关于分配大小的更正。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接