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>
328 inline pointer
allocate(std::size_t n, const_pointer hint=0);
337 inline void deallocate(pointer p, std::size_t n);
344 inline void construct(pointer p, const_reference value);
350 inline void destroy(pointer p);
355 inline pointer
address(reference x)
const {
return &x; }
361 inline const_pointer
address(const_reference x)
const {
return &x; }
384 PoolType memoryPool_;
388 template <std::
size_t s>
396 template <
class U>
struct rebind
403 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
410 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
416 template<
typename T, std::
size_t t1, std::
size_t t2>
423 template<
typename T, std::
size_t t1, std::
size_t t2>
429 template<
typename T, std::
size_t t1, std::
size_t t2>
436 template<
typename T, std::
size_t t1, std::
size_t t2>
442 template<std::
size_t t1, std::
size_t t2>
448 template<std::
size_t t1, std::
size_t t2>
454 template<
class T, std::
size_t S>
456 : head_(0), chunks_(0)
459 dune_static_assert(
sizeof(Reference)<=unionSize,
"Library Error: type of referene is too big");
468 template<
class T, std::
size_t S>
478 Chunk *current=chunks_;
482 Chunk *tmp = current;
483 current = current->next_;
488 template<
class T, std::
size_t S>
491 Chunk* current=chunks_;
494 current=current->next_;
499 template<
class T, std::
size_t S>
502 Chunk *newChunk =
new Chunk;
503 newChunk->next_ = chunks_;
506 char* start = chunks_->memory_;
507 char* last = &start[elements*alignedSize];
508 Reference* ref =
new (start) (Reference);
515 for(
char* element=start+alignedSize; element<last; element=element+alignedSize) {
516 Reference* next =
new (element) (Reference);
523 template<
class T, std::
size_t S>
528 Chunk* current=chunks_;
530 if(static_cast<void*>(¤t->chunk_)<=b &&
531 static_cast<void*>((¤t->chunk_)+chunkSize)>b)
533 current=current->next_;
536 throw std::bad_alloc();
538 Reference* freed =
static_cast<Reference*
>(b);
539 freed->next_ = head_;
545 std::cerr<<
"Tried to free null pointer! "<<b<<std::endl;
546 throw std::bad_alloc();
550 template<
class T, std::
size_t S>
556 Reference* p = head_;
562 template<
class T, std::
size_t s>
566 template<
class T, std::
size_t s>
571 return static_cast<T*
>(memoryPool_.allocate());
573 throw std::bad_alloc();
576 template<
class T, std::
size_t s>
579 for(
size_t i=0; i<n; i++)
580 memoryPool_.free(p++);
583 template<
class T, std::
size_t s>
586 ::new (static_cast<void*>(p))T(value);
589 template<
class T, std::
size_t s>
void deallocate(pointer p, std::size_t n)
Free objects.
Definition: poolallocator.hh:577
const_pointer address(const_reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:361
const void * const_pointer
Definition: poolallocator.hh:393
Pool()
Constructor.
Definition: poolallocator.hh:455
void print(std::ostream &os)
Print elements in pool for debugging.
Definition: poolallocator.hh:489
A memory pool of objects.
Definition: poolallocator.hh:29
void free(void *o)
Free an object.
Definition: poolallocator.hh:524
~Pool()
Destructor.
Definition: poolallocator.hh:469
PoolAllocator< U, s > other
Definition: poolallocator.hh:374
Statically compute the least common multiple of two integers.
void value_type
Definition: poolallocator.hh:395
Rebind the allocator to another type.
Definition: poolallocator.hh:372
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:378
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
PoolAllocator(const PoolAllocator &)
Copy constructor that does not copy the memory pool.
Definition: poolallocator.hh:313
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:584
pointer address(reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:355
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:551
PoolAllocator(const PoolAllocator< U, u > &)
Copy Constructor that does not copy the memory pool.
Definition: poolallocator.hh:306
The number of element each chunk can hold.
Definition: poolallocator.hh:154
PoolAllocator()
Constructor.
Definition: poolallocator.hh:563
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:398
pointer allocate(std::size_t n, const_pointer hint=0)
Allocates objects.
Definition: poolallocator.hh:568
The aligned size of the type.
Definition: poolallocator.hh:136
void * pointer
Definition: poolallocator.hh:392
void destroy(pointer p)
Destroy an object without freeing memory.
Definition: poolallocator.hh:590
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:366
T & reference
The reference type.
Definition: poolallocator.hh:280
const T & const_reference
The constant reference type.
Definition: poolallocator.hh:285