3 #ifndef DUNE_COMMON_POOLALLOCATOR_HH
4 #define DUNE_COMMON_POOLALLOCATOR_HH
19 template<std::
size_t size,
typename T>
26 template<
typename T, std::
size_t s>
29 template<
typename T, std::
size_t s>
86 template<
class T, std::
size_t s>
90 friend struct ::testPoolMain<s,T>;
185 inline void print(std::ostream& os);
221 template<
class T, std::
size_t s>
279 template<
typename U, std::
size_t u>
362 template <std::
size_t s>
370 template <
class U>
struct rebind
377 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
384 template<
typename T1, std::
size_t t1,
typename T2, std::
size_t t2>
390 template<
typename T, std::
size_t t1, std::
size_t t2>
397 template<
typename T, std::
size_t t1, std::
size_t t2>
403 template<
typename T, std::
size_t t1, std::
size_t t2>
410 template<
typename T, std::
size_t t1, std::
size_t t2>
416 template<std::
size_t t1, std::
size_t t2>
422 template<std::
size_t t1, std::
size_t t2>
428 template<
class T, std::
size_t S>
430 : head_(0), chunks_(0)
432 static_assert(
sizeof(T)<=
unionSize,
"Library Error: type T is too big");
433 static_assert(
sizeof(Reference)<=
unionSize,
"Library Error: type of referene is too big");
435 static_assert(
sizeof(T)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one value");
436 static_assert(
sizeof(Reference)<=
chunkSize,
"Library Error: chunkSize must be able to hold at least one reference");
437 static_assert(
chunkSize %
alignment == 0,
"Library Error: compiler cannot calculate!");
438 static_assert(
elements>=1,
"Library Error: we need to hold at least one element!");
442 template<
class T, std::
size_t S>
452 Chunk *current=chunks_;
456 Chunk *tmp = current;
457 current = current->next_;
462 template<
class T, std::
size_t S>
465 Chunk* current=chunks_;
468 current=current->next_;
473 template<
class T, std::
size_t S>
476 Chunk *newChunk =
new Chunk;
477 newChunk->next_ = chunks_;
480 char* start = chunks_->chunk_;
481 char* last = &start[elements*alignedSize];
482 Reference* ref =
new (start) (Reference);
489 for(
char* element=start+alignedSize; element<last; element=element+alignedSize) {
490 Reference* next =
new (element) (Reference);
497 template<
class T, std::
size_t S>
502 Chunk* current=chunks_;
504 if(
static_cast<void*
>(current->chunk_)<=b &&
505 static_cast<void*
>(current->chunk_+chunkSize)>b)
507 current=current->next_;
510 throw std::bad_alloc();
512 Reference* freed =
static_cast<Reference*
>(b);
513 freed->next_ = head_;
519 std::cerr<<
"Tried to free null pointer! "<<b<<std::endl;
520 throw std::bad_alloc();
524 template<
class T, std::
size_t S>
530 Reference* p = head_;
536 template<
class T, std::
size_t s>
540 template<
class T, std::
size_t s>
545 return static_cast<T*
>(memoryPool_.allocate());
547 throw std::bad_alloc();
550 template<
class T, std::
size_t s>
553 for(
size_t i=0; i<n; i++)
554 memoryPool_.free(p++);
557 template<
class T, std::
size_t s>
560 ::new (
static_cast<void*
>(p))T(value);
563 template<
class T, std::
size_t s>
Statically compute the least common multiple of two integers.
void construct(pointer p, const_reference value)
Construct an object.
Definition: poolallocator.hh:558
void free(void *o)
Free an object.
Definition: poolallocator.hh:498
~Pool()
Destructor.
Definition: poolallocator.hh:443
void * allocate()
Get a new or recycled object.
Definition: poolallocator.hh:525
void print(std::ostream &os)
Print elements in pool for debugging.
Definition: poolallocator.hh:463
pointer allocate(std::size_t n, const_pointer hint=0)
Allocates objects.
Definition: poolallocator.hh:542
Pool()
Constructor.
Definition: poolallocator.hh:429
void deallocate(pointer p, std::size_t n)
Free objects.
Definition: poolallocator.hh:551
void destroy(pointer p)
Destroy an object without freeing memory.
Definition: poolallocator.hh:564
PoolAllocator()
Constructor.
Definition: poolallocator.hh:537
Dune namespace.
Definition: alignedallocator.hh:14
constexpr bool operator!=(const DebugAllocator< T > &, const DebugAllocator< T > &)
check whether allocators are not equivalent
Definition: debugallocator.hh:318
constexpr bool operator==(const DebugAllocator< T > &, const DebugAllocator< T > &)
check whether allocators are equivalent
Definition: debugallocator.hh:310
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
Calculate the least common multiple of two numbers.
Definition: lcm.hh:30
A memory pool of objects.
Definition: poolallocator.hh:88
@ elements
The number of element each chunk can hold.
Definition: poolallocator.hh:149
@ chunkSize
The size of each chunk memory chunk.
Definition: poolallocator.hh:143
@ unionSize
The size of a union of Reference and MemberType.
Definition: poolallocator.hh:112
@ alignment
The alignment that suits both the MemberType and the Reference (i.e. their least common multiple).
Definition: poolallocator.hh:126
@ size
Size requirement. At least one object has to stored.
Definition: poolallocator.hh:119
@ alignedSize
The aligned size of the type.
Definition: poolallocator.hh:134
T MemberType
The type of object we allocate memory for.
Definition: poolallocator.hh:106
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:352
const_pointer address(const_reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:335
const T & const_reference
The constant reference type.
Definition: poolallocator.hh:259
std::size_t size_type
The size type.
Definition: poolallocator.hh:264
T value_type
Type of the values we construct and allocate.
Definition: poolallocator.hh:230
@ size
The number of objects to fit into one memory chunk allocated.
Definition: poolallocator.hh:238
T & reference
The reference type.
Definition: poolallocator.hh:254
PoolAllocator(const PoolAllocator &)
Copy constructor that does not copy the memory pool.
Definition: poolallocator.hh:287
const T * const_pointer
The constant pointer type.
Definition: poolallocator.hh:249
pointer address(reference x) const
Convert a reference to a pointer.
Definition: poolallocator.hh:329
T * pointer
The pointer type.
Definition: poolallocator.hh:244
PoolAllocator(const PoolAllocator< U, u > &)
Copy Constructor that does not copy the memory pool.
Definition: poolallocator.hh:280
std::ptrdiff_t difference_type
The difference_type.
Definition: poolallocator.hh:269
int max_size() const noexcept
Not correctly implemented, yet!
Definition: poolallocator.hh:340
Rebind the allocator to another type.
Definition: poolallocator.hh:347
PoolAllocator< U, s > other
Definition: poolallocator.hh:348
void value_type
Definition: poolallocator.hh:369
void * pointer
Definition: poolallocator.hh:366
const void * const_pointer
Definition: poolallocator.hh:367
PoolAllocator< U, s > other
Definition: poolallocator.hh:372