2015-04-22 16:54:05 -05:00
|
|
|
// 7 april 2015
|
2015-05-04 18:26:05 -05:00
|
|
|
#include <string.h>
|
2015-04-22 16:54:05 -05:00
|
|
|
#include "uipriv_unix.h"
|
|
|
|
|
2015-05-07 22:10:19 -05:00
|
|
|
static GPtrArray *allocations;
|
|
|
|
|
2015-05-08 09:24:03 -05:00
|
|
|
#define UINT8(p) ((uint8_t *) (p))
|
|
|
|
#define PVOID(p) ((void *) (p))
|
|
|
|
#define EXTRA (sizeof (size_t) + sizeof (const char **))
|
|
|
|
#define DATA(p) PVOID(UINT8(p) + EXTRA)
|
|
|
|
#define BASE(p) PVOID(UINT8(p) - EXTRA)
|
|
|
|
#define SIZE(p) ((size_t *) (p))
|
|
|
|
#define CCHAR(p) ((const char **) (p))
|
|
|
|
#define TYPE(p) CCHAR(UINT8(p) + sizeof (size_t))
|
|
|
|
|
2015-05-04 18:26:05 -05:00
|
|
|
void initAlloc(void)
|
|
|
|
{
|
2015-05-07 22:10:19 -05:00
|
|
|
allocations = g_ptr_array_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninitComplain(gpointer ptr, gpointer data)
|
|
|
|
{
|
2016-05-13 20:00:12 -05:00
|
|
|
char **str = (char **) data;
|
|
|
|
char *str2;
|
|
|
|
|
|
|
|
if (*str == NULL)
|
|
|
|
*str = g_strdup_printf("");
|
|
|
|
str2 = g_strdup_printf("%s%p %s\n", *str, ptr, *TYPE(ptr));
|
|
|
|
g_free(*str);
|
|
|
|
*str = str2;
|
2015-05-07 22:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uninitAlloc(void)
|
|
|
|
{
|
2016-05-13 20:00:12 -05:00
|
|
|
char *str = NULL;
|
|
|
|
|
2015-05-07 22:10:19 -05:00
|
|
|
if (allocations->len == 0) {
|
|
|
|
g_ptr_array_free(allocations, TRUE);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-13 20:00:12 -05:00
|
|
|
g_ptr_array_foreach(allocations, uninitComplain, &str);
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivUserBug("Some data was leaked; either you left a uiControl lying around or there's a bug in libui itself. Leaked data:\n%s", str);
|
2016-05-13 20:00:12 -05:00
|
|
|
g_free(str);
|
2015-05-04 18:26:05 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 15:36:03 -05:00
|
|
|
void *uiprivAlloc(size_t size, const char *type)
|
2015-04-22 16:54:05 -05:00
|
|
|
{
|
|
|
|
void *out;
|
|
|
|
|
2015-05-08 09:24:03 -05:00
|
|
|
out = g_malloc0(EXTRA + size);
|
2015-05-04 18:55:39 -05:00
|
|
|
*SIZE(out) = size;
|
2015-05-08 09:24:03 -05:00
|
|
|
*TYPE(out) = type;
|
2015-05-07 22:10:19 -05:00
|
|
|
g_ptr_array_add(allocations, out);
|
2015-05-04 18:55:39 -05:00
|
|
|
return DATA(out);
|
2015-04-22 16:54:05 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 15:36:03 -05:00
|
|
|
void *uiprivRealloc(void *p, size_t new, const char *type)
|
2015-04-22 16:54:05 -05:00
|
|
|
{
|
|
|
|
void *out;
|
2015-05-04 18:26:05 -05:00
|
|
|
size_t *s;
|
2015-04-22 16:54:05 -05:00
|
|
|
|
|
|
|
if (p == NULL)
|
2018-04-15 15:36:03 -05:00
|
|
|
return uiprivAlloc(new, type);
|
2015-05-04 18:55:39 -05:00
|
|
|
p = BASE(p);
|
2015-05-08 09:24:03 -05:00
|
|
|
out = g_realloc(p, EXTRA + new);
|
2015-05-04 18:55:39 -05:00
|
|
|
s = SIZE(out);
|
2017-02-20 14:14:53 -06:00
|
|
|
if (new > *s)
|
2015-05-04 18:55:39 -05:00
|
|
|
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
2015-05-04 18:26:05 -05:00
|
|
|
*s = new;
|
2015-05-07 22:10:19 -05:00
|
|
|
if (g_ptr_array_remove(allocations, p) == FALSE)
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivImplBug("%p not found in allocations array in uiprivRealloc()", p);
|
2015-05-07 22:10:19 -05:00
|
|
|
g_ptr_array_add(allocations, out);
|
2015-05-04 18:55:39 -05:00
|
|
|
return DATA(out);
|
2015-04-22 16:54:05 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 15:36:03 -05:00
|
|
|
void uiprivFree(void *p)
|
2015-04-22 16:54:05 -05:00
|
|
|
{
|
2015-05-07 16:45:34 -05:00
|
|
|
if (p == NULL)
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivImplBug("attempt to uiprivFree(NULL)");
|
2015-05-07 22:35:43 -05:00
|
|
|
p = BASE(p);
|
|
|
|
g_free(p);
|
2015-05-07 22:10:19 -05:00
|
|
|
if (g_ptr_array_remove(allocations, p) == FALSE)
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivImplBug("%p not found in allocations array in uiprivFree()", p);
|
2015-04-22 16:54:05 -05:00
|
|
|
}
|