 |
Belle II Software
release-05-02-19
|
35 template <
class T,
int chunkSize = 128 >
class MemoryPool {
65 T*
at(
size_t i) {
return (i >=
m_entries) ? 0 : (*this)[i]; }
79 m_chunks.push_back(
reinterpret_cast<T*
>(malloc(chunkSize *
sizeof(T))));
90 size_t needed_chunks = n / chunkSize + 1;
92 while (
m_chunks.size() < needed_chunks) {
93 m_chunks.push_back(
reinterpret_cast<T*
>(malloc(chunkSize *
sizeof(T))));
103 free(
reinterpret_cast<void*
>(ptr));
std::vector< T * > m_chunks
Pointers to all allocated memory chunks.
void clear()
Clear number of entries, does not free memory or call destructors.
size_t size() const
Return number of elements currently stored.
~MemoryPool()
Free allocated memory.
size_t m_entries
Number of occupied entries.
MemoryPool(int n=0)
Constructor.
Abstract base class for different kinds of events.
T * add()
Returns an pointer to the next free memory segment, allocating additional memory if necessary.
T * at(size_t i)
Return pointer to memory segment for element i, including range check.
void release_memory()
Release all allocated memory, called automatically upon destruction.
void reserve(size_t n)
Make sure there is enough memory allocated for n elements.
T * operator[](size_t i)
Return pointer to memory segment for element i, no range check.