Belle II Software light-2406-ragdoll
MemoryPool< T, chunkSize > Class Template Reference

Class to provide a constant access time memory pool for one kind of objects. More...

#include <MemoryPool.h>

Collaboration diagram for MemoryPool< T, chunkSize >:

Public Member Functions

 MemoryPool (int n=0)
 Constructor.
 
 ~MemoryPool ()
 Free allocated memory.
 
size_t size () const
 Return number of elements currently stored.
 
T * operator[] (size_t i)
 Return pointer to memory segment for element i, no range check.
 
T * at (size_t i)
 Return pointer to memory segment for element i, including range check.
 
void clear ()
 Clear number of entries, does not free memory or call destructors.
 
T * add ()
 Returns an pointer to the next free memory segment, allocating additional memory if necessary.
 
void reserve (size_t n)
 Make sure there is enough memory allocated for n elements.
 
void release_memory ()
 Release all allocated memory, called automatically upon destruction.
 

Protected Attributes

size_t m_entries
 Number of occupied entries.
 
std::vector< T * > m_chunks
 Pointers to all allocated memory chunks.
 

Detailed Description

template<class T, int chunkSize = 128>
class Belle2::MemoryPool< T, chunkSize >

Class to provide a constant access time memory pool for one kind of objects.

It is similar to TClonesArray but uses Templates and is also suited for non TObject classes. The Memory will be allocated in chunks, each large enough to hold chunkSize objects of type T.

This class will not initialize the memory, it is the responsibility of the user to call new(pool.add()) T(...) when adding an element to initialize the memory. This is required to make the Pool usable for classes without default constructor

Definition at line 33 of file MemoryPool.h.

Constructor & Destructor Documentation

◆ MemoryPool()

MemoryPool ( int  n = 0)
inlineexplicit

Constructor.

Parameters
nReserve space for n elements on construction

Definition at line 40 of file MemoryPool.h.

40: m_entries(0) { reserve(n); }
void reserve(size_t n)
Make sure there is enough memory allocated for n elements.
Definition: MemoryPool.h:86
size_t m_entries
Number of occupied entries.
Definition: MemoryPool.h:108

◆ ~MemoryPool()

~MemoryPool ( )
inline

Free allocated memory.

Definition at line 43 of file MemoryPool.h.

43{ release_memory(); }
void release_memory()
Release all allocated memory, called automatically upon destruction.
Definition: MemoryPool.h:98

Member Function Documentation

◆ add()

T * add ( )
inline

Returns an pointer to the next free memory segment, allocating additional memory if necessary.

Returns
Pointer to the next free memory segment.

Definition at line 74 of file MemoryPool.h.

75 {
76 if (m_entries / chunkSize >= m_chunks.size()) {
77 m_chunks.push_back(reinterpret_cast<T*>(malloc(chunkSize * sizeof(T))));
78 }
79 return (*this)[m_entries++];
80 }
std::vector< T * > m_chunks
Pointers to all allocated memory chunks.
Definition: MemoryPool.h:109

◆ at()

T * at ( size_t  i)
inline

Return pointer to memory segment for element i, including range check.

Parameters
iThe index of the memory segment.
Returns
Pointer to the memory segment.

Definition at line 63 of file MemoryPool.h.

63{ return (i >= m_entries) ? 0 : (*this)[i]; }

◆ clear()

void clear ( )
inline

Clear number of entries, does not free memory or call destructors.

Definition at line 68 of file MemoryPool.h.

68{ m_entries = 0; }

◆ operator[]()

T * operator[] ( size_t  i)
inline

Return pointer to memory segment for element i, no range check.

Parameters
iThe index of the memory segment.
Returns
Pointer to the memory segment.

Definition at line 56 of file MemoryPool.h.

56{ return m_chunks[i / chunkSize] + i % chunkSize; }

◆ release_memory()

void release_memory ( )
inline

Release all allocated memory, called automatically upon destruction.

Definition at line 98 of file MemoryPool.h.

99 {
100 for (T* ptr : m_chunks) {
101 free(reinterpret_cast<void*>(ptr));
102 }
103 m_chunks.clear();
104 }

◆ reserve()

void reserve ( size_t  n)
inline

Make sure there is enough memory allocated for n elements.

Parameters
nThe number of elements for which memory should be made available.

Definition at line 86 of file MemoryPool.h.

87 {
88 size_t needed_chunks = n / chunkSize + 1;
89 m_chunks.reserve(needed_chunks);
90 while (m_chunks.size() < needed_chunks) {
91 m_chunks.push_back(reinterpret_cast<T*>(malloc(chunkSize * sizeof(T))));
92 }
93 }

◆ size()

size_t size ( ) const
inline

Return number of elements currently stored.

Returns
The number of elements currently stored

Definition at line 49 of file MemoryPool.h.

49{ return m_entries; }

Member Data Documentation

◆ m_chunks

std::vector<T*> m_chunks
protected

Pointers to all allocated memory chunks.

Definition at line 109 of file MemoryPool.h.

◆ m_entries

size_t m_entries
protected

Number of occupied entries.

Definition at line 108 of file MemoryPool.h.


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