Merge pull request #108725 from dsnopek/gdextension-mem-alloc-pad-align

GDExtension: Add `mem_alloc2` (and friends) so padding can be requested
This commit is contained in:
Thaddeus Crews 2025-09-30 18:35:09 -05:00
commit 76c039fb09
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
2 changed files with 61 additions and 1 deletions

View file

@ -261,6 +261,7 @@ static void gdextension_get_godot_version2(GDExtensionGodotVersion2 *r_godot_ver
} }
// Memory Functions // Memory Functions
#ifndef DISABLE_DEPRECATED
static void *gdextension_mem_alloc(size_t p_size) { static void *gdextension_mem_alloc(size_t p_size) {
return memalloc(p_size); return memalloc(p_size);
} }
@ -272,6 +273,19 @@ static void *gdextension_mem_realloc(void *p_mem, size_t p_size) {
static void gdextension_mem_free(void *p_mem) { static void gdextension_mem_free(void *p_mem) {
memfree(p_mem); memfree(p_mem);
} }
#endif
static void *gdextension_mem_alloc2(size_t p_size, GDExtensionBool p_prepad_align) {
return Memory::alloc_static(p_size, p_prepad_align);
}
static void *gdextension_mem_realloc2(void *p_mem, size_t p_size, GDExtensionBool p_prepad_align) {
return Memory::realloc_static(p_mem, p_size, p_prepad_align);
}
static void gdextension_mem_free2(void *p_mem, GDExtensionBool p_prepad_align) {
Memory::free_static(p_mem, p_prepad_align);
}
// Helper print functions. // Helper print functions.
static void gdextension_print_error(const char *p_description, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) { static void gdextension_print_error(const char *p_description, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
@ -1687,9 +1701,14 @@ void gdextension_setup_interface() {
REGISTER_INTERFACE_FUNC(get_godot_version); REGISTER_INTERFACE_FUNC(get_godot_version);
#endif // DISABLE_DEPRECATED #endif // DISABLE_DEPRECATED
REGISTER_INTERFACE_FUNC(get_godot_version2); REGISTER_INTERFACE_FUNC(get_godot_version2);
#ifndef DISABLE_DEPRECATED
REGISTER_INTERFACE_FUNC(mem_alloc); REGISTER_INTERFACE_FUNC(mem_alloc);
REGISTER_INTERFACE_FUNC(mem_realloc); REGISTER_INTERFACE_FUNC(mem_realloc);
REGISTER_INTERFACE_FUNC(mem_free); REGISTER_INTERFACE_FUNC(mem_free);
#endif // DISABLE_DEPRECATED
REGISTER_INTERFACE_FUNC(mem_alloc2);
REGISTER_INTERFACE_FUNC(mem_realloc2);
REGISTER_INTERFACE_FUNC(mem_free2);
REGISTER_INTERFACE_FUNC(print_error); REGISTER_INTERFACE_FUNC(print_error);
REGISTER_INTERFACE_FUNC(print_error_with_message); REGISTER_INTERFACE_FUNC(print_error_with_message);
REGISTER_INTERFACE_FUNC(print_warning); REGISTER_INTERFACE_FUNC(print_warning);

View file

@ -858,6 +858,7 @@ typedef void (*GDExtensionInterfaceGetGodotVersion2)(GDExtensionGodotVersion2 *r
/** /**
* @name mem_alloc * @name mem_alloc
* @since 4.1 * @since 4.1
* @deprecated in Godot 4.6. Use `mem_alloc2` instead.
* *
* Allocates memory. * Allocates memory.
* *
@ -870,6 +871,7 @@ typedef void *(*GDExtensionInterfaceMemAlloc)(size_t p_bytes);
/** /**
* @name mem_realloc * @name mem_realloc
* @since 4.1 * @since 4.1
* @deprecated in Godot 4.6. Use `mem_realloc2` instead.
* *
* Reallocates memory. * Reallocates memory.
* *
@ -883,6 +885,7 @@ typedef void *(*GDExtensionInterfaceMemRealloc)(void *p_ptr, size_t p_bytes);
/** /**
* @name mem_free * @name mem_free
* @since 4.1 * @since 4.1
* @deprecated in Godot 4.6. Use `mem_free2` instead.
* *
* Frees memory. * Frees memory.
* *
@ -890,7 +893,45 @@ typedef void *(*GDExtensionInterfaceMemRealloc)(void *p_ptr, size_t p_bytes);
*/ */
typedef void (*GDExtensionInterfaceMemFree)(void *p_ptr); 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 * @name print_error