gpt4 book ai didi

c++ - STL 映射的自定义内存分配器

转载 作者:可可西里 更新时间:2023-11-01 15:26:59 28 4
gpt4 key购买 nike

这个问题是关于在插入 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;
}

这是这个程序的输出:

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

输出的最后两列显示 std::pair<const int, int> 的分配器每次插入 map 时都会构造。为什么这是必要的?有什么办法可以抑制这种情况吗?

谢谢!

编辑:此代码在具有 g++ 版本 4.1.2 的 x86 机器上测试。如果你想在 64 位机器上运行它,你必须至少更改行 return malloc(24) .改为 return malloc(48)应该可以。

最佳答案

之所以如此,是因为分配器用于 std::pair<const int, int>但实现实际上需要分配一个更复杂的数据结构,它是其中的一个成员。虽然我希望需要构造和缓存实际的分配器,但每次都重新构造它并不是非法的。这是一个实现细节,如果不更改实现,就无法逃避。创建的实际分配器类型是 St13_Rb_tree_nodeISt4pairIKiiEE (损坏的名称)。

关于c++ - STL 映射的自定义内存分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11373796/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com