diff options
| -rw-r--r-- | .github/workflows/ci.yml | 10 | ||||
| -rw-r--r-- | README.md | 45 | ||||
| -rw-r--r-- | gdextension/extension_api.json | 521 | ||||
| -rw-r--r-- | include/godot_cpp/core/memory.hpp | 14 | ||||
| -rw-r--r-- | include/godot_cpp/core/method_ptrcall.hpp | 8 | ||||
| -rw-r--r-- | include/godot_cpp/core/object.hpp | 4 | ||||
| -rw-r--r-- | include/godot_cpp/templates/cowdata.hpp | 10 | ||||
| -rw-r--r-- | src/core/memory.cpp | 59 |
8 files changed, 331 insertions, 340 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4215a9c..062cefa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,14 +18,14 @@ jobs: matrix: include: - name: 🐧 Linux (GCC) - os: ubuntu-18.04 + os: ubuntu-20.04 platform: linux artifact-name: godot-cpp-linux-glibc2.27-x86_64-release artifact-path: bin/libgodot-cpp.linux.template_release.x86_64.a cache-name: linux-x86_64 - name: 🐧 Linux (GCC, Double Precision) - os: ubuntu-18.04 + os: ubuntu-20.04 platform: linux artifact-name: godot-cpp-linux-glibc2.27-x86_64-double-release artifact-path: bin/libgodot-cpp.linux.template_release.double.x86_64.a @@ -56,7 +56,7 @@ jobs: cache-name: macos-universal - name: 🤖 Android (arm64) - os: ubuntu-18.04 + os: ubuntu-20.04 platform: android artifact-name: godot-cpp-android-arm64-release artifact-path: bin/libgodot-cpp.android.template_release.arm64.a @@ -133,7 +133,7 @@ jobs: linux-cmake: name: 🐧 Build (Linux, GCC, CMake) - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - name: Checkout uses: actions/checkout@v3 @@ -157,7 +157,7 @@ jobs: linux-cmake-ninja: name: 🐧 Build (Linux, GCC, CMake Ninja) - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - name: Checkout uses: actions/checkout@v3 @@ -2,28 +2,30 @@ > **Warning** > -> This repository's `master` branch is only usable with Godot 4.0 ([GDExtension](https://godotengine.org/article/introducing-gd-extensions)). +> This repository's `master` branch is only usable with Godot's ([GDExtension](https://godotengine.org/article/introducing-gd-extensions)) +> API (Godot 4.0 and later). > -> Switch to the [`3.5` branch](https://github.com/godotengine/godot-cpp/tree/3.5) -> if using Godot 3.5.x with GDNative, -> or the [`3.x` branch](https://github.com/godotengine/godot-cpp/tree/3.x) -> if using the Godot 3.x development branch with GDNative. +> For GDNative users (Godot 3.x), switch to the [`3.x`](https://github.com/godotengine/godot-cpp/tree/3.x) +> or the [`3.5`](https://github.com/godotengine/godot-cpp/tree/3.5) branch. This repository contains the *C++ bindings* for the [**Godot Engine**](https://github.com/godotengine/godot)'s GDExtensions API. - [**Versioning**](#versioning) +- [**Compatibility**](#compatibility) - [**Contributing**](#contributing) -- [**Getting Started**](#getting-started) -- [**Included Example**](#included-example) +- [**Getting started**](#getting-started) +- [**Included example**](#included-example) ## Versioning This repositories follows the same branch versioning as the main [Godot Engine repository](https://github.com/godotengine/godot): -- `master` tracks the current development branch. -- `3.x` tracks the development of the next 3.x minor release. -- Other versioned branches (e.g. `3.3`, `3.2`) track the latest stable release +- `master` tracks the current GDExtension development branch for the next Godot + 4.x minor release. +- `3.x` tracks the development of the GDNative plugin for the next 3.x minor + release. +- Other versioned branches (e.g. `4.0`, `3.5`) track the latest stable release in the corresponding branch. Stable releases are also tagged on this repository: @@ -37,18 +39,31 @@ Godot version.** > using `godot-cpp` against a more current version of Godot, see the instructions > in the `gdextension` folder to update the relevant files. +## Compatibility + +**Warning:** The GDExtension API is brand new in Godot 4.0, and is still +considered in **beta** stage, despite Godot 4.0 itself being released. + +This applies to both the GDExtension interface header, the API JSON, and this +first-party `godot-cpp` extension. + +Some compatibility breakage is to be expected as GDExtension and `godot-cpp` +get more used, documented, and critical issues get resolved. See the +[issue tracker](https://github.com/godotengine/godot/issues) for a list of known +issues, and be sure to provide feedback on issues and PRs which affect your use +of this extension. + ## Contributing We greatly appreciate help in maintaining and extending this project. If you wish to help out, ensure you have an account on GitHub and create a "fork" of -this repository. Rémi "Akien" Verschelde wrote an excellent bit of documentation -for the main Godot project on this: -[Pull request workflow](https://docs.godotengine.org/en/stable/community/contributing/pr_workflow.html) +this repository. See [Pull request workflow](https://docs.godotengine.org/en/stable/community/contributing/pr_workflow.html) +for instructions. Please install clang-format and copy the files in `misc/hooks` into `.git/hooks` so formatting is done before your changes are submitted. -## Getting Started +## Getting started It's a bit similar to what it was for 3.x but also a bit different. This new approach is much more akin to how core Godot modules are structured. @@ -110,6 +125,6 @@ void initialize_example_module(ModuleInitializationLevel p_level) { Any node and resource you register will be available in the corresponding `Create...` dialog. Any class will be available to scripting as well. -## Included Example +## Included example Check the project in the `test` folder for an example on how to use and register different things. diff --git a/gdextension/extension_api.json b/gdextension/extension_api.json index 5a22648..eae10aa 100644 --- a/gdextension/extension_api.json +++ b/gdextension/extension_api.json @@ -3,9 +3,9 @@ "version_major": 4, "version_minor": 0, "version_patch": 0, - "version_status": "rc2", + "version_status": "stable", "version_build": "official", - "version_full_name": "Godot Engine v4.0.rc2.official" + "version_full_name": "Godot Engine v4.0.stable.official" }, "builtin_class_sizes": [ { @@ -45859,33 +45859,6 @@ } }, { - "name": "set_default_length", - "is_const": false, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 373806689, - "arguments": [ - { - "name": "default_length", - "type": "float", - "meta": "float" - } - ] - }, - { - "name": "get_default_length", - "is_const": true, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 1740695150, - "return_value": { - "type": "float", - "meta": "float" - } - }, - { "name": "set_autocalculate_length_and_angle", "is_const": false, "is_vararg": false, @@ -77802,6 +77775,31 @@ ] }, { + "name": "set_filters", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 4015028928, + "arguments": [ + { + "name": "filters", + "type": "PackedStringArray" + } + ] + }, + { + "name": "get_filters", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 1139954409, + "return_value": { + "type": "PackedStringArray" + } + }, + { "name": "get_current_dir", "is_const": true, "is_vararg": false, @@ -77913,6 +77911,17 @@ } }, { + "name": "get_line_edit", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 4071694264, + "return_value": { + "type": "LineEdit" + } + }, + { "name": "set_access", "is_const": false, "is_vararg": false, @@ -78088,6 +78097,12 @@ "getter": "get_current_path" }, { + "type": "PackedStringArray", + "name": "filters", + "setter": "set_filters", + "getter": "get_filters" + }, + { "type": "bool", "name": "show_hidden_files", "setter": "set_show_hidden_files", @@ -78868,7 +78883,7 @@ "arguments": [ { "name": "object", - "type": "Variant" + "type": "Object" } ] }, @@ -80857,7 +80872,7 @@ "arguments": [ { "name": "object", - "type": "Variant" + "type": "Object" } ] }, @@ -80873,7 +80888,7 @@ "arguments": [ { "name": "object", - "type": "Variant" + "type": "Object" } ] }, @@ -90065,6 +90080,14 @@ } }, { + "name": "close", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3218959716 + }, + { "name": "file_exists", "is_const": false, "is_vararg": false, @@ -96245,31 +96268,6 @@ ] }, { - "name": "get_joint", - "is_const": false, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 2240911060, - "return_value": { - "type": "bool" - } - }, - { - "name": "set_joint", - "is_const": false, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 2586408642, - "arguments": [ - { - "name": "joint", - "type": "bool" - } - ] - }, - { "name": "get_position", "is_const": false, "is_vararg": false, @@ -96476,12 +96474,6 @@ "getter": "get_skeleton" }, { - "type": "bool", - "name": "joint", - "setter": "set_joint", - "getter": "get_joint" - }, - { "type": "Vector3", "name": "position", "setter": "set_position", @@ -97842,31 +97834,6 @@ ] }, { - "name": "get_skeleton_to_node", - "is_const": false, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 2382534195, - "return_value": { - "type": "Dictionary" - } - }, - { - "name": "set_skeleton_to_node", - "is_const": false, - "is_vararg": false, - "is_static": false, - "is_virtual": false, - "hash": 4155329257, - "arguments": [ - { - "name": "skeleton_to_node", - "type": "Dictionary" - } - ] - }, - { "name": "get_create_animations", "is_const": false, "is_vararg": false, @@ -98137,12 +98104,6 @@ "getter": "get_skeletons" }, { - "type": "Dictionary", - "name": "skeleton_to_node", - "setter": "set_skeleton_to_node", - "getter": "get_skeleton_to_node" - }, - { "type": "bool", "name": "create_animations", "setter": "set_create_animations", @@ -117093,7 +117054,7 @@ "hash": 1286410249, "arguments": [ { - "name": "visible_lines", + "name": "lines_visible", "type": "int", "meta": "int32" } @@ -128504,6 +128465,9 @@ ], "signals": [ { + "name": "synchronized" + }, + { "name": "visibility_changed", "arguments": [ { @@ -130357,6 +130321,56 @@ } }, { + "name": "set_global_start_position", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 743155724, + "arguments": [ + { + "name": "position", + "type": "Vector2" + } + ] + }, + { + "name": "get_global_start_position", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3341600327, + "return_value": { + "type": "Vector2" + } + }, + { + "name": "set_global_end_position", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 743155724, + "arguments": [ + { + "name": "position", + "type": "Vector2" + } + ] + }, + { + "name": "get_global_end_position", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3341600327, + "return_value": { + "type": "Vector2" + } + }, + { "name": "set_enter_cost", "is_const": false, "is_vararg": false, @@ -130628,6 +130642,56 @@ } }, { + "name": "set_global_start_position", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3460891852, + "arguments": [ + { + "name": "position", + "type": "Vector3" + } + ] + }, + { + "name": "get_global_start_position", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3360562783, + "return_value": { + "type": "Vector3" + } + }, + { + "name": "set_global_end_position", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3460891852, + "arguments": [ + { + "name": "position", + "type": "Vector3" + } + ] + }, + { + "name": "get_global_end_position", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 3360562783, + "return_value": { + "type": "Vector3" + } + }, + { "name": "set_enter_cost", "is_const": false, "is_vararg": false, @@ -134715,6 +134779,31 @@ "type": "RID" } ] + }, + { + "name": "set_debug_enabled", + "is_const": false, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 2586408642, + "arguments": [ + { + "name": "enabled", + "type": "bool" + } + ] + }, + { + "name": "get_debug_enabled", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 36873697, + "return_value": { + "type": "bool" + } } ], "signals": [ @@ -136086,21 +136175,31 @@ ] }, { - "name": "process", + "name": "set_debug_enabled", "is_const": false, "is_vararg": false, "is_static": false, "is_virtual": false, - "hash": 373806689, + "hash": 2586408642, "arguments": [ { - "name": "delta_time", - "type": "float", - "meta": "float" + "name": "enabled", + "type": "bool" } ] }, { + "name": "get_debug_enabled", + "is_const": true, + "is_vararg": false, + "is_static": false, + "is_virtual": false, + "hash": 36873697, + "return_value": { + "type": "bool" + } + }, + { "name": "get_process_info", "is_const": true, "is_vararg": false, @@ -195207,120 +195306,6 @@ ] }, { - "name": "ItemType", - "is_bitfield": false, - "values": [ - { - "name": "ITEM_FRAME", - "value": 0 - }, - { - "name": "ITEM_TEXT", - "value": 1 - }, - { - "name": "ITEM_IMAGE", - "value": 2 - }, - { - "name": "ITEM_NEWLINE", - "value": 3 - }, - { - "name": "ITEM_FONT", - "value": 4 - }, - { - "name": "ITEM_FONT_SIZE", - "value": 5 - }, - { - "name": "ITEM_FONT_FEATURES", - "value": 6 - }, - { - "name": "ITEM_COLOR", - "value": 7 - }, - { - "name": "ITEM_OUTLINE_SIZE", - "value": 8 - }, - { - "name": "ITEM_OUTLINE_COLOR", - "value": 9 - }, - { - "name": "ITEM_UNDERLINE", - "value": 10 - }, - { - "name": "ITEM_STRIKETHROUGH", - "value": 11 - }, - { - "name": "ITEM_PARAGRAPH", - "value": 12 - }, - { - "name": "ITEM_INDENT", - "value": 13 - }, - { - "name": "ITEM_LIST", - "value": 14 - }, - { - "name": "ITEM_TABLE", - "value": 15 - }, - { - "name": "ITEM_FADE", - "value": 16 - }, - { - "name": "ITEM_SHAKE", - "value": 17 - }, - { - "name": "ITEM_WAVE", - "value": 18 - }, - { - "name": "ITEM_TORNADO", - "value": 19 - }, - { - "name": "ITEM_RAINBOW", - "value": 20 - }, - { - "name": "ITEM_BGCOLOR", - "value": 21 - }, - { - "name": "ITEM_FGCOLOR", - "value": 22 - }, - { - "name": "ITEM_META", - "value": 23 - }, - { - "name": "ITEM_HINT", - "value": 24 - }, - { - "name": "ITEM_DROPCAP", - "value": 25 - }, - { - "name": "ITEM_CUSTOMFX", - "value": 26 - } - ] - }, - { "name": "MenuItems", "is_bitfield": false, "values": [ @@ -201567,6 +201552,16 @@ } }, { + "name": "_get_global_name", + "is_const": true, + "is_static": false, + "is_vararg": false, + "is_virtual": true, + "return_value": { + "type": "StringName" + } + }, + { "name": "_inherits_script", "is_const": true, "is_static": false, @@ -202077,22 +202072,6 @@ } }, { - "name": "_execute_file", - "is_const": false, - "is_static": false, - "is_vararg": false, - "is_virtual": true, - "return_value": { - "type": "enum::Error" - }, - "arguments": [ - { - "name": "path", - "type": "String" - } - ] - }, - { "name": "_finish", "is_const": false, "is_static": false, @@ -202830,64 +202809,6 @@ ] }, { - "name": "_alloc_instance_binding_data", - "is_const": false, - "is_static": false, - "is_vararg": false, - "is_virtual": true, - "return_value": { - "type": "void*" - }, - "arguments": [ - { - "name": "object", - "type": "Object" - } - ] - }, - { - "name": "_free_instance_binding_data", - "is_const": false, - "is_static": false, - "is_vararg": false, - "is_virtual": true, - "arguments": [ - { - "name": "data", - "type": "void*" - } - ] - }, - { - "name": "_refcount_incremented_instance_binding", - "is_const": false, - "is_static": false, - "is_vararg": false, - "is_virtual": true, - "arguments": [ - { - "name": "object", - "type": "Object" - } - ] - }, - { - "name": "_refcount_decremented_instance_binding", - "is_const": false, - "is_static": false, - "is_vararg": false, - "is_virtual": true, - "return_value": { - "type": "bool" - }, - "arguments": [ - { - "name": "object", - "type": "Object" - } - ] - }, - { "name": "_frame", "is_const": false, "is_static": false, @@ -204927,11 +204848,11 @@ "is_vararg": false, "is_static": false, "is_virtual": false, - "hash": 3975164845, + "hash": 1976431078, "arguments": [ { "name": "node", - "type": "Object" + "type": "CollisionObject3D" } ] }, @@ -204955,11 +204876,11 @@ "is_vararg": false, "is_static": false, "is_virtual": false, - "hash": 3975164845, + "hash": 1976431078, "arguments": [ { "name": "node", - "type": "Object" + "type": "CollisionObject3D" } ] }, @@ -228181,7 +228102,7 @@ { "name": "baseline", "type": "float", - "meta": "float", + "meta": "double", "default_value": "0.0" } ] @@ -228217,7 +228138,7 @@ { "name": "baseline", "type": "float", - "meta": "float", + "meta": "double", "default_value": "0.0" } ] @@ -229184,7 +229105,7 @@ { "name": "chars_per_line", "type": "int", - "meta": "int32", + "meta": "int64", "default_value": "0" } ] @@ -229736,7 +229657,7 @@ { "name": "weight", "type": "int", - "meta": "int32" + "meta": "int64" } ] }, @@ -229771,7 +229692,7 @@ { "name": "stretch", "type": "int", - "meta": "int32" + "meta": "int64" } ] }, @@ -232076,7 +231997,7 @@ { "name": "baseline", "type": "float", - "meta": "float" + "meta": "double" } ] }, @@ -232109,7 +232030,7 @@ { "name": "baseline", "type": "float", - "meta": "float" + "meta": "double" } ] }, @@ -233077,7 +232998,7 @@ { "name": "chars_per_line", "type": "int", - "meta": "int32" + "meta": "int64" } ] }, diff --git a/include/godot_cpp/core/memory.hpp b/include/godot_cpp/core/memory.hpp index 25f87ec..55cdb8b 100644 --- a/include/godot_cpp/core/memory.hpp +++ b/include/godot_cpp/core/memory.hpp @@ -40,6 +40,10 @@ #include <type_traits> +#ifndef PAD_ALIGN +#define PAD_ALIGN 16 //must always be greater than this at much +#endif + void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory @@ -64,9 +68,9 @@ class Memory { Memory(); public: - static void *alloc_static(size_t p_bytes); - static void *realloc_static(void *p_memory, size_t p_bytes); - static void free_static(void *p_ptr); + static void *alloc_static(size_t p_bytes, bool p_pad_align = false); + static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false); + static void free_static(void *p_ptr, bool p_pad_align = false); }; _ALWAYS_INLINE_ void postinitialize_handler(void *) {} @@ -140,7 +144,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") { same strategy used by std::vector, and the Vector class, so it should be safe.*/ size_t len = sizeof(T) * p_elements; - uint64_t *mem = (uint64_t *)Memory::alloc_static(len); + uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true); T *failptr = nullptr; // Get rid of a warning. ERR_FAIL_COND_V(!mem, failptr); *(mem - 1) = p_elements; @@ -169,7 +173,7 @@ void memdelete_arr(T *p_class) { } } - Memory::free_static(ptr); + Memory::free_static(ptr, true); } struct _GlobalNil { diff --git a/include/godot_cpp/core/method_ptrcall.hpp b/include/godot_cpp/core/method_ptrcall.hpp index 6b092bb..6a5227a 100644 --- a/include/godot_cpp/core/method_ptrcall.hpp +++ b/include/godot_cpp/core/method_ptrcall.hpp @@ -168,7 +168,9 @@ MAKE_PTRARG_BY_REFERENCE(Variant); template <class T> struct PtrToArg<T *> { _FORCE_INLINE_ static T *convert(const void *p_ptr) { - return reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks)); + return reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding( + reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), + godot::internal::token, &T::___binding_callbacks)); } typedef Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { @@ -179,7 +181,9 @@ struct PtrToArg<T *> { template <class T> struct PtrToArg<const T *> { _FORCE_INLINE_ static const T *convert(const void *p_ptr) { - return reinterpret_cast<const T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks)); + return reinterpret_cast<const T *>(godot::internal::gde_interface->object_get_instance_binding( + reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), + godot::internal::token, &T::___binding_callbacks)); } typedef const Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index a2fa96e..f5c790a 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -142,7 +142,7 @@ T *Object::cast_to(Object *p_object) { if (casted == nullptr) { return nullptr; } - return dynamic_cast<T *>((Object *)internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); + return reinterpret_cast<T *>(internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); } template <class T> @@ -155,7 +155,7 @@ const T *Object::cast_to(const Object *p_object) { if (casted == nullptr) { return nullptr; } - return dynamic_cast<const T *>((Object *)internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); + return reinterpret_cast<const T *>(internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); } } // namespace godot diff --git a/include/godot_cpp/templates/cowdata.hpp b/include/godot_cpp/templates/cowdata.hpp index 38f9277..1753687 100644 --- a/include/godot_cpp/templates/cowdata.hpp +++ b/include/godot_cpp/templates/cowdata.hpp @@ -217,7 +217,7 @@ void CowData<T>::_unref(void *p_data) { } // free mem - Memory::free_static((uint8_t *)p_data); + Memory::free_static((uint8_t *)p_data, true); } template <class T> @@ -233,7 +233,7 @@ uint32_t CowData<T>::_copy_on_write() { /* in use by more than me */ uint32_t current_size = *_get_size(); - uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size)); + uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); new (mem_new - 2) SafeNumeric<uint32_t>(1); // refcount *(mem_new - 1) = current_size; // size @@ -286,7 +286,7 @@ Error CowData<T>::resize(int p_size) { if (alloc_size != current_alloc_size) { if (current_size == 0) { // alloc from scratch - uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size); + uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY); *(ptr - 1) = 0; // size, currently none new (ptr - 2) SafeNumeric<uint32_t>(1); // refcount @@ -294,7 +294,7 @@ Error CowData<T>::resize(int p_size) { _ptr = (T *)ptr; } else { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size); + uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount @@ -324,7 +324,7 @@ Error CowData<T>::resize(int p_size) { } if (alloc_size != current_alloc_size) { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size); + uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount diff --git a/src/core/memory.cpp b/src/core/memory.cpp index d1d7b59..8feda57 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -34,16 +34,63 @@ namespace godot { -void *Memory::alloc_static(size_t p_bytes) { - return internal::gde_interface->mem_alloc(p_bytes); +void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { +#ifdef DEBUG_ENABLED + bool prepad = false; // Alredy pre paded in the engine. +#else + bool prepad = p_pad_align; +#endif + + void *mem = internal::gde_interface->mem_alloc(p_bytes + (prepad ? PAD_ALIGN : 0)); + ERR_FAIL_COND_V(!mem, nullptr); + + if (prepad) { + uint8_t *s8 = (uint8_t *)mem; + return s8 + PAD_ALIGN; + } else { + return mem; + } } -void *Memory::realloc_static(void *p_memory, size_t p_bytes) { - return internal::gde_interface->mem_realloc(p_memory, p_bytes); +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 + bool prepad = false; // Alredy pre paded in the engine. +#else + bool prepad = p_pad_align; +#endif + + if (prepad) { + mem -= PAD_ALIGN; + mem = (uint8_t *)internal::gde_interface->mem_realloc(mem, p_bytes + PAD_ALIGN); + ERR_FAIL_COND_V(!mem, nullptr); + return mem + PAD_ALIGN; + } else { + return (uint8_t *)internal::gde_interface->mem_realloc(mem, p_bytes); + } } -void Memory::free_static(void *p_ptr) { - internal::gde_interface->mem_free(p_ptr); +void Memory::free_static(void *p_ptr, bool p_pad_align) { + uint8_t *mem = (uint8_t *)p_ptr; + +#ifdef DEBUG_ENABLED + bool prepad = false; // Alredy pre paded in the engine. +#else + bool prepad = p_pad_align; +#endif + + if (prepad) { + mem -= PAD_ALIGN; + } + internal::gde_interface->mem_free(mem); } _GlobalNil::_GlobalNil() { |
