Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mempool.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_MEMPOOL_H_)
21 #define __MITSUBA_CORE_MEMPOOL_H_
22 
23 #include <mitsuba/mitsuba.h>
24 
26 
27 /// Create a new memory pool with an initial set of 128 entries
28 #define MTS_MEMPOOL_GRANULARITY 128
29 
30 /// Set this to one to track down memory pool-related issues
31 #define MTS_DEBUG_MEMPOOL 0
32 
33 /**
34  * \brief Basic memory pool for efficient allocation and deallocation
35  * of objects of the same type.
36  *
37  * This class attempts to keep most instances contiguous in memory, while
38  * having only minimal interaction with the underlying allocator.
39  *
40  * \ingroup libcore
41  */
42 template <typename T> class BasicMemoryPool {
43 public:
44  /// Create a new memory pool with an initial set of 128 entries
45  BasicMemoryPool(size_t nEntries = MTS_MEMPOOL_GRANULARITY) : m_size(0) {
46  increaseCapacity(nEntries);
47  }
48 
49  /// Destruct the memory pool and release all entries
51  for (size_t i=0; i<m_cleanup.size(); ++i)
52  freeAligned(m_cleanup[i]);
53  }
54 
55  /// Acquire an entry
56  inline T *alloc() {
57  if (EXPECT_NOT_TAKEN(m_free.empty()))
58  increaseCapacity();
59  T *result = m_free.back();
60  m_free.pop_back();
61  return result;
62  }
63 
64  void assertNotContained(T *ptr) {
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!");
68 #endif
69  }
70 
71  /// Release an entry
72  inline void release(T *ptr) {
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());
77 #endif
78  m_free.push_back(ptr);
79  }
80 
81  /// Return the total size of the memory pool
82  inline size_t size() const {
83  return m_size;
84  }
85 
86  /// Check if every entry has been released
87  bool unused() const {
88  return m_free.size() == m_size;
89  }
90 
91  /// Return a human-readable description
92  std::string toString() const {
93  std::ostringstream oss;
94  oss << "BasicMemoryPool[size=" << m_size << ", free=" << m_free.size() << "]";
95  return oss.str();
96  }
97 private:
98  void increaseCapacity(size_t nEntries = MTS_MEMPOOL_GRANULARITY) {
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);
103  m_size += nEntries;
104  }
105 private:
106  std::vector<T *> m_free;
107  std::vector<T *> m_cleanup;
108  size_t m_size;
109 };
110 
112 
113 #endif /* __MITSUBA_CORE_MEMPOOL_H_ */
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
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
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.
#define MTS_NAMESPACE_END
Definition: platform.h:138
bool unused() const
Check if every entry has been released.
Definition: mempool.h:87