【问题标题】:std::map allocators binded to heaps for all keys and values?std::map 分配器绑定到所有键和值的堆?
【发布时间】:2012-01-26 15:09:12
【问题描述】:

我想知道如何创建像std::map<T1, T2>(T1allocator, T2allocator, T1deallocator, T2deallocator) 这样的东西,这样当任何人尝试从任何堆中将键值对插入我的地图或修改某些数据时,将在原始地图创建者堆中创建数据而不是在其他尝试将自己的数据传递给它的堆中?


假设我们有一个 DLL 库(在 Windows 上)并且我们动态地(在运行时)将它附加到我们的主应用程序。该库(作为主应用程序)是使用静态运行时编译的。因此可能会获得 2 个不同的 STL 和 2 个不同的堆 - 一个带有 DLL,一个带有 App。

现在假设我们希望我们的插件/dll 仅使用主应用程序堆中的内存。

让我们来看一些简单的演示代码。

我们创建了一个简单的插件界面,我们的主应用程序都知道:

class plugin:
{
public:
    virtual void pass_and_modify_data( boost::shared_ptr<std::map<std::string, std::string> >  a) =0;
};

我们使用以下方法创建了 pugin DLL:

#include "plug_in_interface.h"
class my_plugin: public plugin
{
    public:
        virtual void pass_and_modify_data( boost::shared_ptr<std::map<std::string, std::string> >  a)
        {
            a->insert(std::pair<std::string, std::string>("hello", "World"));
            (*a)["world"] = "hello";
        }
};

现在我们想将地图从我们的应用程序传递给插件,但将所有传递给我们地图的值分配到主应用程序堆上。

如何做这样的事情 - 如何让std::map 将给定的键和值重新分配到主应用程序哈希中,而插件代码开发人员不会注意到? (这里注意我的意思是像使用自定义释放器放入地图字符串或编写任何特殊的非平凡代码)


查看sehe's我要创建的答案和演示:

如果我把它写出来,我的主要内容会是这样的:

#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/flyweight.hpp>
#include "plug_in_interface.h"

using namespace boost;
using namespace boost::flyweights;
typedef flyweight<
    std::string,
    hashed_factory<
     boost::hash<std::string>,
    std::equal_to<key_value>,
    std::allocator<boost::mpl::_1> // here we get errors like C2065: 'key_value' : undeclared identifier    
    >
> customString;

int main()
{
 // boring part -  load plugin... into something like boost::shared_ptr<plugin> my_plugin 
 //intresting part - create map in main heap and make all its values and keys be forced to be allocated or reallocated in main heap

    std::cout << std::endl << "Map allocator mased memory management:" << std::endl;
    std::cin.get();
    {
        boost::shared_ptr<std::map<std::string, std::string> > a  (new std::map<customString, customString>());
        my_plugin->pass_and_modify_data(a);
        std::cout << "modified data: " << (*a)["hello"] << (*a)["world"] << std::endl;
    }
    std::cout << "removed data" << std::endl;
    std::cin.get();

  }

由于 customString 类型的定义方式,目前我无法编译这个东西。编译器显示大量编译器错误。如何使类型 def 更正确?

一般来说,我对 stl::allocators 完全没问题,我真正需要的只是分配器与堆的绑定方式(就像我们可以使用 shared_ptrs 一样),这可能吗?

【问题讨论】:

  • 您是否考虑过只是不这样做? (不要将共享库静态链接到它的运行时......)。此外,如果您在主程序中构建映射并传递它,分配器也会被复制。
  • @sehe:为什么我需要将共享库静态链接到它的运行时这样丑陋的东西 - 使最终用户无需安装任何运行时=)我的主应用程序也是静态编译的,什么插件会被动态编译吗?
  • 这是一个逆转。如果您希望事物动态链接,那么动态加载有什么意义?还有,你问这个问题干嘛?你有你的理由。考虑到这一点,您可能最好在不使用动态链接但使用纯 C 插件接口(使用 HANDLE 习惯用法)的情况下最好解决此问题或使用 COM 进行接口,这正是为此Windows 上的用途

标签: c++ memory-management boost stl map


【解决方案1】:

如果您只想向 map 提供 函数 来进行分配,那么您就不走运了。在 STL 容器中控制内存管理的标准方法是通过allocators。

您不能对键和值使用单独的分配器,因为std::map 只接受一个分配器模板参数 (std::map&lt;Key, Value, Predicate, Allocator&gt;)。但是没有理由这样做——一个分配器就足够了。

如果您只想要一个自定义分配器,这很简单。您可以使用其中一种标准(alloc、pthread_alloc、single_client_alloc 或 malloc_alloc),或阅读this tutorial 来学习如何编写自己的。如果您可以提供有关您要解决的问题的更多信息,我可能会提供更有帮助的答案。

【讨论】:

    【解决方案2】:

    您可以使用 Boost Flywheight 或 Boost Optional。

    虽然与标准容器不完全一样,但它们允许使用工厂/分配策略,您可以使用它。当然,您最终会得到另一个级别的间接,这可能是您想要的,也可能不是。

    见,例如http://www.boost.org/doc/libs/1_48_0/libs/flyweight/doc/tutorial/configuration.html#factories

    hashed_factory&lt;[Hash[,Pred[,Allocator]]]&gt; ....

    假设我们想为 std::string flyweight 配置 hashed_factory,带有一个特殊的哈希谓词 special_hash 和一个自定义分配器 custom_allocator;这将被指定如下:

    typedef flyweight<
      std::string,
      hashed_factory<
        special_hash<std::string>,
        std::equal_to<key_value>,
        custom_allocator<boost::mpl::_1>
      >
    > customString;
    

    由于享元本质上是不可变的,因此它们更适合于键:

     std::map<customString, int> my_funky_map;
    

    现在,使用默认映射分配器来指定映射的元素(对)应该分配到哪里。


    【讨论】:

    • 添加了一些关于我实际尝试测试的细节。尝试了你的建议,得到了大量的编译器错误。您能否在我的示例中展示如何使用享元?
    • @myWallJSON: 呃...你真的尝试过吗?给你:ideone.com/0ncl1 - 请注意,如果 key 和 value 都使用相同的分配器,你最好使用std::map 的分配器模板参数(分配一个 std::pair 字符串),这很多更简单。再说一次,问题是明确地 不是关于那个......
    • 好吧)提供代码编译,我的应用程序也可以用它修改......但遗憾的是,当分发到 lib/app 对时,它不会在主堆中分配值。
    • 哦,好吧,原来我没有很好地阅读这个问题(或者在我最初阅读后它被编辑了)。我在 Q 的评论中发布了我的最新想法。我认为您正在寻找一种方法来为 map 元素的 key 与 de value 部分使用不同的分配器。 (显然,John_Purdy 有相同的解释:'You cannot use separate allocators for the keys and values, because...')
    猜你喜欢
    • 2012-03-10
    • 2014-01-28
    • 2013-10-07
    • 1970-01-01
    • 2011-09-11
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多