|
| 1 | +/*----------------------------------------------------------------------------------------------- |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2014-2024 Kim Kulling |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +this software and associated documentation files (the "Software"), to deal in |
| 8 | +the Software without restriction, including without limitation the rights to |
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +-----------------------------------------------------------------------------------------------*/ |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include <cppcore/CPPCoreCommon.h> |
| 26 | + |
| 27 | +namespace cppcore { |
| 28 | + |
| 29 | +//------------------------------------------------------------------------------------------------- |
| 30 | +/// @class TScratchAllocator |
| 31 | +/// @ingroup CPPCore |
| 32 | +/// |
| 33 | +/// @brief A simple scratch allocator. |
| 34 | +/// |
| 35 | +/// You can use it to manage all the dynamic allocations without caring about cleaning it up. All |
| 36 | +/// Allocations will be a one-shot alloc, which do not need to get released. When calling clear |
| 37 | +/// all allocation will be invalidated and cannot be used anymore. |
| 38 | +//------------------------------------------------------------------------------------------------- |
| 39 | +template<class T> |
| 40 | +class TScratchAllocator { |
| 41 | +public: |
| 42 | + /// @brief The default class constructor. |
| 43 | + TScratchAllocator(); |
| 44 | + |
| 45 | + /// @brief The class constructor with the pool size. |
| 46 | + /// @param[in] numItems The buffer size. |
| 47 | + TScratchAllocator(size_t numItems); |
| 48 | + |
| 49 | + /// @brief The class destructor. |
| 50 | + ~TScratchAllocator(); |
| 51 | + |
| 52 | + /// @brief Will allocate the number of items. |
| 53 | + /// @param[in] numItems The number of items. |
| 54 | + /// @return Pointr showing to the scratch buffer. |
| 55 | + T *alloc(size_t numItems); |
| 56 | + |
| 57 | + /// @brief Will reserve the pool. |
| 58 | + /// @apram[in] size The pool size to reserve. |
| 59 | + void reserve(size_t size); |
| 60 | + |
| 61 | + /// @brief Will clear the pool, memory will be deallocated. |
| 62 | + /// @note All instances which are currently is use will get invalid. PLease use with care. |
| 63 | + void clear(); |
| 64 | + |
| 65 | + /// @brief Returns the capacity of items in the pool allocator. |
| 66 | + /// @return The capacity. |
| 67 | + size_t capacity() const; |
| 68 | + |
| 69 | + /// @brief Will return the reserved memory in bytes. |
| 70 | + /// @return The reserved memory in bytes. |
| 71 | + size_t reservedMem() const; |
| 72 | + |
| 73 | + /// @brief Will return the free memory in the pool in bytes. |
| 74 | + /// @return The free memory in bytes. |
| 75 | + size_t freeMem() const; |
| 76 | + |
| 77 | + /// No copying allowed |
| 78 | + CPPCORE_NONE_COPYING(TScratchAllocator) |
| 79 | + |
| 80 | +private: |
| 81 | + T *mBlock; |
| 82 | + size_t mSize; |
| 83 | + size_t mIndex; |
| 84 | +}; |
| 85 | + |
| 86 | +template<class T> |
| 87 | +inline TScratchAllocator<T>::TScratchAllocator() : |
| 88 | + mBlock(nullptr), mSize(0u), mIndex(0u) { |
| 89 | + // empty |
| 90 | +} |
| 91 | + |
| 92 | +template<class T> |
| 93 | +inline TScratchAllocator<T>::TScratchAllocator(size_t numItems) : |
| 94 | + mBlock(nullptr), mSize(0u), mIndex(0u) { |
| 95 | + reserve(numItems); |
| 96 | +} |
| 97 | + |
| 98 | +template<class T> |
| 99 | +inline TScratchAllocator<T>::~TScratchAllocator() { |
| 100 | + clear(); |
| 101 | +} |
| 102 | + |
| 103 | +template<class T> |
| 104 | +inline T *TScratchAllocator<T>::alloc(size_t numItems) { |
| 105 | + if ((mIndex + numItems) > mSize) { |
| 106 | + return nullptr; |
| 107 | + } |
| 108 | + |
| 109 | + T *ptr = &mBlock[mIndex]; |
| 110 | + mIndex += numItems; |
| 111 | + return ptr; |
| 112 | +} |
| 113 | + |
| 114 | +template<class T> |
| 115 | +void TScratchAllocator<T>::reserve(size_t size) { |
| 116 | + clear(); |
| 117 | + mBlock = new T[size]; |
| 118 | + mSize = size; |
| 119 | + mIndex = 0u; |
| 120 | +} |
| 121 | + |
| 122 | +template<class T> |
| 123 | +inline void TScratchAllocator<T>::clear() { |
| 124 | + delete mBlock; |
| 125 | + mBlock = nullptr; |
| 126 | + mSize = 0u; |
| 127 | +} |
| 128 | + |
| 129 | +template<class T> |
| 130 | +inline size_t TScratchAllocator<T>::capacity() const { |
| 131 | + return mSize; |
| 132 | +} |
| 133 | + |
| 134 | +template<class T> |
| 135 | +size_t TScratchAllocator<T>::reservedMem() const { |
| 136 | + return mIndex; |
| 137 | +} |
| 138 | + |
| 139 | +template<class T> |
| 140 | +size_t TScratchAllocator<T>::freeMem() const { |
| 141 | + return (mSize - mIndex); |
| 142 | +} |
| 143 | + |
| 144 | +using ScratchAllocator = TScratchAllocator<char>; |
| 145 | + |
| 146 | +} // namespace cppcore |
0 commit comments