Fixed up realloc() to zero extra memory on GTK+. Thanks to Rena (irc.badnik.net) and hyphenated (irc.freenode.net/#go-nuts) for confirming my memset() and spotting errors.
This commit is contained in:
parent
8b8be286ff
commit
58954ed201
38
unix/alloc.c
38
unix/alloc.c
|
@ -1,27 +1,55 @@
|
||||||
// 7 april 2015
|
// 7 april 2015
|
||||||
#include <stdio.h>
|
#include <string.h>
|
||||||
#include "uipriv_unix.h"
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
void *uiAlloc(size_t size)
|
void *uiAlloc(size_t size)
|
||||||
{
|
{
|
||||||
void *out;
|
void *out;
|
||||||
|
size_t *s;
|
||||||
|
|
||||||
out = g_malloc0(size);
|
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;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *uiRealloc(void *p, size_t size)
|
void *uiRealloc(void *p, size_t new)
|
||||||
{
|
{
|
||||||
void *out;
|
void *out;
|
||||||
|
size_t *s;
|
||||||
|
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
return uiAlloc(size);
|
return uiAlloc(new);
|
||||||
// TODO fill with 0s
|
out = g_realloc(p, new);
|
||||||
out = g_realloc(p, size);
|
s = (size_t *) g_hash_table_lookup(allocsizes, p);
|
||||||
|
if (s == NULL)
|
||||||
|
complain("no size found for %p in uiRealloc()", p);
|
||||||
|
if (new <= *s)
|
||||||
|
memset(((uint8_t *) out) + *s, 0, new - *s);
|
||||||
|
*s = new;
|
||||||
|
g_hash_table_remove(allocsizes, p);
|
||||||
|
g_hash_table_insert(allocsizes, out, s);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiFree(void *p)
|
void uiFree(void *p)
|
||||||
{
|
{
|
||||||
|
size_t *s;
|
||||||
|
|
||||||
g_free(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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ const char *uiInit(uiInitOptions *o)
|
||||||
g_error_free(err);
|
g_error_free(err);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
initAlloc();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,3 +14,6 @@
|
||||||
// menu.c
|
// menu.c
|
||||||
extern GtkWidget *makeMenubar(uiWindow *);
|
extern GtkWidget *makeMenubar(uiWindow *);
|
||||||
extern void freeMenubar(GtkWidget *);
|
extern void freeMenubar(GtkWidget *);
|
||||||
|
|
||||||
|
// alloc.c
|
||||||
|
extern void initAlloc(void);
|
||||||
|
|
Loading…
Reference in New Issue