简体   繁体   English

如何完全管理像map这样的std容器的堆内存分配?

[英]How to completely manage heap memory allocation of std containers like map?

I'm interested in using multiple std map<int,int> and I want all of them to allocate the elements from a common memory pool. 我对使用多个std map <int,int>感兴趣,我希望所有这些都能从公共内存池中分配元素。 From what I've read so far, I can use custom allocators such as the Boost pool_alloc to achieve this. 从我到目前为止所读到的,我可以使用自定义分配器,如Boost pool_alloc来实现这一点。

My question is, despite using something like Boost pool_alloc to manage the allocation of the elements themselves, will std map still use little bits of heap memory as some form of container overhead that will NOT be managed by boost pool_alloc? 我的问题是,尽管使用像Boost pool_alloc这样的东西来管理元素本身的分配,std map仍然会使用一小部分堆内存作为某种形式的容器开销,不会被boost pool_alloc管理? I'm thinking something along the lines of pointer to elements with regards to the use std map itself? 关于使用std地图本身,我正在考虑指向元素的指针。

The short version 简短的版本

A map type such as: 地图类型,例如:

typedef std::map<
    int,
    int,
    less<int>,
    boost::pool_allocator<pair<const int, int> >
    > 
    AMapType;

Will do all the allocation it needs as it grows using boost::pool_allocators. 将使用boost :: pool_allocators增长所需的所有分配。 Some of the initial structure is likely to be created on the stack. 一些初始结构可能在堆栈上创建。 How much and what the exact structure will be though is implementation dependent. 确切的结构多少和具体是依赖于实现的。

Allocator Rebind 分配器重新绑定

The above is possible through the use of allocator::rebind 通过使用allocator::rebind可以实现上述目的

All std::allocator conformant allocators (such as boost::pool_allocator) provide a rebind template struct in the form: 所有std :: allocator符合的分配器(例如boost :: pool_allocator)都以以下形式提供重新绑定模板结构:

template<class U> struct rebind{
   typedef AllocatorType<U> other;
};

This can be used to obtain the same kind of allocator for a different type: 这可用于为不同类型获取相同类型的分配器:

typedef AllocatorOfOther AllocatorType::rebind<ADifferentType>::other;

gcc std::map internal structure gcc std :: map内部结构

The g++ std::map implementation (I inspected 4.7.3 and 4.1.1) is built purely out of a single node type. g ++ std :: map实现(我检查过4.7.3和4.1.1)完全是由单个节点类型构建的。 This includes the key value pair and the pointers and colour bit needed for the rbTree structure. 这包括键值对以及rbTree结构所需的指针和颜色位。 To allocate this it defines a Node allocator using the rebind struct from the user provided allocator. 为了分配它,它使用来自用户提供的分配器的重新绑定结构来定义节点分配器。 This is used for all the allocations it does as the map grows. 这用于地图增长时它所做的所有分配。 Each new node gets allocated in a single allocation. 每个新节点都在一次分配中分配。

The standard 标准

I checked though the C++11 standard and could not find anything specifying how such structure should be allocated. 我检查了C ++ 11标准,但找不到任何指定如何分配这种结构的内容。 Either I did not find the right part or it is not specified. 要么我找不到合适的部分,要么没有指定。 Not using the user given allocator kind would seem a bit pointless though. 不使用给定分配器类型的用户似乎有点无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM