Redid the GTK+ allocator to be simpler to prepare for the OS X code.
This commit is contained in:
parent
58954ed201
commit
cf44574bbb
40
unix/alloc.c
40
unix/alloc.c
|
@ -2,25 +2,23 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "uipriv_unix.h"
|
#include "uipriv_unix.h"
|
||||||
|
|
||||||
static GHashTable *allocsizes; // map[*void]*size_t
|
|
||||||
|
|
||||||
void initAlloc(void)
|
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 *uiAlloc(size_t size)
|
||||||
{
|
{
|
||||||
void *out;
|
void *out;
|
||||||
size_t *s;
|
|
||||||
|
|
||||||
out = g_malloc0(size);
|
out = g_malloc0(sizeof (size_t) + size);
|
||||||
if (g_hash_table_lookup(allocsizes, out) != NULL)
|
*SIZE(out) = size;
|
||||||
complain("already have a size for %p in uiAlloc()", out);
|
return DATA(out);
|
||||||
s = g_new0(size_t, 1);
|
|
||||||
*s = size;
|
|
||||||
g_hash_table_insert(allocsizes, out, s);
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *uiRealloc(void *p, size_t new)
|
void *uiRealloc(void *p, size_t new)
|
||||||
|
@ -30,26 +28,16 @@ void *uiRealloc(void *p, size_t new)
|
||||||
|
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
return uiAlloc(new);
|
return uiAlloc(new);
|
||||||
|
p = BASE(p);
|
||||||
out = g_realloc(p, new);
|
out = g_realloc(p, new);
|
||||||
s = (size_t *) g_hash_table_lookup(allocsizes, p);
|
s = SIZE(out);
|
||||||
if (s == NULL)
|
|
||||||
complain("no size found for %p in uiRealloc()", p);
|
|
||||||
if (new <= *s)
|
if (new <= *s)
|
||||||
memset(((uint8_t *) out) + *s, 0, new - *s);
|
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
||||||
*s = new;
|
*s = new;
|
||||||
g_hash_table_remove(allocsizes, p);
|
return DATA(out);
|
||||||
g_hash_table_insert(allocsizes, out, s);
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiFree(void *p)
|
void uiFree(void *p)
|
||||||
{
|
{
|
||||||
size_t *s;
|
g_free(BASE(p));
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue