diff --git a/unix/alloc.c b/unix/alloc.c index b2512b20..0e1c77e8 100644 --- a/unix/alloc.c +++ b/unix/alloc.c @@ -2,25 +2,23 @@ #include #include "uipriv_unix.h" -static GHashTable *allocsizes; // map[*void]*size_t - void initAlloc(void) { - allocsizes = g_hash_table_new(g_direct_hash, g_direct_equal); } +#define UINT8(p) ((uint8_t *) (p)) +#define PVOID(p) ((void *) (p)) +#define DATA(p) PVOID(UINT8(p) + sizeof (size_t)) +#define BASE(p) PVOID(UINT8(p) - sizeof (size_t)) +#define SIZE(p) ((size_t *) (p)) + void *uiAlloc(size_t size) { void *out; - size_t *s; - out = g_malloc0(size); - if (g_hash_table_lookup(allocsizes, out) != NULL) - complain("already have a size for %p in uiAlloc()", out); - s = g_new0(size_t, 1); - *s = size; - g_hash_table_insert(allocsizes, out, s); - return out; + out = g_malloc0(sizeof (size_t) + size); + *SIZE(out) = size; + return DATA(out); } void *uiRealloc(void *p, size_t new) @@ -30,26 +28,16 @@ void *uiRealloc(void *p, size_t new) if (p == NULL) return uiAlloc(new); + p = BASE(p); out = g_realloc(p, new); - s = (size_t *) g_hash_table_lookup(allocsizes, p); - if (s == NULL) - complain("no size found for %p in uiRealloc()", p); + s = SIZE(out); if (new <= *s) - memset(((uint8_t *) out) + *s, 0, new - *s); + memset(((uint8_t *) DATA(out)) + *s, 0, new - *s); *s = new; - g_hash_table_remove(allocsizes, p); - g_hash_table_insert(allocsizes, out, s); - return out; + return DATA(out); } void uiFree(void *p) { - size_t *s; - - g_free(p); - s = (size_t *) g_hash_table_lookup(allocsizes, p); - if (s == NULL) - complain("no size found for %p in uiFree()", p); - g_hash_table_remove(allocsizes, p); - g_free(s); + g_free(BASE(p)); }