2023-01-10 15:14:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* memory.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-08-02 16:34:47 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
#include <godot_cpp/core/memory.hpp>
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
#include <godot_cpp/godot.hpp>
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
2023-02-21 10:47:40 +00:00
|
|
|
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
2024-06-24 17:20:55 +00:00
|
|
|
bool prepad = false; // Already pre paded in the engine.
|
2023-02-21 10:47:40 +00:00
|
|
|
#else
|
|
|
|
bool prepad = p_pad_align;
|
|
|
|
#endif
|
|
|
|
|
2024-02-05 17:26:45 +00:00
|
|
|
void *mem = internal::gdextension_interface_mem_alloc(p_bytes + (prepad ? DATA_OFFSET : 0));
|
2023-09-12 13:33:49 +00:00
|
|
|
ERR_FAIL_NULL_V(mem, nullptr);
|
2023-02-21 10:47:40 +00:00
|
|
|
|
|
|
|
if (prepad) {
|
|
|
|
uint8_t *s8 = (uint8_t *)mem;
|
2024-02-05 17:26:45 +00:00
|
|
|
return s8 + DATA_OFFSET;
|
2023-02-21 10:47:40 +00:00
|
|
|
} else {
|
|
|
|
return mem;
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2023-02-21 10:47:40 +00:00
|
|
|
void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
|
|
|
|
if (p_memory == nullptr) {
|
|
|
|
return alloc_static(p_bytes, p_pad_align);
|
|
|
|
} else if (p_bytes == 0) {
|
|
|
|
free_static(p_memory, p_pad_align);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *mem = (uint8_t *)p_memory;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2024-06-24 17:20:55 +00:00
|
|
|
bool prepad = false; // Already pre paded in the engine.
|
2023-02-21 10:47:40 +00:00
|
|
|
#else
|
|
|
|
bool prepad = p_pad_align;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (prepad) {
|
2024-02-05 17:26:45 +00:00
|
|
|
mem -= DATA_OFFSET;
|
|
|
|
mem = (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes + DATA_OFFSET);
|
2023-09-12 13:33:49 +00:00
|
|
|
ERR_FAIL_NULL_V(mem, nullptr);
|
2024-02-05 17:26:45 +00:00
|
|
|
return mem + DATA_OFFSET;
|
2023-02-21 10:47:40 +00:00
|
|
|
} else {
|
2023-04-24 16:45:45 +00:00
|
|
|
return (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes);
|
2023-02-21 10:47:40 +00:00
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2023-02-21 10:47:40 +00:00
|
|
|
void Memory::free_static(void *p_ptr, bool p_pad_align) {
|
|
|
|
uint8_t *mem = (uint8_t *)p_ptr;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2024-06-24 17:20:55 +00:00
|
|
|
bool prepad = false; // Already pre paded in the engine.
|
2023-02-21 10:47:40 +00:00
|
|
|
#else
|
|
|
|
bool prepad = p_pad_align;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (prepad) {
|
2024-02-05 17:26:45 +00:00
|
|
|
mem -= DATA_OFFSET;
|
2023-02-21 10:47:40 +00:00
|
|
|
}
|
2023-04-24 16:45:45 +00:00
|
|
|
internal::gdextension_interface_mem_free(mem);
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2022-02-15 10:58:28 +00:00
|
|
|
_GlobalNil::_GlobalNil() {
|
|
|
|
left = this;
|
|
|
|
right = this;
|
|
|
|
parent = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_GlobalNil _GlobalNilClass::_nil;
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
} // namespace godot
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2023-11-05 21:04:03 +00:00
|
|
|
// p_dummy argument is added to avoid conflicts with the engine functions when both engine and GDExtension are built as a static library on iOS.
|
|
|
|
void *operator new(size_t p_size, const char *p_dummy, const char *p_description) {
|
2021-08-18 14:03:52 +00:00
|
|
|
return godot::Memory::alloc_static(p_size);
|
|
|
|
}
|
2021-08-28 04:59:09 +00:00
|
|
|
|
2023-11-05 21:04:03 +00:00
|
|
|
void *operator new(size_t p_size, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)) {
|
2022-02-15 10:58:28 +00:00
|
|
|
return p_allocfunc(p_size);
|
|
|
|
}
|
|
|
|
|
2021-08-28 04:59:09 +00:00
|
|
|
using namespace godot;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2023-11-05 21:04:03 +00:00
|
|
|
void operator delete(void *p_mem, const char *p_dummy, const char *p_description) {
|
2021-08-28 04:59:09 +00:00
|
|
|
ERR_PRINT("Call to placement delete should not happen.");
|
|
|
|
CRASH_NOW();
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:04:03 +00:00
|
|
|
void operator delete(void *p_mem, const char *p_dummy, void *(*p_allocfunc)(size_t p_size)) {
|
2021-08-28 04:59:09 +00:00
|
|
|
ERR_PRINT("Call to placement delete should not happen.");
|
|
|
|
CRASH_NOW();
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:04:03 +00:00
|
|
|
void operator delete(void *p_mem, const char *p_dummy, void *p_pointer, size_t check, const char *p_description) {
|
2021-08-28 04:59:09 +00:00
|
|
|
ERR_PRINT("Call to placement delete should not happen.");
|
|
|
|
CRASH_NOW();
|
|
|
|
}
|
2022-02-15 10:58:28 +00:00
|
|
|
|
2021-08-28 04:59:09 +00:00
|
|
|
#endif
|