From b60f17ee5a7d579e08f871a6ae5fffb07b1108e5 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 8 May 2015 10:34:32 -0400 Subject: [PATCH] Added type names to the memory allocator on Windows. More TODOs. --- windows/alloc.c | 23 +++++++++++++++++------ windows/init.c | 2 +- windows/menu.c | 8 +++++--- windows/text.c | 6 +++--- windows/util.c | 2 +- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/windows/alloc.c b/windows/alloc.c index 1acb069f..302229f5 100644 --- a/windows/alloc.c +++ b/windows/alloc.c @@ -15,25 +15,35 @@ int initAlloc(void) return heap != NULL; } -void *uiAlloc(size_t size) +#define UINT8(p) ((uint8_t *) (p)) +#define PVOID(p) ((void *) (p)) +#define EXTRA (sizeof (const char **)) +#define DATA(p) PVOID(UINT8(p) + EXTRA) +#define BASE(p) PVOID(UINT8(p) - EXTRA) +#define CCHAR(p) ((const char **) (p)) +#define TYPE(p) CCHAR(UINT8(p)) + +void *uiAlloc(size_t size, const char *type) { void *out; - out = HeapAlloc(heap, HEAP_ZERO_MEMORY, size); + out = HeapAlloc(heap, HEAP_ZERO_MEMORY, EXTRA + size); if (out == NULL) { fprintf(stderr, "memory exhausted in uiAlloc()\n"); abort(); } - return out; + *TYPE(out) = type; + return DATA(out); } -void *uiRealloc(void *p, size_t size) +void *uiRealloc(void *p, size_t size, const char *type) { void *out; if (p == NULL) - return uiAlloc(size); - out = HeapReAlloc(heap, HEAP_ZERO_MEMORY, p, size); + return uiAlloc(size, type); + p = BASE(p); + out = HeapReAlloc(heap, HEAP_ZERO_MEMORY, p, EXTRA + size); if (out == NULL) { fprintf(stderr, "memory exhausted in uiRealloc()\n"); abort(); @@ -45,6 +55,7 @@ void uiFree(void *p) { if (p == NULL) complain("attempt to uiFree(NULL); there's a bug somewhere"); + p = BASE(p); if (HeapFree(heap, 0, p) == 0) logLastError("error freeing memory in uiFree()"); } diff --git a/windows/init.c b/windows/init.c index c07f9d46..dc679ba1 100644 --- a/windows/init.c +++ b/windows/init.c @@ -41,7 +41,7 @@ static const char *loadLastError(const char *message) } wmessage = toUTF16(message); n = _scwprintf(initErrorFormat, initErrorArgs); - wstr = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR)); + wstr = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); snwprintf(wstr, n + 1, initErrorFormat, initErrorArgs); str = toUTF8(wstr); uiFree(wstr); diff --git a/windows/menu.c b/windows/menu.c index f0835ce7..92e46850 100644 --- a/windows/menu.c +++ b/windows/menu.c @@ -1,6 +1,8 @@ // 24 april 2015 #include "uipriv_windows.h" +// TODO migrate to ptrArray + static struct menu **menus = NULL; static uintmax_t len = 0; static uintmax_t cap = 0; @@ -117,7 +119,7 @@ static uiMenuItem *newItem(struct menu *m, int type, const char *name) if (m->len >= m->cap) { m->cap += grow; - m->items = (struct menuItem **) uiRealloc(m->items, m->cap * sizeof (struct menuItem *)); + m->items = (struct menuItem **) uiRealloc(m->items, m->cap * sizeof (struct menuItem *), "struct menuItem *[]"); } item = uiNew(struct menuItem); @@ -208,7 +210,7 @@ uiMenu *uiNewMenu(const char *name) complain("attempt to create a new menu after menus have been finalized"); if (len >= cap) { cap += grow; - menus = (struct menu **) uiRealloc(menus, cap * sizeof (struct menu *)); + menus = (struct menu **) uiRealloc(menus, cap * sizeof (struct menu *), "struct menu *[]"); } m = uiNew(struct menu); @@ -244,7 +246,7 @@ static void appendMenuItem(HMENU menu, struct menuItem *item) if (item->len >= item->cap) { item->cap += grow; - item->hmenus = (HMENU *) uiRealloc(item->hmenus, item->cap * sizeof (HMENU)); + item->hmenus = (HMENU *) uiRealloc(item->hmenus, item->cap * sizeof (HMENU), "HMENU[]"); } item->hmenus[item->len] = menu; item->len++; diff --git a/windows/text.c b/windows/text.c index d31f7a38..8d6d0b0a 100644 --- a/windows/text.c +++ b/windows/text.c @@ -13,7 +13,7 @@ WCHAR *toUTF16(const char *str) n = MBTWC(str, NULL, 0); if (n == 0) logLastError("error figuring out number of characters to convert to in toUTF16()"); - wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR)); + wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]"); if (MBTWC(str, wstr, n) != n) logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); return wstr; @@ -29,7 +29,7 @@ char *toUTF8(const WCHAR *wstr) n = WCTMB(wstr, NULL, 0); if (n == 0) logLastError("error figuring out number of characters to convert to in toUTF8()"); - str = (char *) uiAlloc(n * sizeof (char)); + str = (char *) uiAlloc(n * sizeof (char), "char[]"); if (WCTMB(wstr, str, n) != n) logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()"); return str; @@ -42,7 +42,7 @@ WCHAR *windowText(HWND hwnd) n = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0); // WM_GETTEXTLENGTH does not include the null terminator - text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR)); + text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); // note the comparison: the size includes the null terminator, but the return does not if (GetWindowTextW(hwnd, text, n + 1) != n) logLastError("error getting window text in windowText()"); diff --git a/windows/util.c b/windows/util.c index c8705450..4eb61b50 100644 --- a/windows/util.c +++ b/windows/util.c @@ -17,7 +17,7 @@ intmax_t uiWindowsWindowTextWidth(HWND hwnd) len = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0); if (len == 0) // no text; nothing to do return 0; - text = (WCHAR *) uiAlloc((len + 1) * sizeof (WCHAR)); + text = (WCHAR *) uiAlloc((len + 1) * sizeof (WCHAR), "WCHAR[]"); // note the comparison: the size includes the null terminator, but the return does not if (GetWindowText(hwnd, text, len + 1) != len) logLastError("error getting window text in uiWindowsWindowTextWidth()");