Implemented the memory logging.

This commit is contained in:
Pietro Gagliardi 2015-04-07 23:40:18 -04:00
parent f3e7227739
commit cdd37793c0
7 changed files with 65 additions and 19 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

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

View File

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

View File

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

View File

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