Added type names to the memory allocator on Windows. More TODOs.

This commit is contained in:
Pietro Gagliardi 2015-05-08 10:34:32 -04:00
parent 53a4eefe2a
commit b60f17ee5a
5 changed files with 27 additions and 14 deletions

View File

@ -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()");
}

View File

@ -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);

View File

@ -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++;

View File

@ -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()");

View File

@ -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()");