这个问题是关于在插入std::map时构建自定义分配器实例的。以下是一个用于std::map的自定义分配器,以及一个使用它的小程序:
下面是程序的输出结果:
谢谢!
注:此代码在带有g++版本4.1.2的x86机器上测试。如果您希望在64位机器上运行它,您至少必须更改
#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)
应该可以解决问题。
rebind
成员模板有关。正如 Scrubbins 所说,您为一种类型拥有一个分配器,而map
需要另一种类型的分配器,因此它必须通过rebind
构造一个 via rebind。 - Alan Stokes