使用基于动态/状态的分配器的 STL 实现?
有人知道 STL 实现允许在使用前将动态分配器传递到容器实例吗?
场景是,我们有一个通用内存分配器来管理多个内存池,对于 stl::vector 的每个实例,我们希望从不同的内存池中分配每个实例。
标准 STL 实现的问题是您只能在类型的基础上定义内存池,即所有 int 类型的向量将从同一个池中分配。
我已经将默认的 stl::allocator 换成了具有状态的分配器,即我们想要从中分配此实例的池,但这对于在默认构造函数中分配内容的 stl::list 来说效果不佳。
由于与我们的库相关的原因,我们在 ctor 中也没有针对所有对象的有效池,因此我们希望在用户使用 stl 容器之前调用“设置内存池”函数。
有人遇到过支持这种事情的实现吗?
Does anybody know of an STL implementation that allows for dynamic allocators to be passed in to an instance of a container before use.
The scenario is that we have a general memory allocator that manages a number of pools of memory and for each instance of say stl::vector we want to allocate each instance from a different pool of memory.
The problem with the standard STL implementations is that you can only define the memory pool on a type basis ie all vector's of type int would allocate from the same pool.
I've already swapped out our default stl::allocator for one that has a state ie the pool we want to allocate this instance from but this doesn't work well for say stl::list where it allocates things in the default ctor.
For reasons related to our library we also don't have a valid pool in the ctor for all objects and so we want to call a 'set memory pool' function before users can use the stl container.
Has anybody come across an implementation that supports this kind of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,STL 端口似乎确实支持这种功能,而 Microsoft(VS 2008)和 GNU 实现(stl 大约 gcc 3.4.1 的端口)则不支持这种功能,因为它们在 ctors/dtors 中分配/取消分配内容。
这是我的测试代码,展示了如何执行此操作。警告它无论如何都不是一个完整的实现!
Ok so STL port does seem to support this kind of functionality, where as Microsoft (VS 2008) and the GNU implementation (a port of stl circa gcc 3.4.1) don't as they allocate/deallocate things in the ctors/dtors.
Here's my test code showing how to do this. Warning its not a full implementation by any means!
我不太确定你的问题。因此,我将介绍全状态分配器的情况。
在 C++03 中,任何分配器都应该能够释放由相同类型的另一个分配器分配的资源。
C++0x 标准实际上消除了这个限制,并允许将全状态分配器传递给 STL 容器,只要它们是分配器感知容器(我认为它涵盖了使用 STL 打包的所有容器,因为他们对序列建模)。
例如:
[allocator.adaptor] $20.10 Classscoped_allocator
现在是 C++0x STL 的一部分。I am not so sure about your question precisely. So I'll cover the case of a state-full allocator.
In C++03, any allocator should be able to deallocate resources allocated by another allocator of the same type.
The C++0x standard actually removed this limitation and allows state-full allocators to be passed to STL containers as long as they are Allocator Aware containers (I think it covers all containers packaged with the STL, since they model Sequence).
For example:
[allocator.adaptor] $20.10 Class scoped_allocator
is now part of the C++0x STL.类型化分配器可以使用下面的通用分配器来执行分配。
Allocator需要支持这些功能:
假设你有一个只分配内存和释放内存的机制,你可以用它来实现上面的一些功能。
键入分配器的优点是,您知道您将创建许多大小完全相同的项目,因此可以创建适合您的“页面”。最大的问题可能是你被迫通过 allocate() 返回连续的缓冲区(实际上向量需要它们)。
http://www.cplusplus.com/reference/std/memory/allocator/
你的问题仍然有点不清楚为什么这还不够。您可以使用“一次”逻辑初始化内存池。 (如果是多线程,您可以使用 boost::once 来实现这一点)。
The typed allocator can use a general allocator underneath to perform the allocations.
Allocator needs to support these functions:
Assuming you have a mechanism that just allocates memory and deallocates memory, you can use that to implement some of the functions above.
The advantage of allocators being typed is that you know you are going to create lots of items of exactly the same size and can therefore create your "pages" to fit. The biggest issue can be that you are forced by allocate() to return contiguous buffers (and indeed vector needs them).
http://www.cplusplus.com/reference/std/memory/allocator/
Your question is still a bit unclear as to why this is not sufficient. You can initialise the memory-pool with "once" logic. (If it is multi-threaded you can use boost::once to achieve this).
这并不完全正确。您可以使用不同的向量来保存 int 元素,每个向量具有不同的分配器类型。
不过,对于这个问题——
-- C++ 标准库 (STL) 根本不支持它,因此,虽然可能存在每个对象分配器可以工作的实现,但它不可移植。
有关为什么以及如何使用自定义分配器的详细分析,请参阅 Scott Meyers 的 书 Effective STL,特别是第 11 项:了解合法使用自定义分配器。
This is not exactly true. You can have different vectors holding
int
elements each having a different allocator type.However, regarding the question --
-- it is simply not supported by the C++ Standard Library (STL), therefore, while it may be that there are implementations where per-object allocators work, it is not portable.
For a detailed analysis of why and how to use custom allocators see Scott Meyers's book Effective STL, specifically Item 11: Understand the legitimate use of custom allocators.
一种选择是使用线程局部变量来保存指向要使用的内存池的指针,并在分配器实现中捕获它。例如(使用 boost::thread_specific_ptr) :
此处描述了 myallocator 必须实现的 API。
不幸的是,由于 STL API 的限制,这已经是您在不重新实现 STL 的情况下所能得到的最好结果了。但是,可能有第三方库允许您将分配器传递给某个地方的对象构造函数。
One option would be to use a thread-local variable to hold the pointer to the memory pool to use, and to capture this in your allocator implementation. For example (using boost::thread_specific_ptr):
The API that myallocator must implement is described here.
Unfortunately, due to limitations in the STL API, this is as good as you can get without reimplementing the STL. There may, however, be third-party libraries which let you pass in an allocator to the object's constructor out there somewhere.