memory.cpp 6.3 KB

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