memory.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* memory.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 <godot_cpp/core/memory.hpp>
  31. #include <godot_cpp/godot.hpp>
  32. namespace godot {
  33. void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
  34. #ifdef DEBUG_ENABLED
  35. bool prepad = false; // Already pre paded in the engine.
  36. #else
  37. bool prepad = p_pad_align;
  38. #endif
  39. void *mem = internal::gdextension_interface_mem_alloc(p_bytes + (prepad ? DATA_OFFSET : 0));
  40. ERR_FAIL_NULL_V(mem, nullptr);
  41. if (prepad) {
  42. uint8_t *s8 = (uint8_t *)mem;
  43. return s8 + DATA_OFFSET;
  44. } else {
  45. return mem;
  46. }
  47. }
  48. void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
  49. if (p_memory == nullptr) {
  50. return alloc_static(p_bytes, p_pad_align);
  51. } else if (p_bytes == 0) {
  52. free_static(p_memory, p_pad_align);
  53. return nullptr;
  54. }
  55. uint8_t *mem = (uint8_t *)p_memory;
  56. #ifdef DEBUG_ENABLED
  57. bool prepad = false; // Already pre paded in the engine.
  58. #else
  59. bool prepad = p_pad_align;
  60. #endif
  61. if (prepad) {
  62. mem -= DATA_OFFSET;
  63. mem = (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes + DATA_OFFSET);
  64. ERR_FAIL_NULL_V(mem, nullptr);
  65. return mem + DATA_OFFSET;
  66. } else {
  67. return (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes);
  68. }
  69. }
  70. void Memory::free_static(void *p_ptr, bool p_pad_align) {
  71. uint8_t *mem = (uint8_t *)p_ptr;
  72. #ifdef DEBUG_ENABLED
  73. bool prepad = false; // Already pre paded in the engine.
  74. #else
  75. bool prepad = p_pad_align;
  76. #endif
  77. if (prepad) {
  78. mem -= DATA_OFFSET;
  79. }
  80. internal::gdextension_interface_mem_free(mem);
  81. }
  82. _GlobalNil::_GlobalNil() {
  83. left = this;
  84. right = this;
  85. parent = this;
  86. }
  87. _GlobalNil _GlobalNilClass::_nil;
  88. } // namespace godot
  89. void *operator new(size_t p_size, const char *p_description) {
  90. return godot::Memory::alloc_static(p_size);
  91. }
  92. void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) {
  93. return p_allocfunc(p_size);
  94. }
  95. using namespace godot;
  96. #ifdef _MSC_VER
  97. void operator delete(void *p_mem, const char *p_description) {
  98. ERR_PRINT("Call to placement delete should not happen.");
  99. CRASH_NOW();
  100. }
  101. void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) {
  102. ERR_PRINT("Call to placement delete should not happen.");
  103. CRASH_NOW();
  104. }
  105. void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) {
  106. ERR_PRINT("Call to placement delete should not happen.");
  107. CRASH_NOW();
  108. }
  109. #endif