5#ifndef DUNE_COMMON_POOLALLOCATOR_HH
6#define DUNE_COMMON_POOLALLOCATOR_HH
21template<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>;
120 constexpr static int size = (
sizeof(
MemberType) <= s &&
sizeof(Reference) <= s) ?
185 inline void print(std::ostream& os);
221 template<
class T, std::
size_t s>
276 template<
typename U, std::
size_t u>
359 template <std::
size_t s>
367 template <
class U>
struct rebind
374 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
381 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
387 template<
typename T, std::
size_t t1, std::
size_t t2>
394 template<
typename T, std::
size_t t1, std::
size_t t2>
400 template<
typename T, std::
size_t t1, std::
size_t t2>
407 template<
typename T, std::
size_t t1, std::
size_t t2>
413 template<std::
size_t t1, std::
size_t t2>
419 template<std::
size_t t1, std::
size_t t2>
425 template<
class T, std::
size_t S>
427 : head_(0), chunks_(0)
429 static_assert(
sizeof(T)<=
unionSize,
"Library Error: type T is too big");
430 static_assert(
sizeof(Reference)<=
unionSize,
"Library Error: type of reference is too big");
432 static_assert(
sizeof(T)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one value");
433 static_assert(
sizeof(Reference)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one reference");
434 static_assert(
chunkSize %
alignment == 0,
"Library Error: compiler cannot calculate!");
435 static_assert(
elements>=1,
"Library Error: we need to hold at least one element!");
439 template<
class T, std::
size_t S>
449 Chunk *current=chunks_;
453 Chunk *tmp = current;
454 current = current->next_;
459 template<
class T, std::
size_t S>
462 Chunk* current=chunks_;
465 current=current->next_;
470 template<
class T, std::
size_t S>
473 Chunk *newChunk =
new Chunk;
474 newChunk->next_ = chunks_;
477 char* start = chunks_->chunk_;
478 char* last = &start[elements*alignedSize];
479 Reference* ref =
new (start) (Reference);
486 for(
char* element=start+alignedSize; element<last; element=element+alignedSize) {
487 Reference* next =
new (element) (Reference);
494 template<
class T, std::
size_t S>
499 Chunk* current=chunks_;
501 if(
static_cast<void*
>(current->chunk_)<=b &&
502 static_cast<void*
>(current->chunk_+chunkSize)>b)
504 current=current->next_;
507 throw std::bad_alloc();
509 Reference* freed =
static_cast<Reference*
>(b);
510 freed->next_ = head_;
516 std::cerr<<
"Tried to free null pointer! "<<b<<std::endl;
517 throw std::bad_alloc();
521 template<
class T, std::
size_t S>
527 Reference* p = head_;
533 template<
class T, std::
size_t s>
537 template<
class T, std::
size_t s>
542 return static_cast<T*
>(memoryPool_.allocate());
544 throw std::bad_alloc();
547 template<
class T, std::
size_t s>
550 for(
size_t i=0; i<n; i++)
551 memoryPool_.free(p++);
554 template<
class T, std::
size_t s>
557 ::new (
static_cast<void*
>(p))T(value);
560 template<
class T, std::
size_t s>
void construct(pointer p, const_reference value)
Construct an object.
Definition poolallocator.hh:555
void free(void *o)
Free an object.
Definition poolallocator.hh:495
~Pool()
Destructor.
Definition poolallocator.hh:440
void * allocate()
Get a new or recycled object.
Definition poolallocator.hh:522
void print(std::ostream &os)
Print elements in pool for debugging.
Definition poolallocator.hh:460
pointer allocate(std::size_t n, const_pointer hint=0)
Allocates objects.
Definition poolallocator.hh:539
Pool()
Constructor.
Definition poolallocator.hh:426
void deallocate(pointer p, std::size_t n)
Free objects.
Definition poolallocator.hh:548
void destroy(pointer p)
Destroy an object without freeing memory.
Definition poolallocator.hh:561
PoolAllocator()
Constructor.
Definition poolallocator.hh:534
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:237
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:259
Dune namespace.
Definition alignedallocator.hh:13
Get the 'const' version of a reference to a mutable object.
Definition genericiterator.hh:87
A memory pool of objects.
Definition poolallocator.hh:90
static constexpr int alignment
The alignment that suits both the MemberType and the Reference (i.e. their least common multiple).
Definition poolallocator.hh:127
static constexpr int alignedSize
The aligned size of the type.
Definition poolallocator.hh:135
static constexpr int chunkSize
The size of each chunk memory chunk.
Definition poolallocator.hh:144
static constexpr int size
Size requirement. At least one object has to stored.
Definition poolallocator.hh:120
static constexpr int unionSize
The size of a union of Reference and MemberType.
Definition poolallocator.hh:113
T MemberType
The type of object we allocate memory for.
Definition poolallocator.hh:108
static constexpr int elements
The number of element each chunk can hold.
Definition poolallocator.hh:150
An allocator managing a pool of objects for reuse.
Definition poolallocator.hh:223
Pool< T, size > PoolType
The type of the memory pool we use.
Definition poolallocator.hh:349
const_pointer address(const_reference x) const
Convert a reference to a pointer.
Definition poolallocator.hh:332
const T & const_reference
The constant reference type.
Definition poolallocator.hh:256
std::size_t size_type
The size type.
Definition poolallocator.hh:261
T value_type
Type of the values we construct and allocate.
Definition poolallocator.hh:230
T & reference
The reference type.
Definition poolallocator.hh:251
PoolAllocator(const PoolAllocator &)
Copy constructor that does not copy the memory pool.
Definition poolallocator.hh:284
const T * const_pointer
The constant pointer type.
Definition poolallocator.hh:246
pointer address(reference x) const
Convert a reference to a pointer.
Definition poolallocator.hh:326
T * pointer
The pointer type.
Definition poolallocator.hh:241
PoolAllocator(const PoolAllocator< U, u > &)
Copy Constructor that does not copy the memory pool.
Definition poolallocator.hh:277
std::ptrdiff_t difference_type
The difference_type.
Definition poolallocator.hh:266
int max_size() const noexcept
Not correctly implemented, yet!
Definition poolallocator.hh:337
static constexpr int size
The number of objects to fit into one memory chunk allocated.
Definition poolallocator.hh:236
Rebind the allocator to another type.
Definition poolallocator.hh:344
PoolAllocator< U, s > other
Definition poolallocator.hh:345
void value_type
Definition poolallocator.hh:366
void * pointer
Definition poolallocator.hh:363
const void * const_pointer
Definition poolallocator.hh:364
PoolAllocator< U, s > other
Definition poolallocator.hh:369