memory.cpp 5.4 KB

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