簡體   English   中英

自定義 Memory 分配器 STL map

[英]Custom Memory Allocator for STL map

這個問題是關於在插入 std::map 期間構建自定義分配器的實例。

這是std::map<int,int>的自定義分配器以及使用它的小程序:

#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;
}

這是這個程序的 output:

-------Alloc--CONSTRUCTOR--------bffcdaa6                     St4pairIKiiE
 Construct T Alloc from X Alloc--bffcda77  St13_Rb_tree_nodeISt4pairIKiiEE                     St4pairIKiiE
 Copy Constructor ---------------bffcdad8  St13_Rb_tree_nodeISt4pairIKiiEE
 Destructor ---------------------bffcda77  St13_Rb_tree_nodeISt4pairIKiiEE
 Destructor ---------------------bffcdaa6                     St4pairIKiiE
======>Creating a new pool object.
 Construct T Alloc from X Alloc--bffcd9df                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d028 St4pairIKiiE.
 Destructor ---------------------bffcd9df                     St4pairIKiiE
 Construct T Alloc from X Alloc--bffcd95f                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d048 St4pairIKiiE.
 Destructor ---------------------bffcd95f                     St4pairIKiiE
 Construct T Alloc from X Alloc--bffcd95f                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
+++++++ 0985d068 St4pairIKiiE.
 Destructor ---------------------bffcd95f                     St4pairIKiiE
======>End of map insertions.
 Construct T Alloc from X Alloc--bffcda23                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d068.
 Destructor ---------------------bffcda23                     St4pairIKiiE
 Construct T Alloc from X Alloc--bffcda43                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d048.
 Destructor ---------------------bffcda43                     St4pairIKiiE
 Construct T Alloc from X Alloc--bffcda43                     St4pairIKiiE  St13_Rb_tree_nodeISt4pairIKiiEE
-+-+-+- 0985d028.
 Destructor ---------------------bffcda43                     St4pairIKiiE
 Destructor ---------------------bffcdad8  St13_Rb_tree_nodeISt4pairIKiiEE

output 的最后兩列顯示每次插入 map 時都會構造std::pair<const int, int>的分配器。為什么這是必要的? 有沒有辦法抑制這種情況?

謝謝!

編輯:此代碼在 x86 機器上測試,g++ 版本 4.1.2。 如果您希望在 64 位機器上運行它,您至少必須更改行return malloc(24) 更改為return malloc(48)應該有效。

之所以如此,是因為分配器是針對std::pair<const int, int>但實現實際上需要分配一個更復雜的數據結構,其中包含一個成員。 雖然我希望實際的分配器需要構建和緩存,但每次重構它都不是非法的。 這是一個在不改變實現的情況下無法逃脫的實現細節。 創建的實際分配器類型是St13_Rb_tree_nodeISt4pairIKiiEE (損壞的名稱)。

在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;

我最近有一個項目讓我研究了 C++ 容器的自定義 memory 分配器。 那是我遇到這篇文章的時候。 復制一組只需最少更改即可正常工作的代碼就足夠了。

我將巧妙地更改由 Prasoon Tiwari 起草的 Naszta 代碼示例。 第一個更改是將 MyPool 更改為模板化的 class。抱歉,我的示例將包括名稱更改。 這是模板:

#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 知道它正在創建的對象的類型。 這被認為在如何進行 memory 分配方面給予了更大的靈活性。 您的編譯器將向您展示一些必須在 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 對分配大小的更正。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM