From 477f64ada1135311fd1353c1291d83b1a4e81aee Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 17 Jul 2025 16:27:27 -0500 Subject: [PATCH] Update to use `mem_alloc2`, `mem_realloc2` and `mem_free2` --- gdextension/gdextension_interface.h | 43 ++++++++++++++++++++++++++++- include/godot_cpp/godot.hpp | 6 ++-- src/core/memory.cpp | 33 +++++----------------- src/godot.cpp | 12 ++++---- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/gdextension/gdextension_interface.h b/gdextension/gdextension_interface.h index 34849a671..c8a343a61 100644 --- a/gdextension/gdextension_interface.h +++ b/gdextension/gdextension_interface.h @@ -856,6 +856,7 @@ typedef void (*GDExtensionInterfaceGetGodotVersion2)(GDExtensionGodotVersion2 *r /** * @name mem_alloc * @since 4.1 + * @deprecated in Godot 4.6. Use `mem_alloc2` instead. * * Allocates memory. * @@ -868,6 +869,7 @@ typedef void *(*GDExtensionInterfaceMemAlloc)(size_t p_bytes); /** * @name mem_realloc * @since 4.1 + * @deprecated in Godot 4.6. Use `mem_realloc2` instead. * * Reallocates memory. * @@ -881,6 +883,7 @@ typedef void *(*GDExtensionInterfaceMemRealloc)(void *p_ptr, size_t p_bytes); /** * @name mem_free * @since 4.1 + * @deprecated in Godot 4.6. Use `mem_free2` instead. * * Frees memory. * @@ -888,7 +891,45 @@ typedef void *(*GDExtensionInterfaceMemRealloc)(void *p_ptr, size_t p_bytes); */ typedef void (*GDExtensionInterfaceMemFree)(void *p_ptr); -/* INTERFACE: Godot Core */ +/** + * @name mem_alloc2 + * @since 4.6 + * + * Allocates memory. + * + * @param p_bytes The amount of memory to allocate in bytes. + * @param p_pad_align If true, the returned memory will have prepadding of at least 8 bytes. + * + * @return A pointer to the allocated memory, or NULL if unsuccessful. + */ +typedef void *(*GDExtensionInterfaceMemAlloc2)(size_t p_bytes, GDExtensionBool p_pad_align); + +/** + * @name mem_realloc2 + * @since 4.6 + * + * Reallocates memory. + * + * @param p_ptr A pointer to the previously allocated memory. + * @param p_bytes The number of bytes to resize the memory block to. + * @param p_pad_align If true, the returned memory will have prepadding of at least 8 bytes. + * + * @return A pointer to the allocated memory, or NULL if unsuccessful. + */ +typedef void *(*GDExtensionInterfaceMemRealloc2)(void *p_ptr, size_t p_bytes, GDExtensionBool p_pad_align); + +/** + * @name mem_free2 + * @since 4.6 + * + * Frees memory. + * + * @param p_ptr A pointer to the previously allocated memory. + * @param p_pad_align If true, the given memory was allocated with prepadding. + */ +typedef void (*GDExtensionInterfaceMemFree2)(void *p_ptr, GDExtensionBool p_pad_align); + +//* INTERFACE: Godot Core */ /** * @name print_error diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp index cf64b7e7d..87d899634 100644 --- a/include/godot_cpp/godot.hpp +++ b/include/godot_cpp/godot.hpp @@ -44,9 +44,9 @@ extern "C" GDExtensionGodotVersion2 godot_version; // All of the GDExtension interface functions. extern "C" GDExtensionInterfaceGetGodotVersion2 gdextension_interface_get_godot_version2; -extern "C" GDExtensionInterfaceMemAlloc gdextension_interface_mem_alloc; -extern "C" GDExtensionInterfaceMemRealloc gdextension_interface_mem_realloc; -extern "C" GDExtensionInterfaceMemFree gdextension_interface_mem_free; +extern "C" GDExtensionInterfaceMemAlloc2 gdextension_interface_mem_alloc2; +extern "C" GDExtensionInterfaceMemRealloc2 gdextension_interface_mem_realloc2; +extern "C" GDExtensionInterfaceMemFree2 gdextension_interface_mem_free2; extern "C" GDExtensionInterfacePrintError gdextension_interface_print_error; extern "C" GDExtensionInterfacePrintErrorWithMessage gdextension_interface_print_error_with_message; extern "C" GDExtensionInterfacePrintWarning gdextension_interface_print_warning; diff --git a/src/core/memory.cpp b/src/core/memory.cpp index fca794d21..0bd687618 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -36,20 +36,15 @@ namespace godot { void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { #ifdef DEBUG_ENABLED - bool prepad = false; // Already pre paded in the engine. + bool prepad = true; #else bool prepad = p_pad_align; #endif - void *mem = internal::gdextension_interface_mem_alloc(p_bytes + (prepad ? DATA_OFFSET : 0)); + void *mem = internal::gdextension_interface_mem_alloc2(p_bytes, prepad); ERR_FAIL_NULL_V(mem, nullptr); - if (prepad) { - uint8_t *s8 = (uint8_t *)mem; - return s8 + DATA_OFFSET; - } else { - return mem; - } + return mem; } void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { @@ -60,37 +55,23 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { return nullptr; } - uint8_t *mem = (uint8_t *)p_memory; - #ifdef DEBUG_ENABLED - bool prepad = false; // Already pre paded in the engine. + bool prepad = true; #else bool prepad = p_pad_align; #endif - if (prepad) { - mem -= DATA_OFFSET; - mem = (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes + DATA_OFFSET); - ERR_FAIL_NULL_V(mem, nullptr); - return mem + DATA_OFFSET; - } else { - return (uint8_t *)internal::gdextension_interface_mem_realloc(mem, p_bytes); - } + return internal::gdextension_interface_mem_realloc2(p_memory, p_bytes, prepad); } void Memory::free_static(void *p_ptr, bool p_pad_align) { - uint8_t *mem = (uint8_t *)p_ptr; - #ifdef DEBUG_ENABLED - bool prepad = false; // Already pre paded in the engine. + bool prepad = true; #else bool prepad = p_pad_align; #endif - if (prepad) { - mem -= DATA_OFFSET; - } - internal::gdextension_interface_mem_free(mem); + internal::gdextension_interface_mem_free2(p_ptr, p_pad_align); } _GlobalNil::_GlobalNil() { diff --git a/src/godot.cpp b/src/godot.cpp index dec614222..259b7ed61 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -51,9 +51,9 @@ GDExtensionGodotVersion2 godot_version = {}; // All of the GDExtension interface functions. GDExtensionInterfaceGetGodotVersion2 gdextension_interface_get_godot_version2 = nullptr; -GDExtensionInterfaceMemAlloc gdextension_interface_mem_alloc = nullptr; -GDExtensionInterfaceMemRealloc gdextension_interface_mem_realloc = nullptr; -GDExtensionInterfaceMemFree gdextension_interface_mem_free = nullptr; +GDExtensionInterfaceMemAlloc2 gdextension_interface_mem_alloc2 = nullptr; +GDExtensionInterfaceMemRealloc2 gdextension_interface_mem_realloc2 = nullptr; +GDExtensionInterfaceMemFree2 gdextension_interface_mem_free2 = nullptr; GDExtensionInterfacePrintError gdextension_interface_print_error = nullptr; GDExtensionInterfacePrintErrorWithMessage gdextension_interface_print_error_with_message = nullptr; GDExtensionInterfacePrintWarning gdextension_interface_print_warning = nullptr; @@ -337,9 +337,9 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge return false; } - LOAD_PROC_ADDRESS(mem_alloc, GDExtensionInterfaceMemAlloc); - LOAD_PROC_ADDRESS(mem_realloc, GDExtensionInterfaceMemRealloc); - LOAD_PROC_ADDRESS(mem_free, GDExtensionInterfaceMemFree); + LOAD_PROC_ADDRESS(mem_alloc2, GDExtensionInterfaceMemAlloc2); + LOAD_PROC_ADDRESS(mem_realloc2, GDExtensionInterfaceMemRealloc2); + LOAD_PROC_ADDRESS(mem_free2, GDExtensionInterfaceMemFree2); LOAD_PROC_ADDRESS(print_error_with_message, GDExtensionInterfacePrintErrorWithMessage); LOAD_PROC_ADDRESS(print_warning, GDExtensionInterfacePrintWarning); LOAD_PROC_ADDRESS(print_warning_with_message, GDExtensionInterfacePrintWarningWithMessage);