All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Attributes | List of all members
lar::BulkAllocator< T > Class Template Reference

Aggressive allocator reserving a lot of memory in advance. More...

#include <BulkAllocator.h>

Inheritance diagram for lar::BulkAllocator< T >:

Classes

struct  rebind
 

Public Types

using BaseAllocator_t = std::allocator< T >
 
typedef BaseAllocator_t::size_type size_type
 
typedef
BaseAllocator_t::difference_type 
difference_type
 
typedef BaseAllocator_t::value_type value_type
 
typedef BaseAllocator_t::pointer pointer
 
typedef
BaseAllocator_t::const_pointer 
const_pointer
 
typedef BaseAllocator_t::reference reference
 
typedef
BaseAllocator_t::const_reference 
const_reference
 

Public Member Functions

 BulkAllocator () noexcept
 Default constructor: uses the default chunk size. More...
 
 BulkAllocator (size_type ChunkSize, bool bPreallocate=false) noexcept
 Constructor: sets chunk size and optionally allocates the first chunk. More...
 
 BulkAllocator (const BulkAllocator &a) noexcept=default
 Copy constructor: default. More...
 
 BulkAllocator (BulkAllocator &&a) noexcept=default
 Move constructor: default. More...
 
template<class U >
 BulkAllocator (const BulkAllocator< U > &a) noexcept
 General copy constructor; currently, it does not preallocate. More...
 
BulkAllocatoroperator= (const BulkAllocator &a)=default
 Copy assignment: default. More...
 
BulkAllocatoroperator= (BulkAllocator &&a)=default
 Move assignment: default. More...
 
 ~BulkAllocator ()
 Destructor; memory is freed only if no allocators are left around. More...
 
pointer allocate (size_type n, const void *=0)
 Allocates memory for n elements. More...
 
void deallocate (pointer p, size_type n)
 Frees n elements at p. More...
 

Static Public Member Functions

static void Free ()
 Releases all the allocated memory: dangerous! More...
 
static size_type GetChunkSize ()
 Returns the chunk size of the underlying global allocator. More...
 
static void SetChunkSize (size_type ChunkSize)
 Sets chunk size of global allocator; only affects future allocations! More...
 

Private Types

typedef
details::bulk_allocator::BulkAllocatorBase
< T > 
SharedAllocator_t
 shared allocator type More...
 

Private Member Functions

void CreateGlobalAllocator (size_type ChunkSize, bool bPreallocate=false)
 Makes sure we have a valid "global allocator". More...
 

Static Private Attributes

static SharedAllocator_t GlobalAllocator
 The allocator shared by all instances of this object. More...
 

Additional Inherited Members

- Public Attributes inherited from std::allocator< T >
elements
 STL member. More...
 

Detailed Description

template<typename T>
class lar::BulkAllocator< T >

Aggressive allocator reserving a lot of memory in advance.

Parameters
Ttype being allocated

This allocator appropriates memory in large chunks of GetChunkSize() elements of type T. The memory will never be deleted! (but read further)

Note
With C++17, an allocator called std::pmr::monotonic_buffer_resource is available that seems to have pretty much the same functionality as this one (but STL quality). When C++17 is adopted by LArSoft (and the supported compilers have a complete enough support for it), the users of BulkAllocator should migrate to that one. Note that the interface is different, and probably the way to use it is also different.

Deletion policy

This allocator does not release not reuse deallocated memory. This design choice is meant to reflect a specific use case where a large amount of elements is created and then used, and the created object is fairly static. Tracking freed memory fragments takes time and more memory, and reusing them too. Nevertheless, the allocator has a user count; when no user is present, all the memory is deallocated. This can be convenient, or disastrous: remember that the elements of a container can (or just might) not survive after the container is destroyed. Clearing the container will not trigger this self-distruction; if you are completely sure that no other container is currently using the same allocator, you can explicitly Free() its memory.

One allocator for them all

Since STL containers do not necessarely store their allocator but they may create it with a default constructor, allocators should be formally stateless, and every newly-created allocator should be equivalent (or else a copy of an allocator will not know what the original has allocated already).

This is implemented hiding a singleton in the allocator (as a static member). Each allocator type has its own singleton, i.e., a BulkAllocator<int> does not share memory with a BulkAllocator<double>, but all BulkAllocator<int> share.

Definition at line 92 of file BulkAllocator.h.

Member Typedef Documentation

template<typename T>
using lar::BulkAllocator< T >::BaseAllocator_t = std::allocator<T>

Definition at line 94 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::const_pointer lar::BulkAllocator< T >::const_pointer

Definition at line 103 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::const_reference lar::BulkAllocator< T >::const_reference

Definition at line 106 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::difference_type lar::BulkAllocator< T >::difference_type

Definition at line 98 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::pointer lar::BulkAllocator< T >::pointer

Definition at line 102 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::reference lar::BulkAllocator< T >::reference

Definition at line 105 of file BulkAllocator.h.

shared allocator type

Definition at line 158 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::size_type lar::BulkAllocator< T >::size_type

Definition at line 97 of file BulkAllocator.h.

template<typename T>
typedef BaseAllocator_t::value_type lar::BulkAllocator< T >::value_type

