33 template <
class T,
int chunkSize = 128 >
class MemoryPool {
77 m_chunks.push_back(
reinterpret_cast<T*
>(malloc(chunkSize *
sizeof(T))));
88 size_t needed_chunks = n / chunkSize + 1;
90 while (
m_chunks.size() < needed_chunks) {
91 m_chunks.push_back(
reinterpret_cast<T*
>(malloc(chunkSize *
sizeof(T))));
101 free(
reinterpret_cast<void*
>(ptr));
Class to provide a constant access time memory pool for one kind of objects.
~MemoryPool()
Free allocated memory.
MemoryPool(int n=0)
Constructor.
size_t size() const
Return number of elements currently stored.
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.
T * add()
Returns an pointer to the next free memory segment, allocating additional memory if necessary.
void clear()
Clear number of entries, does not free memory or call destructors.
std::vector< T * > m_chunks
Pointers to all allocated memory chunks.
T * operator[](size_t i)
Return pointer to memory segment for element i, no range check.
void reserve(size_t n)
Make sure there is enough memory allocated for n elements.
size_t m_entries
Number of occupied entries.
Abstract base class for different kinds of events.