memory.cpp 4.8 KB

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