3 #ifndef DUNE_COMMON_POOLALLOCATOR_HH
4 #define DUNE_COMMON_POOLALLOCATOR_HH
20 template<std::
size_t size,
typename T>
27 template<
typename T, std::
size_t s>
30 template<
typename T, std::
size_t s>
87 template<
class T, std::
size_t s>
91 friend struct ::testPoolMain<s,T>;
182 unsigned long long lmemory =
reinterpret_cast<unsigned long long>(chunk_);
187 memory_ =
reinterpret_cast<char *
>(lmemory);
205 inline void free(
void* o);
210 inline void print(std::ostream& os);
215 Pool(
const Pool<MemberType,s>&);
217 void operator=(
const Pool<MemberType,s>& pool)
const;
246 template<
class T, std::
size_t s>
304 template<
typename U, std::
size_t u>
327 inline pointer
allocate(std::size_t n, const_pointer hint=0);
336 inline void deallocate(pointer p, std::size_t n);
343 inline void construct(pointer p, const_reference value);
349 inline void destroy(pointer p);
354 inline pointer
address(reference x)
const {
return &x; }
360 inline const_pointer
address(const_reference x)
const {
return &x; }
383 PoolType memoryPool_;
387 template <std::
size_t s>
395 template <
class U>
struct rebind
402 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
409 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
415 template<
typename T, std::
size_t t1, std::
size_t t2>
422 template<
typename T, std::
size_t t1, std::
size_t t2>
428 template<
typename T, std::
size_t t1, std::
size_t t2>
435 template<
typename T, std::
size_t t1, std::
size_t t2>
441 template<std::
size_t t1, std::
size_t t2>
447 template<std::
size_t t1, std::
size_t t2>
453 template<
class T, std::
size_t S>
455 : head_(0), chunks_(0)
457 static_assert(
sizeof(T)<=unionSize,
"Library Error: type T is too big");
458 static_assert(
sizeof(Reference)<=unionSize,
"Library Error: type of referene is too big");
459 static_assert(unionSize<=
alignedSize,
"Library Error: alignedSize too small");
460 static_assert(
sizeof(T)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one value");
461 static_assert(
sizeof(Reference)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one reference");
463 static_assert(
elements>=1,
"Library Error: we need to hold at least one element!");
467 template<
class T, std::
size_t S>
477 Chunk *current=chunks_;
481 Chunk *tmp = current;
482 current = current->next_;
487 template<
class T, std::
size_t S>
490 Chunk* current=chunks_;
493 current=current->next_;
498 template<
class T, std::
size_t S>
501 Chunk *newChunk =
new Chunk;
502 newChunk->next_ = chunks_;
505 char* start = chunks_->memory_;
506 char* last = &start[elements*alignedSize];
507 Reference* ref =
new (start) (Reference);
514 for(
char* element=start+alignedSize; element<last; element=element+alignedSize) {
515 Reference* next =
new (element) (Reference);
522 template<
class T, std::
size_t S>
527 Chunk* current=chunks_;
529 if(static_cast<void*>(¤t->chunk_)<=b &&
530 static_cast<void*>((¤t->chunk_)+chunkSize)>b)
532 current=current->next_;
535 throw std::bad_alloc();
537 Reference* freed =
static_cast<Reference*
>(b);
538 freed->next_ = head_;
544 std::cerr<<
"Tried to free null pointer! "<<b<<std::endl;
545 throw std::bad_alloc();
549 template<
class T, std::
size_t S>
555 Reference* p = head_;
561 template<
class T, std::
size_t s>
565 template<
class T, std::
size_t s>
570 return static_cast<T*
>(memoryPool_.allocate());
572 throw std::bad_alloc();
575 template<
class T, std::
size_t s>
578 for(
size_t i=0; i<n; i++)
579 memoryPool_.free(p++);
582 template<
class T, std::
size_t s>
585 ::new (static_cast<void*>(p))T(value);
588 template<
class T, std::
size_t s>
int max_size() const
Not correctly implemented, yet!
Definition: poolallocator.hh:365
const T & const_reference
The constant reference type.
Definition: poolallocator.hh:284
T MemberType
The type of object we allocate memory for.
Definition: poolallocator.hh:107
T value_type
Type of the values we construct and allocate.
Definition: poolallocator.hh:255
The size of a union of Reference and MemberType.
Definition: poolallocator.hh:113
PoolAllocator(const PoolAllocator< U, u > &)
Copy Constructor that does not copy the memory pool.
Definition: poolallocator.hh:305
T * pointer
The pointer type.
Definition: poolallocator.hh:269
The number of objects to fit into one memory chunk allocated.
Definition: poolallocator.hh:263
std::size_t size_type
The size type.
Definition: poolallocator.hh:289
The size of each chunk memory chunk.
Definition: poolallocator.hh:146
The aligned size of the type.
Definition: poolallocator.hh:135
void destroy(pointer p)
Destroy an object without freeing memory.
Definition: poolallocator.hh:589
PoolAllocator< U, s > other
Definition: poolallocator.hh:397
void deallocate(pointer p, std::size_t n)
Free objects.
Definition: poolallocator.hh:576
void free(void *o)
Free an object.
Definition: poolallocator.hh:523
void value_type
Definition: poolallocator.hh:394
const T * const_pointer
The constant pointer type.
Definition: poolallocator.hh:274
Calculate the least common multiple of two numbers.
Definition: lcm.hh:29
std::ptrdiff_t difference_type
The difference_type.
Definition: poolallocator.hh:294
void * allocate()
Get a new or recycled object.
Definition: poolallocator.hh:550
const void * const_pointer
Definition: poolallocator.hh:392
void * pointer
Definition: poolallocator.hh:391
Rebind the allocator to another type.
Definition: poolallocator.hh:371
Calculates the alignment requirement of a type.
Definition: alignment.hh:95
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:230
void construct(pointer p, const_reference value)
Construct an object.
Definition: poolallocator.hh:583
T & reference
The reference type.
Definition: poolallocator.hh:279
PoolAllocator< U, s > other
Definition: poolallocator.hh:373
PoolAllocator()
Constructor.
Definition: poolallocator.hh:562
An allocator managing a pool of objects for reuse.
Definition: poolallocator.hh:31
void print(std::ostream &os)
Print elements in pool for debugging.
Definition: poolallocator.hh:488
Statically compute the least common multiple of two integers.
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:252
pointer allocate(std::size_t n, const_pointer hint=0)
Allocates objects.
Definition: poolallocator.hh:567
The number of element each chunk can hold.
Definition: poolallocator.hh:153
PoolAllocator(const PoolAllocator &)
Copy constructor that does not copy the memory pool.
Definition: poolallocator.hh:312
This file implements a template class to determine alignment requirements of types at compile time...
Pool()
Constructor.
Definition: poolallocator.hh:454
Pool< T, size > PoolType
The type of the memory pool we use.
Definition: poolallocator.hh:377
const_pointer address(const_reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:360
pointer address(reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:354
Size requirement. At least one object has to stored.
Definition: poolallocator.hh:120
A memory pool of objects.
Definition: poolallocator.hh:28
The alignment that suits both the MemberType and the Reference (i.e. their least common multiple)...
Definition: poolallocator.hh:127
~Pool()
Destructor.
Definition: poolallocator.hh:468