| 1 | /**************************************************************************/ |
| 2 | /* jolt_temp_allocator.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | /* |
| 32 | Adapted to Godot from the Jolt Physics library. |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | Copyright 2021 Jorrit Rouwe |
| 37 | |
| 38 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in |
| 39 | the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 40 | the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 41 | |
| 42 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 43 | |
| 44 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR |
| 45 | A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 46 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 47 | */ |
| 48 | |
| 49 | #include "jolt_temp_allocator.h" |
| 50 | |
| 51 | #include "../jolt_project_settings.h" |
| 52 | |
| 53 | #include "core/variant/variant.h" |
| 54 | |
| 55 | #include "Jolt/Core/Memory.h" |
| 56 | |
| 57 | namespace { |
| 58 | |
| 59 | template <typename TValue, typename TAlignment> |
| 60 | constexpr TValue align_up(TValue p_value, TAlignment p_alignment) { |
| 61 | return (p_value + p_alignment - 1) & ~(p_alignment - 1); |
| 62 | } |
| 63 | |
| 64 | } //namespace |
| 65 | |
| 66 | JoltTempAllocator::JoltTempAllocator() : |
| 67 | capacity((uint64_t)JoltProjectSettings::temp_memory_b), |
| 68 | base(static_cast<uint8_t *>(JPH::Allocate((size_t)capacity))) { |
| 69 | } |
| 70 | |
| 71 | JoltTempAllocator::~JoltTempAllocator() { |
| 72 | JPH::Free(base); |
| 73 | } |
| 74 | |
| 75 | void *JoltTempAllocator::Allocate(uint32_t p_size) { |
| 76 | if (p_size == 0) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | p_size = align_up(p_size, 16U); |
| 81 | |
| 82 | const uint64_t new_top = top + p_size; |
| 83 | |
| 84 | void *ptr = nullptr; |
| 85 | |
| 86 | if (new_top <= capacity) { |
| 87 | ptr = base + top; |
| 88 | } else { |
| 89 | WARN_PRINT_ONCE(vformat("Jolt Physics temporary memory allocator exceeded capacity of %d MiB. " |
| 90 | "Falling back to slower general-purpose allocator. " |
| 91 | "Consider increasing maximum temporary memory in project settings.", |
| 92 | JoltProjectSettings::temp_memory_mib)); |
| 93 | |
| 94 | ptr = JPH::Allocate(p_size); |
| 95 | } |
| 96 | |
| 97 | top = new_top; |
| 98 | |
| 99 | return ptr; |
| 100 | } |
| 101 | |
| 102 | void JoltTempAllocator::Free(void *p_ptr, uint32_t p_size) { |
| 103 | if (p_ptr == nullptr) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | p_size = align_up(p_size, 16U); |
| 108 | |
| 109 | const uint64_t new_top = top - p_size; |
| 110 | |
| 111 | if (top <= capacity) { |
| 112 | if (base + new_top != p_ptr) { |
| 113 | CRASH_NOW_MSG("Jolt Physics temporary memory was freed in the wrong order."); |
| 114 | } |
| 115 | } else { |
| 116 | JPH::Free(p_ptr); |
| 117 | } |
| 118 | |
| 119 | top = new_top; |
| 120 | } |