Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
lar::details::bulk_allocator::BulkAllocatorBase< T > Class Template Reference

A class managing a memory pool. More...

#include <BulkAllocator.h>

Inheritance diagram for lar::details::bulk_allocator::BulkAllocatorBase< T >:
lar::details::bulk_allocator::ReferenceCounter

Classes

class  MemoryChunk_t
 Internal memory chunk; like a std::vector, but does not construct. More...
 

Public Types

typedef size_t size_type
 
typedef T value_type
 
typedef T * pointer
 
- Public Types inherited from lar::details::bulk_allocator::ReferenceCounter
typedef unsigned int Counter_t
 type of user counter More...
 

Public Member Functions

 BulkAllocatorBase (size_type NewChunkSize=DefaultChunkSize, bool bPreallocate=false)
 Constructor; preallocates memory if explicitly requested. More...
 
 ~BulkAllocatorBase ()
 Destructor: releases all the memory pool (. More...
 
void Free ()
 Releases the pool of memory; all pointer to it become invalid. More...
 
pointer Get (size_type n)
 Returns a pointer to memory for n new values of type T. More...
 
void Release (pointer)
 Releases memory pointed by the specified pointer (but it does not). More...
 
void AddUser ()
 Add a new pool user with the current parameters. More...
 
void AddUser (size_type NewChunkSize, bool bPreallocate=false)
 Add a new pool user with new parameters. More...
 
bool RemoveUser ()
 Removed a user to the users count; if no user is left, free the pool. More...
 
size_type AllocatedCount () const
 Returns the total number of entries in the pool. More...
 
size_type UsedCount () const
 Returns the total number of used entries in the pool. More...
 
size_type FreeCount () const
 Returns the total number of unused entries in the pool. More...
 
size_type NChunks () const
 Returns the number of memory pool chunks allocated. More...
 
std::array< size_type, 2 > GetCounts () const
 Returns an array equivalent to { UsedCount(), FreeCount() }. More...
 
void SetChunkSize (size_type NewChunkSize, bool force=false)
 Sets the chunk size for the future allocations. More...
 
size_type GetChunkSize () const
 Returns the current chunk size. More...
 
void Preallocate ()
 
- Public Member Functions inherited from lar::details::bulk_allocator::ReferenceCounter
bool hasUsers () const
 Returns whether there are currently users. More...
 
Counter_t Count () const
 Returns the number of registered users. More...
 
void AddUser ()
 Adds a user to the users count. More...
 
bool RemoveUser ()
 Removed a user to the users count; returns false if no user yet. More...
 

Private Types

typedef std::allocator< T > Allocator_t
 
typedef Allocator_t::difference_type difference_type
 
typedef std::vector< MemoryChunk_tMemoryPool_t
 

Private Member Functions

void Preallocate (size_type n)
 Preallocates a chunk of the given size; allocates if free space < n. More...
 

Private Attributes

Allocator_t allocator
 the actual allocator we use More...
 
size_type ChunkSize
 size of the chunks to add More...
 
MemoryPool_t MemoryPool
 list of all memory chunks; first is free More...
 

Static Private Attributes

static size_type DefaultChunkSize = 10000
 Default chunk size (default: 10000) More...
 

Detailed Description

template<typename T>
class lar::details::bulk_allocator::BulkAllocatorBase< T >

A class managing a memory pool.

The management policy is to allocate big chunks of memory. Memory is never freed, until the last user is removed (which is responsibility of the caller), this object is destroyed of Free() is explicitly called. The amount of memory on

This class has a users counter. The count must be explicitly handled by the caller.

Definition at line 31 of file BulkAllocator.h.

Member Typedef Documentation

template<typename T>
typedef std::allocator<T> lar::details::bulk_allocator::BulkAllocatorBase< T >::Allocator_t
private

Definition at line 318 of file BulkAllocator.h.

template<typename T>
typedef Allocator_t::difference_type lar::details::bulk_allocator::BulkAllocatorBase< T >::difference_type
private

Definition at line 319 of file BulkAllocator.h.

Definition at line 378 of file BulkAllocator.h.

template<typename T>
typedef T* lar::details::bulk_allocator::BulkAllocatorBase< T >::pointer

Definition at line 264 of file BulkAllocator.h.

template<typename T>
typedef size_t lar::details::bulk_allocator::BulkAllocatorBase< T >::size_type

Definition at line 262 of file BulkAllocator.h.

template<typename T>
typedef T lar::details::bulk_allocator::BulkAllocatorBase< T >::value_type

Definition at line 263 of file BulkAllocator.h.

Constructor & Destructor Documentation

template<typename T >
lar::details::bulk_allocator::BulkAllocatorBase< T >::BulkAllocatorBase ( size_type  NewChunkSize = DefaultChunkSize,
bool  bPreallocate = false 
)

Constructor; preallocates memory if explicitly requested.

Definition at line 418 of file BulkAllocator.h.

418  :
419  ChunkSize(NewChunkSize), MemoryPool()
420  {
421  Preallocate(bPreallocate? ChunkSize: 0);
422  if (bDebug) {
423  std::cout << "BulkAllocatorBase[" << ((void*) this)
424  << "] created for type " << demangle<value_type>()
425  << " with chunk size " << GetChunkSize()
426  << " x" << sizeof(value_type) << " byte => "
427  << (GetChunkSize()*sizeof(value_type)) << " bytes/chunk"
428  << std::endl;
429  } // if debug
430  } // BulkAllocatorBase<T>::BulkAllocatorBase()
size_type ChunkSize
size of the chunks to add
size_type GetChunkSize() const
Returns the current chunk size.
MemoryPool_t MemoryPool
list of all memory chunks; first is free
QTextStream & endl(QTextStream &s)

Destructor: releases all the memory pool (.

See also
Free())

Definition at line 272 of file BulkAllocator.h.

272 { Free(); }
void Free()
Releases the pool of memory; all pointer to it become invalid.

Member Function Documentation

template<typename T>
void lar::details::bulk_allocator::BulkAllocatorBase< T >::AddUser ( )
inline

Add a new pool user with the current parameters.

Definition at line 284 of file BulkAllocator.h.

void AddUser()
Adds a user to the users count.
template<typename T >
void lar::details::bulk_allocator::BulkAllocatorBase< T >::AddUser ( size_type  NewChunkSize,
bool  bPreallocate = false 
)

Add a new pool user with new parameters.

Definition at line 455 of file BulkAllocator.h.

456  {
457  AddUser();
458  SetChunkSize(NewChunkSize);
459  Preallocate(bPreallocate? ChunkSize: 0);
460  } // BulkAllocatorBase<T>::AddUser(size_type, bool )
size_type ChunkSize
size of the chunks to add
void SetChunkSize(size_type NewChunkSize, bool force=false)
Sets the chunk size for the future allocations.
void AddUser()
Add a new pool user with the current parameters.
template<typename T >
BulkAllocatorBase< T >::size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::AllocatedCount ( ) const

Returns the total number of entries in the pool.

Definition at line 472 of file BulkAllocator.h.

473  {
474  size_type n = 0;
475  for (const auto& chunk: MemoryPool) n += chunk.size();
476  return n;
477  } // AllocatedCount()
std::void_t< T > n
MemoryPool_t MemoryPool
list of all memory chunks; first is free
template<typename T >
void lar::details::bulk_allocator::BulkAllocatorBase< T >::Free ( )

Releases the pool of memory; all pointer to it become invalid.

Definition at line 434 of file BulkAllocator.h.

434  {
435  if (bDebug) {
436  std::cout << "BulkAllocatorBase[" << ((void*) this) << "] freeing "
437  << NChunks() << " memory chunks with " << AllocatedCount()
438  << " elements" << std::endl;
439  } // if debug
440  MemoryPool.clear();
441  } // BulkAllocatorBase<T>::Free()
size_type AllocatedCount() const
Returns the total number of entries in the pool.
size_type NChunks() const
Returns the number of memory pool chunks allocated.
MemoryPool_t MemoryPool
list of all memory chunks; first is free
QTextStream & endl(QTextStream &s)
template<typename T>
size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::FreeCount ( ) const

Returns the total number of unused entries in the pool.

template<typename T >
BulkAllocatorBase< T >::pointer lar::details::bulk_allocator::BulkAllocatorBase< T >::Get ( size_type  n)
inline

Returns a pointer to memory for n new values of type T.

Definition at line 523 of file BulkAllocator.h.

524  {
525  if (n == 0) return nullptr;
526  // get the free pointer from the latest chunk (#0)
527  pointer ptr = MemoryPool.front().get(n);
528  if (ptr) return ptr;
529  // no free element left in that chunk:
530  // - create a new one in the first position of the pool (move the rest)
531  // - return the pointer from the new pool
532  // (if it fails, it fails... but how could that happen?)
533  if (bDebug) {
534  std::array<size_type, 2> stats = GetCounts();
535  std::cout << "BulkAllocatorBase[" << ((void*) this)
536  << "] allocating " << std::max(ChunkSize, n)
537  << " more elements (on top of the current " << (stats[0] + stats[1])
538  << " elements, " << stats[1] << " unused)" << std::endl;
539  } // if debug
540  return MemoryPool.emplace
541  (MemoryPool.begin(), allocator, std::max(ChunkSize, n))->get(n);
542  } // BulkAllocatorBase<T>::Get()
std::array< size_type, 2 > GetCounts() const
Returns an array equivalent to { UsedCount(), FreeCount() }.
size_type ChunkSize
size of the chunks to add
std::void_t< T > n
Allocator_t allocator
the actual allocator we use
static int max(int a, int b)
MemoryPool_t MemoryPool
list of all memory chunks; first is free
QTextStream & endl(QTextStream &s)
template<typename T>
size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::GetChunkSize ( ) const
inline

Returns the current chunk size.

Definition at line 311 of file BulkAllocator.h.

311 { return ChunkSize; }
size_type ChunkSize
size of the chunks to add
template<typename T >
std::array< typename BulkAllocatorBase< T >::size_type, 2 > lar::details::bulk_allocator::BulkAllocatorBase< T >::GetCounts ( ) const

Returns an array equivalent to { UsedCount(), FreeCount() }.

Definition at line 492 of file BulkAllocator.h.

493  {
494  // BUG the double brace syntax is required to work around clang bug 21629
495  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
496  std::array<BulkAllocatorBase<T>::size_type, 2> stats = {{ 0U, 0U }};
497  for (const auto& chunk: MemoryPool) {
498  stats[0] += chunk.used();
499  stats[1] += chunk.available();
500  } // for
501  return stats;
502  } // BulkAllocatorBase<T>::GetCounts()
MemoryPool_t MemoryPool
list of all memory chunks; first is free
template<typename T>
size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::NChunks ( ) const
inline

Returns the number of memory pool chunks allocated.

Definition at line 302 of file BulkAllocator.h.

302 { return MemoryPool.size(); }
MemoryPool_t MemoryPool
list of all memory chunks; first is free
template<typename T>
void lar::details::bulk_allocator::BulkAllocatorBase< T >::Preallocate ( )
inline

Preallocates a chunk of the current ChunkSize;

See also
Preallocate(size_type)

Definition at line 315 of file BulkAllocator.h.

template<typename T >
void lar::details::bulk_allocator::BulkAllocatorBase< T >::Preallocate ( size_type  n)
private

Preallocates a chunk of the given size; allocates if free space < n.

Definition at line 464 of file BulkAllocator.h.

464  {
465  if (MemoryPool.empty() || (MemoryPool.front().available() < n))
466  MemoryPool.emplace_back(allocator, n);
467  } // BulkAllocatorBase<T>::RemoveUser()
std::void_t< T > n
Allocator_t allocator
the actual allocator we use
MemoryPool_t MemoryPool
list of all memory chunks; first is free
template<typename T>
void lar::details::bulk_allocator::BulkAllocatorBase< T >::Release ( pointer  )
inline

Releases memory pointed by the specified pointer (but it does not).

Definition at line 281 of file BulkAllocator.h.

281 {}
template<typename T >
bool lar::details::bulk_allocator::BulkAllocatorBase< T >::RemoveUser ( )

Removed a user to the users count; if no user is left, free the pool.

Definition at line 445 of file BulkAllocator.h.

445  {
447  if (hasUsers()) return true;
448  Free();
449  return false;
450  } // BulkAllocatorBase<T>::RemoveUser()
bool RemoveUser()
Removed a user to the users count; returns false if no user yet.
void Free()
Releases the pool of memory; all pointer to it become invalid.
bool hasUsers() const
Returns whether there are currently users.
template<typename T >
void lar::details::bulk_allocator::BulkAllocatorBase< T >::SetChunkSize ( size_type  NewChunkSize,
bool  force = false 
)

Sets the chunk size for the future allocations.

Definition at line 507 of file BulkAllocator.h.

508  {
509  if ((GetChunkSize() == NewChunkSize) && !force) return;
510  if (bDebug) {
511  std::cout << "BulkAllocatorBase[" << ((void*) this) << "]"
512  << " changing chunk size: " << GetChunkSize() << " => "
513  << NewChunkSize << ": x" << sizeof(value_type) << " byte => "
514  << (NewChunkSize*sizeof(value_type)) << " bytes/chunk"
515  << std::endl;
516  }
517  ChunkSize = NewChunkSize;
518  } // BulkAllocatorBase<T>::SetChunkSize()
size_type ChunkSize
size of the chunks to add
size_type GetChunkSize() const
Returns the current chunk size.
QTextStream & endl(QTextStream &s)
template<typename T >
BulkAllocatorBase< T >::size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::UsedCount ( ) const

Returns the total number of used entries in the pool.

Definition at line 482 of file BulkAllocator.h.

483  {
484  size_type n = 0;
485  for (const auto& chunk: MemoryPool) n += chunk.used();
486  return n;
487  } // BulkAllocatorBase<T>::UsedCount()
std::void_t< T > n
MemoryPool_t MemoryPool
list of all memory chunks; first is free

Member Data Documentation

template<typename T>
Allocator_t lar::details::bulk_allocator::BulkAllocatorBase< T >::allocator
private

the actual allocator we use

Definition at line 321 of file BulkAllocator.h.

template<typename T>
size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::ChunkSize
private

size of the chunks to add

Definition at line 380 of file BulkAllocator.h.

template<typename T>
BulkAllocatorBase< T >::size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::DefaultChunkSize = 10000
staticprivate

Default chunk size (default: 10000)

Definition at line 384 of file BulkAllocator.h.

template<typename T>
MemoryPool_t lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool
private

list of all memory chunks; first is free

Definition at line 381 of file BulkAllocator.h.


The documentation for this class was generated from the following file: