4 #ifndef DUNE_COMMON_POOLALLOCATOR_HH
5 #define DUNE_COMMON_POOLALLOCATOR_HH
22 template<std::
size_t size,
typename T>
28 template<
typename T, std::
size_t s>
31 template<
typename T, std::
size_t s>
88 template<
class T, std::
size_t s>
92 friend struct ::testPoolMain<s,T>;
183 unsigned long long lmemory =
reinterpret_cast<unsigned long long>(chunk_);
188 memory_ =
reinterpret_cast<char *
>(lmemory);
206 inline void free(
void* o);
211 inline void print(std::ostream& os);
216 Pool(
const Pool<MemberType,s>&);
218 void operator=(
const Pool<MemberType,s>& pool)
const;
247 template<
class T, std::
size_t s>
305 template<
typename U, std::
size_t u>
315 inline pointer
allocate(std::size_t n, const_pointer hint=0);
324 inline void deallocate(pointer p, std::size_t n);
331 inline void construct(pointer p, const_reference value);
337 inline void destroy(pointer p);
342 inline pointer
address(reference x)
const {
return &x; }
348 inline const_pointer
address(const_reference x)
const {
return &x; }
371 PoolType memoryPool_;
375 template <std::
size_t s>
383 template <
class U>
struct rebind
388 template<
typename T, std::
size_t t>
395 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
402 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
408 template<
typename T, std::
size_t t1, std::
size_t t2>
415 template<
typename T, std::
size_t t1, std::
size_t t2>
421 template<
typename T, std::
size_t t1, std::
size_t t2>
428 template<
typename T, std::
size_t t1, std::
size_t t2>
434 template<std::
size_t t1, std::
size_t t2>
440 template<std::
size_t t1, std::
size_t t2>
446 template<
class T, std::
size_t S>
448 : head_(0), chunks_(0)
451 dune_static_assert(
sizeof(Reference)<=unionSize,
"Library Error: type of referene is too big");
462 template<
class T, std::
size_t S>
472 Chunk *current=chunks_;
476 Chunk *tmp = current;
477 current = current->next_;
482 template<
class T, std::
size_t S>
485 Chunk* current=chunks_;
488 current=current->next_;
493 template<
class T, std::
size_t S>
496 Chunk *newChunk =
new Chunk;
497 newChunk->next_ = chunks_;
500 char* start = chunks_->memory_;
501 char* last = &start[elements*alignedSize];
502 Reference* ref =
new (start) (Reference);
509 for(
char* element=start+alignedSize; element<last; element=element+alignedSize) {
510 Reference* next =
new (element) (Reference);
517 template<
class T, std::
size_t S>
521 Reference* freed =
static_cast<Reference*
>(b);
522 freed->next_ = head_;
526 std::cerr<<
"Tried to free null pointer! "<<b<<std::endl;
529 template<
class T, std::
size_t S>
535 Reference* p = head_;
541 template<
class T, std::
size_t s>
545 template<
class T, std::
size_t s>
550 return static_cast<T*
>(memoryPool_.allocate());
552 throw std::bad_alloc();
555 template<
class T, std::
size_t s>
558 for(
size_t i=0; i<n; i++)
559 memoryPool_.free(p++);
562 template<
class T, std::
size_t s>
565 ::new (static_cast<void*>(p))T(value);
568 template<
class T, std::
size_t s>
void deallocate(pointer p, std::size_t n)
Free objects.
Definition: poolallocator.hh:556
const_pointer address(const_reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:348
const void * const_pointer
Definition: poolallocator.hh:380
Pool()
Constructor.
Definition: poolallocator.hh:447
void print(std::ostream &os)
Print elements in pool for debugging.
Definition: poolallocator.hh:483
A memory pool of objects.
Definition: poolallocator.hh:29
void free(void *o)
Free an object.
Definition: poolallocator.hh:518
~Pool()
Destructor.
Definition: poolallocator.hh:463
PoolAllocator< U, s > other
Definition: poolallocator.hh:361
Statically compute the least common multiple of two integers.
void value_type
Definition: poolallocator.hh:382
Rebind the allocator to another type.
Definition: poolallocator.hh:359
PoolAllocator(const PoolAllocator< T, t > &)
Definition: poolallocator.hh:389
An allocator managing a pool of objects for reuse.
Definition: poolallocator.hh:32
T MemberType
The type of object we allocate memory for.
Definition: poolallocator.hh:108
const T * const_pointer
The constant pointer type.
Definition: poolallocator.hh:275
T * pointer
The pointer type.
Definition: poolallocator.hh:270
This file implements a template class to determine alignment requirements of types at compile time...
Pool< T, size > PoolType
The type of the memory pool we use.
Definition: poolallocator.hh:365
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
Calculate the least common multiple of two numbers.
Definition: lcm.hh:30
void construct(pointer p, const_reference value)
Construct an object.
Definition: poolallocator.hh:563
pointer address(reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:342
The size of each chunk memory chunk.
Definition: poolallocator.hh:147
T value_type
Type of the values we construct and allocate.
Definition: poolallocator.hh:256
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:253
Calculates the alignment requirement of a type.
Definition: alignment.hh:99
Definition: poolallocator.hh:23
Fallback implementation of the C++0x static_assert feature.
void * allocate()
Get a new or recycled object.
Definition: poolallocator.hh:530
PoolAllocator(const PoolAllocator< U, u > &)
Coopy Constructor.
Definition: poolallocator.hh:306
The number of element each chunk can hold.
Definition: poolallocator.hh:154
PoolAllocator()
Constructor.
Definition: poolallocator.hh:542
std::size_t size_type
The size type.
Definition: poolallocator.hh:290
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:231
The size of a union of Reference and MemberType.
Definition: poolallocator.hh:114
The alignment that suits both the MemberType and the Reference (i.e. their least common multiple)...
Definition: poolallocator.hh:128
The number of objects to fit into one memory chunk allocated.
Definition: poolallocator.hh:264
PoolAllocator< U, s > other
Definition: poolallocator.hh:385
pointer allocate(std::size_t n, const_pointer hint=0)
Allocates objects.
Definition: poolallocator.hh:547
The aligned size of the type.
Definition: poolallocator.hh:136
void * pointer
Definition: poolallocator.hh:379
void destroy(pointer p)
Destroy an object without freeing memory.
Definition: poolallocator.hh:569
std::ptrdiff_t difference_type
The difference_type.
Definition: poolallocator.hh:295
Size requirement. At least one object has to stored.
Definition: poolallocator.hh:121
int max_size() const
Not correctly implemented, yet!
Definition: poolallocator.hh:353
T & reference
The reference type.
Definition: poolallocator.hh:280
const T & const_reference
The constant reference type.
Definition: poolallocator.hh:285