summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-01-12 14:19:38 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-01-12 14:19:38 -0500
commit46e58f70eb8fde8741705d5ae20692187d915cbc (patch)
tree0fb2e05eb1c25af3273245da7f6be522f6fe2d33
parent7659a5ff48bb51baa6fcf5dfe13c8583413f7f4a (diff)
fixed memory leaks
-rw-r--r--allocator.c2
-rw-r--r--identifiers.c6
-rw-r--r--main.c3
-rw-r--r--package.c12
-rw-r--r--tokenizer.c2
-rw-r--r--types.c12
-rw-r--r--types.h12
7 files changed, 34 insertions, 15 deletions
diff --git a/allocator.c b/allocator.c
index adf67c5..3ad11c1 100644
--- a/allocator.c
+++ b/allocator.c
@@ -8,7 +8,7 @@ static void *err_malloc(size_t bytes);
static void *err_calloc(size_t n, size_t sz);
static void *err_realloc(void *prev, size_t new_size);
#ifdef TOC_DEBUG
-#define NO_ALLOCATOR 1 /* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator */
+/* #define NO_ALLOCATOR 1 /\* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator *\/ */
#endif
/* number of bytes a page hold, not including the header */
#define PAGE_BYTES (16384 - sizeof(Page))
diff --git a/identifiers.c b/identifiers.c
index 6c17cd7..9b9fff0 100644
--- a/identifiers.c
+++ b/identifiers.c
@@ -214,8 +214,10 @@ static IdentDecl *ident_decl(Identifier i) {
}
static void idents_free(Identifiers *ids) {
- arr_foreach(ids->slots, IdentSlotPtr, slot) {
- free(*slot);
+ arr_foreach(ids->slots, IdentSlotPtr, slotp) {
+ IdentSlot *slot = *slotp;
+ if (slot) arr_clear(&slot->decls);
+ free(slot);
}
arr_clear(&ids->slots);
}
diff --git a/main.c b/main.c
index 4dc2785..f5a7398 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@
/*
TODO:
+compile without -Wno-unused-function
packages
---
X ::= newtype(int); or something
@@ -151,7 +152,7 @@ int main(int argc, char **argv) {
free(contents - 1); /* -1 because we put a 0 byte at the beginning */
allocr_free_all(&main_allocr);
evalr_free(&ev);
-
+ typer_free(&tr);
fclose(out);
idents_free(&idents);
return 0;
diff --git a/package.c b/package.c
index 78c6a3f..7024bd8 100644
--- a/package.c
+++ b/package.c
@@ -246,16 +246,15 @@ static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname
return false;
fseek(f, decls_offset, SEEK_SET);
/* read declarations */
- Statement *stmts = NULL;
+ p->stmts = NULL;
while (import_u8(&i)) {
- Statement *s = arr_add(&stmts);
+ Statement *s = arr_add(&p->stmts);
s->kind = STMT_DECL;
import_decl(&i, &s->decl);
}
free(i.ident_map);
- /* TOOD: LEAK! exit at some point */
- if (!block_enter(NULL, stmts, 0))
+ if (!block_enter(NULL, p->stmts, 0))
return false;
return true;
@@ -1195,3 +1194,8 @@ static bool import_footer(Importer *im) {
return true;
}
+
+static void package_free(Package *pkg) {
+ idents_free(&pkg->idents);
+ arr_clear(&pkg->stmts);
+}
diff --git a/tokenizer.c b/tokenizer.c
index 48579bc..72eb005 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -217,7 +217,7 @@ until everything is done
*/
static void tokr_create(Tokenizer *t, Identifiers *idents, ErrCtx *err_ctx, Allocator *allocr) {
t->tokens = NULL;
- arr_resv(&t->tokens, 256);
+ arr_resva(&t->tokens, 256, allocr);
t->allocr = allocr;
t->idents = idents;
t->err_ctx = err_ctx;
diff --git a/types.c b/types.c
index 36876c6..07b46d9 100644
--- a/types.c
+++ b/types.c
@@ -986,7 +986,7 @@ static bool types_expr(Typer *tr, Expression *e) {
e->pkg.name_ident = name_ident;
if (!name_ident->pkg) {
char *filename = typer_malloc(tr, name_str_len + 5);
- Package *pkg = name_ident->pkg = allocr_calloc(tr->allocr, 1, sizeof *pkg);
+ Package *pkg = name_ident->pkg = err_calloc(1, sizeof *pkg);
memcpy(filename, name_str.data, name_str_len);
strcpy(filename + name_str.n, ".top");
/* TODO: library paths */
@@ -999,6 +999,7 @@ static bool types_expr(Typer *tr, Expression *e) {
if (!import_pkg(tr->allocr, pkg, fp, filename, tr->idents, tr->err_ctx, e->where)) {
return false;
}
+ *(Package **)arr_add(&tr->pkgs) = pkg;
fclose(fp);
}
} break;
@@ -2288,6 +2289,7 @@ static bool types_stmt(Typer *tr, Statement *s) {
static void typer_create(Typer *tr, Evaluator *ev, ErrCtx *err_ctx, Allocator *allocr, Identifiers *idents) {
tr->block = NULL;
tr->blocks = NULL;
+ tr->pkgs = NULL;
tr->fn = NULL;
tr->evalr = ev;
tr->err_ctx = err_ctx;
@@ -2352,3 +2354,11 @@ static bool types_file(Typer *tr, ParsedFile *f) {
return ret;
}
+static void typer_free(Typer *tr) {
+ typedef Package *PackagePtr;
+ arr_foreach(tr->pkgs, PackagePtr, pkg) {
+ package_free(*pkg);
+ free(*pkg);
+ }
+ arr_clear(&tr->pkgs);
+}
diff --git a/types.h b/types.h
index 1f6a337..4bea917 100644
--- a/types.h
+++ b/types.h
@@ -762,6 +762,12 @@ typedef struct Evaluator {
bool enabled;
} Evaluator;
+typedef struct Package {
+ Identifier name;
+ Identifiers idents;
+ Statement *stmts;
+} Package;
+
typedef struct Typer {
Allocator *allocr;
Evaluator *evalr;
@@ -776,13 +782,9 @@ typedef struct Typer {
ErrCtx *err_ctx;
/* for checking for problematic struct circular dependencies */
bool *is_reference_stack;
+ Package **pkgs; /* all packages which have been imported */
} Typer;
-typedef struct Package {
- Identifier name;
- Identifiers idents;
-} Package;
-
typedef struct Exporter {
FILE *out; /* .top (toc package) to output to */
bool started;