13 #error ("BulkAllocator.h is currently broken; see issue #19494.")
17 #ifndef BULKALLOCATOR_H
18 #define BULKALLOCATOR_H
29 namespace bulk_allocator {
42 virtual const char*
what()
const throw()
override {
return msg; }
45 const char*
msg =
nullptr;
97 typedef typename BaseAllocator_t::size_type
size_type;
102 typedef typename BaseAllocator_t::pointer
pointer;
204 template <
typename T>
206 std::string
name =
typeid(T).
name();
209 std::unique_ptr<char, void(*)(void*)> res
210 { abi::__cxa_demangle(name.c_str(), NULL, NULL, &status), std::free };
211 return (status==0) ? res.get() :
name ;
217 template <
typename T>
218 inline std::string
demangle(
const T&) {
return demangle<T>(); }
221 namespace bulk_allocator {
259 template <
typename T>
305 std::array<size_type, 2>
GetCounts()
const;
392 template <
typename T>
400 template <
typename T>
407 std::swap(free,
from.free);
412 template <
typename T>
416 template <
typename T>
423 std::cout <<
"BulkAllocatorBase[" << ((
void*)
this)
424 <<
"] created for type " << demangle<value_type>()
433 template <
typename T>
436 std::cout <<
"BulkAllocatorBase[" << ((
void*)
this) <<
"] freeing "
438 <<
" elements" << std::endl;
444 template <
typename T>
453 template <
typename T>
463 template <
typename T>
470 template <
typename T>
475 for (
const auto& chunk:
MemoryPool) n += chunk.size();
480 template <
typename T>
485 for (
const auto& chunk:
MemoryPool) n += chunk.used();
490 template <
typename T>
491 std::array<typename BulkAllocatorBase<T>::size_type, 2>
496 std::array<BulkAllocatorBase<T>::size_type, 2> stats = {{ 0U, 0U }};
498 stats[0] += chunk.used();
499 stats[1] += chunk.available();
505 template <
typename T>
511 std::cout <<
"BulkAllocatorBase[" << ((
void*)
this) <<
"]"
513 << NewChunkSize <<
": x" <<
sizeof(
value_type) <<
" byte => "
514 << (NewChunkSize*
sizeof(
value_type)) <<
" bytes/chunk"
521 template <
typename T>
525 if (n == 0)
return nullptr;
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;
549 template <
typename T>
553 template <
typename T>
557 GlobalAllocator.AddUser(ChunkSize, bPreallocate);
560 template <
typename T>
563 {
return GlobalAllocator.Get(n); }
565 template <
typename T>
567 return GlobalAllocator.Release(p);
572 #endif // BULKALLOCATOR_H
A class managing a memory pool.
BaseAllocator_t::pointer pointer
MemoryChunk_t & operator=(const MemoryChunk_t &)=delete
Can't assign.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
void Release(pointer)
Releases memory pointed by the specified pointer (but it does not).
Internal memory chunk; like a std::vector, but does not construct.
BaseAllocator_t::difference_type difference_type
BulkAllocatorBase(size_type NewChunkSize=DefaultChunkSize, bool bPreallocate=false)
Constructor; preallocates memory if explicitly requested.
bool RemoveUser()
Removed a user to the users count; returns false if no user yet.
void deallocate(pointer p, size_type n)
Frees n elements at p.
void CreateGlobalAllocator(size_type ChunkSize, bool bPreallocate=false)
Makes sure we have a valid "global allocator".
Counter_t Count() const
Returns the number of registered users.
pointer Get(size_type n)
Returns a pointer to memory for n new values of type T.
std::array< size_type, 2 > GetCounts() const
Returns an array equivalent to { UsedCount(), FreeCount() }.
virtual const char * what() const override
void AddUser()
Adds a user to the users count.
Allocator_t::difference_type difference_type
void Free()
Releases the pool of memory; all pointer to it become invalid.
MemoryChunk_t(Allocator_t &alloc, size_type n)
memory_error(const char *message)
static void Free()
Releases all the allocated memory: dangerous!
static SharedAllocator_t GlobalAllocator
The allocator shared by all instances of this object.
BaseAllocator_t::reference reference
~BulkAllocator()
Destructor; memory is freed only if no allocators are left around.
bool full() const
Returns whether the chunk is full.
Exception thrown when BulkAllocator-specific allocation errors happen.
size_type ChunkSize
size of the chunks to add
BulkAllocator() noexcept
Default constructor: uses the default chunk size.
size_type AllocatedCount() const
Returns the total number of entries in the pool.
BaseAllocator_t::size_type size_type
Allocator_t * allocator
reference to the allocator to be used
A simple reference counter, keep track of a number of users.
BaseAllocator_t::const_reference const_reference
pointer allocate(size_type n, const void *=0)
Allocates memory for n elements.
BulkAllocator(size_type ChunkSize, bool bPreallocate=false) noexcept
Constructor: sets chunk size and optionally allocates the first chunk.
~BulkAllocatorBase()
Destructor: releases all the memory pool (.
size_type size() const
Returns the number of elements in this pool.
BulkAllocator & operator=(const BulkAllocator &a)=default
Copy assignment: default.
auto end(FixedBins< T, C > const &) noexcept
details::bulk_allocator::BulkAllocatorBase< T > SharedAllocator_t
shared allocator type
Allocator_t allocator
the actual allocator we use
Aggressive allocator reserving a lot of memory in advance.
std::string demangle()
Demangles the name of a type.
pointer end
end of the pool
std::allocator< T > Allocator_t
size_type NChunks() const
Returns the number of memory pool chunks allocated.
static size_type DefaultChunkSize
Default chunk size (default: 10000)
size_type available() const
Returns the number of free elements in this pool.
auto begin(FixedBins< T, C > const &) noexcept
size_type UsedCount() const
Returns the total number of used entries in the pool.
MemoryChunk_t()
< Default constructor (does nothing)
size_type FreeCount() const
Returns the total number of unused entries in the pool.
size_type GetChunkSize() const
Returns the current chunk size.
bool hasUsers() const
Returns whether there are currently users.
unsigned int Counter_t
type of user counter
pointer free
first unused element of the pool
static void SetChunkSize(size_type ChunkSize)
Sets chunk size of global allocator; only affects future allocations!
pointer begin
start of the pool
BaseAllocator_t::value_type value_type
BaseAllocator_t::const_pointer const_pointer
void SetChunkSize(size_type NewChunkSize, bool force=false)
Sets the chunk size for the future allocations.
std::vector< MemoryChunk_t > MemoryPool_t
std::allocator< T > BaseAllocator_t
size_type used() const
Returns the number of used elements in this pool.
static size_type GetChunkSize()
Returns the chunk size of the underlying global allocator.
bool RemoveUser()
Removed a user to the users count; if no user is left, free the pool.
MemoryPool_t MemoryPool
list of all memory chunks; first is free
void AddUser()
Add a new pool user with the current parameters.
BEGIN_PROLOG could also be cout