Skip to content

[DRAFT] Update to use mem_alloc2, mem_realloc2 and mem_free2 #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion gdextension/gdextension_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -881,14 +883,53 @@ 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.
*
* @param p_ptr A pointer to the previously allocated memory.
*/
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
Expand Down
6 changes: 3 additions & 3 deletions include/godot_cpp/godot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 7 additions & 26 deletions src/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions src/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading