20 #if !defined(__MITSUBA_CORE_MEMPOOL_H_)
21 #define __MITSUBA_CORE_MEMPOOL_H_
23 #include <mitsuba/mitsuba.h>
28 #define MTS_MEMPOOL_GRANULARITY 128
31 #define MTS_DEBUG_MEMPOOL 0
46 increaseCapacity(nEntries);
51 for (
size_t i=0; i<m_cleanup.size(); ++i)
57 if (EXPECT_NOT_TAKEN(m_free.empty()))
59 T *result = m_free.back();
65 #if MTS_DEBUG_MEMPOOL == 1
66 if (std::find(m_free.begin(), m_free.end(), ptr) != m_free.end())
67 SLog(
EError,
"BasicMemoryPool:assertNotContained(): Memory pool inconsistency!");
73 #if MTS_DEBUG_MEMPOOL == 1
74 if (std::find(m_free.begin(), m_free.end(), ptr) != m_free.end())
75 SLog(
EError,
"BasicMemoryPool::release(): Memory pool "
76 "inconsistency. Tried to release %s", ptr->toString().c_str());
78 m_free.push_back(ptr);
82 inline size_t size()
const {
88 return m_free.size() == m_size;
93 std::ostringstream oss;
94 oss <<
"BasicMemoryPool[size=" << m_size <<
", free=" << m_free.size() <<
"]";
99 T *ptr =
static_cast<T *
>(
allocAligned(
sizeof(T) * nEntries));
100 for (
size_t i=0; i<nEntries; ++i)
101 m_free.push_back(&ptr[i]);
102 m_cleanup.push_back(ptr);
106 std::vector<T *> m_free;
107 std::vector<T *> m_cleanup;
void assertNotContained(T *ptr)
Definition: mempool.h:64
T * alloc()
Acquire an entry.
Definition: mempool.h:56
#define MTS_MEMPOOL_GRANULARITY
Create a new memory pool with an initial set of 128 entries.
Definition: mempool.h:28
#define SLog(level, fmt,...)
Write a Log message to the console (static version - to be used outside of classes that derive from O...
Definition: logger.h:49
Basic memory pool for efficient allocation and deallocation of objects of the same type...
Definition: mempool.h:42
BasicMemoryPool(size_t nEntries=128)
Create a new memory pool with an initial set of 128 entries.
Definition: mempool.h:45
void release(T *ptr)
Release an entry.
Definition: mempool.h:72
MTS_EXPORT_CORE void *__restrict allocAligned(size_t size)
Allocate an aligned region of memory.
size_t size() const
Return the total size of the memory pool.
Definition: mempool.h:82
std::string toString() const
Return a human-readable description.
Definition: mempool.h:92
Error message, causes an exception to be thrown.
Definition: formatter.h:33
~BasicMemoryPool()
Destruct the memory pool and release all entries.
Definition: mempool.h:50
MTS_EXPORT_CORE void freeAligned(void *ptr)
Free an aligned region of memory.
bool unused() const
Check if every entry has been released.
Definition: mempool.h:87