memory_pool_static_malloc.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*************************************************************************/
  2. /* memory_pool_static_malloc.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_static_malloc.h"
  30. #include "error_macros.h"
  31. #include "os/memory.h"
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include "os/copymem.h"
  35. #include "os/os.h"
  36. /**
  37. * NOTE NOTE NOTE NOTE
  38. * in debug mode, this prepends the memory size to the allocated block
  39. * so BE CAREFUL!
  40. */
  41. void* MemoryPoolStaticMalloc::alloc(size_t p_bytes,const char *p_description) {
  42. #if DEFAULT_ALIGNMENT == 1
  43. return _alloc(p_bytes, p_description);
  44. #else
  45. int total = p_bytes + DEFAULT_ALIGNMENT;
  46. uint8_t* ptr = (uint8_t*)_alloc(total, p_description);
  47. ERR_FAIL_COND_V( !ptr, ptr );
  48. int ofs = (DEFAULT_ALIGNMENT - ((uintptr_t)ptr & (DEFAULT_ALIGNMENT - 1)));
  49. ptr[ofs-1] = ofs;
  50. return (void*)(ptr + ofs);
  51. #endif
  52. };
  53. void* MemoryPoolStaticMalloc::_alloc(size_t p_bytes,const char *p_description) {
  54. ERR_FAIL_COND_V(p_bytes==0,0);
  55. MutexLock lock(mutex);
  56. #ifdef DEBUG_MEMORY_ENABLED
  57. void *mem=malloc(p_bytes+sizeof(RingPtr)); /// add for size and ringlist
  58. if (!mem) {
  59. printf("**ERROR: out of memory while allocating %i bytes by %s?\n",(int) p_bytes, p_description);
  60. printf("**ERROR: memory usage is %i\n", (int)get_total_usage());
  61. };
  62. ERR_FAIL_COND_V(!mem,0); //out of memory, or unreasonable request
  63. /* setup the ringlist element */
  64. RingPtr *ringptr = (RingPtr*)mem;
  65. /* setup the ringlist element data (description and size ) */
  66. ringptr->size = p_bytes;
  67. ringptr->descr=p_description;
  68. if (ringlist) { /* existing ringlist */
  69. /* assign next */
  70. ringptr->next = ringlist->next;
  71. ringlist->next = ringptr;
  72. /* assign prev */
  73. ringptr->prev = ringlist;
  74. ringptr->next->prev = ringptr;
  75. } else { /* non existing ringlist */
  76. ringptr->next=ringptr;
  77. ringptr->prev=ringptr;
  78. ringlist=ringptr;
  79. }
  80. total_mem+=p_bytes;
  81. /* update statistics */
  82. if (total_mem > max_mem )
  83. max_mem = total_mem;
  84. total_pointers++;
  85. if (total_pointers > max_pointers)
  86. max_pointers=total_pointers;
  87. return ringptr + 1; /* return memory after ringptr */
  88. #else
  89. void *mem=malloc(p_bytes);
  90. ERR_FAIL_COND_V(!mem,0); //out of memory, or unreasonable request
  91. return mem;
  92. #endif
  93. }
  94. void* MemoryPoolStaticMalloc::realloc(void *p_memory,size_t p_bytes) {
  95. #if DEFAULT_ALIGNMENT == 1
  96. return _realloc(p_memory,p_bytes);
  97. #else
  98. if (!p_memory)
  99. return alloc(p_bytes);
  100. int total = p_bytes + DEFAULT_ALIGNMENT;
  101. uint8_t* mem = (uint8_t*)p_memory;
  102. int ofs = *(mem-1);
  103. mem = mem - ofs;
  104. uint8_t* ptr = (uint8_t*)_realloc(mem, total);
  105. ERR_FAIL_COND_V(ptr == NULL, NULL);
  106. int new_ofs = (DEFAULT_ALIGNMENT - ((uintptr_t)ptr & (DEFAULT_ALIGNMENT - 1)));
  107. if (new_ofs != ofs) {
  108. //printf("realloc moving %i bytes\n", p_bytes);
  109. movemem((ptr + new_ofs), (ptr + ofs), p_bytes);
  110. ptr[new_ofs-1] = new_ofs;
  111. };
  112. return ptr + new_ofs;
  113. #endif
  114. };
  115. void* MemoryPoolStaticMalloc::_realloc(void *p_memory,size_t p_bytes) {
  116. if (p_memory==NULL) {
  117. return alloc( p_bytes );
  118. }
  119. if (p_bytes==0) {
  120. this->free(p_memory);
  121. ERR_FAIL_COND_V( p_bytes < 0 , NULL );
  122. return NULL;
  123. }
  124. MutexLock lock(mutex);
  125. #ifdef DEBUG_MEMORY_ENABLED
  126. RingPtr *ringptr = (RingPtr*)p_memory;
  127. ringptr--; /* go back an element to find the tingptr */
  128. bool single_element = (ringptr->next == ringptr) && (ringptr->prev == ringptr);
  129. bool is_list = ( ringlist == ringptr );
  130. RingPtr *new_ringptr=(RingPtr*)::realloc(ringptr, p_bytes+sizeof(RingPtr));
  131. ERR_FAIL_COND_V( new_ringptr == 0, NULL ); /// reallocation failed
  132. /* actualize mem used */
  133. total_mem -= new_ringptr->size;
  134. new_ringptr->size = p_bytes;
  135. total_mem += new_ringptr->size;
  136. if (total_mem > max_mem ) //update statistics
  137. max_mem = total_mem;
  138. if (new_ringptr == ringptr )
  139. return ringptr + 1; // block didn't move, don't do anything
  140. if (single_element) {
  141. new_ringptr->next=new_ringptr;
  142. new_ringptr->prev=new_ringptr;
  143. } else {
  144. new_ringptr->next->prev=new_ringptr;
  145. new_ringptr->prev->next=new_ringptr;
  146. }
  147. if (is_list)
  148. ringlist=new_ringptr;
  149. return new_ringptr + 1;
  150. #else
  151. return ::realloc( p_memory, p_bytes );
  152. #endif
  153. }
  154. void MemoryPoolStaticMalloc::free(void *p_ptr) {
  155. ERR_FAIL_COND( !MemoryPoolStatic::get_singleton());
  156. #if DEFAULT_ALIGNMENT == 1
  157. _free(p_ptr);
  158. #else
  159. uint8_t* mem = (uint8_t*)p_ptr;
  160. int ofs = *(mem-1);
  161. mem = mem - ofs;
  162. _free(mem);
  163. #endif
  164. };
  165. void MemoryPoolStaticMalloc::_free(void *p_ptr) {
  166. MutexLock lock(mutex);
  167. #ifdef DEBUG_MEMORY_ENABLED
  168. if (p_ptr==0) {
  169. printf("**ERROR: STATIC ALLOC: Attempted free of NULL pointer.\n");
  170. return;
  171. };
  172. RingPtr *ringptr = (RingPtr*)p_ptr;
  173. ringptr--; /* go back an element to find the ringptr */
  174. #if 0
  175. { // check for existing memory on free.
  176. RingPtr *p = ringlist;
  177. bool found=false;
  178. if (ringlist) {
  179. do {
  180. if (p==ringptr) {
  181. found=true;
  182. break;
  183. }
  184. p=p->next;
  185. } while (p!=ringlist);
  186. }
  187. if (!found) {
  188. printf("**ERROR: STATIC ALLOC: Attempted free of unknown pointer at %p\n",(ringptr+1));
  189. return;
  190. }
  191. }
  192. #endif
  193. /* proceed to erase */
  194. bool single_element = (ringptr->next == ringptr) && (ringptr->prev == ringptr);
  195. bool is_list = ( ringlist == ringptr );
  196. if (single_element) {
  197. /* just get rid of it */
  198. ringlist=0;
  199. } else {
  200. /* auto-remove from ringlist */
  201. if (is_list)
  202. ringlist=ringptr->next;
  203. ringptr->prev->next = ringptr->next;
  204. ringptr->next->prev = ringptr->prev;
  205. }
  206. total_mem -= ringptr->size;
  207. total_pointers--;
  208. // catch more errors
  209. zeromem(ringptr,sizeof(RingPtr)+ringptr->size);
  210. ::free(ringptr); //just free that pointer
  211. #else
  212. ERR_FAIL_COND(p_ptr==0);
  213. ::free(p_ptr);
  214. #endif
  215. }
  216. size_t MemoryPoolStaticMalloc::get_available_mem() const {
  217. return 0xffffffff;
  218. }
  219. size_t MemoryPoolStaticMalloc::get_total_usage() {
  220. #ifdef DEBUG_MEMORY_ENABLED
  221. return total_mem;
  222. #else
  223. return 0;
  224. #endif
  225. }
  226. size_t MemoryPoolStaticMalloc::get_max_usage() {
  227. return max_mem;
  228. }
  229. /* Most likely available only if memory debugger was compiled in */
  230. int MemoryPoolStaticMalloc::get_alloc_count() {
  231. return 0;
  232. }
  233. void * MemoryPoolStaticMalloc::get_alloc_ptr(int p_alloc_idx) {
  234. return 0;
  235. }
  236. const char* MemoryPoolStaticMalloc::get_alloc_description(int p_alloc_idx) {
  237. return "";
  238. }
  239. size_t MemoryPoolStaticMalloc::get_alloc_size(int p_alloc_idx) {
  240. return 0;
  241. }
  242. void MemoryPoolStaticMalloc::dump_mem_to_file(const char* p_file) {
  243. #ifdef DEBUG_MEMORY_ENABLED
  244. ERR_FAIL_COND( !ringlist ); /** WTF BUG !? */
  245. RingPtr *p = ringlist;
  246. FILE *f = fopen(p_file,"wb");
  247. do {
  248. fprintf(f,"%p-%i-%s\n", p+1, (int)p->size, (p->descr?p->descr:"") );
  249. p=p->next;
  250. } while (p!=ringlist);
  251. fclose(f);
  252. #endif
  253. }
  254. MemoryPoolStaticMalloc::MemoryPoolStaticMalloc() {
  255. #ifdef DEBUG_MEMORY_ENABLED
  256. total_mem=0;
  257. total_pointers=0;
  258. ringlist=0;
  259. max_mem=0;
  260. max_pointers=0;
  261. #endif
  262. mutex=NULL;
  263. #ifndef NO_THREADS
  264. mutex=Mutex::create(); // at this point, this should work
  265. #endif
  266. }
  267. MemoryPoolStaticMalloc::~MemoryPoolStaticMalloc() {
  268. Mutex *old_mutex=mutex;
  269. mutex=NULL;
  270. if (old_mutex)
  271. memdelete(old_mutex);
  272. #ifdef DEBUG_MEMORY_ENABLED
  273. if (OS::get_singleton()->is_stdout_verbose()) {
  274. if (total_mem > 0 ) {
  275. printf("**ERROR: STATIC ALLOC: ** MEMORY LEAKS DETECTED **\n");
  276. printf("**ERROR: STATIC ALLOC: %i bytes of memory in use at exit.\n",(int)total_mem);
  277. if (1){
  278. printf("**ERROR: STATIC ALLOC: Following is the list of leaked allocations: \n");
  279. ERR_FAIL_COND( !ringlist ); /** WTF BUG !? */
  280. RingPtr *p = ringlist;
  281. do {
  282. printf("\t%p - %i bytes - %s\n", (RingPtr*)(p+1), (int)p->size, (p->descr?p->descr:"") );
  283. p=p->next;
  284. } while (p!=ringlist);
  285. printf("**ERROR: STATIC ALLOC: End of Report.\n");
  286. };
  287. printf("mem - max %i, pointers %i, leaks %i.\n",(int)max_mem,max_pointers,(int)total_mem);
  288. } else {
  289. printf("INFO: mem - max %i, pointers %i, no leaks.\n",(int)max_mem,max_pointers);
  290. }
  291. }
  292. #endif
  293. }