cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TScratchAllocator.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2025 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#include <memory>
27#include <limits>
28
29namespace cppcore {
30
31//-------------------------------------------------------------------------------------------------
40//-------------------------------------------------------------------------------------------------
41template<class T>
43public:
45 TScratchAllocator() = default;
46
49 explicit TScratchAllocator(size_t numItems);
50
53
57 T *alloc(size_t numItems);
58
61 void reserve(size_t size);
62
65 void clear();
66
69 size_t capacity() const;
70
73 size_t reservedMem() const;
74
77 size_t freeMem() const;
78
81
82private:
83 T *mBlock{nullptr};
84 size_t mSize{0u};
85 size_t mIndex{0u};
86};
87
88template<class T>
89inline TScratchAllocator<T>::TScratchAllocator(size_t numItems) : mSize(numItems) {
90 reserve(numItems);
91}
92
93template<class T>
97
98template<class T>
99inline T *TScratchAllocator<T>::alloc(size_t numItems) {
100 if (numItems == 0) {
101 return nullptr;
102 }
103
104 // Check for overflow
105 if (numItems > std::numeric_limits<size_t>::max() / sizeof(T)) {
106 return nullptr;
107 }
108
109 if ((mIndex + numItems) > mSize) {
110 return nullptr;
111 }
112
113 // Ensure alignment
114 size_t alignment = alignof(T);
115 mIndex = (mIndex + alignment - 1) & ~(alignment - 1);
116
117 T *ptr = &mBlock[mIndex];
118 mIndex += numItems;
119 return ptr;
120}
121
122template<class T>
124 clear();
125 mBlock = new T[size];
126 mSize = size;
127 mIndex = 0u;
128}
129
130template<class T>
132 delete [] mBlock;
133 mBlock = nullptr;
134 mSize = 0u;
135}
136
137template<class T>
138inline size_t TScratchAllocator<T>::capacity() const {
139 return mSize;
140}
141
142template<class T>
144 return mIndex;
145}
146
147template<class T>
148inline size_t TScratchAllocator<T>::freeMem() const {
149 return (mSize - mIndex);
150}
151
153
154} // namespace cppcore
#define CPPCORE_NONE_COPYING(name)
This macro marks a class a not copyable.
A simple scratch allocator.
size_t capacity() const
Returns the capacity of items in the pool allocator.
void clear()
Will clear the pool, memory will be deallocated.
T * alloc(size_t numItems)
Will allocate the number of items.
size_t freeMem() const
Will return the free memory in the pool in bytes.
size_t reservedMem() const
Will return the reserved memory in bytes.
TScratchAllocator()=default
The default class constructor.
void reserve(size_t size)
Will reserve the pool.
TScratchAllocator(size_t numItems)
The class constructor with the pool size.
~TScratchAllocator()
The class destructor.
TScratchAllocator< char > ScratchAllocator