cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TStackAllocator.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27#include <string>
28
29namespace cppcore {
30
31//-------------------------------------------------------------------------------------------------
42//-------------------------------------------------------------------------------------------------
43template <class T>
45public:
47 TStackAllocator() = default;
48
51 explicit TStackAllocator(size_t initSize);
52
55
58 T *alloc(size_t size);
59
63 bool release(T *ptr);
64
66 void reserve(size_t size);
67
69 void clear();
70
72 void reset();
73
76 size_t capacity() const;
77
80 size_t reservedMem() const;
81
84 size_t freeMem() const;
85
88 void dumpAllocations(std::string &allocs);
89
90 // To copying allowed
92
93private:
94 using byte_t = unsigned char;
95
96 struct Header {
97 size_t mSize{0u};
98 };
99 size_t mCapacity{0u};
100 size_t mTop{0u};
101 size_t mNumAllocs{0u};
102 byte_t *mData{nullptr};
103};
104
105template <class T>
106inline TStackAllocator<T>::TStackAllocator(size_t initSize) {
107 reserve(initSize);
108}
109
110template <class T>
114
115template <class T>
116inline T *TStackAllocator<T>::alloc(size_t size) {
117 if (size == 0) {
118 return nullptr;
119 }
120
121 const size_t range = size * sizeof(T) + sizeof(Header);
122 const size_t limit = mTop + range;
123 if (limit > mCapacity) {
124 return nullptr;
125 }
126
127 // head
128 Header *header = (Header *)(&mData[mTop]);
129 header->mSize = size * sizeof(T);
130 mTop += sizeof(Header);
131
132 // data
133 T *ptr = (T *)(&mData[mTop]);
134 mTop += header->mSize;
135 ++mNumAllocs;
136
137 return ptr;
138}
139
140template <class T>
141inline bool TStackAllocator<T>::release(T *ptr) {
142 if (nullptr == ptr) {
143 return false;
144 }
145
146 // head
147 byte_t *tmp = (byte_t *)ptr;
148 tmp = tmp - sizeof(Header);
149 Header *head = (Header *)tmp;
150 const size_t allocSize = head->mSize;
151
152 // data
153 mTop -= allocSize;
154 mTop -= sizeof(Header);
155 mNumAllocs--;
156
157 return true;
158}
159
160template <class T>
161inline void TStackAllocator<T>::reserve(size_t size) {
162 if (size > mCapacity) {
163 clear();
164 mCapacity = size * sizeof(T);
165 mData = new byte_t[mCapacity];
166 }
167}
168
169template <class T>
171 delete[] mData;
172 mData = nullptr;
173 mCapacity = 0;
174 mTop = 0;
175 mNumAllocs = 0;
176}
177
178template <class T>
180 mTop = 0;
181 mNumAllocs = 0;
182}
183
184template <class T>
185inline size_t TStackAllocator<T>::capacity() const {
186 return mCapacity;
187}
188
189template <class T>
190inline size_t TStackAllocator<T>::reservedMem() const {
191 return mTop;
192}
193
194template <class T>
195inline size_t TStackAllocator<T>::freeMem() const {
196 return (mCapacity - mTop);
197}
198
199template <class T>
200inline void TStackAllocator<T>::dumpAllocations(std::string &allocs) {
201 allocs.clear();
202 allocs += "Number allocations = ";
203 allocs += std::to_string(mNumAllocs);
204 allocs += "\n";
205}
206
207} // Namespace cppcorecppcore
#define CPPCORE_NONE_COPYING(name)
This macro marks a class a not copyable.
size_t freeMem() const
Returns the free memory in bytes.
void reserve(size_t size)
bool release(T *ptr)
Will release the last item from the stack.
size_t reservedMem() const
Returns the allocated memory in bytes.
~TStackAllocator()
The class destructor.
TStackAllocator()=default
The default class constructor.
void reset()
Will reset the allocated memory, all used data will be marked as invalid.
void clear()
Will clear the whole memory, all used data will be marked as invalid.
size_t capacity() const
Returns the number of allocated instances on the stack.
void dumpAllocations(std::string &allocs)
Will dump a statistic overview into the given string.
T * alloc(size_t size)
Will allocate the number of items from the stack.