Definition at line 100 of file BulkAllocator.h.

Constructor & Destructor Documentation

template<typename T>
lar::BulkAllocator< T >::BulkAllocator ( )
inlinenoexcept

Default constructor: uses the default chunk size.

Definition at line 114 of file BulkAllocator.h.

114 : BulkAllocator(GetChunkSize(), false) {}
BulkAllocator() noexcept
Default constructor: uses the default chunk size.
static size_type GetChunkSize()
Returns the chunk size of the underlying global allocator.
template<typename T>
lar::BulkAllocator< T >::BulkAllocator ( size_type  ChunkSize,
bool  bPreallocate = false 
)
inlinenoexcept

Constructor: sets chunk size and optionally allocates the first chunk.

Definition at line 117 of file BulkAllocator.h.

118  { CreateGlobalAllocator(ChunkSize, bPreallocate); }
void CreateGlobalAllocator(size_type ChunkSize, bool bPreallocate=false)
Makes sure we have a valid &quot;global allocator&quot;.
template<typename T>
lar::BulkAllocator< T >::BulkAllocator ( const BulkAllocator< T > &  a)
defaultnoexcept

Copy constructor: default.

template<typename T>
lar::BulkAllocator< T >::BulkAllocator ( BulkAllocator< T > &&  a)
defaultnoexcept

Move constructor: default.

template<typename T>
template<class U >
lar::BulkAllocator< T >::BulkAllocator ( const BulkAllocator< U > &  a)
inlinenoexcept

General copy constructor; currently, it does not preallocate.

Definition at line 128 of file BulkAllocator.h.

128  :
129  BaseAllocator_t(a) { SetChunkSize(a.GetChunkSize()); }
process_name gaushit a
static void SetChunkSize(size_type ChunkSize)
Sets chunk size of global allocator; only affects future allocations!
std::allocator< T > BaseAllocator_t
Definition: BulkAllocator.h:94
template<typename T>
lar::BulkAllocator< T >::~BulkAllocator ( )
inline

Destructor; memory is freed only if no allocators are left around.

Definition at line 138 of file BulkAllocator.h.

static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
bool RemoveUser()
Removed a user to the users count; if no user is left, free the pool.

Member Function Documentation

template<typename T >
BulkAllocator< T >::pointer lar::BulkAllocator< T >::allocate ( size_type  n,
const void *  = 0 
)
inline

Allocates memory for n elements.

Definition at line 562 of file BulkAllocator.h.

563  { return GlobalAllocator.Get(n); }
pointer Get(size_type n)
Returns a pointer to memory for n new values of type T.
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
template<typename T >
void lar::BulkAllocator< T >::CreateGlobalAllocator ( size_type  ChunkSize,
bool  bPreallocate = false 
)
private

Makes sure we have a valid "global allocator".

Definition at line 555 of file BulkAllocator.h.

556  {
557  GlobalAllocator.AddUser(ChunkSize, bPreallocate);
558  } // BulkAllocator<T>::CreateGlobalAllocator()
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
void AddUser()
Add a new pool user with the current parameters.
template<typename T >
void lar::BulkAllocator< T >::deallocate ( pointer  p,
size_type  n 
)
inline

Frees n elements at p.

Definition at line 566 of file BulkAllocator.h.

566  {
567  return GlobalAllocator.Release(p);
568  } // BulkAllocator<T>::deallocate()
void Release(pointer)
Releases memory pointed by the specified pointer (but it does not).
pdgs p
Definition: selectors.fcl:22
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
template<typename T>
static void lar::BulkAllocator< T >::Free ( )
inlinestatic

Releases all the allocated memory: dangerous!

Definition at line 147 of file BulkAllocator.h.

147 { GlobalAllocator.Free(); }
void Free()
Releases the pool of memory; all pointer to it become invalid.
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
template<typename T>
static size_type lar::BulkAllocator< T >::GetChunkSize ( )
inlinestatic

Returns the chunk size of the underlying global allocator.

Definition at line 150 of file BulkAllocator.h.

150 { return GlobalAllocator.GetChunkSize(); }
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
size_type GetChunkSize() const
Returns the current chunk size.
template<typename T>
BulkAllocator& lar::BulkAllocator< T >::operator= ( const BulkAllocator< T > &  a)
default

Copy assignment: default.

template<typename T>
BulkAllocator& lar::BulkAllocator< T >::operator= ( BulkAllocator< T > &&  a)
default

Move assignment: default.

template<typename T>
static void lar::BulkAllocator< T >::SetChunkSize ( size_type  ChunkSize)
inlinestatic

Sets chunk size of global allocator; only affects future allocations!

Definition at line 153 of file BulkAllocator.h.

154  { GlobalAllocator.SetChunkSize(ChunkSize); }
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
void SetChunkSize(size_type NewChunkSize, bool force=false)
Sets the chunk size for the future allocations.

Member Data Documentation

template<typename T>
BulkAllocator< T >::SharedAllocator_t lar::BulkAllocator< T >::GlobalAllocator
staticprivate

The allocator shared by all instances of this object.

Definition at line 164 of file BulkAllocator.h.


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