summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-12-18 17:35:54 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2019-12-18 17:35:54 -0500
commit32e8dc1da3cfed115fd449667c5b6134705b0089 (patch)
tree467b7b516c503393a7ffc359fc0eeeec47b3e618
parent93093f5a220582ade7e51008d6c234a508381089 (diff)
fixed leaks
-rw-r--r--README.html36
-rw-r--r--allocator.c2
-rw-r--r--arr.c21
-rw-r--r--docs/00.html7
-rw-r--r--docs/01.html4
-rw-r--r--toc.c3
-rw-r--r--types.c27
7 files changed, 64 insertions, 36 deletions
diff --git a/README.html b/README.html
index ca851af..2f08d15 100644
--- a/README.html
+++ b/README.html
@@ -11,17 +11,17 @@ and there are almost definitely bugs right now.</strong>
I would recommend against using it for anything big or important.
Many parts of it may change in the future.</p>
-<p><code>toc</code> improves on C's syntax (and semantics) in many ways,
+<p><code>toc</code> improves on C&rsquo;s syntax (and semantics) in many ways,
To declare <code>x</code> as an integer and set it to 5,
you can do:</p>
<p><code>
-x := 5; // Declare x and set x to 5 (infer type) <br />
-x : int = 5; // Explicitly make the type int. <br />
-x : int; x = 5; // Declare x as an integer, then set it to 5.
+x := 5; // Declare x and set x to 5 (infer type)
+x : int = 5; // Explicitly make the type int.
+x : int; x = 5; // Declare x as an integer, then set it to 5.
</code></p>
-<p><code>toc</code> is statically typed and has many of C's features, but
+<p><code>toc</code> is statically typed and has many of C&rsquo;s features, but
it is nearly as fast in theory.</p>
<p>See <code>docs</code> for more information (in progress).</p>
@@ -37,11 +37,12 @@ it is nearly as fast in theory.</p>
<p><code>toc</code> compiles to C for three reasons:</p>
<ul>
-<li>Speed. C is one of the most performant programming languages out there. It also has compilers which are very good at optimizing (better than anything I could write). </li>
+<li>Speed. C is one of the most performant programming languages out there. It also has compilers which are very good at optimizing (better than anything I could write).</li>
<li>Portability. C is probably the most portable language. It has existed for >30 years and can run on practically anything. Furthermore, all major languages nowadays can call functions written in C.</li>
-<li>Laziness. I don't really want to deal with writing something which outputs machine code, and it would certainly be more buggy than something which outputs C.</li>
+<li>Laziness. I don&rsquo;t really want to deal with writing something which outputs machine code, and it would certainly be more buggy than something which outputs C.</li>
</ul>
+
<hr />
<h3><code>toc</code> Source Code</h3>
@@ -50,15 +51,15 @@ it is nearly as fast in theory.</p>
<h4>Build system</h4>
-<p><code>toc</code> is set up as a unity build, meaning that there is only one translation unit. So, <code>main.c</code> <code>#include</code>s <code>toc.c</code>, which <code>#include</code>s all of <code>toc</code>'s files.</p>
+<p><code>toc</code> is set up as a unity build, meaning that there is only one translation unit. So, <code>main.c</code> <code>#include</code>s <code>toc.c</code>, which <code>#include</code>s all of <code>toc</code>&rsquo;s files.</p>
<h5>Why?</h5>
-<p>This improves compilation speeds (especially from scratch), since you don't have to include headers a bunch of times for each translation unit. This is more of a problem in C++, where, for example, doing <code>#include &lt;map&gt;</code> ends up turning into 25,000 lines after preprocessing. All of toc's source code, which includes most of the C standard library, at the time of this writing (Dec 2019) is only 22,000 lines after preprocessing; imagine including all of that once for each translation unit which includes <code>map</code>. It also obviates the need for fancy build systems like CMake.</p>
+<p>This improves compilation speeds (especially from scratch), since you don&rsquo;t have to include headers a bunch of times for each translation unit. This is more of a problem in C++, where, for example, doing <code>#include &lt;map&gt;</code> ends up turning into 25,000 lines after preprocessing. All of toc&rsquo;s source code, which includes most of the C standard library, at the time of this writing (Dec 2019) is only 22,000 lines after preprocessing; imagine including all of that once for each translation unit which includes <code>map</code>. It also obviates the need for fancy build systems like CMake.</p>
<h4>New features</h4>
-<p>Here are all the C99 features which <code>toc</code> depends on (I might have forgotten some...):</p>
+<p>Here are all the C99 features which <code>toc</code> depends on (I might have forgotten some&hellip;):</p>
<ul>
<li>Declare anywhere</li>
@@ -67,15 +68,17 @@ it is nearly as fast in theory.</p>
<li>Flexible array members</li>
</ul>
+
<p>The last three of those could all be removed fairly easily (assuming the system actually has 8-, 16-, 32-, and 64-bit signed and unsigned types).</p>
<p>And here are all of its C11 features:</p>
<ul>
<li>Anonymous structures/unions</li>
-<li><code>max_align_t</code> and <code>alignof</code> - It can still compile without these but it won't technically be standard-compliant</li>
+<li><code>max_align_t</code> and <code>alignof</code> - It can still compile without these but it won&rsquo;t technically be standard-compliant</li>
</ul>
+
<h4>More</h4>
<p>See <code>main.c</code> for a bit more information.</p>
@@ -93,13 +96,14 @@ it is nearly as fast in theory.</p>
<tr><td>0.1.1</td><td>Better constant parameter inference.</td><td>2019 Dec 16</td></tr>
</table>
+
<hr />
<h3>Report a bug</h3>
-<p>If you find a bug, you can report it through <a href="https://github.com/pommicket/toc/issues">GitHub's issue tracker</a>, or by emailing pommicket@gmail.com.</p>
+<p>If you find a bug, you can report it through <a href="https://github.com/pommicket/toc/issues">GitHub&rsquo;s issue tracker</a>, or by emailing pommicket@gmail.com.</p>
-<p>Just send me the <code>toc</code> source code which results in the bug, and I'll try to fix it. </p>
+<p>Just send me the <code>toc</code> source code which results in the bug, and I&rsquo;ll try to fix it.</p>
<hr />
@@ -114,7 +118,8 @@ int main() {
}
</code></pre>
-<p>Is completely fine. <code>x</code> will hold an unspecified value after the jump (but it isn't used so it doesn't really matter). Apparently, in C++, this is an ill-formed program. This is a bit ridiculous since</p>
+
+<p>Is completely fine. <code>x</code> will hold an unspecified value after the jump (but it isn&rsquo;t used so it doesn&rsquo;t really matter). Apparently, in C++, this is an ill-formed program. This is a bit ridiculous since</p>
<pre><code>
int main() {
@@ -125,4 +130,5 @@ int main() {
}
</code></pre>
-<p>is fine. So that's an interesting little "fun fact": <code>int x = 5;</code> isn't always the same as <code>int x; x = 5;</code> in C++.</p>
+
+<p>is fine. So that&rsquo;s an interesting little &ldquo;fun fact&rdquo;: <code>int x = 5;</code> isn&rsquo;t always the same as <code>int x; x = 5;</code> in C++.</p>
diff --git a/allocator.c b/allocator.c
index 6721e94..da255e4 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 (maybe) 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/arr.c b/arr.c
index 16737d4..4b67f4f 100644
--- a/arr.c
+++ b/arr.c
@@ -130,14 +130,22 @@ static void *arr_end_(void *arr, size_t item_sz) {
}
/* OPTIM: shrink array */
-static void arr_remove_last_(void **arr, size_t item_sz) {
+static void arr_remove_last_(void **arr) {
assert(arr_hdr(*arr)->len);
- if (--arr_hdr(*arr)->len == 0)
- *arr = NULL;
- (void)item_sz;
-
+ if (--arr_hdr(*arr)->len == 0) {
+ arr_clear_(arr);
+ }
}
+static void arr_remove_lasta_(void **arr, size_t item_sz, Allocator *a) {
+ assert(arr_hdr(*arr)->len);
+ if (--arr_hdr(*arr)->len == 0) {
+ arr_cleara_(arr, item_sz, a);
+ }
+}
+
+
+
static void arr_copya_(void **out, void *in, size_t item_sz, Allocator *a) {
size_t len = arr_len(in);
arr_resva_(out, len, item_sz, a);
@@ -179,7 +187,8 @@ You shouldn't rely on this, though, e.g. by doing
/* one past last, or NULL if empty */
#define arr_end(arr) arr_end_((void *)(arr), sizeof *(arr))
#define arr_foreach(arr, type, var) for (type *var = arr, *join(var,_foreach_end) = arr_end(arr); var < join(var,_foreach_end); ++var) /* NOTE: < is useful here because currently it's possible for var_foreach_end to be NULL but var could start out not null */
-#define arr_remove_last(arr) arr_remove_last_((void **)(arr), sizeof **(arr))
+#define arr_remove_last(arr) arr_remove_last_((void **)(arr)), (void)sizeof **(arr)
+#define arr_remove_lasta(arr, a) arr_remove_lasta_((void **)(arr), sizeof **(arr), (a))
#define arr_copya(out, in, a) do { assert(sizeof *(in) == sizeof **(out)); arr_copya_((void **)(out), (in), sizeof **(out), (a)); } while(0)
#ifdef TOC_DEBUG
diff --git a/docs/00.html b/docs/00.html
index cd0c352..b8c961c 100644
--- a/docs/00.html
+++ b/docs/00.html
@@ -30,7 +30,7 @@ x := 0.0;
when no type is specified, it defaults to an <code>int</code>, whereas <code>0.0</code>
defaults to a <code>float</code>.</p>
-<p>Here are all of toc's builtin types and their ranges of values:</p>
+<p>Here are all of toc&rsquo;s builtin types and their ranges of values:</p>
<ul>
<li><code>int</code> - A 64-bit signed integer (always), -9223372036854775808 to 9223372036854775807</li>
@@ -49,13 +49,14 @@ defaults to a <code>float</code>.</p>
<li><code>char</code> - A character. The specific values are technically platform-dependent, but usually there are 256 of them.</li>
</ul>
+
<p>At the moment, it is not technically guaranteed that <code>f32</code>/<code>float</code> is actually 32-bit and that <code>f64</code> is actually 64-bit; they are platform dependent. Perhaps someday there will be a version of toc which does not compile to C, where that could be guaranteed.</p>
<p>To make declarations constant, use <code>::</code> instead of <code>:</code>. e.g.</p>
<p><code>
-x ::= 5+3; <br />
+x ::= 5+3;
y :: float = 5.123;
</code></p>
-<p>Here, "constant" means constant at compile time, not read-only as it does in C. One interesting thing about toc is that normal functions can run at compile time, so pretty much any expression is a valid initializer for a constant, e.g. doing <code>x ::= some_function();</code> runs <code>some_function</code> at compile time, not at run time.</p>
+<p>Here, &ldquo;constant&rdquo; means constant at compile time, not read-only as it does in C. One interesting thing about toc is that normal functions can run at compile time, so pretty much any expression is a valid initializer for a constant, e.g. doing <code>x ::= some_function();</code> runs <code>some_function</code> at compile time, not at run time.</p>
diff --git a/docs/01.html b/docs/01.html
index 633295b..6dce358 100644
--- a/docs/01.html
+++ b/docs/01.html
@@ -7,7 +7,7 @@ main ::= fn() {
};
</code></p>
-<p>It declares a constant, <code>main</code>, which is a function with an empty body. Note that the syntax for declaring functions is the same as the syntax for declaring constants (it isn't something like <code>fn main() { ... }</code>).</p>
+<p>It declares a constant, <code>main</code>, which is a function with an empty body. Note that the syntax for declaring functions is the same as the syntax for declaring constants (it isn&rsquo;t something like <code>fn main() { ... }</code>).</p>
<p>Assuming you have compiled the compiler (see <code>README.md</code> for instructions about that), you can compile it with</p>
@@ -15,4 +15,4 @@ main ::= fn() {
toc &lt;your filename&gt;
</code></p>
-<p>You will get a file called <code>out.c</code>, which you can then put through your C compiler to get an executable file which does nothing. Congratulations! You've written your first toc program.</p>
+<p>You will get a file called <code>out.c</code>, which you can then put through your C compiler to get an executable file which does nothing. Congratulations! You&rsquo;ve written your first toc program.</p>
diff --git a/toc.c b/toc.c
index 1a6684c..e58f4ec 100644
--- a/toc.c
+++ b/toc.c
@@ -3,6 +3,9 @@
This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
+
+/* NOTE: all stages should use the same allocator! */
+
/* Includes all of toc's files */
#include <assert.h>
#include <ctype.h>
diff --git a/types.c b/types.c
index 46b51c4..be95204 100644
--- a/types.c
+++ b/types.c
@@ -337,7 +337,7 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Type *t, U16 flags) {
}
ret:
- arr_remove_last(&tr->blocks);
+ arr_remove_lasta(&tr->blocks, tr->allocr);
tr->block = prev_block;
/* cleanup */
if (entered_fn) {
@@ -854,7 +854,7 @@ static bool types_expr(Typer *tr, Expression *e) {
break;
case EXPR_EACH: {
EachExpr *ea = e->each;
- *(Expression **)arr_add(&tr->in_expr_decls) = e;
+ *(Expression **)typer_arr_add(tr, &tr->in_expr_decls) = e;
if (!each_enter(e)) return false;
if (ea->flags & EACH_IS_RANGE) {
/* TODO: allow user-defined numerical types */
@@ -971,7 +971,7 @@ static bool types_expr(Typer *tr, Expression *e) {
ea->range.stepval = stepval;
}
- arr_remove_last(&tr->in_expr_decls);
+ arr_remove_lasta(&tr->in_expr_decls, tr->allocr);
if (!types_block(tr, &ea->body)) return false;
each_exit(e);
@@ -1252,7 +1252,7 @@ static bool types_expr(Typer *tr, Expression *e) {
arr_foreach(fn->params, Declaration, param) {
arr_foreach(param->idents, Identifier, ident) {
if (param->flags & DECL_INFER) {
- *(Identifier *)arr_add(&inferred_idents) = *ident;
+ *(Identifier *)typer_arr_add(tr, &inferred_idents) = *ident;
} else if ((param->flags & DECL_ANNOTATES_TYPE)
&& !(param->flags & DECL_HAS_EXPR)) {
@@ -1270,11 +1270,18 @@ static bool types_expr(Typer *tr, Expression *e) {
size_t ninferred_idents = arr_len(inferred_idents);
if (ninferred_idents) {
- Value *inferred_vals = typer_malloc(tr, ninferred_idents * sizeof *inferred_vals);
- Type *inferred_types = typer_malloc(tr, ninferred_idents * sizeof *inferred_types);
+ Value *inferred_vals;
+ Type *inferred_types;
+ size_t inferred_vals_size = ninferred_idents * sizeof *inferred_vals;
+ inferred_vals = typer_malloc(tr, inferred_vals_size);
+ size_t inferred_types_size = ninferred_idents * sizeof *inferred_types;
+ inferred_types = typer_malloc(tr, inferred_types_size);
if (!infer_ident_vals(tr, decl_types, arg_types, inferred_idents, inferred_vals, inferred_types))
return false;
+
+ allocr_free(tr->allocr, inferred_idents, ninferred_idents * sizeof *inferred_idents);
+
{
Type *type = inferred_types;
for (i = 0; i < ninferred_idents; ++i) {
@@ -1307,6 +1314,8 @@ static bool types_expr(Typer *tr, Expression *e) {
++i;
}
}
+ allocr_free(tr->allocr, inferred_vals, inferred_vals_size);
+ allocr_free(tr->allocr, inferred_types, inferred_types_size);
}
@@ -1444,7 +1453,7 @@ static bool types_expr(Typer *tr, Expression *e) {
ErrCtx *err_ctx = e->where.ctx;
*(Location *)typer_arr_add(tr, &err_ctx->instance_stack) = e->where;
bool success = types_fn(tr, &c->instance->fn, &f->type, e->where, c->instance);
- arr_remove_last(&err_ctx->instance_stack);
+ arr_remove_lasta(&err_ctx->instance_stack, tr->allocr);
if (!success) return false;
arr_cleara(&table_index_type.tuple, tr->allocr);
}
@@ -1910,7 +1919,7 @@ static bool types_block(Typer *tr, Block *b) {
goto ret;
}
b->ret_expr = e;
- arr_remove_last(&b->stmts);
+ arr_remove_lasta(&b->stmts, tr->allocr);
}
}
}
@@ -2019,7 +2028,7 @@ static bool types_decl(Typer *tr, Declaration *d) {
d->type.kind = TYPE_UNKNOWN;
tr->evalr->enabled = false; /* disable evaluator completely so that it doesn't accidentally try to access this declaration */
}
- arr_remove_last(&tr->in_decls);
+ arr_remove_lasta(&tr->in_decls, tr->allocr);
return success;
}