Fix large memory chunks allocation with tcg_malloc.

An attempt to allocate a large memory chunk after a small one resulted in
circular links in list of pools.  It caused the same memory being
allocated twice for different arrays.

Now pools for large memory chunks are kept in separate list and are
freed during pool reset because current allocator can not reuse them.

Signed-off-by: Kirill Batuzov <batuzovk@ispras.ru>
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
This commit is contained in:
Kirill Batuzov 2012-03-02 13:22:17 +04:00 committed by Blue Swirl
parent ae7d54d489
commit 4055299ef0
2 changed files with 10 additions and 6 deletions

View File

@ -173,11 +173,9 @@ void *tcg_malloc_internal(TCGContext *s, int size)
/* big malloc: insert a new pool (XXX: could optimize) */ /* big malloc: insert a new pool (XXX: could optimize) */
p = g_malloc(sizeof(TCGPool) + size); p = g_malloc(sizeof(TCGPool) + size);
p->size = size; p->size = size;
if (s->pool_current) p->next = s->pool_first_large;
s->pool_current->next = p; s->pool_first_large = p;
else return p->data;
s->pool_first = p;
p->next = s->pool_current;
} else { } else {
p = s->pool_current; p = s->pool_current;
if (!p) { if (!p) {
@ -208,6 +206,12 @@ void *tcg_malloc_internal(TCGContext *s, int size)
void tcg_pool_reset(TCGContext *s) void tcg_pool_reset(TCGContext *s)
{ {
TCGPool *p, *t;
for (p = s->pool_first_large; p; p = t) {
t = p->next;
g_free(p);
}
s->pool_first_large = NULL;
s->pool_cur = s->pool_end = NULL; s->pool_cur = s->pool_end = NULL;
s->pool_current = NULL; s->pool_current = NULL;
} }

View File

@ -337,7 +337,7 @@ typedef struct TCGContext TCGContext;
struct TCGContext { struct TCGContext {
uint8_t *pool_cur, *pool_end; uint8_t *pool_cur, *pool_end;
TCGPool *pool_first, *pool_current; TCGPool *pool_first, *pool_current, *pool_first_large;
TCGLabel *labels; TCGLabel *labels;
int nb_labels; int nb_labels;
TCGTemp *temps; /* globals first, temps after */ TCGTemp *temps; /* globals first, temps after */