Added allocation tracking to the GTK+ backend.
This commit is contained in:
parent
3af991651d
commit
f544414299
26
unix/alloc.c
26
unix/alloc.c
|
@ -2,8 +2,28 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "uipriv_unix.h"
|
#include "uipriv_unix.h"
|
||||||
|
|
||||||
|
static GPtrArray *allocations;
|
||||||
|
|
||||||
void initAlloc(void)
|
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))
|
#define UINT8(p) ((uint8_t *) (p))
|
||||||
|
@ -18,6 +38,7 @@ void *uiAlloc(size_t size)
|
||||||
|
|
||||||
out = g_malloc0(sizeof (size_t) + size);
|
out = g_malloc0(sizeof (size_t) + size);
|
||||||
*SIZE(out) = size;
|
*SIZE(out) = size;
|
||||||
|
g_ptr_array_add(allocations, out);
|
||||||
return DATA(out);
|
return DATA(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +55,9 @@ void *uiRealloc(void *p, size_t new)
|
||||||
if (new <= *s)
|
if (new <= *s)
|
||||||
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
||||||
*s = new;
|
*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);
|
return DATA(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,4 +66,6 @@ void uiFree(void *p)
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
complain("attempt to uiFree(NULL); there's a bug somewhere");
|
complain("attempt to uiFree(NULL); there's a bug somewhere");
|
||||||
g_free(BASE(p));
|
g_free(BASE(p));
|
||||||
|
if (g_ptr_array_remove(allocations, p) == FALSE)
|
||||||
|
complain("%p not found in allocations array in uiFree()", p);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ const char *uiInit(uiInitOptions *o)
|
||||||
void uiUninit(void)
|
void uiUninit(void)
|
||||||
{
|
{
|
||||||
// TODO free menus
|
// TODO free menus
|
||||||
// TODO uninit alloc
|
uninitAlloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiFreeInitError(const char *err)
|
void uiFreeInitError(const char *err)
|
||||||
|
|
|
@ -17,3 +17,4 @@ extern void freeMenubar(GtkWidget *);
|
||||||
|
|
||||||
// alloc.c
|
// alloc.c
|
||||||
extern void initAlloc(void);
|
extern void initAlloc(void);
|
||||||
|
extern void uninitAlloc(void);
|
||||||
|
|
Loading…
Reference in New Issue