| 1 | /* |
| 2 | Open Asset Import Library (assimp) |
| 3 | ---------------------------------------------------------------------- |
| 4 | |
| 5 | Copyright (c) 2006-2025, assimp team |
| 6 | |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use of this software in source and binary forms, |
| 10 | with or without modification, are permitted provided that the |
| 11 | following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above |
| 14 | copyright notice, this list of conditions and the |
| 15 | following disclaimer. |
| 16 | |
| 17 | * Redistributions in binary form must reproduce the above |
| 18 | copyright notice, this list of conditions and the |
| 19 | following disclaimer in the documentation and/or other |
| 20 | materials provided with the distribution. |
| 21 | |
| 22 | * Neither the name of the assimp team, nor the names of its |
| 23 | contributors may be used to endorse or promote products |
| 24 | derived from this software without specific prior |
| 25 | written permission of the assimp team. |
| 26 | |
| 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | ---------------------------------------------------------------------- |
| 39 | */ |
| 40 | |
| 41 | /** @file StackAllocator.h |
| 42 | * @brief A very bare-bone allocator class that is suitable when |
| 43 | * allocating many small objects, e.g. during parsing. |
| 44 | * Individual objects are not freed, instead only the whole memory |
| 45 | * can be deallocated. |
| 46 | */ |
| 47 | #ifndef AI_STACK_ALLOCATOR_H_INC |
| 48 | #define AI_STACK_ALLOCATOR_H_INC |
| 49 | |
| 50 | #include <vector> |
| 51 | #include <stdint.h> |
| 52 | #include <stddef.h> |
| 53 | |
| 54 | namespace Assimp { |
| 55 | |
| 56 | /** @brief A very bare-bone allocator class that is suitable when |
| 57 | * allocating many small objects, e.g. during parsing. |
| 58 | * Individual objects are not freed, instead only the whole memory |
| 59 | * can be deallocated. |
| 60 | */ |
| 61 | class StackAllocator { |
| 62 | public: |
| 63 | /// @brief Constructs the allocator |
| 64 | StackAllocator(); |
| 65 | |
| 66 | /// @brief Destructs the allocator and frees all memory |
| 67 | ~StackAllocator(); |
| 68 | |
| 69 | // non copyable |
| 70 | StackAllocator(const StackAllocator &) = delete; |
| 71 | StackAllocator &operator=(const StackAllocator &) = delete; |
| 72 | |
| 73 | /// @brief Returns a pointer to byteSize bytes of heap memory that persists |
| 74 | /// for the lifetime of the allocator (or until FreeAll is called). |
| 75 | inline void *Allocate(size_t byteSize); |
| 76 | |
| 77 | /// @brief Releases all the memory owned by this allocator. |
| 78 | // Memory provided through function Allocate is not valid anymore after this function has been called. |
| 79 | inline void FreeAll(); |
| 80 | |
| 81 | private: |
| 82 | constexpr const static size_t g_maxBytesPerBlock = 64 * 1024 * 1024; // The maximum size (in bytes) of a block |
| 83 | constexpr const static size_t g_startBytesPerBlock = 16 * 1024; // Size of the first block. Next blocks will double in size until maximum size of g_maxBytesPerBlock |
| 84 | size_t m_blockAllocationSize = g_startBytesPerBlock; // Block size of the current block |
| 85 | size_t m_subIndex = g_maxBytesPerBlock; // The current byte offset in the current block |
| 86 | std::vector<uint8_t *> m_storageBlocks; // A list of blocks |
| 87 | }; |
| 88 | |
| 89 | } // namespace Assimp |
| 90 | |
| 91 | /// @brief Fixes an undefined reference error when linking in certain build environments. |
| 92 | // May throw warnings about needing stdc++17, but should compile without issues on modern compilers. |
| 93 | inline const size_t Assimp::StackAllocator::g_maxBytesPerBlock; |
| 94 | inline const size_t Assimp::StackAllocator::g_startBytesPerBlock; |
| 95 | |
| 96 | #include "StackAllocator.inl" |
| 97 | |
| 98 | #endif // include guard |
| 99 | |