memory.cpp 4.4 KB

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