memory_pool_static_nedmalloc.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* memory_pool_static_nedmalloc.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. #ifdef NEDMALLOC_ENABLED
  30. //
  31. // C++ Implementation: memory_static_malloc
  32. //
  33. // Description:
  34. //
  35. //
  36. // Author: Juan Linietsky <red@lunatea>, (C) 2006
  37. //
  38. // Copyright: See COPYING file that comes with this distribution
  39. //
  40. //
  41. #include "memory_pool_static_nedmalloc.h"
  42. #include "error_macros.h"
  43. #include "os/memory.h"
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include "os/copymem.h"
  47. #include "os/os.h"
  48. #include "nedmalloc.h"
  49. /**
  50. * NOTE NOTE NOTE NOTE
  51. * in debug mode, this appends the memory size before the allocated, returned pointer
  52. * so BE CAREFUL!
  53. */
  54. void* MemoryPoolStaticNedMalloc::alloc(size_t p_bytes,const char *p_description) {
  55. ERR_FAIL_COND_V(p_bytes==0,0);
  56. MutexLock lock(mutex);
  57. void *mem=nedalloc::nedmalloc(p_bytes);
  58. ERR_FAIL_COND_V(!mem,0); //out of memory, or unreasonable request
  59. return mem;
  60. }
  61. void* MemoryPoolStaticNedMalloc::realloc(void *p_memory,size_t p_bytes) {
  62. if (p_memory==NULL) {
  63. return alloc( p_bytes );
  64. }
  65. if (p_bytes<=0) {
  66. this->free(p_memory);
  67. ERR_FAIL_COND_V( p_bytes < 0 , NULL );
  68. return NULL;
  69. }
  70. MutexLock lock(mutex);
  71. return nedalloc::nedrealloc( p_memory, p_bytes );
  72. }
  73. void MemoryPoolStaticNedMalloc::free(void *p_ptr) {
  74. MutexLock lock(mutex);
  75. ERR_FAIL_COND(p_ptr==0);
  76. nedalloc::nedfree(p_ptr);
  77. }
  78. size_t MemoryPoolStaticNedMalloc::get_available_mem() const {
  79. return 0xffffffff;
  80. }
  81. size_t MemoryPoolStaticNedMalloc::get_total_usage() {
  82. return nedalloc::nedmalloc_footprint();
  83. }
  84. /* Most likely available only if memory debugger was compiled in */
  85. int MemoryPoolStaticNedMalloc::get_alloc_count() {
  86. return 0;
  87. }
  88. void * MemoryPoolStaticNedMalloc::get_alloc_ptr(int p_alloc_idx) {
  89. return 0;
  90. }
  91. const char* MemoryPoolStaticNedMalloc::get_alloc_description(int p_alloc_idx) {
  92. return "";
  93. }
  94. size_t MemoryPoolStaticNedMalloc::get_alloc_size(int p_alloc_idx) {
  95. return 0;
  96. }
  97. void MemoryPoolStaticNedMalloc::debug_print_all_memory() {
  98. nedalloc::nedmalloc_stats();
  99. }
  100. MemoryPoolStaticNedMalloc::MemoryPoolStaticNedMalloc() {
  101. mutex=NULL;
  102. #ifndef NO_THREADS
  103. mutex=Mutex::create(); // at this point, this should work
  104. #endif
  105. }
  106. MemoryPoolStaticNedMalloc::~MemoryPoolStaticNedMalloc() {
  107. Mutex *old_mutex=mutex;
  108. mutex=NULL;
  109. if (old_mutex)
  110. memdelete(old_mutex);
  111. #ifdef DEBUG_ENABLED
  112. if (OS::get_singleton()->is_stdout_verbose())
  113. nedalloc::nedmalloc_stats();
  114. #endif
  115. }
  116. #endif