diff --git a/alloc_darwin.m b/alloc_darwin.m index 4b768b66..df29566c 100644 --- a/alloc_darwin.m +++ b/alloc_darwin.m @@ -1,27 +1,34 @@ // 4 december 2014 +#import #import "uipriv_darwin.h" // TODO is there a better alternative to NSCAssert()? preferably a built-in allocator that panics on out of memory for us? -void *uiAlloc(size_t size) +void *uiAlloc(size_t size, const char *type) { void *out; out = malloc(size); NSCAssert(out != NULL, @"out of memory in uiAlloc()"); memset(out, 0, size); +#ifdef uiLogAllocations + fprintf(stderr, "%p alloc %s\n", out, type); +#endif return 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); + return uiAlloc(size, type); out = realloc(p, size); NSCAssert(out != NULL, @"out of memory in uiRealloc()"); // TODO zero the extra memory +#ifdef uiLogAllocations + fprintf(stderr, "%p realloc %p\n", p, out); +#endif return out; } @@ -30,4 +37,7 @@ void uiFree(void *p) if (p == NULL) return; free(p); +#ifdef uiLogAllocations + fprintf(stderr, "%p free\n", p); +#endif } diff --git a/alloc_unix.c b/alloc_unix.c index f956bb2e..238cf39e 100644 --- a/alloc_unix.c +++ b/alloc_unix.c @@ -1,18 +1,36 @@ // 7 april 2015 +#include #include "uipriv_unix.h" -void *uiAlloc(size_t size) +void *uiAlloc(size_t size, const char *type) { - return g_malloc0(size); + void *out; + + out = g_malloc0(size); +#ifdef uiLogAllocations + fprintf(stderr, "%p alloc %s\n", out, type); +#endif + return 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, type); // TODO fill with 0s - return g_realloc(p, size); + out = g_realloc(p, size); +#ifdef uiLogAllocations + fprintf(stderr, "%p realloc %p\n", p, out); +#endif + return out; } void uiFree(void *p) { g_free(p); +#ifdef uiLogAllocations + fprintf(stderr, "%p free\n", p); +#endif } diff --git a/alloc_windows.c b/alloc_windows.c index 7922d5d2..55ebea70 100644 --- a/alloc_windows.c +++ b/alloc_windows.c @@ -7,7 +7,7 @@ // passing NULL to tableRealloc() acts like tableAlloc() // passing NULL to tableFree() is a no-op -void *uiAlloc(size_t size) +void *uiAlloc(size_t size, const char *type) { void *out; @@ -15,19 +15,25 @@ void *uiAlloc(size_t size) if (out == NULL) abort(); // TODO figure this part out ZeroMemory(out, size); +#ifdef uiLogAllocations + fprintf(stderr, "%p alloc %s\n", out, type); +#endif return 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); + return uiAlloc(size, type); out = realloc(p, size); if (out == NULL) abort(); // TODO zero the extra memory +#ifdef uiLogAllocations + fprintf(stderr, "%p realloc %p\n", p, out); +#endif return out; } @@ -36,4 +42,7 @@ void uiFree(void *p) if (p == NULL) return; free(p); +#ifdef uiLogAllocations + fprintf(stderr, "%p free\n", p); +#endif } diff --git a/container_unix.c b/container_unix.c index 099944b8..51926398 100644 --- a/container_unix.c +++ b/container_unix.c @@ -5,6 +5,9 @@ G_DEFINE_TYPE(uiContainer, uiContainer, GTK_TYPE_CONTAINER) static void uiContainer_init(uiContainer *c) { +#ifdef uiLogAllocations + fprintf(stderr, "%p alloc uiContainer\n", c); +#endif c->children = g_ptr_array_new(); gtk_widget_set_has_window(GTK_WIDGET(c), FALSE); } @@ -18,6 +21,9 @@ static void uiContainer_dispose(GObject *obj) static void uiContainer_finalize(GObject *obj) { G_OBJECT_CLASS(uiContainer_parent_class)->finalize(obj); +#ifdef uiLogAllocations + fprintf(stderr, "%p free\n", obj); +#endif } static void uiContainer_add(GtkContainer *container, GtkWidget *widget) diff --git a/stack.c b/stack.c index 55dc384f..433c6b5c 100644 --- a/stack.c +++ b/stack.c @@ -188,10 +188,10 @@ void uiStackAdd(uiControl *st, uiControl *c, int stretchy) if (s->len >= s->cap) { s->cap += stackCapGrow; - s->controls = (uiControl **) uiRealloc(s->controls, s->cap * sizeof (uiControl *)); - s->stretchy = (int *) uiRealloc(s->stretchy, s->cap * sizeof (int)); - s->width = (intmax_t *) uiRealloc(s->width, s->cap * sizeof (intmax_t)); - s->height = (intmax_t *) uiRealloc(s->height, s->cap * sizeof (intmax_t)); + s->controls = (uiControl **) uiRealloc(s->controls, s->cap * sizeof (uiControl *), "uiControl *[]"); + s->stretchy = (int *) uiRealloc(s->stretchy, s->cap * sizeof (int), "int[]"); + s->width = (intmax_t *) uiRealloc(s->width, s->cap * sizeof (intmax_t), "intmax_t[]"); + s->height = (intmax_t *) uiRealloc(s->height, s->cap * sizeof (intmax_t), "intmax_t[]"); } s->controls[s->len] = c; s->stretchy[s->len] = stretchy; diff --git a/uipriv.h b/uipriv.h index ccd72e84..2999d0cf 100644 --- a/uipriv.h +++ b/uipriv.h @@ -18,7 +18,10 @@ struct uiControl { void (*resize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *); }; -extern void *uiAlloc(size_t); -#define uiNew(T) ((T *) uiAlloc(sizeof (T))) -extern void *uiRealloc(void *, size_t); +// TODO write this comment +#define uiLogAllocations + +extern void *uiAlloc(size_t, const char *); +#define uiNew(T) ((T *) uiAlloc(sizeof (T), #T )) +extern void *uiRealloc(void *, size_t, const char *); extern void uiFree(void *); diff --git a/util_windows.c b/util_windows.c index 8e0532b5..4f883b97 100644 --- a/util_windows.c +++ b/util_windows.c @@ -11,7 +11,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; @@ -30,7 +30,7 @@ intmax_t uiWindowsWindowTextWidth(HWND hwnd) len = GetWindowTextLengthW(hwnd); 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[]"); if (GetWindowText(hwnd, text, len + 1) == 0) // should only happen on error given explicit test for len == 0 above logLastError("error getting window text in uiWindowsWindowTextWidth()"); dc = GetDC(hwnd);