memory_pool_dynamic_static.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*************************************************************************/
  2. /* memory_pool_dynamic_static.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "memory_pool_dynamic_static.h"
  30. #include "os/memory.h"
  31. #include "os/os.h"
  32. #include "ustring.h"
  33. #include "print_string.h"
  34. #include <stdio.h>
  35. MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) {
  36. uint64_t check = p_id/MAX_CHUNKS;
  37. uint64_t idx = p_id%MAX_CHUNKS;
  38. if (!chunk[idx].mem || chunk[idx].check!=check)
  39. return NULL;
  40. return &chunk[idx];
  41. }
  42. const MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) const {
  43. uint64_t check = p_id/MAX_CHUNKS;
  44. uint64_t idx = p_id%MAX_CHUNKS;
  45. if (!chunk[idx].mem || chunk[idx].check!=check)
  46. return NULL;
  47. return &chunk[idx];
  48. }
  49. MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount,const char* p_description) {
  50. _THREAD_SAFE_METHOD_
  51. int idx=-1;
  52. for (int i=0;i<MAX_CHUNKS;i++) {
  53. last_alloc++;
  54. if (last_alloc>=MAX_CHUNKS)
  55. last_alloc=0;
  56. if ( !chunk[last_alloc].mem ) {
  57. idx=last_alloc;
  58. break;
  59. }
  60. }
  61. if (idx==-1) {
  62. ERR_EXPLAIN("Out of dynamic Memory IDs");
  63. ERR_FAIL_V(INVALID_ID);
  64. //return INVALID_ID;
  65. }
  66. //chunk[idx].mem = Memory::alloc_static(p_amount,p_description);
  67. chunk[idx].mem = memalloc(p_amount);
  68. if (!chunk[idx].mem)
  69. return INVALID_ID;
  70. chunk[idx].size=p_amount;
  71. chunk[idx].check=++last_check;
  72. chunk[idx].descr=p_description;
  73. chunk[idx].lock=0;
  74. total_usage+=p_amount;
  75. if (total_usage>max_usage)
  76. max_usage=total_usage;
  77. ID id = chunk[idx].check*MAX_CHUNKS + (uint64_t)idx;
  78. return id;
  79. }
  80. void MemoryPoolDynamicStatic::free(ID p_id) {
  81. _THREAD_SAFE_METHOD_
  82. Chunk *c = get_chunk(p_id);
  83. ERR_FAIL_COND(!c);
  84. total_usage-=c->size;
  85. memfree(c->mem);
  86. c->mem=0;
  87. if (c->lock>0) {
  88. ERR_PRINT("Freed ID Still locked");
  89. }
  90. }
  91. Error MemoryPoolDynamicStatic::realloc(ID p_id, size_t p_amount) {
  92. _THREAD_SAFE_METHOD_
  93. Chunk *c = get_chunk(p_id);
  94. ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
  95. ERR_FAIL_COND_V(c->lock > 0 , ERR_LOCKED );
  96. void * new_mem = memrealloc(c->mem,p_amount);
  97. ERR_FAIL_COND_V(!new_mem,ERR_OUT_OF_MEMORY);
  98. total_usage-=c->size;
  99. c->mem=new_mem;
  100. c->size=p_amount;
  101. total_usage+=c->size;
  102. if (total_usage>max_usage)
  103. max_usage=total_usage;
  104. return OK;
  105. }
  106. bool MemoryPoolDynamicStatic::is_valid(ID p_id) {
  107. _THREAD_SAFE_METHOD_
  108. Chunk *c = get_chunk(p_id);
  109. return c!=NULL;
  110. }
  111. size_t MemoryPoolDynamicStatic::get_size(ID p_id) const {
  112. _THREAD_SAFE_METHOD_
  113. const Chunk *c = get_chunk(p_id);
  114. ERR_FAIL_COND_V(!c,0);
  115. return c->size;
  116. }
  117. const char* MemoryPoolDynamicStatic::get_description(ID p_id) const {
  118. _THREAD_SAFE_METHOD_
  119. const Chunk *c = get_chunk(p_id);
  120. ERR_FAIL_COND_V(!c,"");
  121. return c->descr;
  122. }
  123. bool MemoryPoolDynamicStatic::is_locked(ID p_id) const {
  124. _THREAD_SAFE_METHOD_
  125. const Chunk *c = get_chunk(p_id);
  126. ERR_FAIL_COND_V(!c,false);
  127. return c->lock>0;
  128. }
  129. Error MemoryPoolDynamicStatic::lock(ID p_id) {
  130. _THREAD_SAFE_METHOD_
  131. Chunk *c = get_chunk(p_id);
  132. ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
  133. c->lock++;
  134. return OK;
  135. }
  136. void * MemoryPoolDynamicStatic::get(ID p_id) {
  137. _THREAD_SAFE_METHOD_
  138. const Chunk *c = get_chunk(p_id);
  139. ERR_FAIL_COND_V(!c,NULL);
  140. ERR_FAIL_COND_V( c->lock==0, NULL );
  141. return c->mem;
  142. }
  143. Error MemoryPoolDynamicStatic::unlock(ID p_id) {
  144. _THREAD_SAFE_METHOD_
  145. Chunk *c = get_chunk(p_id);
  146. ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
  147. ERR_FAIL_COND_V( c->lock<=0, ERR_INVALID_PARAMETER );
  148. c->lock--;
  149. return OK;
  150. }
  151. size_t MemoryPoolDynamicStatic::get_available_mem() const {
  152. return Memory::get_static_mem_available();
  153. }
  154. size_t MemoryPoolDynamicStatic::get_total_usage() const {
  155. _THREAD_SAFE_METHOD_
  156. return total_usage;
  157. }
  158. MemoryPoolDynamicStatic::MemoryPoolDynamicStatic() {
  159. last_check=1;
  160. last_alloc=0;
  161. total_usage=0;
  162. max_usage=0;
  163. }
  164. MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() {
  165. #ifdef DEBUG_MEMORY_ENABLED
  166. if (OS::get_singleton()->is_stdout_verbose()) {
  167. if (total_usage>0) {
  168. ERR_PRINT("DYNAMIC ALLOC: ** MEMORY LEAKS DETECTED **");
  169. ERR_PRINT(String("DYNAMIC ALLOC: "+String::num(total_usage)+" bytes of memory in use at exit.").ascii().get_data());
  170. ERR_PRINT("DYNAMIC ALLOC: Following is the list of leaked allocations:");
  171. for (int i=0;i<MAX_CHUNKS;i++) {
  172. if (chunk[i].mem) {
  173. ERR_PRINT(String("\t"+String::num(chunk[i].size)+" bytes - "+String(chunk[i].descr)).ascii().get_data());
  174. }
  175. }
  176. ERR_PRINT("DYNAMIC ALLOC: End of Report.");
  177. print_line("INFO: dynmem - max: "+itos(max_usage)+", "+itos(total_usage)+" leaked.");
  178. } else {
  179. print_line("INFO: dynmem - max: "+itos(max_usage)+", no leaks.");
  180. }
  181. }
  182. #endif
  183. }