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