1 /*==========================================================================
2 SeqAn - The Library for Sequence Analysis
4 ============================================================================
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 ============================================================================
18 $Id: basic_allocator_singlepool.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_BASIC_ALLOCATOR_SINGLE_POOL_H
22 #define SEQAN_HEADER_BASIC_ALLOCATOR_SINGLE_POOL_H
24 namespace SEQAN_NAMESPACE_MAIN
26 //////////////////////////////////////////////////////////////////////////////
27 // SinglePool Allocator
28 //////////////////////////////////////////////////////////////////////////////
31 .Spec.Single Pool Allocator:
33 ..general:Class.Allocator
34 ..summary:Allocator that pools memory blocks of specific size.
35 ..signature:Allocator< SinglePool<SIZE, ParentAllocator> >
36 ..param.SIZE:Size of memory blocks that are pooled.
37 ...value:An unsigned integer with $SIZE >= sizeof(void *)$.
38 ..param.ParentAllocator:An allocator that is by the pool allocator used to allocate memory.
39 ...default:@Spec.Simple Allocator@
40 ...note:The single pool allocator only supports @Function.clear@ if this function is also implemented for $ParentAllocator$.
41 ..remarks:A pool allocator allocates several memory blocks at once.
42 Freed blocks are not immediately deallocated but recycled in subsequential allocations.
43 This way, the number of calls to the heap manager is reduced, and that speeds up memory management.
44 ...text:The single pool allocator only pools memory blocks of size $SIZE$.
45 Blocks of other sizes are allocated and deallocated using an allocator of type $ParentAllocator$.
46 ...text:Using the single pool allocator for blocksizes larger than some KB is not advised.
49 template <size_t SIZE, typename TParentAllocator = SimpleAllocator>
52 //////////////////////////////////////////////////////////////////////////////
54 template <size_t SIZE, typename TParentAllocator>
55 struct Allocator<SinglePool<SIZE, TParentAllocator> >
60 ITEMS_PER_BLOCK = (SIZE_PER_ITEM < 0x0100) ? 0x01000 / SIZE_PER_ITEM : 16,
61 STORAGE_SIZE = SIZE * ITEMS_PER_BLOCK,
63 STORAGE_SIZE_MIN = SIZE
66 char * data_recycled_blocks;
67 char * data_current_begin;
68 char * data_current_end;
69 char * data_current_free;
70 Holder<TParentAllocator> data_parent_allocator;
75 data_recycled_blocks = data_current_end = data_current_free = 0;
76 //dont need to initialize data_current_begin
79 Allocator(size_t reserve_item_count)
82 data_recycled_blocks = 0;
84 size_t storage_size = (reserve_item_count * SIZE > STORAGE_SIZE_MIN) ? reserve_item_count * SIZE : STORAGE_SIZE_MIN;
85 allocate( parentAllocator( *this ), data_current_begin, storage_size );
86 data_current_end = data_current_begin + storage_size;
87 data_current_free = data_current_begin;
90 Allocator(TParentAllocator & parent_alloc)
93 setValue(data_parent_allocator, parent_alloc);
95 data_recycled_blocks = data_current_end = data_current_free = 0;
96 //dont need to initialize data_current_begin
99 Allocator(size_t reserve_item_count, TParentAllocator & parent_alloc)
102 data_recycled_blocks = 0;
104 setValue(data_parent_allocator, parent_alloc);
106 size_t storage_size = (reserve_item_count * SIZE > STORAGE_SIZE_MIN) ? reserve_item_count * SIZE : STORAGE_SIZE_MIN;
107 allocate( parentAllocator( *this ), data_current_begin, storage_size );
108 data_current_end = data_current_begin + storage_size;
109 data_current_free = data_current_begin;
113 Allocator(Allocator const &)
115 data_recycled_blocks = data_current_end = data_current_free = 0;
116 //dont need to initialize data_current_begin
119 operator = (Allocator const &)
131 //////////////////////////////////////////////////////////////////////////////
133 template <size_t SIZE, typename TParentAllocator>
134 inline TParentAllocator &
135 parentAllocator(Allocator<SinglePool<SIZE, TParentAllocator> > & me)
138 return value(me.data_parent_allocator);
141 //////////////////////////////////////////////////////////////////////////////
143 template <size_t SIZE, typename TParentAllocator>
145 clear(Allocator<SinglePool<SIZE, TParentAllocator> > & me)
149 me.data_recycled_blocks = me.data_current_end = me.data_current_free = 0;
151 clear(parentAllocator(me));
154 //////////////////////////////////////////////////////////////////////////////
156 template <size_t SIZE, typename TParentAllocator, typename TValue, typename TSize, typename TUsage>
158 allocate(Allocator<SinglePool<SIZE, TParentAllocator> > & me,
161 Tag<TUsage> const tag_)
164 typedef Allocator<SinglePool<SIZE, TParentAllocator> > TAllocator;
165 size_t bytes_needed = count * sizeof(TValue);
167 if (bytes_needed != TAllocator::SIZE_PER_ITEM)
169 allocate(parentAllocator(me), data, count, tag_);
174 if (me.data_recycled_blocks)
176 ptr = me.data_recycled_blocks;
177 me.data_recycled_blocks = * reinterpret_cast<char **>(ptr);
181 ptr = me.data_current_free;
182 if (ptr + bytes_needed > me.data_current_end)
183 {//not enough free space in current storage: allocate new
184 allocate(parentAllocator(me), ptr, (size_t) TAllocator::STORAGE_SIZE, tag_);
185 me.data_current_begin = ptr;
186 me.data_current_end = ptr + TAllocator::STORAGE_SIZE;
188 me.data_current_free = ptr + bytes_needed;
191 data = reinterpret_cast<TValue *>(ptr);
194 //////////////////////////////////////////////////////////////////////////////
196 template <size_t SIZE, typename TParentAllocator, typename TValue, typename TSize, typename TUsage>
198 deallocate(Allocator<SinglePool<SIZE, TParentAllocator> > & me,
201 Tag<TUsage> const tag_)
204 typedef Allocator<SinglePool<SIZE, TParentAllocator> > TAllocator;
206 size_t bytes_needed = count * sizeof(TValue);
208 if (bytes_needed != TAllocator::SIZE_PER_ITEM)
210 deallocate(parentAllocator(me), data, count, tag_);
214 //link in recycling list
215 *reinterpret_cast<char **>(data) = me.data_recycled_blocks;
216 me.data_recycled_blocks = reinterpret_cast<char *>(data);
219 //////////////////////////////////////////////////////////////////////////////
220 //////////////////////////////////////////////////////////////////////////////
221 // alternative Interface that takes a Type instead of a SIZE
222 //////////////////////////////////////////////////////////////////////////////
225 template <typename TValue, typename TParentAllocator = SimpleAllocator>
228 template <typename TValue, typename TParentAllocator>
229 struct Allocator<SinglePool2<TValue, TParentAllocator> >
231 Allocator<SinglePool<sizeof(TValue), TParentAllocator> > data_alloc;
234 Allocator(size_t reserve_item_count)
235 : data_alloc(reserve_item_count)
239 Allocator(TParentAllocator & parent_alloc)
240 : data_alloc(parent_alloc)
244 Allocator(size_t reserve_item_count, TParentAllocator & parent_alloc)
245 : data_alloc(reserve_item_count, parent_alloc)
251 //////////////////////////////////////////////////////////////////////////////
253 template <typename TValue, typename TParentAllocator>
254 inline TParentAllocator &
255 parentAllocator(Allocator<SinglePool2<TValue, TParentAllocator> > & me)
258 return parentAllocator(me.data_alloc);
261 //////////////////////////////////////////////////////////////////////////////
263 template <typename TValue, typename TParentAllocator>
265 clear(Allocator<SinglePool2<TValue, TParentAllocator> > & me)
268 clear(me.data_alloc);
271 //////////////////////////////////////////////////////////////////////////////
273 template <typename TValue, typename TParentAllocator, typename TValue2, typename TSize, typename TUsage>
275 allocate(Allocator<SinglePool2<TValue, TParentAllocator> > & me,
278 Tag<TUsage> const tag_)
281 allocate(me.data_alloc, data, count, tag_);
284 template <typename TValue, typename TParentAllocator, typename TValue2, typename TSize, typename TUsage>
286 deallocate(Allocator<SinglePool2<TValue, TParentAllocator> > & me,
289 Tag<TUsage> const tag_)
292 deallocate(me.data_alloc, data, count, tag_);
295 //////////////////////////////////////////////////////////////////////////////
297 } //namespace SEQAN_NAMESPACE_MAIN
299 #endif //#ifndef SEQAN_HEADER_...