memory_pool_dynamic_static.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*************************************************************************/
  2. /* memory_pool_dynamic_static.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_pool_dynamic_static.h"
  31. #include "os/memory.h"
  32. #include "os/os.h"
  33. #include "print_string.h"
  34. #include "ustring.h"
  35. #include <stdio.h>
  36. MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) {
  37. uint64_t check = p_id / MAX_CHUNKS;
  38. uint64_t idx = p_id % MAX_CHUNKS;
  39. if (!chunk[idx].mem || chunk[idx].check != check)
  40. return NULL;
  41. return &chunk[idx];
  42. }
  43. const MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) const {
  44. uint64_t check = p_id / MAX_CHUNKS;
  45. uint64_t idx = p_id % MAX_CHUNKS;
  46. if (!chunk[idx].mem || chunk[idx].check != check)
  47. return NULL;
  48. return &chunk[idx];
  49. }
  50. MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount, const char *p_description) {
  51. _THREAD_SAFE_METHOD_
  52. int idx = -1;
  53. for (int i = 0; i < MAX_CHUNKS; i++) {
  54. last_alloc++;
  55. if (last_alloc >= MAX_CHUNKS)
  56. last_alloc = 0;
  57. if (!chunk[last_alloc].mem) {
  58. idx = last_alloc;
  59. break;
  60. }
  61. }
  62. if (idx == -1) {
  63. ERR_EXPLAIN("Out of dynamic Memory IDs");
  64. ERR_FAIL_V(INVALID_ID);
  65. //return INVALID_ID;
  66. }
  67. //chunk[idx].mem = Memory::alloc_static(p_amount,p_description);
  68. chunk[idx].mem = memalloc(p_amount);
  69. if (!chunk[idx].mem)
  70. return INVALID_ID;
  71. chunk[idx].size = p_amount;
  72. chunk[idx].check = ++last_check;
  73. chunk[idx].descr = p_description;
  74. chunk[idx].lock = 0;
  75. total_usage += p_amount;
  76. if (total_usage > max_usage)
  77. max_usage = total_usage;
  78. ID id = chunk[idx].check * MAX_CHUNKS + (uint64_t)idx;
  79. return id;
  80. }
  81. void MemoryPoolDynamicStatic::free(ID p_id) {
  82. _THREAD_SAFE_METHOD_
  83. Chunk *c = get_chunk(p_id);
  84. ERR_FAIL_COND(!c);
  85. total_usage -= c->size;
  86. memfree(c->mem);
  87. c->mem = 0;
  88. if (c->lock > 0) {
  89. ERR_PRINT("Freed ID Still locked");
  90. }
  91. }
  92. Error MemoryPoolDynamicStatic::realloc(ID p_id, size_t p_amount) {
  93. _THREAD_SAFE_METHOD_
  94. Chunk *c = get_chunk(p_id);
  95. ERR_FAIL_COND_V(!c, ERR_INVALID_PARAMETER);
  96. ERR_FAIL_COND_V(c->lock > 0, ERR_LOCKED);
  97. void *new_mem = memrealloc(c->mem, p_amount);
  98. ERR_FAIL_COND_V(!new_mem, ERR_OUT_OF_MEMORY);
  99. total_usage -= c->size;
  100. c->mem = new_mem;
  101. c->size = p_amount;
  102. total_usage += c->size;
  103. if (total_usage > max_usage)
  104. max_usage = total_usage;
  105. return OK;
  106. }
  107. bool MemoryPoolDynamicStatic::is_valid(ID p_id) {
  108. _THREAD_SAFE_METHOD_
  109. Chunk *c = get_chunk(p_id);
  110. return c != NULL;
  111. }
  112. size_t MemoryPoolDynamicStatic::get_size(ID p_id) const {
  113. _THREAD_SAFE_METHOD_
  114. const Chunk *c = get_chunk(p_id);
  115. ERR_FAIL_COND_V(!c, 0);
  116. return c->size;
  117. }
  118. const char *MemoryPoolDynamicStatic::get_description(ID p_id) const {
  119. _THREAD_SAFE_METHOD_
  120. const Chunk *c = get_chunk(p_id);
  121. ERR_FAIL_COND_V(!c, "");
  122. return c->descr;
  123. }
  124. bool MemoryPoolDynamicStatic::is_locked(ID p_id) const {
  125. _THREAD_SAFE_METHOD_
  126. const Chunk *c = get_chunk(p_id);
  127. ERR_FAIL_COND_V(!c, false);
  128. return c->lock > 0;
  129. }
  130. Error MemoryPoolDynamicStatic::lock(ID p_id) {
  131. _THREAD_SAFE_METHOD_
  132. Chunk *c = get_chunk(p_id);
  133. ERR_FAIL_COND_V(!c, ERR_INVALID_PARAMETER);
  134. c->lock++;
  135. return OK;
  136. }
  137. void *MemoryPoolDynamicStatic::get(ID p_id) {
  138. _THREAD_SAFE_METHOD_
  139. const Chunk *c = get_chunk(p_id);
  140. ERR_FAIL_COND_V(!c, NULL);
  141. ERR_FAIL_COND_V(c->lock == 0, NULL);
  142. return c->mem;
  143. }
  144. Error MemoryPoolDynamicStatic::unlock(ID p_id) {
  145. _THREAD_SAFE_METHOD_
  146. Chunk *c = get_chunk(p_id);
  147. ERR_FAIL_COND_V(!c, ERR_INVALID_PARAMETER);
  148. ERR_FAIL_COND_V(c->lock <= 0, ERR_INVALID_PARAMETER);
  149. c->lock--;
  150. return OK;
  151. }
  152. size_t MemoryPoolDynamicStatic::get_available_mem() const {
  153. return Memory::get_static_mem_available();
  154. }
  155. size_t MemoryPoolDynamicStatic::get_total_usage() const {
  156. _THREAD_SAFE_METHOD_
  157. return total_usage;
  158. }
  159. MemoryPoolDynamicStatic::MemoryPoolDynamicStatic() {
  160. last_check = 1;
  161. last_alloc = 0;
  162. total_usage = 0;
  163. max_usage = 0;
  164. }
  165. MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() {
  166. #ifdef DEBUG_MEMORY_ENABLED
  167. if (OS::get_singleton()->is_stdout_verbose()) {
  168. if (total_usage > 0) {
  169. ERR_PRINT("DYNAMIC ALLOC: ** MEMORY LEAKS DETECTED **");
  170. ERR_PRINT(String("DYNAMIC ALLOC: " + String::num(total_usage) + " bytes of memory in use at exit.").ascii().get_data());
  171. ERR_PRINT("DYNAMIC ALLOC: Following is the list of leaked allocations:");
  172. for (int i = 0; i < MAX_CHUNKS; i++) {
  173. if (chunk[i].mem) {
  174. ERR_PRINT(String("\t" + String::num(chunk[i].size) + " bytes - " + String(chunk[i].descr)).ascii().get_data());
  175. }
  176. }
  177. ERR_PRINT("DYNAMIC ALLOC: End of Report.");
  178. print_line("INFO: dynmem - max: " + itos(max_usage) + ", " + itos(total_usage) + " leaked.");
  179. } else {
  180. print_line("INFO: dynmem - max: " + itos(max_usage) + ", no leaks.");
  181. }
  182. }
  183. #endif
  184. }