summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--new/alloc_darwin.m16
-rw-r--r--new/alloc_unix.c26
-rw-r--r--new/alloc_windows.c15
-rw-r--r--new/container_unix.c6
-rw-r--r--new/stack.c8
-rw-r--r--new/uipriv.h9
-rw-r--r--new/util_windows.c4
7 files changed, 65 insertions, 19 deletions
diff --git a/new/alloc_darwin.m b/new/alloc_darwin.m
index 4b768b6..df29566 100644
--- a/new/alloc_darwin.m
+++ b/new/alloc_darwin.m
@@ -1,27 +1,34 @@
// 4 december 2014
+#import <stdio.h>
#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/new/alloc_unix.c b/new/alloc_unix.c
index f956bb2..238cf39 100644
--- a/new/alloc_unix.c
+++ b/new/alloc_unix.c
@@ -1,18 +1,36 @@
// 7 april 2015
+#include <stdio.h>
#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/new/alloc_windows.c b/new/alloc_windows.c
index 7922d5d..55ebea7 100644
--- a/new/alloc_windows.c
+++ b/new/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/new/container_unix.c b/new/container_unix.c
index 099944b..5192639 100644
--- a/new/container_unix.c
+++ b/new/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/new/stack.c b/new/stack.c
index 55dc384..433c6b5 100644
--- a/new/stack.c
+++ b/new/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/new/uipriv.h b/new/uipriv.h
index ccd72e8..2999d0c 100644
--- a/new/uipriv.h
+++ b/new/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/new/util_windows.c b/new/util_windows.c
index 8e0532b..4f883b9 100644
--- a/new/util_windows.c
+++ b/new/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);