【问题标题】:'Tag' template parameter in boost::pool_allocator and boost::fast_pool_allocator to support controlling the instance of the underlying pool?boost::pool_allocator 和 boost::fast_pool_allocator 中的 'Tag' 模板参数以支持控制底层池的实例?
【发布时间】:2014-11-29 01:56:36
【问题描述】:

更新:问题不再适用,因为我第一次写它,所以我修改了问题的标题。 现在应该提出问题是否修改pool_allocatorboost_pool_allocator 以接受Tag 参数来控制所使用的底层池的选择(如问题中所述)是一个好主意 -或者是否可以在不修改库的情况下实现这一点 - 如果没有其他方法,这是否对 Boost.Pool 有用,作为一项重要的增强。


对于一个项目,我非常依赖 Boost.Pool(因为标准的内存释放需要非常非常长的时间(约 30 分钟)来处理数百万个永远不会再次使用的小对象) .

(请参阅 How to prevent destructors from being called on objects managed by boost::fast_pool_allocator?How do you determine the size of the nodes created by a 'std::map' for use with 'boost::pool_allocator' (in a cross-platform way)?,了解我在编写此项目时发布的另外两个与 Boost Pool 相关的问题。)

我的场景是这样的:我有许多不同的池,通常存储相同大小的对象,需要在不同的时间释放它们。为了实现这一点,我利用boost::fast_pool_allocator 中的Tag 模板参数来控制池(特别是何时删除哪个池)。

例如,我的代码有(在许多其他使用不同标签的 Boost Pool 中)这一行 - 注意接近末尾的自定义分配器boost::fast_pool_allocator):

typedef boost::multiprecision::number<
  boost::multiprecision::cpp_int_backend<
    0,
    0,
    boost::multiprecision::signed_magnitude,
    boost::multiprecision::unchecked,
    boost::fast_pool_allocator<
      boost::multiprecision::limb_type,
      boost::default_user_allocator_malloc_free,
      newgene_cpp_int_tag,
      boost::details::pool::null_mutex
    >
  >
> newgene_cpp_int;

... 之前定义的自定义标签为

// Here is the 'tag' type used in the above line of code
// ... this distinguishes the pool used
struct newgene_cpp_int_tag 
{};

经过大量努力并定义了至少 20 种不同的池类型,我的内存问题得到了解决,我继续前进。

那是 Boost.Pool 1.55 版。

看到在 Boost.Pool 版本 1.56 中,Tagpool_allocatorfast_pool_allocator 的参数已被删除,这让我大吃一惊。

这里有两个版本:

1.55 版:

// boost::pool_allocator, version 1.55
template <typename T,
    typename UserAllocator,
    typename Tag,  // <-- This was removed in version 1.56: How to distinguish pools?
    typename Mutex,
    unsigned NextSize,
    unsigned MaxSize >
class pool_allocator {...}

1.56 版:

// boost::pool_allocator, version 1.56
template <typename T,
    typename UserAllocator,
    typename Mutex,
    unsigned NextSize,
    unsigned MaxSize >
class pool_allocator {...}

如您所见,可以区分用于存储对象的底层池的Tag 参数已在 Boost 版本 1.56 中删除。

不幸的是,没有 Boost.Pool(1.56 版)的发行说明——也就是说,查看at the release notes 显示没有提及 Boost.Pool 的任何变化。此外,10 分钟的 Google 搜索显示没有任何似乎讨论该问题的链接。

我该怎么做?当然,我可以编写自己的池分配器类(但如果我这样做,我几乎肯定会坚持使用 Boost 的 1.55 版——而且我宁愿不花时间和精力来编写我自己的自定义分配器,因为 Boost 的1.55 版pool_allocator 工作得很好)。

特别是,我想知道 - Boost.Pool 是否放弃了对独立控制池的支持,以便可以在不同时间释放它们,而不必担心池之间的对象大小是否相同?如果是这样,我将别无选择,只能坚持使用 Boost 1.55 版,而不是能够升级。真可惜。

或者,是否有其他方法可以控制与pool_allocator 一起使用的池(Tag 模板参数除外)?

我想知道在 1.56 版中我是否可以继续使用 Boost Pool 的 pool_allocator 作为可以控制使用哪些池的自定义分配器(如果可以,如何这样做,因为 Tag模板参数已在 1.56 版中删除)- 我也想知道为什么 Tag 模板参数在 1.56 版中被删除,特别是如果它阻止了使用此分配器控制池的能力。

谢谢!

【问题讨论】:

  • 据我所知,the Boost 1.55 pool_allocator/fast_pool_allocator 也没有Tag 模板参数。你确定你不是在看某种自定义修改吗?
  • @T.C.你说的对!我的上帝,我忘记了我只是为了这个目的而修改了课程。明天,我将更新这个问题以表明这一点,并且也许会问这是否是可能需要的类修改类型 - 我当然需要它!向所有为此花时间的人道歉。
  • 我已经更新了问题的标题,并在问题的开头添加了更新,以表明这一点。
  • @DanNissenbaum:关闭这个问题可能会更好,因为它从来都不是有效的,然后建议您在 Boost 邮件列表或错误跟踪器上进行修改(或者甚至可能在这里作为一个新问题) .
  • @JohnZwinck 我考虑关闭它,但认为其他人在使用pool_allocatorfast_pool_allocator 时可能会遇到无法控制池的情况,并且可能会遇到这个问题 - 很明显它是一个简单的任务是修改轻量级pool_alloc.hpp 文件以通过所需的标签,以启用此功能 - 我并不疯狂地想知道。 :) 所以我想我会保持打开状态,只需更改标题并在问题顶部添加更新。

标签: c++ boost pool allocator


【解决方案1】:

我想 Tag 模板参数被透明地“转发”到底层池。

标签类型仍然存在,但不是公共接口的一部分。例如。 here:

const pointer ret = static_cast&lt;pointer&gt;(
singleton_pool&lt;pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
NextSize, MaxSize&gt;::ordered_malloc(n) );

因此,您可以(再次 :))修改代码以将您自己的标签传递给底层池,而不是硬编码 pool_allocator_tagfast_pool_allocator_tag


我建议您将更改保留为补丁,以便更轻松地跟上上游更改。你可以在 git@github.com:boostorg/pool.git 创建一个库的分支,这样你就可以git rebase你的更改。

【讨论】:

  • 从未想过克隆 Boost 存储库(尽管我以前从未对它们进行过更改)。好主意,我会这样做的!
猜你喜欢
  • 2011-12-14
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多