Some allocator cleanup in the new Table.

This commit is contained in:
Pietro Gagliardi 2014-12-07 16:22:51 -05:00
parent 75c9f73870
commit e224950cf4
2 changed files with 36 additions and 6 deletions

View File

@ -72,11 +72,7 @@ static LRESULT CALLBACK tableWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
if (uMsg == WM_NCCREATE) {
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
// TODO find something that DOES set a last error code to use instead, or figure out how to abuse SEH temporarily so we can use HeapAlloc()...
t = (struct table *) malloc(sizeof (struct table));
if (t == NULL)
panic("error allocating internal Table data structure");
ZeroMemory(t, sizeof (struct table));
t = (struct table *) tableAlloc(sizeof (struct table), "error allocating internal Table data structure");
t->hwnd = hwnd;
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) t);
}
@ -87,7 +83,7 @@ static LRESULT CALLBACK tableWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
if (uMsg == WM_DESTROY) {
// TODO free appropriate (after figuring this part out) components of t
// TODO send DESTROY events to accessibility listeners (when appropriate)
free(t);
tableFree(t, "error allocating internal Table data structure");
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
if (runHandlers(handlers, t, uMsg, wParam, lParam, &lResult))

View File

@ -12,3 +12,37 @@ static BOOL runHandlers(const handlerfunc list[], struct table *t, UINT uMsg, WP
return TRUE;
return FALSE;
}
// memory allocation stuff
// each of these functions do an implicit ZeroMemory()
// we're using LocalAlloc() because:
// - whether the malloc() family supports the last-error code is undefined
// - the HeapAlloc() family DOES NOT support the last-error code; you're supposed to use Windows exceptions, and I can't find a clean way to do this with MinGW-w64 that doesn't rely on inline assembly or external libraries (unless they added __try/__except blocks)
// - there's no VirtualReAlloc() to complement VirtualAlloc() and I'm not sure if we can even get the original allocated size back out reliably to write it ourselves (http://blogs.msdn.com/b/oldnewthing/archive/2012/03/16/10283988.aspx)
// needless to say, TODO
static void *tableAlloc(size_t size, const char *panicMessage)
{
HLOCAL out;
out = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, size);
if (out == NULL)
panic(panicMessage);
return (void *) out;
}
static void *tableRealloc(void *p, size_t size, const char *panicMessage)
{
HLOCAL out;
out = LocalReAlloc((HLOCAL) p, size, LMEM_ZEROINIT);
if (out == NULL)
panic(panicMessage);
return (void *) out;
}
static void tableFree(void *p, const char *panicMessage)
{
if (LocalFree((HLOCAL) p) != NULL)
panic(panicMessage);
}