Belle II Software  release-05-02-19
MemoryPool.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <vector>
14 #include <cstddef>
15 #include <cstdlib>
16 
17 namespace Belle2 {
35  template < class T, int chunkSize = 128 > class MemoryPool {
36 
37  public:
42  explicit MemoryPool(int n = 0): m_entries(0) { reserve(n); }
43 
46 
51  size_t size() const { return m_entries; }
52 
58  T* operator[](size_t i) { return m_chunks[i / chunkSize] + i % chunkSize; }
59 
65  T* at(size_t i) { return (i >= m_entries) ? 0 : (*this)[i]; }
66 
70  void clear() { m_entries = 0; }
71 
76  T* add()
77  {
78  if (m_entries / chunkSize >= m_chunks.size()) {
79  m_chunks.push_back(reinterpret_cast<T*>(malloc(chunkSize * sizeof(T))));
80  }
81  return (*this)[m_entries++];
82  }
83 
88  void reserve(size_t n)
89  {
90  size_t needed_chunks = n / chunkSize + 1;
91  m_chunks.reserve(needed_chunks);
92  while (m_chunks.size() < needed_chunks) {
93  m_chunks.push_back(reinterpret_cast<T*>(malloc(chunkSize * sizeof(T))));
94  }
95  }
96 
100  void release_memory()
101  {
102  for (T* ptr : m_chunks) {
103  free(reinterpret_cast<void*>(ptr));
104  }
105  m_chunks.clear();
106  }
107 
108  protected:
109 
110  size_t m_entries;
111  std::vector<T*> m_chunks;
112  };
113 
115 } //end namespace Belle2
Belle2::MemoryPool::m_chunks
std::vector< T * > m_chunks
Pointers to all allocated memory chunks.
Definition: MemoryPool.h:119
Belle2::MemoryPool::clear
void clear()
Clear number of entries, does not free memory or call destructors.
Definition: MemoryPool.h:78
Belle2::MemoryPool::size
size_t size() const
Return number of elements currently stored.
Definition: MemoryPool.h:59
Belle2::MemoryPool::~MemoryPool
~MemoryPool()
Free allocated memory.
Definition: MemoryPool.h:53
Belle2::MemoryPool::m_entries
size_t m_entries
Number of occupied entries.
Definition: MemoryPool.h:118
Belle2::MemoryPool::MemoryPool
MemoryPool(int n=0)
Constructor.
Definition: MemoryPool.h:50
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MemoryPool::add
T * add()
Returns an pointer to the next free memory segment, allocating additional memory if necessary.
Definition: MemoryPool.h:84
Belle2::MemoryPool::at
T * at(size_t i)
Return pointer to memory segment for element i, including range check.
Definition: MemoryPool.h:73
Belle2::MemoryPool::release_memory
void release_memory()
Release all allocated memory, called automatically upon destruction.
Definition: MemoryPool.h:108
Belle2::MemoryPool::reserve
void reserve(size_t n)
Make sure there is enough memory allocated for n elements.
Definition: MemoryPool.h:96
Belle2::MemoryPool::operator[]
T * operator[](size_t i)
Return pointer to memory segment for element i, no range check.
Definition: MemoryPool.h:66