增强池分配

发布于 2024-08-27 04:10:37 字数 302 浏览 9 评论 0原文

为什么 boost::fast_pool_allocator 构建在单例池之上,而不是每个分配器实例都有一个单独的池?或者换句话说,为什么只提供这一点,而不是为每个分配器提供一个池的选项?这是一个坏主意吗?

我有一个类在内部使用大约 10 种不同的 boost::unordered_map 类型。如果我使用了 std::allocator,那么当它调用删除时,所有内存都会返回到系统,而现在我必须在某些时候对许多不同的分配器类型调用release_memory。 我会愚蠢地推出自己的使用 pool 而不是 singleton_pool 的分配器吗?

谢谢

Why is the boost::fast_pool_allocator built on top of a singleton pool, and not a separate pool per allocator instance? Or to put it another way, why only provide that, and not the option of having a pool per allocator? Would having that be a bad idea?

I have a class that internally uses about 10 different boost::unordered_map types. If I'd used the std::allocator then all the memory would go back to the system when it called delete, whereas now I have to call release_memory on many different allocator types at some point.
Would I be stupid to roll my own allocator that uses pool instead of singleton_pool?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-09-03 04:10:37

分配器很难拥有状态,因为分配器的所有实例都必须“等效”才能被标准库使用(至少可移植)。

从 20.1.5/4“分配器要求”开始:

本国际标准中描述的容器的实现允许假设其分配器模板参数满足表 32 中的以下两个附加要求。

  • 给定分配器类型的所有实例都必须可互换,并且始终彼此比较相等

给定分配器类型的所有实例都需要可互换,并且始终 继续说道:

鼓励实现者提供可以接受封装更通用内存模型并支持非相等实例的分配器的库。在此类实现中,容器对分配器施加的任何超出表 32 中出现的要求的要求,以及分配器实例比较不相等时容器和算法的语义,都是实现定义的。

因此,可以编写一个实现来允许非等效的分配器实例,但是您的分配器依赖于实现定义的行为。

请参阅这个其他SO答案了解一些额外的信息详细信息(看起来我需要对该答案进行一些承诺的更新......)

It's difficult for an allocator to have state, since all instances of an allocator had to be 'equivalent' to be used by the standard library (at least portably).

From 20.1.5/4 "Allocator requirements":

Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table 32.

  • All instances of a given allocator type are required to be interchangeable and always compare equal to each other

It then goes on to say:

Implementors are encouraged to supply libraries that can accept allocators that encapsulate more general memory models and that support non-equal instances. In such implementations, any requirements imposed on allocators by containers beyond those requirements that appear in Table 32, and the semantics of containers and algorithms when allocator instances compare non-equal, are implementation-defined.

So an implementation can be written to allow non-equivalent allocator instances, but then your allocator is dependent in implementation-defined behavior.

See this other SO answer for some additional details (and it looks like I need to tend to some promised updating of that answer...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文