Skip to content

Commit 9f69286

Browse files
committed
Memory fixes for cachepot and heap
1 parent 84afe38 commit 9f69286

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

src/ast.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,34 @@ void free_expression(expr_t *expr) {
854854
free_expression((expr_t *)expr->add.right);
855855
break;
856856
}
857+
case EXPR_TYPE_CACHEPOT: {
858+
cachepot_t *cachepot = expr->cachepot;
859+
hashtable_t *hash = cachepot->hash;
860+
{
861+
int size;
862+
int i = 0;
863+
struct key_val_pair *ptr1;
864+
struct key_val_pair *ptr2;
865+
866+
if (hash != NULL) {
867+
size = hash->size;
868+
while (i < size) {
869+
ptr1 = hash->table[i];
870+
while (ptr1 != NULL) {
871+
expr_t *e = ptr1->data;
872+
ptr2 = ptr1;
873+
ptr1 = ptr1->next;
874+
free(ptr2->key);
875+
free_expression(e);
876+
free(e);
877+
}
878+
i++;
879+
}
880+
}
881+
}
882+
hashtable_free(hash);
883+
free(cachepot);
884+
} break;
857885
case EXPR_TYPE_DICT: {
858886
dictionary_t *dict = expr->dict;
859887
if (dict->initialized) {

src/ast.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ typedef struct libFunction {
586586
intptr_t p; \
587587
*sb = calloc(sz + 1, sizeof(stackval_t)); \
588588
assert(*sb != NULL); \
589-
p = ((intptr_t) * sb) % sizeof(stackval_t); \
589+
p = ((intptr_t)*sb) % sizeof(stackval_t); \
590590
if (p != 0) { \
591591
p = (sizeof(stackval_t) - (p % sizeof(stackval_t))); \
592592
} \
@@ -600,7 +600,7 @@ typedef struct libFunction {
600600
heapval_t hpbv; \
601601
*hb = calloc(hz + 2, sizeof(heapval_t)); \
602602
assert(*hb != NULL); \
603-
p = ((intptr_t) * hb) % sizeof(heapval_t); \
603+
p = ((intptr_t)*hb) % sizeof(heapval_t); \
604604
p = (sizeof(heapval_t) - (p % sizeof(heapval_t))); \
605605
hpbv.sv.type = INT32TYPE; \
606606
hpbv.sv.i = (int32_t)hz; \
@@ -890,7 +890,7 @@ extern void releaseContext(void *);
890890
hv.sv = *a; \
891891
if (hv.sv.type == TEXT || hv.sv.type == VECTORTYPE || hv.sv.type == DICTTYPE \
892892
|| hv.sv.type == CLASSTYPE || hv.sv.type == RAWDATATYPE || hv.sv.type == BIGINT \
893-
|| hv.sv.type == PRIOQUEUE) { \
893+
|| hv.sv.type == PRIOQUEUE || hv.sv.type == CACHEPOT) { \
894894
hv.toFree = true; \
895895
} else { \
896896
hv.toFree = false; \

src/garbage.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ static void sweep(uint32_t markVal, EXPRESSION_PARAMS()) {
132132
} else if (heap[i].sv.type == BIGINT) {
133133
mpz_clear(*heap[i].sv.bigInt);
134134
free(heap[i].sv.bigInt);
135+
} else if (heap[i].sv.type == CACHEPOT) {
136+
expr_t e;
137+
e.type = EXPR_TYPE_CACHEPOT;
138+
e.cachepot = heap[i].sv.cachepot;
139+
free_expression(&e);
135140
} else if (heap[i].sv.type == RAWDATATYPE) {
136141
free(heap[i].sv.rawdata->data);
137142
free(heap[i].sv.rawdata);
@@ -199,6 +204,11 @@ void free_heap(void *hp, void *hbp) {
199204
} else if (((heapval_t *)hp)[i].sv.type == DICTTYPE) {
200205
hashtable_free(((heapval_t *)hp)[i].sv.dict->hash);
201206
free(((heapval_t *)hp)[i].sv.dict);
207+
} else if (((heapval_t *)hp)[i].sv.type == CACHEPOT) {
208+
expr_t e;
209+
e.type = EXPR_TYPE_CACHEPOT;
210+
e.cachepot = ((heapval_t *)hp)[i].sv.cachepot;
211+
free_expression(&e);
202212
} else if (((heapval_t *)hp)[i].sv.type == PRIOQUEUE) {
203213
priority_queue_t *pq = ((heapval_t *)hp)[i].sv.prioqueue;
204214
int i = 0;

src/interpret.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ interpret_state_t interpret_statements_(void *stmt, PROVIDE_CONTEXT_ARGS(), args
108108
}
109109

110110
/* In case we are dealing with a cachepot */
111-
if (sv.type == CACHEPOT) {
112-
hvp = ast_emalloc(sizeof(heapval_t));
113-
hvp->sv = sv;
114-
} else {
115-
ALLOC_HEAP(&sv, hp, &hvp, &heapUpdated);
116-
}
111+
// if (sv.type == CACHEPOT) {
112+
// hvp = ast_emalloc(sizeof(heapval_t));
113+
// hvp->sv = sv;
114+
//} else {
115+
ALLOC_HEAP(&sv, hp, &hvp, &heapUpdated);
116+
//}
117117

118118
/* Check if the variable is to be put in the class namespace */
119119
if (classCtx != NULL) {

src/library/libprioqueue.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ int ric_heap_pop(LIBRARY_PARAMS()) {
8181
stackval_t stv;
8282
priority_queue_t *arg = NULL;
8383
expr_t *popped = NULL;
84+
heapval_t *hp = PROVIDE_CONTEXT()->hp;
85+
heapval_t *hpv = NULL;
86+
int dummy;
8487
void *sp = PROVIDE_CONTEXT()->sp;
8588
size_t *sc = PROVIDE_CONTEXT()->sc;
8689

@@ -110,6 +113,14 @@ int ric_heap_pop(LIBRARY_PARAMS()) {
110113
popped = priority_queue_pop(arg);
111114

112115
push_expression(popped, EXPRESSION_ARGS());
116+
117+
// allocate the popped argument to the heap so that it
118+
// can be freed later
119+
POP_VAL(&stv, sp, sc);
120+
ALLOC_HEAP(&stv, hp, &hpv, &dummy);
121+
122+
push_expression(popped, EXPRESSION_ARGS());
123+
113124
free(popped);
114125
return 0;
115126
}

0 commit comments

Comments
 (0)