summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-02-07 19:37:55 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-02-07 19:37:55 -0500
commitd81bd17093ecda178bae3281cfe79180fc21a416 (patch)
treeb754e1779200e0dd7f64be4c71267bd090ab9128
parentf94bf833f031611b49fa9674af44dba1f55e7c24 (diff)
fixed leaks
-rw-r--r--cgen.c6
-rw-r--r--data_structures.c10
-rw-r--r--toc.c1
-rw-r--r--types.c4
4 files changed, 15 insertions, 6 deletions
diff --git a/cgen.c b/cgen.c
index 62fd4fc..b6055e2 100644
--- a/cgen.c
+++ b/cgen.c
@@ -13,7 +13,7 @@ static void cgen_create(CGenerator *g, FILE *out, Identifiers *ids, Evaluator *e
g->globals = ids;
g->allocr = allocr;
g->nms_prefix = NULL;
- *(char *)arr_add(&g->nms_prefix) = '\0';
+ *(char *)arr_adda(&g->nms_prefix, g->allocr) = '\0';
}
static bool cgen_stmt(CGenerator *g, Statement *s);
@@ -287,7 +287,7 @@ static void cgen_nms_enter(CGenerator *g, Namespace *n) {
char *s = cgen_nms_prefix(g, n);
size_t chars_so_far = arr_len(g->nms_prefix) - 1; /* -1 for '\0' byte */
size_t new_chars = strlen(s) + 1; /* + 1 for '\0' byte */
- arr_set_len(&g->nms_prefix, chars_so_far + new_chars);
+ arr_set_lena(&g->nms_prefix, chars_so_far + new_chars, g->allocr);
for (size_t i = 0; i < new_chars; ++i) {
g->nms_prefix[i+chars_so_far] = s[i];
}
@@ -296,7 +296,7 @@ static void cgen_nms_enter(CGenerator *g, Namespace *n) {
static void cgen_nms_exit(CGenerator *g, Namespace *n, Namespace *prev) {
char *s = cgen_nms_prefix(g, n);
- arr_set_len(&g->nms_prefix, arr_len(g->nms_prefix) - strlen(s));
+ arr_set_lena(&g->nms_prefix, arr_len(g->nms_prefix) - strlen(s), g->allocr);
g->nms = prev;
free(s);
}
diff --git a/data_structures.c b/data_structures.c
index 8efa124..1aac916 100644
--- a/data_structures.c
+++ b/data_structures.c
@@ -89,6 +89,10 @@ static void arr_cleara_(void **arr, size_t size, Allocator *allocr) {
}
static void arr_set_len_(void **arr, size_t n, size_t item_sz) {
+ if (n == 0) {
+ arr_clear_(arr);
+ return;
+ }
if (n > arr_len(*arr)) {
arr_resv_(arr, n, item_sz);
}
@@ -96,6 +100,10 @@ static void arr_set_len_(void **arr, size_t n, size_t item_sz) {
/* OPTIM: shrink */
}
static void arr_set_lena_(void **arr, size_t n, size_t item_sz, Allocator *a) {
+ if (n == 0) {
+ arr_cleara_(arr, item_sz, a);
+ return;
+ }
arr_resva_(arr, n, item_sz, a);
arr_hdr(*arr)->len = n;
}
@@ -272,7 +280,7 @@ static void str_hash_table_grow(StrHashTable *t) {
if (slots_cap <= 2 * t->nentries) {
StrHashTableSlot **new_slots = NULL;
size_t new_slots_cap = slots_cap * 2 + 10;
- arr_set_len(&new_slots, new_slots_cap);
+ arr_set_lena(&new_slots, new_slots_cap, t->allocr);
arr_zero(new_slots);
arr_foreach(t->slots, StrHashTableSlotPtr, slotp) {
StrHashTableSlot *slot = *slotp;
diff --git a/toc.c b/toc.c
index 3b09888..1f5ef0e 100644
--- a/toc.c
+++ b/toc.c
@@ -107,6 +107,7 @@ static char *read_file_contents(Allocator *a, const char *filename, Location whe
}
}
++contents;
+ fclose(in);
return contents;
}
diff --git a/types.c b/types.c
index ede7275..560f0fc 100644
--- a/types.c
+++ b/types.c
@@ -20,12 +20,12 @@ static inline void *typer_arr_add_(Typer *tr, void **arr, size_t sz) {
}
static inline void typer_block_enter(Typer *tr, Block *b) {
- *(Block **)arr_add(&tr->blocks) = b;
+ *(Block **)arr_adda(&tr->blocks, tr->allocr) = b;
tr->block = b;
}
static inline void typer_block_exit(Typer *tr) {
- arr_remove_last(&tr->blocks);
+ arr_remove_lasta(&tr->blocks, tr->allocr);
tr->block = *(Block **)arr_last(tr->blocks);
}