Skip to content

Commit

Permalink
Adding garbage pileup factor, plus minor fix for rehash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Dec 12, 2023
1 parent bb6463a commit bbff6f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
} \
Expand Down
8 changes: 8 additions & 0 deletions src/garbage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions src/garbage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "hashtable.h"
#include <time.h>

#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());
Expand Down
19 changes: 18 additions & 1 deletion src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit bbff6f9

Please sign in to comment.