Cuberite
A lightweight, fast and extensible game server for Minecraft
AllocationPool.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 
6 
7 
8 template <class T>
10 {
11 public:
13  {
14  public:
15  virtual ~cStarvationCallbacks() {}
16 
18  virtual void OnStartUsingReserve() = 0;
19 
21  virtual void OnEndUsingReserve() = 0;
22 
25  virtual void OnOutOfReserve() = 0;
26  };
27 
28  virtual ~cAllocationPool() {}
29 
31  virtual T * Allocate() = 0;
32 
34  virtual void Free(T * a_ptr) = 0;
35 
37  bool IsEqual(const cAllocationPool & a_Other) const NOEXCEPT
38  {
39  return ((this == &a_Other) || DoIsEqual(a_Other) || a_Other.DoIsEqual(*this));
40  }
41 
42  friend bool operator == (const cAllocationPool & a_Lhs, const cAllocationPool & a_Rhs)
43  {
44  return a_Lhs.IsEqual(a_Rhs);
45  }
46 
47  friend bool operator != (const cAllocationPool & a_Lhs, const cAllocationPool & a_Rhs)
48  {
49  return !a_Lhs.IsEqual(a_Rhs);
50  }
51 
52 private:
53  virtual bool DoIsEqual(const cAllocationPool & a_Other) const NOEXCEPT = 0;
54 };
55 
56 
57 
58 
59 
62 template <class T>
64  public cAllocationPool<T>
65 {
66 public:
67 
68  cListAllocationPool(std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> a_Callbacks, size_t a_MinElementsInReserve, size_t a_MaxElementsInReserve) :
69  m_MinElementsInReserve(a_MinElementsInReserve),
70  m_MaxElementsInReserve(a_MaxElementsInReserve),
71  m_Callbacks(std::move(a_Callbacks))
72  {
73  for (size_t i = 0; i < m_MinElementsInReserve; i++)
74  {
75  void * space = malloc(sizeof(T));
76  if (space == nullptr)
77  {
78  m_Callbacks->OnStartUsingReserve();
79  break;
80  }
81  m_FreeList.push_front(space);
82  }
83  }
84 
85 
86  virtual ~cListAllocationPool() override
87  {
88  while (!m_FreeList.empty())
89  {
90  free(m_FreeList.front());
91  m_FreeList.pop_front();
92  }
93  }
94 
95 
96  virtual T * Allocate() override
97  {
98  if (m_FreeList.size() <= m_MinElementsInReserve)
99  {
100  void * space = malloc(sizeof(T));
101  if (space != nullptr)
102  {
103  #if defined(_MSC_VER) && defined(_DEBUG)
104  // The debugging-new that is set up using macros in Globals.h doesn't support the placement-new syntax used here.
105  // Temporarily disable the macro
106  #pragma push_macro("new")
107  #undef new
108  #endif
109 
110  return new(space) T;
111 
112  #if defined(_MSC_VER) && defined(_DEBUG)
113  // Re-enable the debugging-new macro
114  #pragma pop_macro("new")
115  #endif
116  }
117  else if (m_FreeList.size() == m_MinElementsInReserve)
118  {
119  m_Callbacks->OnStartUsingReserve();
120  }
121  else if (m_FreeList.empty())
122  {
123  m_Callbacks->OnOutOfReserve();
124  // Try again until the memory is avalable
125  return Allocate();
126  }
127  }
128  // placement new, used to initalize the object
129 
130  #if defined(_MSC_VER) && defined(_DEBUG)
131  // The debugging-new that is set up using macros in Globals.h doesn't support the placement-new syntax used here.
132  // Temporarily disable the macro
133  #pragma push_macro("new")
134  #undef new
135  #endif
136 
137  T * ret = new (m_FreeList.front()) T;
138 
139  #if defined(_MSC_VER) && defined(_DEBUG)
140  // Re-enable the debugging-new macro
141  #pragma pop_macro("new")
142  #endif
143 
144  m_FreeList.pop_front();
145  return ret;
146  }
147 
148 
149  virtual void Free(T * a_ptr) override
150  {
151  if (a_ptr == nullptr)
152  {
153  return;
154  }
155 
156  a_ptr->~T(); // placement destruct.
157 
158  if (m_FreeList.size() >= m_MaxElementsInReserve)
159  {
160  free(a_ptr);
161  return;
162  }
163 
164  m_FreeList.push_front(a_ptr);
165  if (m_FreeList.size() == m_MinElementsInReserve)
166  {
167  m_Callbacks->OnEndUsingReserve();
168  }
169  }
170 
171 private:
176  std::list<void *> m_FreeList;
177  std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> m_Callbacks;
178 
179  virtual bool DoIsEqual(const cAllocationPool<T> & a_Other) const NOEXCEPT override
180  {
181  return (dynamic_cast<const cListAllocationPool<T>*>(&a_Other) != nullptr);
182  }
183 };
184 
185 
186 
187 
Definition: FastNBT.h:131
Allocates memory storing unused elements in a linked list.
virtual void OnStartUsingReserve()=0
Is called when the reserve buffer starts to be used.
size_t m_MaxElementsInReserve
Maximum free list size before returning memory to the OS.
friend bool operator==(const cAllocationPool &a_Lhs, const cAllocationPool &a_Rhs)
virtual T * Allocate() override
Allocates a pointer to T.
size_t m_MinElementsInReserve
The minimum number of elements to keep in the free list before malloc fails.
virtual void Free(T *a_ptr)=0
Frees the pointer passed in a_ptr, invalidating it.
cListAllocationPool(std::unique_ptr< typename cAllocationPool< T >::cStarvationCallbacks > a_Callbacks, size_t a_MinElementsInReserve, size_t a_MaxElementsInReserve)
virtual T * Allocate()=0
Allocates a pointer to T.
virtual void Free(T *a_ptr) override
Frees the pointer passed in a_ptr, invalidating it.
virtual bool DoIsEqual(const cAllocationPool &a_Other) const NOEXCEPT=0
std::unique_ptr< typename cAllocationPool< T >::cStarvationCallbacks > m_Callbacks
std::list< void * > m_FreeList
virtual void OnOutOfReserve()=0
Is called when the allocation pool is unable to allocate memory.
virtual ~cAllocationPool()
virtual bool DoIsEqual(const cAllocationPool< T > &a_Other) const NOEXCEPT override
bool IsEqual(const cAllocationPool &a_Other) const NOEXCEPT
Two pools compare equal if memory allocated by one can be freed by the other.
friend bool operator!=(const cAllocationPool &a_Lhs, const cAllocationPool &a_Rhs)
virtual ~cListAllocationPool() override
virtual void OnEndUsingReserve()=0
Is called once the reserve buffer has returned to normal size.