From bbff6f967f36286de0d13158987f99d8e4ec2559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rickard=20Hallerb=C3=A4ck?= Date: Wed, 13 Dec 2023 00:42:43 +0100 Subject: [PATCH] Adding garbage pileup factor, plus minor fix for rehash --- src/ast.h | 2 +- src/garbage.c | 8 ++++++++ src/garbage.h | 2 ++ src/hashtable.c | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ast.h b/src/ast.h index abc5f27..3c7e46b 100644 --- a/src/ast.h +++ b/src/ast.h @@ -855,7 +855,7 @@ extern void releaseContext(void *); } \ if (i == size) { \ fprintf(stderr, "Error: Heap full (size: %d)\n", size); \ - fprintf(stderr, " The heap size can be increased with the -ah flag"); \ + fprintf(stderr, " The heap size can be increased with the -ah flag\n"); \ fprintf(stderr, " See: ric -h for more information\n"); \ exit(1); \ } \ diff --git a/src/garbage.c b/src/garbage.c index 8176d37..2cbba91 100644 --- a/src/garbage.c +++ b/src/garbage.c @@ -158,7 +158,15 @@ static void sweep(uint32_t markVal, EXPRESSION_PARAMS()) { } void mark_and_sweep(hashtable_t *varDecs, EXPRESSION_PARAMS()) { + static unsigned long count = 0; uint32_t markVal; + + count += 1; + + if ( count % GARBAGE_COLLECTION_PILEUP_FACTOR != 0 ) { + return; + } + /* Rest assure, you are doing this safely */ getContext(PROVIDE_CONTEXT()->syncCtx); /* Generate mark value */ diff --git a/src/garbage.h b/src/garbage.h index 3854a95..2bd8faf 100644 --- a/src/garbage.h +++ b/src/garbage.h @@ -8,6 +8,8 @@ #include "hashtable.h" #include +#define GARBAGE_COLLECTION_PILEUP_FACTOR 20 + void mark_and_sweep(hashtable_t *varDecs, EXPRESSION_PARAMS()); void mark(hashtable_t *varDecs, uint32_t markVal, EXPRESSION_PARAMS()); diff --git a/src/hashtable.c b/src/hashtable.c index 61000ec..e295f63 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -43,6 +43,23 @@ void free_hashtable_table(hashtable_t *hash) { free(hash->table); } +void _free_hashtable_table_rehash(hashtable_t *hash) { + int size = hash->size; + int i = 0; + struct key_val_pair *ptr1; + struct key_val_pair *ptr2; + while (i < size) { + ptr1 = hash->table[i]; + while (ptr1 != NULL) { + ptr2 = ptr1; + ptr1 = ptr1->next; + free(ptr2); + } + i++; + } + free(hash->table); +} + void hashtable_rehash(hashtable_t *hashtable) { int size_old = hashtable->size; int newsize = size_old * 2; @@ -57,7 +74,7 @@ void hashtable_rehash(hashtable_t *hashtable) { } i++; } - free_hashtable_table(hashtable); + _free_hashtable_table_rehash(hashtable); hashtable->table = newhash->table; hashtable->size = newsize; }