memory.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* memory.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_macros.h"
  32. #include "core/os/copymem.h"
  33. #include "core/safe_refcount.h"
  34. #include <stdio.h>
  35. #include <stdlib.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. ERR_EXPLAINC("Call to placement delete should not happen.");
  45. CRASH_NOW();
  46. }
  47. void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) {
  48. ERR_EXPLAINC("Call to placement delete should not happen.");
  49. CRASH_NOW();
  50. }
  51. void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) {
  52. ERR_EXPLAINC("Call to placement delete should not happen.");
  53. CRASH_NOW();
  54. }
  55. #endif
  56. #ifdef DEBUG_ENABLED
  57. uint64_t Memory::mem_usage = 0;
  58. uint64_t Memory::max_usage = 0;
  59. #endif
  60. uint64_t Memory::alloc_count = 0;
  61. void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
  62. #ifdef DEBUG_ENABLED
  63. bool prepad = true;
  64. #else
  65. bool prepad = p_pad_align;
  66. #endif
  67. void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0));
  68. ERR_FAIL_COND_V(!mem, NULL);
  69. atomic_increment(&alloc_count);
  70. if (prepad) {
  71. uint64_t *s = (uint64_t *)mem;
  72. *s = p_bytes;
  73. uint8_t *s8 = (uint8_t *)mem;
  74. #ifdef DEBUG_ENABLED
  75. atomic_add(&mem_usage, p_bytes);
  76. atomic_exchange_if_greater(&max_usage, mem_usage);
  77. #endif
  78. return s8 + PAD_ALIGN;
  79. } else {
  80. return mem;
  81. }
  82. }
  83. void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
  84. if (p_memory == NULL) {
  85. return alloc_static(p_bytes, p_pad_align);
  86. }
  87. uint8_t *mem = (uint8_t *)p_memory;
  88. #ifdef DEBUG_ENABLED
  89. bool prepad = true;
  90. #else
  91. bool prepad = p_pad_align;
  92. #endif
  93. if (prepad) {
  94. mem -= PAD_ALIGN;
  95. uint64_t *s = (uint64_t *)mem;
  96. #ifdef DEBUG_ENABLED
  97. if (p_bytes > *s) {
  98. atomic_add(&mem_usage, p_bytes - *s);
  99. atomic_exchange_if_greater(&max_usage, mem_usage);
  100. } else {
  101. atomic_sub(&mem_usage, *s - p_bytes);
  102. }
  103. #endif
  104. if (p_bytes == 0) {
  105. free(mem);
  106. return NULL;
  107. } else {
  108. *s = p_bytes;
  109. mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
  110. ERR_FAIL_COND_V(!mem, NULL);
  111. s = (uint64_t *)mem;
  112. *s = p_bytes;
  113. return mem + PAD_ALIGN;
  114. }
  115. } else {
  116. mem = (uint8_t *)realloc(mem, p_bytes);
  117. ERR_FAIL_COND_V(mem == NULL && p_bytes > 0, NULL);
  118. return mem;
  119. }
  120. }
  121. void Memory::free_static(void *p_ptr, bool p_pad_align) {
  122. ERR_FAIL_COND(p_ptr == NULL);
  123. uint8_t *mem = (uint8_t *)p_ptr;
  124. #ifdef DEBUG_ENABLED
  125. bool prepad = true;
  126. #else
  127. bool prepad = p_pad_align;
  128. #endif
  129. atomic_decrement(&alloc_count);
  130. if (prepad) {
  131. mem -= PAD_ALIGN;
  132. #ifdef DEBUG_ENABLED
  133. uint64_t *s = (uint64_t *)mem;
  134. atomic_sub(&mem_usage, *s);
  135. #endif
  136. free(mem);
  137. } else {
  138. free(mem);
  139. }
  140. }
  141. uint64_t Memory::get_mem_available() {
  142. return -1; // 0xFFFF...
  143. }
  144. uint64_t Memory::get_mem_usage() {
  145. #ifdef DEBUG_ENABLED
  146. return mem_usage;
  147. #else
  148. return 0;
  149. #endif
  150. }
  151. uint64_t Memory::get_mem_max_usage() {
  152. #ifdef DEBUG_ENABLED
  153. return max_usage;
  154. #else
  155. return 0;
  156. #endif
  157. }
  158. _GlobalNil::_GlobalNil() {
  159. color = 1;
  160. left = this;
  161. right = this;
  162. parent = this;
  163. }
  164. _GlobalNil _GlobalNilClass::_nil;