LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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
 
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 ()
 
bool hasUsers () const
 Returns whether there are currently users. More...
 
Counter_t Count () const
 Returns the number of registered users. 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 29 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 321 of file BulkAllocator.h.

type of user counter

Definition at line 229 of file BulkAllocator.h.

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

Definition at line 322 of file BulkAllocator.h.

template<typename T>
typedef std::vector<MemoryChunk_t> lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool_t
private

Definition at line 381 of file BulkAllocator.h.

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

Definition at line 269 of file BulkAllocator.h.

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

Definition at line 267 of file BulkAllocator.h.

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

Definition at line 268 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.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::ChunkSize, lar::details::bulk_allocator::BulkAllocatorBase< T >::GetChunkSize(), and lar::details::bulk_allocator::BulkAllocatorBase< T >::Preallocate().

420  : ChunkSize(NewChunkSize), MemoryPool()
421  {
422  Preallocate(bPreallocate ? ChunkSize : 0);
423  if (bDebug) {
424  std::cout << "BulkAllocatorBase[" << ((void*)this) << "] created for type "
425  << demangle<value_type>() << " with chunk size " << GetChunkSize() << " x"
426  << sizeof(value_type) << " byte => " << (GetChunkSize() * sizeof(value_type))
427  << " bytes/chunk" << std::endl;
428  } // if debug
429  } // 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

Destructor: releases all the memory pool (.

See also
Free())

Definition at line 275 of file BulkAllocator.h.

References Get, and n.

275 { 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 287 of file BulkAllocator.h.

References lar::details::bulk_allocator::ReferenceCounter::AddUser().

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::AddUser().

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 451 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::AddUser(), lar::details::bulk_allocator::BulkAllocatorBase< T >::ChunkSize, lar::details::bulk_allocator::BulkAllocatorBase< T >::Preallocate(), and lar::details::bulk_allocator::BulkAllocatorBase< T >::SetChunkSize().

452  {
453  AddUser();
454  SetChunkSize(NewChunkSize);
455  Preallocate(bPreallocate ? ChunkSize : 0);
456  } // 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 466 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool, and n.

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::Free().

467  {
468  size_type n = 0;
469  for (const auto& chunk : MemoryPool)
470  n += chunk.size();
471  return n;
472  } // AllocatedCount()
Char_t n[5]
MemoryPool_t MemoryPool
list of all memory chunks; first is free
Counter_t lar::details::bulk_allocator::ReferenceCounter::Count ( ) const
inlineinherited

Returns the number of registered users.

Definition at line 235 of file BulkAllocator.h.

References util::counter().

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 432 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::AllocatedCount(), lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool, and lar::details::bulk_allocator::BulkAllocatorBase< T >::NChunks().

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::RemoveUser().

433  {
434  if (bDebug) {
435  std::cout << "BulkAllocatorBase[" << ((void*)this) << "] freeing " << NChunks()
436  << " memory chunks with " << AllocatedCount() << " elements" << std::endl;
437  } // if debug
438  MemoryPool.clear();
439  } // 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
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 511 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::allocator, lar::details::bulk_allocator::BulkAllocatorBase< T >::ChunkSize, lar::details::bulk_allocator::BulkAllocatorBase< T >::GetCounts(), and lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool.

512  {
513  if (n == 0) return nullptr;
514  // get the free pointer from the latest chunk (#0)
515  pointer ptr = MemoryPool.front().get(n);
516  if (ptr) return ptr;
517  // no free element left in that chunk:
518  // - create a new one in the first position of the pool (move the rest)
519  // - return the pointer from the new pool
520  // (if it fails, it fails... but how could that happen?)
521  if (bDebug) {
522  std::array<size_type, 2> stats = GetCounts();
523  std::cout << "BulkAllocatorBase[" << ((void*)this) << "] allocating "
524  << std::max(ChunkSize, n) << " more elements (on top of the current "
525  << (stats[0] + stats[1]) << " elements, " << stats[1] << " unused)"
526  << std::endl;
527  } // if debug
528  return MemoryPool.emplace(MemoryPool.begin(), allocator, std::max(ChunkSize, n))->get(n);
529  } // 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
Allocator_t allocator
the actual allocator we use
Char_t n[5]
MemoryPool_t MemoryPool
list of all memory chunks; first is free
template<typename T>
size_type lar::details::bulk_allocator::BulkAllocatorBase< T >::GetChunkSize ( ) const
inline
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 484 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool.

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::Get().

486  {
487  // BUG the double brace syntax is required to work around clang bug 21629
488  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
489  std::array<BulkAllocatorBase<T>::size_type, 2> stats = {{0U, 0U}};
490  for (const auto& chunk : MemoryPool) {
491  stats[0] += chunk.used();
492  stats[1] += chunk.available();
493  } // for
494  return stats;
495  } // BulkAllocatorBase<T>::GetCounts()
MemoryPool_t MemoryPool
list of all memory chunks; first is free
bool lar::details::bulk_allocator::ReferenceCounter::hasUsers ( ) const
inlineinherited

Returns whether there are currently users.

Definition at line 232 of file BulkAllocator.h.

References util::counter().

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::RemoveUser().

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 305 of file BulkAllocator.h.

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::Free().

305 { 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 ( size_type  n)
private

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

Definition at line 459 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::allocator, lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool, and n.

460  {
461  if (MemoryPool.empty() || (MemoryPool.front().available() < n))
462  MemoryPool.emplace_back(allocator, n);
463  } // BulkAllocatorBase<T>::RemoveUser()
Allocator_t allocator
the actual allocator we use
Char_t n[5]
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 284 of file BulkAllocator.h.

284 {}
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 442 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::Free(), lar::details::bulk_allocator::ReferenceCounter::hasUsers(), and lar::details::bulk_allocator::ReferenceCounter::RemoveUser().

443  {
445  if (hasUsers()) return true;
446  Free();
447  return false;
448  } // 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 498 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::ChunkSize, and lar::details::bulk_allocator::BulkAllocatorBase< T >::GetChunkSize().

Referenced by lar::details::bulk_allocator::BulkAllocatorBase< T >::AddUser().

499  {
500  if ((GetChunkSize() == NewChunkSize) && !force) return;
501  if (bDebug) {
502  std::cout << "BulkAllocatorBase[" << ((void*)this) << "]"
503  << " changing chunk size: " << GetChunkSize() << " => " << NewChunkSize << ": x"
504  << sizeof(value_type) << " byte => " << (NewChunkSize * sizeof(value_type))
505  << " bytes/chunk" << std::endl;
506  }
507  ChunkSize = NewChunkSize;
508  } // BulkAllocatorBase<T>::SetChunkSize()
size_type ChunkSize
size of the chunks to add
size_type GetChunkSize() const
Returns the current chunk size.
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 475 of file BulkAllocator.h.

References lar::details::bulk_allocator::BulkAllocatorBase< T >::MemoryPool, and n.

476  {
477  size_type n = 0;
478  for (const auto& chunk : MemoryPool)
479  n += chunk.used();
480  return n;
481  } // BulkAllocatorBase<T>::UsedCount()
Char_t n[5]
MemoryPool_t MemoryPool
list of all memory chunks; first is free

Member Data Documentation

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

Default chunk size (default: 10000)

Definition at line 387 of file BulkAllocator.h.


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