STL自定义分配器,用于管理不同的内存空间 [英] STL custom allocators to manage different memory spaces

查看:264
本文介绍了STL自定义分配器,用于管理不同的内存空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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?

推荐答案

不幸的是,STL分配器不能具有状态(或者至少必须非常小心如何使用该状态)-每个特定分配器类型的实例必须等效于STL容器可以有效地与它们协同工作.我现在不记得详细信息,但是我知道Scott Meyers在有效的STL" ,第10项:请注意分配器的约定和限制.

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.

但是,您可以使用模板化的分配器,这些分配器与封装在分配器类型中的分配器之间的差异非常相似,并使用分配器模板的不同实例"(每个模板实例"是不同的类型).同样,我的记忆是Meyers对此进行了非常清晰的讨论.

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.

例如,请参见 Anthony Aue的文章中的本段,使用STL的自定义池分配器提高性能" :

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

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.

另请参见如果其他人同时不提供详细信息,我将在今晚晚些时候更新.

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

这篇关于STL自定义分配器,用于管理不同的内存空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