STL自定义分配器来管理不同的内存空间

发布于 2024-08-08 17:29:58 字数 171 浏览 1 评论 0原文

我想使用 STL 自定义分配器类的不同实例来管理不同的内存空间,然后能够为 STL 容器指定分配器实例,以便每个容器仅从其分配的内存空间中提取。但我不知道如何才能做到这一点。我了解如何将分配器类型传递到 STL 容器的模板参数中,但我想要类似于将分配器实例传递到 STL 容器的构造函数中的东西。 STL中有没有办法做到这一点?

I would like to use different instances of an STL custom allocator class to manage different memory spaces, and then be able to specify an allocator instance to an STL container such that each container only draws from its assigned memory space. But I don't see how I can do that. I see how I can pass an allocator type into the template parameters of an STL container, but I want something akin to passing an allocator instance into the constructor of an STL container. Is there a way to do this in STL?

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

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

发布评论

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

评论(3

╰ゝ天使的微笑 2024-08-15 17:29:58

不幸的是,STL 分配器不能有状态(或者至少必须非常小心如何使用该状态) - 特定分配器类型的每个实例必须是等效的,STL 容器才能有效地与它们一起工作。我现在不记得细节,但我知道 Scott Meyers 在 “有效的 STL”,第 10 条:了解分配器约定和限制。

但是,您可以拥有与封装在分配器类型中的分配器之间的差异非常相似的模板化分配器,并使用分配器模板的不同“实例化”(每个模板“实例化”是不同的类型) )。再说一遍,我的记忆是迈耶斯非常清楚地讨论了这一点。

例如,请参阅 Anthony Aue 的文章中的此段落,“使用 STL 的自定义池分配器提高性能”

一个可能更严重的警告是,由于分配器使用非静态数据,因此它在技术上不符合标准,因为标准要求相同类型的分配器是等效的。有关该问题的完整解释,请参阅有效的 STL(第 10 项)。这相当于要求给定类型的分配器能够释放由该类型的分配器的任何其他实例分配的内存。对于标准容器的许多用途来说,这个要求是不必要的(有些人可能会说是严厉的)。然而,有两种情况下这个要求是绝对必要的:list::splice 和 swap()。 swap() 的情况尤其严重,因为需要它才能以异常安全的方式在容器上实现某些操作(请参阅 Exceptional C++,第 12 条)。从技术上讲,交换可以(在某些情况下)在分配器比较不相等的情况下实现——项目可以被复制,或者分配器可以与数据一起交换——但情况并非总是如此。因此,如果您使用 swap() 或 list::splice,您应该确保使用 HoldingPolicySingleton;否则,你一定会遇到一些非常令人讨厌的行为。

另请参阅 Stephan T. Lavavej 在 此新闻组主题

如果其他人在此期间没有提供详细信息,我将在今晚晚些时候更新。

Unfortunately STL allocators cannot have state (or at least have to be very careful how that state is used) - each instance of a particular allocator type must be equivalent for STL containers to work effectively with them. I don't recall the details right now, but I know that Scott Meyers discusses this problem at length in "Effective STL", Item 10: Be aware of allocator conventions and restrictions.

However, you can have templated allocators that are very similar with the differences between the allocators being encapsulated in the allocator type and use different 'instantiations' of the allocator template (each template 'instantiation' is a different type). Again, my recollection is that Meyers discusses this pretty clearly.

For example see this paragraph from an article by Anthony Aue, "Improving Performance with Custom Pool Allocators for STL":

A potentially more serious caveat is that, since the allocator uses nonstatic data, it's not technically Standard compliant because the Standard requires that allocators of the same type be equivalent. See Effective STL (Item 10) for a thorough explanation of the issue. This amounts to requiring that an allocator for a given type be able to deallocate memory allocated by any other instance of an allocator for that type. For many uses of standard containers, this requirement is unnecessary (some might say Draconian). However, there are two cases where this requirement is absolutely necessary: list::splice and swap(). The case of swap() is especially serious because it is needed in order to implement certain operations on containers in an exception-safe manner (see Exceptional C++, Item 12). Technically, swap could be (and in some cases, is) implemented in the face of allocators that don't compare equally—items could be copied or the allocators could be swapped along with the data—but this is not always the case. For this reason, if you're using swap() or list::splice, you should make sure to use HoldingPolicySingleton; otherwise, you're bound to run into some really nasty behavior.

See also Stephan T. Lavavej's discussion in this newsgroup thread.

I'll update later tonight if someone else doesn't give the details in the meantime.

菩提树下叶撕阳。 2024-08-15 17:29:58

STL 容器允许您将分配器作为参数传递给构造函数。

例如,以下是向量的适当构造函数:

explicit vector(const Allocator& = Allocator());
explicit vector(size_type n, const T& value = T(),
  const Allocator& = Allocator());
template <class InputIterator>
vector(InputIterator first, InputIterator last,
  const Allocator& = Allocator());

默认情况下,它们仅使用默认构造的分配器。

The STL containers allow you to pass the allocator in as an argument to the constructor.

For example here are the appropriate constructors for vector:

explicit vector(const Allocator& = Allocator());
explicit vector(size_type n, const T& value = T(),
  const Allocator& = Allocator());
template <class InputIterator>
vector(InputIterator first, InputIterator last,
  const Allocator& = Allocator());

By default, they just use a default constructed allocator.

偏闹i 2024-08-15 17:29:58

也许您可以编写一组分配器类型,其中包含指向单独内存空间的静态指向。

然后,当STL容器构造其分配器时,分配器使用分配给该分配器的内存空间。

为简单起见,假设您要使用两个内存空间。创建两种分配器类型,每个空间一种。根据需要将分配器类型传递给 STL 容器构造函数。

Perhaps you could code a set of allocator types which contains a static pointing to seperate memory spaces.

Then, when the STL container constructs its allocator, the allocator uses the memory spaceassigned to that allocator.

For simplicity, assume you want to use two memory spaces. Create two allocator types, one for each space. Pass the allocator type to the STL container constructors as required.

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