From 5d4f79f441e8548d47a1bfdecaccc15a6a444322 Mon Sep 17 00:00:00 2001 From: Anthony Smith Date: Wed, 1 Jul 2015 10:01:16 -0700 Subject: [PATCH] Support custom dynamic memory functions. --- include/GLFW/glfw3.h | 67 ++++++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 2 +- src/cocoa_init.m | 2 +- src/cocoa_monitor.m | 20 ++++++------- src/cocoa_window.m | 8 +++--- src/egl_context.c | 8 +++--- src/glx_context.c | 4 +-- src/input.c | 9 ++---- src/internal.h | 19 +++++++++++++ src/iokit_joystick.m | 16 +++++------ src/linux_joystick.c | 22 +++++++-------- src/memory.c | 55 ++++++++++++++++++++++++++++++++++++ src/mir_init.c | 2 +- src/mir_monitor.c | 2 +- src/mir_window.c | 10 +++---- src/monitor.c | 24 ++++++++-------- src/wgl_context.c | 9 +++--- src/win32_init.c | 11 ++++---- src/win32_monitor.c | 3 +- src/win32_platform.h | 1 - src/win32_window.c | 23 ++++++++------- src/window.c | 4 +-- src/winmm_joystick.c | 4 +-- src/wl_init.c | 2 +- src/wl_monitor.c | 8 +++--- src/wl_window.c | 4 +-- src/x11_init.c | 2 +- src/x11_monitor.c | 10 +++---- src/x11_window.c | 12 ++++---- 29 files changed, 248 insertions(+), 115 deletions(-) create mode 100644 src/memory.c diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 9a7ca1764..34146b1f2 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -43,6 +43,11 @@ extern "C" { * This is the reference documentation for context related functions. For more * information, see the @ref context. */ +/*! @defgroup memory Dynamic Memory + * + * This is the reference documentation for memory management related functions + * and types. + */ /*! @defgroup init Initialization, version and errors * * This is the reference documentation for initialization and termination of @@ -587,6 +592,16 @@ extern "C" { * the user, as appropriate. */ #define GLFW_FORMAT_UNAVAILABLE 0x00010009 +/*! @brief GLFW has been initialized. + * + * This occurs if a GLFW function was called that may not be called unless the + * library is not [initialized](@ref intro_init). + * + * @par Analysis + * Application programmer error. Terminate GLFW before calling any function + * that requires initialization. + */ +#define GLFW_INITIALIZED 0x00010010 /*! @} */ #define GLFW_FOCUSED 0x00020001 @@ -742,6 +757,38 @@ typedef struct GLFWcursor GLFWcursor; */ typedef void (* GLFWerrorfun)(int,const char*); +/*! @brief The function signature for malloc-like memory callbacks. + * + * This is the function signature for allocating memory. + * + * @param[in] size The size in bytes that should be allocated. + * + * @ingroup memory + * + */ +typedef void* (* GLFWmallocfun)(size_t); + +/*! @brief The function signature for calloc-like memory callbacks. + * + * This is the function signature for allocating memory calloc-style. + * + * @param[in] num The number of elements to allocate. + * @param[in] size The size of each element. + * + * @ingroup memory + */ +typedef void* (*GLFWcallocfun)(size_t, size_t); + +/*! @brief The function signature for freeing allocated memory. + * + * This is the function signature for deallocating memory. + * + * @param[in] ptr The pointer that should be deallocated. + * + * @ingroup memory + */ +typedef void (* GLFWfreefun)(void*); + /*! @brief The function signature for window position callbacks. * * This is the function signature for window position callback functions. @@ -1046,6 +1093,26 @@ typedef struct GLFWimage * GLFW API functions *************************************************************************/ +/*! @brief Sets the memory functions of the GLFW library. + * + * This function sets the memory allocation/deallocation functions that the + * GLFW library will use. This function can be called before calling + * @ref glfwInit. + * + * If @ref glfwInit has already been called, @ref glfwTerminate must be called + * before the memory functions can be changed. + * + * @param[in] mallocfun The function to be called when malloc normally would. + * @param[in] callocfun The function to be called when calloc normally would. + * @param[in] freefun The function to be called when free normally would. + * + * @return 'GL_TRUE' if successful, or 'GL_FALSE' if an + * [error](@ref error_handling) occurred. + * + * @ingroup memory + */ +GLFWAPI int glfwSetMemoryFuncs(GLFWmallocfun mallocfun, GLFWcallocfun callocfun, GLFWfreefun freefun); + /*! @brief Initializes the GLFW library. * * This function initializes the GLFW library. Before most GLFW functions can diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 77a8501f6..9ef2d9cc4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,7 @@ set(common_HEADERS internal.h "${GLFW_BINARY_DIR}/src/glfw_config.h" "${GLFW_SOURCE_DIR}/include/GLFW/glfw3.h" "${GLFW_SOURCE_DIR}/include/GLFW/glfw3native.h") -set(common_SOURCES context.c init.c input.c monitor.c window.c) +set(common_SOURCES context.c init.c input.c memory.c monitor.c window.c) if (_GLFW_COCOA) set(glfw_HEADERS ${common_HEADERS} cocoa_platform.h iokit_joystick.h diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 8bb6b4276..147157837 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -241,7 +241,7 @@ void _glfwPlatformTerminate(void) [_glfw.ns.cursor release]; _glfw.ns.cursor = nil; - free(_glfw.ns.clipboardString); + _memory.free(_glfw.ns.clipboardString); _glfwTerminateJoysticks(); _glfwTerminateContextAPI(); diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index a99aa33f4..1f7ee089c 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -66,7 +66,7 @@ static char* getDisplayName(CGDirectDisplayID displayID) size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value), kCFStringEncodingUTF8); - name = calloc(size + 1, sizeof(char)); + name = _memory.calloc(size + 1, sizeof(char)); CFStringGetCString(value, name, size, kCFStringEncodingUTF8); CFRelease(info); @@ -252,8 +252,8 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) *count = 0; CGGetOnlineDisplayList(0, NULL, &displayCount); - displays = calloc(displayCount, sizeof(CGDirectDisplayID)); - monitors = calloc(displayCount, sizeof(_GLFWmonitor*)); + displays = _memory.calloc(displayCount, sizeof(CGDirectDisplayID)); + monitors = _memory.calloc(displayCount, sizeof(_GLFWmonitor*)); CGGetOnlineDisplayList(displayCount, displays, &displayCount); NSArray* screens = [NSScreen screens]; @@ -287,13 +287,13 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) monitor = _glfwAllocMonitor(name, size.width, size.height); monitor->ns.displayID = displays[i]; - free(name); + _memory.free(name); found++; monitors[found - 1] = monitor; } - free(displays); + _memory.free(displays); *count = found; return monitors; @@ -327,7 +327,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count) modes = CGDisplayCopyAllDisplayModes(monitor->ns.displayID, NULL); found = CFArrayGetCount(modes); - result = calloc(found, sizeof(GLFWvidmode)); + result = _memory.calloc(found, sizeof(GLFWvidmode)); for (i = 0; i < found; i++) { @@ -373,7 +373,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode *mode) void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) { uint32_t i, size = CGDisplayGammaTableCapacity(monitor->ns.displayID); - CGGammaValue* values = calloc(size * 3, sizeof(CGGammaValue)); + CGGammaValue* values = _memory.calloc(size * 3, sizeof(CGGammaValue)); CGGetDisplayTransferByTable(monitor->ns.displayID, size, @@ -391,13 +391,13 @@ void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) ramp->blue[i] = (unsigned short) (values[i + size * 2] * 65535); } - free(values); + _memory.free(values); } void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) { int i; - CGGammaValue* values = calloc(ramp->size * 3, sizeof(CGGammaValue)); + CGGammaValue* values = _memory.calloc(ramp->size * 3, sizeof(CGGammaValue)); for (i = 0; i < ramp->size; i++) { @@ -412,7 +412,7 @@ void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) values + ramp->size, values + ramp->size * 2); - free(values); + _memory.free(values); } diff --git a/src/cocoa_window.m b/src/cocoa_window.m index b03632c09..5828b8ef3 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -612,7 +612,7 @@ static int translateKey(unsigned int key) if (count) { NSEnumerator* e = [files objectEnumerator]; - char** paths = calloc(count, sizeof(char*)); + char** paths = _memory.calloc(count, sizeof(char*)); int i; for (i = 0; i < count; i++) @@ -621,8 +621,8 @@ static int translateKey(unsigned int key) _glfwInputDrop(window, count, (const char**) paths); for (i = 0; i < count; i++) - free(paths[i]); - free(paths); + _memory.free(paths[i]); + _memory.free(paths); } return YES; @@ -1285,7 +1285,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) return NULL; } - free(_glfw.ns.clipboardString); + _memory.free(_glfw.ns.clipboardString); _glfw.ns.clipboardString = strdup([object UTF8String]); return _glfw.ns.clipboardString; diff --git a/src/egl_context.c b/src/egl_context.c index d62262381..15e8ca68b 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -100,10 +100,10 @@ static GLboolean chooseFBConfigs(const _GLFWctxconfig* ctxconfig, return GL_FALSE; } - nativeConfigs = calloc(nativeCount, sizeof(EGLConfig)); + nativeConfigs = _memory.calloc(nativeCount, sizeof(EGLConfig)); eglGetConfigs(_glfw.egl.display, nativeConfigs, nativeCount, &nativeCount); - usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + usableConfigs = _memory.calloc(nativeCount, sizeof(_GLFWfbconfig)); usableCount = 0; for (i = 0; i < nativeCount; i++) @@ -163,8 +163,8 @@ static GLboolean chooseFBConfigs(const _GLFWctxconfig* ctxconfig, if (closest) *result = closest->egl; - free(nativeConfigs); - free(usableConfigs); + _memory.free(nativeConfigs); + _memory.free(usableConfigs); return closest ? GL_TRUE : GL_FALSE; } diff --git a/src/glx_context.c b/src/glx_context.c index 1101912f9..f63cfea7e 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -71,7 +71,7 @@ static GLboolean chooseFBConfig(const _GLFWfbconfig* desired, GLXFBConfig* resul return GL_FALSE; } - usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + usableConfigs = _memory.calloc(nativeCount, sizeof(_GLFWfbconfig)); usableCount = 0; for (i = 0; i < nativeCount; i++) @@ -129,7 +129,7 @@ static GLboolean chooseFBConfig(const _GLFWfbconfig* desired, GLXFBConfig* resul *result = closest->glx; XFree(nativeConfigs); - free(usableConfigs); + _memory.free(usableConfigs); return closest ? GL_TRUE : GL_FALSE; } diff --git a/src/input.c b/src/input.c index eefbf0860..d3296b20e 100644 --- a/src/input.c +++ b/src/input.c @@ -28,9 +28,6 @@ #include "internal.h" #include -#if defined(_MSC_VER) - #include -#endif // Internal key state used for sticky keys #define _GLFW_STICK 3 @@ -365,7 +362,7 @@ GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot) _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - cursor = calloc(1, sizeof(_GLFWcursor)); + cursor = _memory.calloc(1, sizeof(_GLFWcursor)); cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; @@ -395,7 +392,7 @@ GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape) return NULL; } - cursor = calloc(1, sizeof(_GLFWcursor)); + cursor = _memory.calloc(1, sizeof(_GLFWcursor)); cursor->next = _glfw.cursorListHead; _glfw.cursorListHead = cursor; @@ -440,7 +437,7 @@ GLFWAPI void glfwDestroyCursor(GLFWcursor* handle) *prev = cursor->next; } - free(cursor); + _memory.free(cursor); } GLFWAPI void glfwSetCursor(GLFWwindow* windowHandle, GLFWcursor* cursorHandle) diff --git a/src/internal.h b/src/internal.h index 986f62302..2821f8002 100644 --- a/src/internal.h +++ b/src/internal.h @@ -69,6 +69,7 @@ #include "../deps/GL/glext.h" #endif +typedef struct _GLFWmemory _GLFWmemory; typedef struct _GLFWwndconfig _GLFWwndconfig; typedef struct _GLFWctxconfig _GLFWctxconfig; typedef struct _GLFWfbconfig _GLFWfbconfig; @@ -151,6 +152,18 @@ typedef struct _GLFWcursor _GLFWcursor; // Platform-independent structures //======================================================================== +/*! @brief Memory Configuration + * + * This is used to store the functions used for allocating and deallocating + * dynamic memory. + */ +struct _GLFWmemory +{ + GLFWmallocfun malloc; + GLFWcallocfun calloc; + GLFWfreefun free; +}; + /*! @brief Window configuration. * * Parameters relating to the creation of the window but not directly related @@ -370,6 +383,12 @@ extern GLboolean _glfwInitialized; */ extern _GLFWlibrary _glfw; +/*! @brief All global data protected by @ref _glfwInitialized. + * This should only be touched before a call to @ref glfwInit or after a call to + * @ref glfwTerminate. + */ +extern _GLFWmemory _memory; + //======================================================================== // Platform API functions diff --git a/src/iokit_joystick.m b/src/iokit_joystick.m index ebce5c8ff..e395fb9fe 100644 --- a/src/iokit_joystick.m +++ b/src/iokit_joystick.m @@ -109,7 +109,7 @@ static void addJoystickElement(_GLFWjoydevice* joystick, if (elementsArray) { - _GLFWjoyelement* element = calloc(1, sizeof(_GLFWjoyelement)); + _GLFWjoyelement* element = _memory.calloc(1, sizeof(_GLFWjoyelement)); CFArrayAppendValue(elementsArray, element); @@ -171,22 +171,22 @@ static void removeJoystick(_GLFWjoydevice* joystick) return; for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->axisElements, i)); + _memory.free((void*) CFArrayGetValueAtIndex(joystick->axisElements, i)); CFArrayRemoveAllValues(joystick->axisElements); CFRelease(joystick->axisElements); for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->buttonElements, i)); + _memory.free((void*) CFArrayGetValueAtIndex(joystick->buttonElements, i)); CFArrayRemoveAllValues(joystick->buttonElements); CFRelease(joystick->buttonElements); for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->hatElements, i)); + _memory.free((void*) CFArrayGetValueAtIndex(joystick->hatElements, i)); CFArrayRemoveAllValues(joystick->hatElements); CFRelease(joystick->hatElements); - free(joystick->axes); - free(joystick->buttons); + _memory.free(joystick->axes); + _memory.free(joystick->buttons); memset(joystick, 0, sizeof(_GLFWjoydevice)); } @@ -311,9 +311,9 @@ static void matchCallback(void* context, CFRelease(arrayRef); - joystick->axes = calloc(CFArrayGetCount(joystick->axisElements), + joystick->axes = _memory.calloc(CFArrayGetCount(joystick->axisElements), sizeof(float)); - joystick->buttons = calloc(CFArrayGetCount(joystick->buttonElements) + + joystick->buttons = _memory.calloc(CFArrayGetCount(joystick->buttonElements) + CFArrayGetCount(joystick->hatElements) * 4, 1); } diff --git a/src/linux_joystick.c b/src/linux_joystick.c index 5a9d8e2a2..35408259f 100644 --- a/src/linux_joystick.c +++ b/src/linux_joystick.c @@ -97,8 +97,8 @@ static void openJoystickDevice(const char* path) ioctl(fd, JSIOCGBUTTONS, &buttonCount); _glfw.linux_js.js[joy].buttonCount = (int) buttonCount; - _glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float)); - _glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1); + _glfw.linux_js.js[joy].axes = _memory.calloc(axisCount, sizeof(float)); + _glfw.linux_js.js[joy].buttons = _memory.calloc(buttonCount, 1); _glfw.linux_js.js[joy].present = GL_TRUE; #endif // __linux__ @@ -146,10 +146,10 @@ static void pollJoystickEvents(void) { // The joystick was disconnected - free(_glfw.linux_js.js[i].axes); - free(_glfw.linux_js.js[i].buttons); - free(_glfw.linux_js.js[i].name); - free(_glfw.linux_js.js[i].path); + _memory.free(_glfw.linux_js.js[i].axes); + _memory.free(_glfw.linux_js.js[i].buttons); + _memory.free(_glfw.linux_js.js[i].name); + _memory.free(_glfw.linux_js.js[i].path); memset(&_glfw.linux_js.js[i], 0, sizeof(_glfw.linux_js.js[i])); } @@ -268,14 +268,14 @@ void _glfwTerminateJoysticks(void) if (_glfw.linux_js.js[i].present) { close(_glfw.linux_js.js[i].fd); - free(_glfw.linux_js.js[i].axes); - free(_glfw.linux_js.js[i].buttons); - free(_glfw.linux_js.js[i].name); - free(_glfw.linux_js.js[i].path); + _memory.free(_glfw.linux_js.js[i].axes); + _memory.free(_glfw.linux_js.js[i].buttons); + _memory.free(_glfw.linux_js.js[i].name); + _memory.free(_glfw.linux_js.js[i].path); } } - regfree(&_glfw.linux_js.regex); + reg_memory.free(&_glfw.linux_js.regex); if (_glfw.linux_js.inotify > 0) { diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 000000000..35b8b4a83 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,55 @@ +//======================================================================== +// GLFW 3.1 Wayland - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2015 Anthony Smith +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include + +// initialize the global memory state to use +// the standard malloc, calloc and free functions +_GLFWmemory _memory = { malloc, calloc, free }; + +////////////////////////////////////////////////////////////////////////// +////// GLFW public API ////// +////////////////////////////////////////////////////////////////////////// + +GLFWAPI int glfwSetMemoryFuncs(GLFWmallocfun mallocfun, GLFWcallocfun callocfun, GLFWfreefun freefun) +{ + if(_glfwInitialized) + { + _glfwInputError(GLFW_INITIALIZED, NULL); + return GL_FALSE; + } + if(!mallocfun || !callocfun || !freefun) + { + _glfwInputError(GLFW_INVALID_VALUE, NULL); + return GL_FALSE; + } + _memory.malloc = mallocfun; + _memory.calloc = callocfun; + _memory.free = freefun; + return GL_TRUE; +} diff --git a/src/mir_init.c b/src/mir_init.c index 153e40056..6fff04fad 100644 --- a/src/mir_init.c +++ b/src/mir_init.c @@ -58,7 +58,7 @@ int _glfwPlatformInit(void) _glfwInitTimer(); _glfwInitJoysticks(); - _glfw.mir.event_queue = calloc(1, sizeof(EventQueue)); + _glfw.mir.event_queue = _memory.calloc(1, sizeof(EventQueue)); _glfwInitEventQueue(_glfw.mir.event_queue); error = pthread_mutex_init(&_glfw.mir.event_mutex, NULL); diff --git a/src/mir_monitor.c b/src/mir_monitor.c index 93073d334..6b933b785 100644 --- a/src/mir_monitor.c +++ b/src/mir_monitor.c @@ -99,7 +99,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) if (out->output_id != monitor->mir.output_id) continue; - modes = calloc(out->num_modes, sizeof(GLFWvidmode)); + modes = _memory.calloc(out->num_modes, sizeof(GLFWvidmode)); for (*found = 0; *found < out->num_modes; (*found)++) { diff --git a/src/mir_window.c b/src/mir_window.c index cbc8e0597..ff54ee3c5 100644 --- a/src/mir_window.c +++ b/src/mir_window.c @@ -40,8 +40,8 @@ typedef struct EventNode static void deleteNode(EventQueue* queue, EventNode* node) { - free(node->event); - free(node); + _memory.free(node->event); + _memory.free(node); } static int emptyEventQueue(EventQueue* queue) @@ -51,8 +51,8 @@ static int emptyEventQueue(EventQueue* queue) static EventNode* newEventNode(MirEvent const* event, _GLFWwindow* context) { - EventNode* new_node = calloc(1, sizeof(EventNode)); - new_node->event = calloc(1, sizeof(MirEvent)); + EventNode* new_node = _memory.calloc(1, sizeof(EventNode)); + new_node->event = _memory.calloc(1, sizeof(MirEvent)); new_node->window = context; memcpy(new_node->event, event, sizeof(MirEvent)); @@ -443,7 +443,7 @@ void _glfwDeleteEventQueue(EventQueue* queue) node = node_next; } - free(queue); + _memory.free(queue); } } diff --git a/src/monitor.c b/src/monitor.c index 9a155bd73..2e875ee8b 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -74,7 +74,7 @@ static int refreshVideoModes(_GLFWmonitor* monitor) qsort(modes, modeCount, sizeof(GLFWvidmode), compareVideoModes); - free(monitor->modes); + _memory.free(monitor->modes); monitor->modes = modes; monitor->modeCount = modeCount; @@ -165,7 +165,7 @@ void _glfwInputMonitorChange(void) _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM) { - _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor)); + _GLFWmonitor* monitor = _memory.calloc(1, sizeof(_GLFWmonitor)); monitor->name = strdup(name); monitor->widthMM = widthMM; monitor->heightMM = heightMM; @@ -181,24 +181,24 @@ void _glfwFreeMonitor(_GLFWmonitor* monitor) _glfwFreeGammaArrays(&monitor->originalRamp); _glfwFreeGammaArrays(&monitor->currentRamp); - free(monitor->modes); - free(monitor->name); - free(monitor); + _memory.free(monitor->modes); + _memory.free(monitor->name); + _memory.free(monitor); } void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size) { - ramp->red = calloc(size, sizeof(unsigned short)); - ramp->green = calloc(size, sizeof(unsigned short)); - ramp->blue = calloc(size, sizeof(unsigned short)); + ramp->red = _memory.calloc(size, sizeof(unsigned short)); + ramp->green = _memory.calloc(size, sizeof(unsigned short)); + ramp->blue = _memory.calloc(size, sizeof(unsigned short)); ramp->size = size; } void _glfwFreeGammaArrays(GLFWgammaramp* ramp) { - free(ramp->red); - free(ramp->green); - free(ramp->blue); + _memory.free(ramp->red); + _memory.free(ramp->green); + _memory.free(ramp->blue); memset(ramp, 0, sizeof(GLFWgammaramp)); } @@ -210,7 +210,7 @@ void _glfwFreeMonitors(_GLFWmonitor** monitors, int count) for (i = 0; i < count; i++) _glfwFreeMonitor(monitors[i]); - free(monitors); + _memory.free(monitors); } const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor, diff --git a/src/wgl_context.c b/src/wgl_context.c index 96ebcfb11..515a570f3 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -28,7 +28,6 @@ #include "internal.h" #include -#include #include @@ -122,7 +121,7 @@ static GLboolean choosePixelFormat(_GLFWwindow* window, NULL); } - usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig)); + usableConfigs = _memory.calloc(nativeCount, sizeof(_GLFWfbconfig)); usableCount = 0; for (i = 0; i < nativeCount; i++) @@ -240,7 +239,7 @@ static GLboolean choosePixelFormat(_GLFWwindow* window, _glfwInputError(GLFW_API_UNAVAILABLE, "WGL: The driver does not appear to support OpenGL"); - free(usableConfigs); + _memory.free(usableConfigs); return GL_FALSE; } @@ -250,12 +249,12 @@ static GLboolean choosePixelFormat(_GLFWwindow* window, _glfwInputError(GLFW_PLATFORM_ERROR, "WGL: Failed to find a suitable pixel format"); - free(usableConfigs); + _memory.free(usableConfigs); return GL_FALSE; } *result = closest->wgl; - free(usableConfigs); + _memory.free(usableConfigs); return GL_TRUE; } diff --git a/src/win32_init.c b/src/win32_init.c index c39c67c16..ce88281d7 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -28,7 +28,6 @@ #include "internal.h" #include -#include #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG) @@ -283,11 +282,11 @@ WCHAR* _glfwCreateWideStringFromUTF8(const char* source) if (!length) return NULL; - target = calloc(length, sizeof(WCHAR)); + target = _memory.calloc(length, sizeof(WCHAR)); if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length)) { - free(target); + _memory.free(target); return NULL; } @@ -305,11 +304,11 @@ char* _glfwCreateUTF8FromWideString(const WCHAR* source) if (!length) return NULL; - target = calloc(length, sizeof(char)); + target = _memory.calloc(length, sizeof(char)); if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL)) { - free(target); + _memory.free(target); return NULL; } @@ -360,7 +359,7 @@ void _glfwPlatformTerminate(void) UIntToPtr(_glfw.win32.foregroundLockTimeout), SPIF_SENDCHANGE); - free(_glfw.win32.clipboardString); + _memory.free(_glfw.win32.clipboardString); _glfwTerminateJoysticks(); _glfwTerminateContextAPI(); diff --git a/src/win32_monitor.c b/src/win32_monitor.c index be5686f82..2746005e4 100644 --- a/src/win32_monitor.c +++ b/src/win32_monitor.c @@ -30,7 +30,6 @@ #include #include #include -#include // These constants are missing on MinGW #ifndef EDS_ROTATEDMODE @@ -150,7 +149,7 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) GetDeviceCaps(dc, VERTSIZE)); DeleteDC(dc); - free(name); + _memory.free(name); wcscpy(monitor->win32.adapterName, adapter.DeviceName); wcscpy(monitor->win32.displayName, display.DeviceName); diff --git a/src/win32_platform.h b/src/win32_platform.h index 9bb49f16b..744fb1a13 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -66,7 +66,6 @@ #include #if defined(_MSC_VER) - #include #define strdup _strdup #endif diff --git a/src/win32_window.c b/src/win32_window.c index daad58bf1..3af224179 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -28,7 +28,6 @@ #include "internal.h" #include -#include #include #include #include @@ -601,7 +600,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, int i; const int count = DragQueryFileW(hDrop, 0xffffffff, NULL, 0); - char** paths = calloc(count, sizeof(char*)); + char** paths = _memory.calloc(count, sizeof(char*)); // Move the mouse to the position of the drop DragQueryPoint(hDrop, &pt); @@ -610,19 +609,19 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, for (i = 0; i < count; i++) { const UINT length = DragQueryFileW(hDrop, i, NULL, 0); - WCHAR* buffer = calloc(length + 1, sizeof(WCHAR)); + WCHAR* buffer = _memory.calloc(length + 1, sizeof(WCHAR)); DragQueryFileW(hDrop, i, buffer, length + 1); paths[i] = _glfwCreateUTF8FromWideString(buffer); - free(buffer); + _memory.free(buffer); } _glfwInputDrop(window, count, (const char**) paths); for (i = 0; i < count; i++) - free(paths[i]); - free(paths); + _memory.free(paths[i]); + _memory.free(paths); DragFinish(hDrop); return 0; @@ -696,7 +695,7 @@ static int createWindow(_GLFWwindow* window, GetModuleHandleW(NULL), window); // Pass object to WM_CREATE - free(wideTitle); + _memory.free(wideTitle); if (!window->win32.handle) { @@ -869,7 +868,7 @@ void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) } SetWindowTextW(window->win32.handle, wideTitle); - free(wideTitle); + _memory.free(wideTitle); } void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos) @@ -1224,7 +1223,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) stringHandle = GlobalAlloc(GMEM_MOVEABLE, wideSize); if (!stringHandle) { - free(wideString); + _memory.free(wideString); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to allocate global handle for clipboard"); @@ -1237,7 +1236,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) if (!OpenClipboard(window->win32.handle)) { GlobalFree(stringHandle); - free(wideString); + _memory.free(wideString); _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard"); return; @@ -1247,7 +1246,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) SetClipboardData(CF_UNICODETEXT, stringHandle); CloseClipboard(); - free(wideString); + _memory.free(wideString); } const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) @@ -1270,7 +1269,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) return NULL; } - free(_glfw.win32.clipboardString); + _memory.free(_glfw.win32.clipboardString); _glfw.win32.clipboardString = _glfwCreateUTF8FromWideString(GlobalLock(stringHandle)); diff --git a/src/window.c b/src/window.c index 47964dfdc..43ef151f9 100644 --- a/src/window.c +++ b/src/window.c @@ -153,7 +153,7 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, if (!_glfwIsValidContextConfig(&ctxconfig)) return NULL; - window = calloc(1, sizeof(_GLFWwindow)); + window = _memory.calloc(1, sizeof(_GLFWwindow)); window->next = _glfw.windowListHead; _glfw.windowListHead = window; @@ -400,7 +400,7 @@ GLFWAPI void glfwDestroyWindow(GLFWwindow* handle) *prev = window->next; } - free(window); + _memory.free(window); } GLFWAPI int glfwWindowShouldClose(GLFWwindow* handle) diff --git a/src/winmm_joystick.c b/src/winmm_joystick.c index fcf6d94ef..26f39a26d 100644 --- a/src/winmm_joystick.c +++ b/src/winmm_joystick.c @@ -63,7 +63,7 @@ void _glfwTerminateJoysticks(void) int i; for (i = 0; i < GLFW_JOYSTICK_LAST; i++) - free(_glfw.winmm_js[i].name); + _memory.free(_glfw.winmm_js[i].name); } @@ -169,7 +169,7 @@ const char* _glfwPlatformGetJoystickName(int joy) if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR) return NULL; - free(_glfw.winmm_js[joy].name); + _memory.free(_glfw.winmm_js[joy].name); _glfw.winmm_js[joy].name = _glfwCreateUTF8FromWideString(jc.szPname); return _glfw.winmm_js[joy].name; diff --git a/src/wl_init.c b/src/wl_init.c index ea621f998..3b4a29734 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -554,7 +554,7 @@ int _glfwPlatformInit(void) _glfw.wl.registry = wl_display_get_registry(_glfw.wl.display); wl_registry_add_listener(_glfw.wl.registry, ®istryListener, NULL); - _glfw.wl.monitors = calloc(4, sizeof(_GLFWmonitor*)); + _glfw.wl.monitors = _memory.calloc(4, sizeof(_GLFWmonitor*)); _glfw.wl.monitorsSize = 4; _glfw.wl.xkb.context = xkb_context_new(0); diff --git a/src/wl_monitor.c b/src/wl_monitor.c index fbcefd38d..1655f7840 100644 --- a/src/wl_monitor.c +++ b/src/wl_monitor.c @@ -139,7 +139,7 @@ void _glfwAddOutput(uint32_t name, uint32_t version) return; } - monitor->wl.modes = calloc(4, sizeof(_GLFWvidmodeWayland)); + monitor->wl.modes = _memory.calloc(4, sizeof(_GLFWvidmodeWayland)); monitor->wl.modesSize = 4; monitor->wl.output = output; @@ -173,12 +173,12 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) if (_glfw.wl.monitorsCount == 0) goto err; - monitors = calloc(monitorsCount, sizeof(_GLFWmonitor*)); + monitors = _memory.calloc(monitorsCount, sizeof(_GLFWmonitor*)); for (i = 0; i < monitorsCount; i++) { _GLFWmonitor* origMonitor = _glfw.wl.monitors[i]; - monitor = calloc(1, sizeof(_GLFWmonitor)); + monitor = _memory.calloc(1, sizeof(_GLFWmonitor)); monitor->modes = _glfwPlatformGetVideoModes(origMonitor, @@ -213,7 +213,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) GLFWvidmode *modes; int i, modesCount = monitor->wl.modesCount; - modes = calloc(modesCount, sizeof(GLFWvidmode)); + modes = _memory.calloc(modesCount, sizeof(GLFWvidmode)); for (i = 0; i < modesCount; i++) modes[i] = monitor->wl.modes[i].base; diff --git a/src/wl_window.c b/src/wl_window.c index 1d292767b..6438956d8 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -187,13 +187,13 @@ createAnonymousFile(off_t size) return -1; } - name = malloc(strlen(path) + sizeof(template)); + name = _memory.malloc(strlen(path) + sizeof(template)); strcpy(name, path); strcat(name, template); fd = createTmpfileCloexec(name); - free(name); + _memory.free(name); if (fd < 0) return -1; diff --git a/src/x11_init.c b/src/x11_init.c index 0bd384859..b7242ca55 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -766,7 +766,7 @@ void _glfwPlatformTerminate(void) _glfw.x11.cursor = (Cursor) 0; } - free(_glfw.x11.clipboardString); + _memory.free(_glfw.x11.clipboardString); if (_glfw.x11.im) { diff --git a/src/x11_monitor.c b/src/x11_monitor.c index 69d872531..ba6236153 100644 --- a/src/x11_monitor.c +++ b/src/x11_monitor.c @@ -214,7 +214,7 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) RROutput primary = XRRGetOutputPrimary(_glfw.x11.display, _glfw.x11.root); - monitors = calloc(sr->noutput, sizeof(_GLFWmonitor*)); + monitors = _memory.calloc(sr->noutput, sizeof(_GLFWmonitor*)); if (_glfw.x11.xinerama.available) screens = XineramaQueryScreens(_glfw.x11.display, &screenCount); @@ -286,14 +286,14 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) "X11: RandR monitor support seems broken"); _glfw.x11.randr.monitorBroken = GL_TRUE; - free(monitors); + _memory.free(monitors); monitors = NULL; } } if (!monitors) { - monitors = calloc(1, sizeof(_GLFWmonitor*)); + monitors = _memory.calloc(1, sizeof(_GLFWmonitor*)); monitors[0] = _glfwAllocMonitor("Display", DisplayWidthMM(_glfw.x11.display, _glfw.x11.screen), @@ -348,7 +348,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count) ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc); oi = XRRGetOutputInfo(_glfw.x11.display, sr, monitor->x11.output); - result = calloc(oi->nmode, sizeof(GLFWvidmode)); + result = _memory.calloc(oi->nmode, sizeof(GLFWvidmode)); for (i = 0; i < oi->nmode; i++) { @@ -379,7 +379,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count) else { *count = 1; - result = calloc(1, sizeof(GLFWvidmode)); + result = _memory.calloc(1, sizeof(GLFWvidmode)); _glfwPlatformGetVideoMode(monitor, result); } diff --git a/src/x11_window.c b/src/x11_window.c index ae27eed9a..7b8fa2fb4 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -223,7 +223,7 @@ static char** parseUriList(char* text, int* count) (*count)++; - char* path = calloc(strlen(line) + 1, 1); + char* path = _memory.calloc(strlen(line) + 1, 1); paths = realloc(paths, *count * sizeof(char*)); paths[*count - 1] = path; @@ -672,7 +672,7 @@ static Atom writeTargetToProperty(const XSelectionRequestEvent* request) static void handleSelectionClear(XEvent* event) { - free(_glfw.x11.clipboardString); + _memory.free(_glfw.x11.clipboardString); _glfw.x11.clipboardString = NULL; } @@ -1202,8 +1202,8 @@ static void processEvent(XEvent *event) _glfwInputDrop(window, count, (const char**) paths); for (i = 0; i < count; i++) - free(paths[i]); - free(paths); + _memory.free(paths[i]); + _memory.free(paths); } XFree(data); @@ -1853,7 +1853,7 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) { - free(_glfw.x11.clipboardString); + _memory.free(_glfw.x11.clipboardString); _glfw.x11.clipboardString = strdup(string); XSetSelectionOwner(_glfw.x11.display, @@ -1884,7 +1884,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) return _glfw.x11.clipboardString; } - free(_glfw.x11.clipboardString); + _memory.free(_glfw.x11.clipboardString); _glfw.x11.clipboardString = NULL; for (i = 0; i < formatCount; i++)