memory.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* memory.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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.h"
  30. #include "error_macros.h"
  31. #include "copymem.h"
  32. #include <stdio.h>
  33. void * operator new(size_t p_size,const char *p_description) {
  34. return Memory::alloc_static( p_size, p_description );
  35. }
  36. void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)) {
  37. return p_allocfunc(p_size);
  38. }
  39. #include <stdio.h>
  40. void * Memory::alloc_static(size_t p_bytes,const char *p_alloc_from) {
  41. ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL );
  42. return MemoryPoolStatic::get_singleton()->alloc(p_bytes,p_alloc_from);
  43. }
  44. void * Memory::realloc_static(void *p_memory,size_t p_bytes) {
  45. ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL );
  46. return MemoryPoolStatic::get_singleton()->realloc(p_memory,p_bytes);
  47. }
  48. void Memory::free_static(void *p_ptr) {
  49. ERR_FAIL_COND( !MemoryPoolStatic::get_singleton());
  50. MemoryPoolStatic::get_singleton()->free(p_ptr);
  51. }
  52. size_t Memory::get_static_mem_available() {
  53. ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0);
  54. return MemoryPoolStatic::get_singleton()->get_available_mem();
  55. }
  56. size_t Memory::get_static_mem_max_usage() {
  57. ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0);
  58. return MemoryPoolStatic::get_singleton()->get_max_usage();
  59. }
  60. size_t Memory::get_static_mem_usage() {
  61. ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0);
  62. return MemoryPoolStatic::get_singleton()->get_total_usage();
  63. }
  64. void Memory::dump_static_mem_to_file(const char* p_file) {
  65. MemoryPoolStatic::get_singleton()->dump_mem_to_file(p_file);
  66. }
  67. MID Memory::alloc_dynamic(size_t p_bytes, const char *p_descr) {
  68. MemoryPoolDynamic::ID id = MemoryPoolDynamic::get_singleton()->alloc(p_bytes,p_descr);
  69. return MID(id);
  70. }
  71. Error Memory::realloc_dynamic(MID p_mid,size_t p_bytes) {
  72. MemoryPoolDynamic::ID id = p_mid.data?p_mid.data->id:MemoryPoolDynamic::INVALID_ID;
  73. if (id==MemoryPoolDynamic::INVALID_ID)
  74. return ERR_INVALID_PARAMETER;
  75. return MemoryPoolDynamic::get_singleton()->realloc(p_mid, p_bytes);
  76. }
  77. size_t Memory::get_dynamic_mem_available() {
  78. return MemoryPoolDynamic::get_singleton()->get_available_mem();
  79. }
  80. size_t Memory::get_dynamic_mem_usage() {
  81. return MemoryPoolDynamic::get_singleton()->get_total_usage();
  82. }
  83. void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description) {
  84. void *failptr=0;
  85. ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */
  86. return p_pointer;
  87. }
  88. _GlobalNil::_GlobalNil() {
  89. color=1;
  90. left=this;
  91. right=this;
  92. parent=this;
  93. }
  94. _GlobalNil _GlobalNilClass::_nil;