StackAllocator.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  * 1. The origin of this software must not be misrepresented; you must not
12  * claim that you wrote the original software. If you use this software
13  * in a product, an acknowledgment in the product documentation would be
14  * appreciated but is not required.
15  * 2. Altered source versions must be plainly marked as such, and must not be
16  * misrepresented as being the original software.
17  * 3. This notice may not be removed or altered from any source distribution.
18  */
19 
20 #ifndef PLAYRHO_COMMON_STACKALLOCATOR_HPP
21 #define PLAYRHO_COMMON_STACKALLOCATOR_HPP
22 
24 
25 namespace playrho {
26 
35 {
36 public:
37 
39  using size_type = std::size_t;
40 
42  struct Conf
43  {
46  };
47 
50  {
51  return Conf{};
52  }
53 
55  explicit StackAllocator(Conf config = GetDefaultConf()) noexcept;
56 
57  ~StackAllocator() noexcept;
58 
60  StackAllocator(const StackAllocator& copy) = delete;
61 
62  StackAllocator(StackAllocator&& other) = delete;
63 
65  StackAllocator& operator= (const StackAllocator& other) = delete;
66 
67  StackAllocator& operator= (StackAllocator&& other) = delete;
68 
73  void* Allocate(size_type size) noexcept;
74 
76  void Free(void* p) noexcept;
77 
79  template <typename T>
81  {
82  return static_cast<T*>(Allocate(size * sizeof(T)));
83  }
84 
88  void operator()(void *p) noexcept
89  {
90  Free(p);
91  }
92 
94  auto GetMaxAllocation() const noexcept
95  {
96  return m_maxAllocation;
97  }
98 
102  auto GetEntryCount() const noexcept
103  {
104  return m_entryCount;
105  }
106 
112  auto GetIndex() const noexcept
113  {
114  return m_index;
115  }
116 
118  auto GetAllocation() const noexcept
119  {
120  return m_allocation;
121  }
122 
124  auto GetPreallocatedSize() const noexcept
125  {
126  return m_size;
127  }
128 
130  auto GetMaxEntries() const noexcept
131  {
132  return m_max_entries;
133  }
134 
135 private:
136 
138  struct AllocationRecord
139  {
140  void* data;
141  size_type size;
142  bool usedMalloc;
143  };
144 
145  char* const m_data;
146  AllocationRecord* const m_entries;
147  size_type const m_size;
148  size_type const m_max_entries;
149 
150  size_type m_index = 0;
151  size_type m_allocation = 0;
152  size_type m_maxAllocation = 0;
153  size_type m_entryCount = 0;
154 };
155 
156 } // namespace playrho
157 
158 #endif // PLAYRHO_COMMON_STACKALLOCATOR_HPP