| 1 | /**************************************************************************/ |
| 2 | /* memory.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 | #include "memory.h" |
| 32 | |
| 33 | #include "core/profiling/profiling.h" |
| 34 | #include "core/templates/safe_refcount.h" |
| 35 | |
| 36 | #include <cstdlib> |
| 37 | |
| 38 | void *operator new(size_t p_size, const char *p_description) { |
| 39 | return Memory::alloc_static(p_size, false); |
| 40 | } |
| 41 | |
| 42 | void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) { |
| 43 | return p_allocfunc(p_size); |
| 44 | } |
| 45 | |
| 46 | #ifdef _MSC_VER |
| 47 | void operator delete(void *p_mem, const char *p_description) { |
| 48 | CRASH_NOW_MSG("Call to placement delete should not happen."); |
| 49 | } |
| 50 | |
| 51 | void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) { |
| 52 | CRASH_NOW_MSG("Call to placement delete should not happen."); |
| 53 | } |
| 54 | |
| 55 | void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) { |
| 56 | CRASH_NOW_MSG("Call to placement delete should not happen."); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | #ifdef DEBUG_ENABLED |
| 61 | static SafeNumeric<uint64_t> _current_mem_usage; |
| 62 | static SafeNumeric<uint64_t> _max_mem_usage; |
| 63 | #endif |
| 64 | |
| 65 | void *Memory::alloc_aligned_static(size_t p_bytes, size_t p_alignment) { |
| 66 | DEV_ASSERT(is_power_of_2(p_alignment)); |
| 67 | |
| 68 | void *p1, *p2; |
| 69 | if ((p1 = (void *)malloc(p_bytes + p_alignment - 1 + sizeof(uint32_t))) == nullptr) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | GodotProfileAlloc(p1, p_bytes + p_alignment - 1 + sizeof(uint32_t)); |
| 73 | |
| 74 | p2 = (void *)(((uintptr_t)p1 + sizeof(uint32_t) + p_alignment - 1) & ~((p_alignment)-1)); |
| 75 | *((uint32_t *)p2 - 1) = (uint32_t)((uintptr_t)p2 - (uintptr_t)p1); |
| 76 | return p2; |
| 77 | } |
| 78 | |
| 79 | void *Memory::realloc_aligned_static(void *p_memory, size_t p_bytes, size_t p_prev_bytes, size_t p_alignment) { |
| 80 | if (p_memory == nullptr) { |
| 81 | return alloc_aligned_static(p_bytes, p_alignment); |
| 82 | } |
| 83 | |
| 84 | void *ret = alloc_aligned_static(p_bytes, p_alignment); |
| 85 | if (ret) { |
| 86 | memcpy(ret, p_memory, p_prev_bytes); |
| 87 | } |
| 88 | free_aligned_static(p_memory); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | void Memory::free_aligned_static(void *p_memory) { |
| 93 | uint32_t offset = *((uint32_t *)p_memory - 1); |
| 94 | void *p = (void *)((uint8_t *)p_memory - offset); |
| 95 | GodotProfileFree(p); |
| 96 | free(p); |
| 97 | } |
| 98 | |
| 99 | template <bool p_ensure_zero> |
| 100 | void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { |
| 101 | #ifdef DEBUG_ENABLED |
| 102 | bool prepad = true; |
| 103 | #else |
| 104 | bool prepad = p_pad_align; |
| 105 | #endif |
| 106 | |
| 107 | void *mem; |
| 108 | if constexpr (p_ensure_zero) { |
| 109 | mem = calloc(1, p_bytes + (prepad ? DATA_OFFSET : 0)); |
| 110 | } else { |
| 111 | mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0)); |
| 112 | } |
| 113 | |
| 114 | ERR_FAIL_NULL_V(mem, nullptr); |
| 115 | GodotProfileAlloc(mem, p_bytes + (prepad ? DATA_OFFSET : 0)); |
| 116 | |
| 117 | if (prepad) { |
| 118 | uint8_t *s8 = (uint8_t *)mem; |
| 119 | |
| 120 | uint64_t *s = (uint64_t *)(s8 + SIZE_OFFSET); |
| 121 | *s = p_bytes; |
| 122 | |
| 123 | #ifdef DEBUG_ENABLED |
| 124 | uint64_t new_mem_usage = _current_mem_usage.add(p_bytes); |
| 125 | _max_mem_usage.exchange_if_greater(new_mem_usage); |
| 126 | #endif |
| 127 | return s8 + DATA_OFFSET; |
| 128 | } else { |
| 129 | return mem; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | template void *Memory::alloc_static<true>(size_t p_bytes, bool p_pad_align); |
| 134 | template void *Memory::alloc_static<false>(size_t p_bytes, bool p_pad_align); |
| 135 | |
| 136 | void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { |
| 137 | if (p_memory == nullptr) { |
| 138 | return alloc_static(p_bytes, p_pad_align); |
| 139 | } |
| 140 | |
| 141 | uint8_t *mem = (uint8_t *)p_memory; |
| 142 | |
| 143 | #ifdef DEBUG_ENABLED |
| 144 | bool prepad = true; |
| 145 | #else |
| 146 | bool prepad = p_pad_align; |
| 147 | #endif |
| 148 | |
| 149 | if (prepad) { |
| 150 | mem -= DATA_OFFSET; |
| 151 | uint64_t *s = (uint64_t *)(mem + SIZE_OFFSET); |
| 152 | |
| 153 | #ifdef DEBUG_ENABLED |
| 154 | if (p_bytes > *s) { |
| 155 | uint64_t new_mem_usage = _current_mem_usage.add(p_bytes - *s); |
| 156 | _max_mem_usage.exchange_if_greater(new_mem_usage); |
| 157 | } else { |
| 158 | _current_mem_usage.sub(*s - p_bytes); |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | if (p_bytes == 0) { |
| 163 | GodotProfileFree(mem); |
| 164 | free(mem); |
| 165 | return nullptr; |
| 166 | } else { |
| 167 | *s = p_bytes; |
| 168 | |
| 169 | GodotProfileFree(mem); |
| 170 | mem = (uint8_t *)realloc(mem, p_bytes + DATA_OFFSET); |
| 171 | ERR_FAIL_NULL_V(mem, nullptr); |
| 172 | GodotProfileAlloc(mem, p_bytes + DATA_OFFSET); |
| 173 | |
| 174 | s = (uint64_t *)(mem + SIZE_OFFSET); |
| 175 | |
| 176 | *s = p_bytes; |
| 177 | |
| 178 | return mem + DATA_OFFSET; |
| 179 | } |
| 180 | } else { |
| 181 | GodotProfileFree(mem); |
| 182 | mem = (uint8_t *)realloc(mem, p_bytes); |
| 183 | |
| 184 | ERR_FAIL_COND_V(mem == nullptr && p_bytes > 0, nullptr); |
| 185 | GodotProfileAlloc(mem, p_bytes); |
| 186 | |
| 187 | return mem; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void Memory::free_static(void *p_ptr, bool p_pad_align) { |
| 192 | ERR_FAIL_NULL(p_ptr); |
| 193 | |
| 194 | uint8_t *mem = (uint8_t *)p_ptr; |
| 195 | |
| 196 | #ifdef DEBUG_ENABLED |
| 197 | bool prepad = true; |
| 198 | #else |
| 199 | bool prepad = p_pad_align; |
| 200 | #endif |
| 201 | |
| 202 | if (prepad) { |
| 203 | mem -= DATA_OFFSET; |
| 204 | |
| 205 | #ifdef DEBUG_ENABLED |
| 206 | uint64_t *s = (uint64_t *)(mem + SIZE_OFFSET); |
| 207 | _current_mem_usage.sub(*s); |
| 208 | #endif |
| 209 | |
| 210 | GodotProfileFree(mem); |
| 211 | free(mem); |
| 212 | } else { |
| 213 | GodotProfileFree(mem); |
| 214 | free(mem); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | uint64_t Memory::get_mem_available() { |
| 219 | return -1; // 0xFFFF... |
| 220 | } |
| 221 | |
| 222 | uint64_t Memory::get_mem_usage() { |
| 223 | #ifdef DEBUG_ENABLED |
| 224 | return _current_mem_usage.get(); |
| 225 | #else |
| 226 | return 0; |
| 227 | #endif |
| 228 | } |
| 229 | |
| 230 | uint64_t Memory::get_mem_max_usage() { |
| 231 | #ifdef DEBUG_ENABLED |
| 232 | return _max_mem_usage.get(); |
| 233 | #else |
| 234 | return 0; |
| 235 | #endif |
| 236 | } |
| 237 | |
| 238 | _GlobalNil::_GlobalNil() { |
| 239 | left = this; |
| 240 | right = this; |
| 241 | parent = this; |
| 242 | } |
| 243 | |
| 244 | _GlobalNil _GlobalNilClass::_nil; |