Added pluggable memory allocator and threading stub.

This commit is contained in:
Camilla Berglund
2011-03-07 14:09:13 +01:00
parent cf7819df8d
commit ccbb956341
8 changed files with 92 additions and 14 deletions
+55
View File
@@ -45,6 +45,30 @@ static void glfw_atexit(void)
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
//========================================================================
// Allocate memory using the allocator
//========================================================================
void* _glfwMalloc(size_t size)
{
return _glfwLibrary.allocator.malloc(size);
}
//========================================================================
// Free memory using the allocator
//========================================================================
void _glfwFree(void* ptr)
{
_glfwLibrary.allocator.free(ptr);
}
//////////////////////////////////////////////////////////////////////////
////// GLFW public API //////
//////////////////////////////////////////////////////////////////////////
@@ -54,12 +78,43 @@ static void glfw_atexit(void)
//========================================================================
GLFWAPI int glfwInit(void)
{
return glfwInitWithModels(NULL, NULL);
}
//========================================================================
// Initialize various GLFW state using custom model interfaces
//========================================================================
GLFWAPI int glfwInitWithModels(GLFWthreadmodel* threading, GLFWallocator* allocator)
{
if (_glfwInitialized)
return GL_TRUE;
memset(&_glfwLibrary, 0, sizeof(_glfwLibrary));
if (threading)
_glfwLibrary.threading = *threading;
if (allocator)
{
// Verify that the specified model is complete
if (!allocator->malloc || !allocator->free)
{
_glfwSetError(GLFW_INVALID_VALUE, NULL);
return GL_FALSE;
}
_glfwLibrary.allocator = *allocator;
}
else
{
// Use the libc malloc and free
_glfwLibrary.allocator.malloc = malloc;
_glfwLibrary.allocator.free = free;
}
// Not all window hints are cleared to zero, so this needs to be here
// despite the memset above
_glfwClearWindowHints();
+7
View File
@@ -228,6 +228,9 @@ struct _GLFWlibrary
GLFWkeyfun keyCallback;
GLFWcharfun charCallback;
GLFWthreadmodel threading;
GLFWallocator allocator;
GLFWgammaramp currentRamp;
GLFWgammaramp originalRamp;
int originalRampSize;
@@ -310,6 +313,10 @@ void* _glfwPlatformGetProcAddress(const char* procname);
// Prototypes for platform independent internal functions
//========================================================================
// Memory management (init.c)
void* _glfwMalloc(size_t size);
void _glfwFree(void* ptr);
// Fullscren management (fullscreen.c)
void _glfwSplitBPP(int bpp, int* red, int* green, int* blue);
+3 -3
View File
@@ -188,7 +188,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found)
return NULL;
}
result = (_GLFWfbconfig*) malloc(sizeof(_GLFWfbconfig) * count);
result = (_GLFWfbconfig*) _glfwMalloc(sizeof(_GLFWfbconfig) * count);
if (!result)
{
_glfwSetError(GLFW_OUT_OF_MEMORY, "Win32/WGL: Failed to allocate _GLFWfbconfig array");
@@ -1182,13 +1182,13 @@ static int choosePixelFormat(_GLFWwindow* window, const _GLFWfbconfig* fbconfig)
closest = _glfwChooseFBConfig(fbconfig, fbconfigs, fbcount);
if (!closest)
{
free(fbconfigs);
_glfwFree(fbconfigs);
return 0;
}
pixelFormat = (int) closest->platformID;
free(fbconfigs);
_glfwFree(fbconfigs);
fbconfigs = NULL;
closest = NULL;
+2 -2
View File
@@ -360,7 +360,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height,
return NULL;
}
window = (_GLFWwindow*) malloc(sizeof(_GLFWwindow));
window = (_GLFWwindow*) _glfwMalloc(sizeof(_GLFWwindow));
if (!window)
{
_glfwSetError(GLFW_OUT_OF_MEMORY, "glfwOpenWindow: Failed to allocate window structure");
@@ -692,7 +692,7 @@ GLFWAPI void glfwCloseWindow(GLFWwindow handle)
*prev = window->next;
}
free(window);
_glfwFree(window);
}
+6 -6
View File
@@ -356,7 +356,7 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
return 0;
}
rgbarray = (int*) malloc(sizeof(int) * viscount);
rgbarray = (int*) _glfwMalloc(sizeof(int) * viscount);
rgbcount = 0;
// Build RGB array
@@ -400,7 +400,7 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
sc = XRRGetScreenInfo(_glfwLibrary.X11.display, _glfwLibrary.X11.root);
sizelist = XRRConfigSizes(sc, &sizecount);
resarray = (struct _glfwResolution*) malloc(sizeof(struct _glfwResolution) * sizecount);
resarray = (struct _glfwResolution*) _glfwMalloc(sizeof(struct _glfwResolution) * sizecount);
for (k = 0; k < sizecount; k++)
{
@@ -417,7 +417,7 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
#if defined(_GLFW_HAS_XF86VIDMODE)
XF86VidModeGetAllModeLines(_glfwLibrary.X11.display, screen, &modecount, &modelist);
resarray = (struct _glfwResolution*) malloc(sizeof(struct _glfwResolution) * modecount);
resarray = (struct _glfwResolution*) _glfwMalloc(sizeof(struct _glfwResolution) * modecount);
for (k = 0; k < modecount; k++)
{
@@ -446,7 +446,7 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
if (!resarray)
{
rescount = 1;
resarray = (struct _glfwResolution*) malloc(sizeof(struct _glfwResolution) * rescount);
resarray = (struct _glfwResolution*) _glfwMalloc(sizeof(struct _glfwResolution) * rescount);
resarray[0].width = DisplayWidth(_glfwLibrary.X11.display, screen);
resarray[0].height = DisplayHeight(_glfwLibrary.X11.display, screen);
@@ -470,8 +470,8 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
// Free visuals list
XFree(vislist);
free(resarray);
free(rgbarray);
_glfwFree(resarray);
_glfwFree(rgbarray);
return count;
}
+3 -3
View File
@@ -304,7 +304,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found)
}
}
result = (_GLFWfbconfig*) malloc(sizeof(_GLFWfbconfig) * count);
result = (_GLFWfbconfig*) _glfwMalloc(sizeof(_GLFWfbconfig) * count);
if (!result)
{
_glfwSetError(GLFW_OUT_OF_MEMORY, "X11/GLX: Failed to allocate _GLFWfbconfig array");
@@ -1341,12 +1341,12 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
result = _glfwChooseFBConfig(fbconfig, fbconfigs, fbcount);
if (!result)
{
free(fbconfigs);
_glfwFree(fbconfigs);
return GL_FALSE;
}
closest = *result;
free(fbconfigs);
_glfwFree(fbconfigs);
}
if (!createContext(window, wndconfig, (GLXFBConfigID) closest.platformID))