http://www.codeproject.com/KB/stl/blockallocator.aspx
Introduction
block_allocator is a custom STL allocator for use with STL as implemented in Microsoft VC++. Rather than doing allocations on a per-node basis,
block_allocator allocates memory in fixed sized chunks, and delivers portions of these chunks as requested. Typical speed improvements of 40% have been obtained with respect to the default allocator. The size of the chunks, set by the user, should not be too little (reduced speed improvements) nor too large (memory wasted). Experiment and see what sizes fit best to your application.
block_allocator can substitute for the default allocator in the following containers:
list,set,multiset,map,multimap,
vector or
queue. Note however that
vector and
queue already perform allocation in chunks. The usage of
block_allocator is fairly simple, for instance:
// block allocated list of ints with chunks of 1024 elements std::list<int,block_allocator<int,1024> > l;Normal containers and block allocated containers can coexist without problems.
Compatibility mode with MSVC++ 6.0/7.0
Due to limitations of the standard library provided with these compilers, the mode of usage explained above does not work here. To circumvent this problem one must proceed as follows: For each of the containers supported, there's an associated block allocated container derived from it thru use of block_allocator. You have to define an activating macro for each container to be defined prior to the inclusion ofblockallocator.h:
list -> block_allocated_list(macroDEFINE_BLOCK_ALLOCATED_LIST),set -> block_allocated_set(macroDEFINE_BLOCK_ALLOCATED_SET),multiset -> block_allocated_multiset(macroDEFINE_BLOCK_ALLOCATED_MULTISET),map -> block_allocated_map(macroDEFINE_BLOCK_ALLOCATED_MAP),multimap -> block_allocated_multimap(macroDEFINE_BLOCK_ALLOCATED_MULTIMAP),
To use block allocation based STL in your application, define the corresponding activating macro, include blockallocator.h and then change your declarations as follows:
list<type> -> block_allocated_list<type,chunk_size>set<key> -> block_allocated_set<key,chunk_size>multiset<key> -> block_allocated_multiset<key,chunk_size>map<key,type> -> block_allocated_map<key,type,chunk_size>multimap<key,type> -> block_allocated_multimap<key,type,chunk_size>
where chunk_size is the size of the chunks. You can enter too the other optional template parameters (see MSVC++ STL docs for more info).
The MSVC++ 6.0/7.0 compatibility mode can also be used in MSVC++ 7.1, so you need not modify your block_allocator-related code when porting legacy code to 7.1.
Multithreading issues
Each block allocated container instance uses its own block_allocator, so no multithreading problems should arise as long as your program conveniently protects their containers for concurrent access (or if no two threads access the same container instance). This is the same scenario posed by regular STL classes (remember operations on containers are not guarded by CRITICAL_SECTIONs or anything similar), so the moral of it all is: If your program was multithread safe without block_allocator, it'll continue to be with it.
Version history
- 29th Feb, 2000 - 1.1
- Initial release in CodeProject.
- 22nd Mar, 2001 - 1.2
- Included definitions for
operator==andoperator!=. The lack of these caused linking errors when invokinglist::swap()and similar methods. The funny thing about it is that no one ever reported this seemingly important bug, so eitherswap()is not that much used or not that many people useblock_allocator!
- Included definitions for
- 25th Oct, 2006 - 1.3
block_allocatornow works with MSVC++ 7.1 and 8.0. Thanks to James May for helping with testing this new version of the code.
- 30th Oct, 2006 - 1.4
- Fixed some
typedefs incorrectly madeprivateinblock_allocated_list,block_allocated_set, etc.
- Fixed some

块分配器是一种用于Microsoft VC++的自定义STL分配器,通过固定大小的内存块分配方式提高容器操作速度约40%。适用于list、set、multiset、map及multimap等容器。兼容MSVC++6.0/7.0,并支持多线程。
545

被折叠的 条评论
为什么被折叠?



