Added allocation tracking to the GTK+ backend.

This commit is contained in:
Pietro Gagliardi 2015-05-07 23:10:19 -04:00
parent 3af991651d
commit f544414299
3 changed files with 28 additions and 1 deletions

View File

@ -2,8 +2,28 @@
#include <string.h>
#include "uipriv_unix.h"
static GPtrArray *allocations;
void initAlloc(void)
{
allocations = g_ptr_array_new();
}
static void uninitComplain(gpointer ptr, gpointer data)
{
fprintf(stderr, "[libui] %p\n", ptr);
}
// TODO bring back the type names for this
void uninitAlloc(void)
{
if (allocations->len == 0) {
g_ptr_array_free(allocations, TRUE);
return;
}
fprintf(stderr, "[libui] leaked allocations:\n");
g_ptr_array_foreach(allocations, uninitComplain, NULL);
complain("either you left something around or there's a bug in libui");
}
#define UINT8(p) ((uint8_t *) (p))
@ -18,6 +38,7 @@ void *uiAlloc(size_t size)
out = g_malloc0(sizeof (size_t) + size);
*SIZE(out) = size;
g_ptr_array_add(allocations, out);
return DATA(out);
}
@ -34,6 +55,9 @@ void *uiRealloc(void *p, size_t new)
if (new <= *s)
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
*s = new;
if (g_ptr_array_remove(allocations, p) == FALSE)
complain("%p not found in allocations array in uiRealloc()", p);
g_ptr_array_add(allocations, out);
return DATA(out);
}
@ -42,4 +66,6 @@ void uiFree(void *p)
if (p == NULL)
complain("attempt to uiFree(NULL); there's a bug somewhere");
g_free(BASE(p));
if (g_ptr_array_remove(allocations, p) == FALSE)
complain("%p not found in allocations array in uiFree()", p);
}

View File

@ -21,7 +21,7 @@ const char *uiInit(uiInitOptions *o)
void uiUninit(void)
{
// TODO free menus
// TODO uninit alloc
uninitAlloc();
}
void uiFreeInitError(const char *err)

View File

@ -17,3 +17,4 @@ extern void freeMenubar(GtkWidget *);
// alloc.c
extern void initAlloc(void);
extern void uninitAlloc(void);