Skip to content

Commit

Permalink
Fix leak with strings, re issue #162
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed May 11, 2023
1 parent 498af06 commit ef5c770
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,34 @@ void trim_heap(query *q)

cell *c = a->heap;

for (pl_idx_t i = 0; i < a->hp; i++, c++)
for (pl_idx_t i = 0; i < a->hp; i++, c++) {
unshare_cell(c);
init_cell(c);
}

page *save = a;
q->pages = a = a->next;
free(save->heap);
free(save);
}

#define DEBUG_TRIM 0

const page *a = q->pages;

if (DEBUG_TRIM) printf("*** %u : %u -> %u\n", a->nbr, q->st.hp, a->hp);

for (pl_idx_t i = q->st.hp; a && (i < a->hp); i++) {
cell *c = a->heap + i;
if (DEBUG_TRIM) if (is_managed(c)) printf("*** got one\n");
unshare_cell(c);
init_cell(c);
}

if (DEBUG_TRIM) printf("\n");
}

static bool is_in_ref_list(const cell *c, pl_idx_t c_ctx, const reflist *rlist)
bool is_in_ref_list(const cell *c, pl_idx_t c_ctx, const reflist *rlist)
{
while (rlist) {
if ((c == rlist->ptr) && (c_ctx == rlist->ctx))
Expand Down Expand Up @@ -497,8 +514,8 @@ static cell *deep_clone2_to_tmp(query *q, cell *p1, pl_idx_t p1_ctx, unsigned de

cell *save_p1 = p1;
pl_idx_t save_p1_ctx = p1_ctx;
//p1 = deref(q, p1, p1_ctx);
//p1_ctx = q->latest_ctx;
p1 = deref(q, p1, p1_ctx);
p1_ctx = q->latest_ctx;

pl_idx_t save_idx = tmp_heap_used(q);
cell *tmp = alloc_on_tmp(q, 1);
Expand Down Expand Up @@ -664,29 +681,23 @@ cell *clone_to_heap(query *q, bool prefix, cell *p1, unsigned extras)

if (prefix) {
// Needed for follow() to work
tmp->tag = TAG_INTERNED;
tmp->arity = 0;
*tmp = (cell){0};
tmp->tag = TAG_EMPTY;
tmp->nbr_cells = 1;
tmp->flags = FLAG_BUILTIN;

static builtins *s_fn_ptr = NULL;

if (!s_fn_ptr)
s_fn_ptr = get_fn_ptr(fn_iso_true_0);

tmp->fn_ptr = s_fn_ptr;
}

cell *src = p1, *dst = tmp+(prefix?1:0);

for (pl_idx_t i = 0; i < p1->nbr_cells; i++, dst++) {
*dst = *src++;
share_cell(dst);
for (pl_idx_t i = 0; i < p1->nbr_cells; i++, dst++, src++) {
*dst = *src;
share_cell(src);

if (is_var(dst) && !is_ref(dst)) {
dst->flags |= FLAG_VAR_REF;
dst->var_ctx = q->st.curr_frame;
}
if (!is_var(src) || is_ref(src))
continue;

dst->flags |= FLAG_VAR_REF;
dst->var_ctx = q->st.curr_frame;
}

return tmp;
Expand Down

0 comments on commit ef5c770

Please sign in to comment.