Skip to content

Commit

Permalink
Memory fixes for cachepot and heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Dec 28, 2023
1 parent 84afe38 commit 9f69286
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
28 changes: 28 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,34 @@ void free_expression(expr_t *expr) {
free_expression((expr_t *)expr->add.right);
break;
}
case EXPR_TYPE_CACHEPOT: {
cachepot_t *cachepot = expr->cachepot;
hashtable_t *hash = cachepot->hash;
{
int size;
int i = 0;
struct key_val_pair *ptr1;
struct key_val_pair *ptr2;

if (hash != NULL) {
size = hash->size;
while (i < size) {
ptr1 = hash->table[i];
while (ptr1 != NULL) {
expr_t *e = ptr1->data;
ptr2 = ptr1;
ptr1 = ptr1->next;
free(ptr2->key);
free_expression(e);
free(e);
}
i++;
}
}
}
hashtable_free(hash);
free(cachepot);
} break;
case EXPR_TYPE_DICT: {
dictionary_t *dict = expr->dict;
if (dict->initialized) {
Expand Down
6 changes: 3 additions & 3 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ typedef struct libFunction {
intptr_t p; \
*sb = calloc(sz + 1, sizeof(stackval_t)); \
assert(*sb != NULL); \
p = ((intptr_t) * sb) % sizeof(stackval_t); \
p = ((intptr_t)*sb) % sizeof(stackval_t); \
if (p != 0) { \
p = (sizeof(stackval_t) - (p % sizeof(stackval_t))); \
} \
Expand All @@ -600,7 +600,7 @@ typedef struct libFunction {
heapval_t hpbv; \
*hb = calloc(hz + 2, sizeof(heapval_t)); \
assert(*hb != NULL); \
p = ((intptr_t) * hb) % sizeof(heapval_t); \
p = ((intptr_t)*hb) % sizeof(heapval_t); \
p = (sizeof(heapval_t) - (p % sizeof(heapval_t))); \
hpbv.sv.type = INT32TYPE; \
hpbv.sv.i = (int32_t)hz; \
Expand Down Expand Up @@ -890,7 +890,7 @@ extern void releaseContext(void *);
hv.sv = *a; \
if (hv.sv.type == TEXT || hv.sv.type == VECTORTYPE || hv.sv.type == DICTTYPE \
|| hv.sv.type == CLASSTYPE || hv.sv.type == RAWDATATYPE || hv.sv.type == BIGINT \
|| hv.sv.type == PRIOQUEUE) { \
|| hv.sv.type == PRIOQUEUE || hv.sv.type == CACHEPOT) { \
hv.toFree = true; \
} else { \
hv.toFree = false; \
Expand Down
10 changes: 10 additions & 0 deletions src/garbage.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ static void sweep(uint32_t markVal, EXPRESSION_PARAMS()) {
} else if (heap[i].sv.type == BIGINT) {
mpz_clear(*heap[i].sv.bigInt);
free(heap[i].sv.bigInt);
} else if (heap[i].sv.type == CACHEPOT) {
expr_t e;
e.type = EXPR_TYPE_CACHEPOT;
e.cachepot = heap[i].sv.cachepot;
free_expression(&e);
} else if (heap[i].sv.type == RAWDATATYPE) {
free(heap[i].sv.rawdata->data);
free(heap[i].sv.rawdata);
Expand Down Expand Up @@ -199,6 +204,11 @@ void free_heap(void *hp, void *hbp) {
} else if (((heapval_t *)hp)[i].sv.type == DICTTYPE) {
hashtable_free(((heapval_t *)hp)[i].sv.dict->hash);
free(((heapval_t *)hp)[i].sv.dict);
} else if (((heapval_t *)hp)[i].sv.type == CACHEPOT) {
expr_t e;
e.type = EXPR_TYPE_CACHEPOT;
e.cachepot = ((heapval_t *)hp)[i].sv.cachepot;
free_expression(&e);
} else if (((heapval_t *)hp)[i].sv.type == PRIOQUEUE) {
priority_queue_t *pq = ((heapval_t *)hp)[i].sv.prioqueue;
int i = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/interpret.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ interpret_state_t interpret_statements_(void *stmt, PROVIDE_CONTEXT_ARGS(), args
}

/* In case we are dealing with a cachepot */
if (sv.type == CACHEPOT) {
hvp = ast_emalloc(sizeof(heapval_t));
hvp->sv = sv;
} else {
ALLOC_HEAP(&sv, hp, &hvp, &heapUpdated);
}
// if (sv.type == CACHEPOT) {
// hvp = ast_emalloc(sizeof(heapval_t));
// hvp->sv = sv;
//} else {
ALLOC_HEAP(&sv, hp, &hvp, &heapUpdated);
//}

/* Check if the variable is to be put in the class namespace */
if (classCtx != NULL) {
Expand Down
11 changes: 11 additions & 0 deletions src/library/libprioqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ int ric_heap_pop(LIBRARY_PARAMS()) {
stackval_t stv;
priority_queue_t *arg = NULL;
expr_t *popped = NULL;
heapval_t *hp = PROVIDE_CONTEXT()->hp;
heapval_t *hpv = NULL;
int dummy;
void *sp = PROVIDE_CONTEXT()->sp;
size_t *sc = PROVIDE_CONTEXT()->sc;

Expand Down Expand Up @@ -110,6 +113,14 @@ int ric_heap_pop(LIBRARY_PARAMS()) {
popped = priority_queue_pop(arg);

push_expression(popped, EXPRESSION_ARGS());

// allocate the popped argument to the heap so that it
// can be freed later
POP_VAL(&stv, sp, sc);
ALLOC_HEAP(&stv, hp, &hpv, &dummy);

push_expression(popped, EXPRESSION_ARGS());

free(popped);
return 0;
}

0 comments on commit 9f69286

Please sign in to comment.