LArSoft
v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
|
Aggressive allocator reserving a lot of memory in advance. More...
#include "BulkAllocator.h"
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... | |
BulkAllocator & | operator= (const BulkAllocator &a)=default |
Copy assignment: default. More... | |
BulkAllocator & | operator= (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... | |
Public Attributes | |
T | elements |
STL member. 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... | |
Aggressive allocator reserving a lot of memory in advance.
T | type being allocated |
This allocator appropriates memory in large chunks of GetChunkSize() elements of type T. The memory will never be deleted! (but read further)
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.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.
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 89 of file BulkAllocator.h.
using lar::BulkAllocator< T >::BaseAllocator_t = std::allocator<T> |
Definition at line 91 of file BulkAllocator.h.
typedef BaseAllocator_t::const_pointer lar::BulkAllocator< T >::const_pointer |
Definition at line 100 of file BulkAllocator.h.
typedef BaseAllocator_t::const_reference lar::BulkAllocator< T >::const_reference |
Definition at line 103 of file BulkAllocator.h.
typedef BaseAllocator_t::difference_type lar::BulkAllocator< T >::difference_type |
Definition at line 95 of file BulkAllocator.h.
typedef BaseAllocator_t::pointer lar::BulkAllocator< T >::pointer |
Definition at line 99 of file BulkAllocator.h.
typedef BaseAllocator_t::reference lar::BulkAllocator< T >::reference |
Definition at line 102 of file BulkAllocator.h.
|
private |
shared allocator type
Definition at line 158 of file BulkAllocator.h.
typedef BaseAllocator_t::size_type lar::BulkAllocator< T >::size_type |
Definition at line 94 of file BulkAllocator.h.
typedef BaseAllocator_t::value_type lar::BulkAllocator< T >::value_type |
Definition at line 97 of file BulkAllocator.h.
|
inlinenoexcept |
Default constructor: uses the default chunk size.
Definition at line 111 of file BulkAllocator.h.
|
inlinenoexcept |
Constructor: sets chunk size and optionally allocates the first chunk.
Definition at line 114 of file BulkAllocator.h.
|
defaultnoexcept |
Copy constructor: default.
|
defaultnoexcept |
Move constructor: default.
|
inlinenoexcept |
General copy constructor; currently, it does not preallocate.
Definition at line 127 of file BulkAllocator.h.
References lar::BulkAllocator< T >::GetChunkSize().
|
inline |
Destructor; memory is freed only if no allocators are left around.
Definition at line 139 of file BulkAllocator.h.
References n.
|
inline |
Allocates memory for n elements.
Definition at line 544 of file BulkAllocator.h.
|
private |
Makes sure we have a valid "global allocator".
Definition at line 538 of file BulkAllocator.h.
|
inline |
Frees n elements at p.
Definition at line 551 of file BulkAllocator.h.
|
inlinestatic |
Releases all the allocated memory: dangerous!
Definition at line 148 of file BulkAllocator.h.
|
inlinestatic |
Returns the chunk size of the underlying global allocator.
Definition at line 151 of file BulkAllocator.h.
Referenced by lar::BulkAllocator< T >::BulkAllocator().
|
default |
Copy assignment: default.
|
default |
Move assignment: default.
|
inlinestatic |
Sets chunk size of global allocator; only affects future allocations!
Definition at line 154 of file BulkAllocator.h.
|
inherited |
STL member.
|
staticprivate |
The allocator shared by all instances of this object.
Definition at line 164 of file BulkAllocator.h.