memory.cpp 6.5 KB

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