memory.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* memory.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. 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. uint64_t Memory::mem_usage = 0;
  55. uint64_t Memory::max_usage = 0;
  56. #endif
  57. uint64_t Memory::alloc_count = 0;
  58. void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
  59. #ifdef DEBUG_ENABLED
  60. bool prepad = true;
  61. #else
  62. bool prepad = p_pad_align;
  63. #endif
  64. void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0));
  65. ERR_FAIL_COND_V(!mem, NULL);
  66. atomic_increment(&alloc_count);
  67. if (prepad) {
  68. uint64_t *s = (uint64_t *)mem;
  69. *s = p_bytes;
  70. uint8_t *s8 = (uint8_t *)mem;
  71. #ifdef DEBUG_ENABLED
  72. atomic_add(&mem_usage, p_bytes);
  73. atomic_exchange_if_greater(&max_usage, mem_usage);
  74. #endif
  75. return s8 + PAD_ALIGN;
  76. } else {
  77. return mem;
  78. }
  79. }
  80. void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
  81. if (p_memory == NULL) {
  82. return alloc_static(p_bytes, p_pad_align);
  83. }
  84. uint8_t *mem = (uint8_t *)p_memory;
  85. #ifdef DEBUG_ENABLED
  86. bool prepad = true;
  87. #else
  88. bool prepad = p_pad_align;
  89. #endif
  90. if (prepad) {
  91. mem -= PAD_ALIGN;
  92. uint64_t *s = (uint64_t *)mem;
  93. #ifdef DEBUG_ENABLED
  94. if (p_bytes > *s) {
  95. atomic_add(&mem_usage, p_bytes - *s);
  96. atomic_exchange_if_greater(&max_usage, mem_usage);
  97. } else {
  98. atomic_sub(&mem_usage, *s - p_bytes);
  99. }
  100. #endif
  101. if (p_bytes == 0) {
  102. free(mem);
  103. return NULL;
  104. } else {
  105. *s = p_bytes;
  106. mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
  107. ERR_FAIL_COND_V(!mem, NULL);
  108. s = (uint64_t *)mem;
  109. *s = p_bytes;
  110. return mem + PAD_ALIGN;
  111. }
  112. } else {
  113. mem = (uint8_t *)realloc(mem, p_bytes);
  114. ERR_FAIL_COND_V(mem == NULL && p_bytes > 0, NULL);
  115. return mem;
  116. }
  117. }
  118. void Memory::free_static(void *p_ptr, bool p_pad_align) {
  119. ERR_FAIL_COND(p_ptr == NULL);
  120. uint8_t *mem = (uint8_t *)p_ptr;
  121. #ifdef DEBUG_ENABLED
  122. bool prepad = true;
  123. #else
  124. bool prepad = p_pad_align;
  125. #endif
  126. atomic_decrement(&alloc_count);
  127. if (prepad) {
  128. mem -= PAD_ALIGN;
  129. #ifdef DEBUG_ENABLED
  130. uint64_t *s = (uint64_t *)mem;
  131. atomic_sub(&mem_usage, *s);
  132. #endif
  133. free(mem);
  134. } else {
  135. free(mem);
  136. }
  137. }
  138. uint64_t Memory::get_mem_available() {
  139. return -1; // 0xFFFF...
  140. }
  141. uint64_t Memory::get_mem_usage() {
  142. #ifdef DEBUG_ENABLED
  143. return mem_usage;
  144. #else
  145. return 0;
  146. #endif
  147. }
  148. uint64_t Memory::get_mem_max_usage() {
  149. #ifdef DEBUG_ENABLED
  150. return max_usage;
  151. #else
  152. return 0;
  153. #endif
  154. }
  155. _GlobalNil::_GlobalNil() {
  156. color = 1;
  157. left = this;
  158. right = this;
  159. parent = this;
  160. }
  161. _GlobalNil _GlobalNilClass::_nil;