Skip to content

Commit 77df0ab

Browse files
[rcore_desktop_glfw.c] fix: make sure that GLFW uses RL_*alloc macros (#4777)
Raylib allows for providing custom allocators via macros. GLFW supports this too, but via function pointers. Make sure that GLFW uses those Raylib macros, by wrapping them in function calls and setting them up inside of InitPlatform(). This is possible because of glfwInitAllocator() and GLFWallocator. Fixes: #4776 Relates-to: #4751
1 parent 11090ab commit 77df0ab

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/platforms/rcore_desktop_glfw.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
#include "GLFW/glfw3native.h" // Required for: glfwGetCocoaWindow()
8787
#endif
8888

89+
#include <stddef.h> // Required for: size_t
90+
8991
//----------------------------------------------------------------------------------
9092
// Types and Structures Definition
9193
//----------------------------------------------------------------------------------
@@ -127,6 +129,11 @@ static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffs
127129
static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
128130
static void JoystickCallback(int jid, int event); // GLFW3 Joystick Connected/Disconnected Callback
129131

132+
// Wrappers used by glfwInitAllocator
133+
static void* AllocateWrapper(size_t size, void* user); // GLFW3 GLFWallocatefun, wrapps around RL_MALLOC macro
134+
static void* ReallocateWrapper(void* block, size_t size, void* user); // GLFW3 GLFWreallocatefun, wrapps around RL_MALLOC macro
135+
static void DeallocateWrapper(void* block, void* user); // GLFW3 GLFWdeallocatefun, wraps around RL_FREE macro
136+
130137
//----------------------------------------------------------------------------------
131138
// Module Functions Declaration
132139
//----------------------------------------------------------------------------------
@@ -1287,21 +1294,38 @@ static void SetDimensionsFromMonitor(GLFWmonitor *monitor)
12871294
if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
12881295
}
12891296

1297+
// Function wrappers around RL_*alloc macros, used by glfwInitAllocator() inside of InitPlatform()
1298+
// We need to provide these because GLFWallocator expects function pointers with specific signatures.
1299+
// Similar wrappers exist in utils.c but we cannot reuse them here due to declaration mismatch.
1300+
// https://www.glfw.org/docs/latest/intro_guide.html#init_allocator
1301+
static void* AllocateWrapper(size_t size, void* user)
1302+
{
1303+
(void)user;
1304+
return RL_MALLOC(size);
1305+
}
1306+
static void* ReallocateWrapper(void* block, size_t size, void* user)
1307+
{
1308+
(void)user;
1309+
return RL_REALLOC(block, size);
1310+
}
1311+
static void DeallocateWrapper(void* block, void* user)
1312+
{
1313+
(void)user;
1314+
RL_FREE(block);
1315+
}
1316+
12901317
// Initialize platform: graphics, inputs and more
12911318
int InitPlatform(void)
12921319
{
12931320
glfwSetErrorCallback(ErrorCallback);
1294-
/*
1295-
// TODO: Setup GLFW custom allocators to match raylib ones
1321+
12961322
const GLFWallocator allocator = {
1297-
.allocate = MemAlloc,
1298-
.deallocate = MemFree,
1299-
.reallocate = MemRealloc,
1300-
.user = NULL
1323+
.allocate = AllocateWrapper,
1324+
.deallocate = DeallocateWrapper,
1325+
.reallocate = ReallocateWrapper,
1326+
.user = NULL, // RL_*ALLOC macros are not capable of handling user-provided data
13011327
};
1302-
13031328
glfwInitAllocator(&allocator);
1304-
*/
13051329

13061330
#if defined(__APPLE__)
13071331
glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);

0 commit comments

Comments
 (0)