aboutsummaryrefslogtreecommitdiff
path: root/fptr_ast
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-08-31 14:45:52 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-08-31 14:45:52 +0300
commit752675bc0c9ba6082038bc25cefe838c1f58b5de (patch)
tree358c76ccf2a6195d7f6ab1e4d72b40d32bbb75af /fptr_ast
parenta0c6ff2c222beb72493f2e40ed27492061dfdf22 (diff)
downloadplayground-752675bc0c9ba6082038bc25cefe838c1f58b5de.tar.gz
playground-752675bc0c9ba6082038bc25cefe838c1f58b5de.zip
free resources
Diffstat (limited to 'fptr_ast')
-rw-r--r--fptr_ast/Makefile10
-rw-r--r--fptr_ast/fptr.c100
-rw-r--r--fptr_ast/main.c6
-rw-r--r--fptr_ast/ref.c4
-rw-r--r--fptr_ast/struct.c27
5 files changed, 144 insertions, 3 deletions
diff --git a/fptr_ast/Makefile b/fptr_ast/Makefile
index 530e543..68cc338 100644
--- a/fptr_ast/Makefile
+++ b/fptr_ast/Makefile
@@ -1,10 +1,14 @@
all: struct fptr ref
struct: struct.c main.c common.h
- $(CC) -Wall -Wextra -g -O2 -Ideps/conts/include -DSTRUCT main.c -o struct
+ $(CC) $(CFLAGS) -Wall -Wextra -g -O2 -Ideps/conts/include -DSTRUCT main.c -o struct
fptr: fptr.c main.c common.h
- $(CC) -Wall -Wextra -g -O2 -Ideps/conts/include -DFPTR main.c -o fptr
+ $(CC) $(CFLAGS) -Wall -Wextra -g -O2 -Ideps/conts/include -DFPTR main.c -o fptr
ref: ref.c common.h
- $(CC) -Wall -Wextra -g -O2 ref.c -o ref
+ $(CC) $(CFLAGS) -Wall -Wextra -g -O2 ref.c -o ref
+
+.PHONY: clean
+clean:
+ rm struct fptr ref
diff --git a/fptr_ast/fptr.c b/fptr_ast/fptr.c
index 7959735..4c7f6f1 100644
--- a/fptr_ast/fptr.c
+++ b/fptr_ast/fptr.c
@@ -49,6 +49,12 @@ static proc *gen_proc(int header, ...)
#define gen_proc(...) gen_proc(0, __VA_ARGS__, NULL)
+static void destroy_proc(proc *proc)
+{
+ vars_destroy(&proc->vars);
+ free(proc);
+}
+
static void proc_set(proc *proc, char *name, intptr_t v)
{
foreach(vars, var, &proc->vars) {
@@ -65,9 +71,11 @@ static void proc_set(proc *proc, char *name, intptr_t v)
typedef struct node node;
typedef intptr_t (*node_run_t)(struct node *);
+typedef void (*node_destroy_t)(struct node *);
typedef struct node {
node_run_t run;
+ node_destroy_t destroy;
} node;
static intptr_t run(node *n)
@@ -76,6 +84,12 @@ static intptr_t run(node *n)
return n->run(n);
}
+static void destroy(node *n)
+{
+ assert(n);
+ n->destroy(n);
+}
+
typedef struct load {
node node;
node *addr;
@@ -86,6 +100,12 @@ static intptr_t run_load(load *l)
return *(intptr_t *)run(l->addr);
}
+static void destroy_load(load *l)
+{
+ destroy(l->addr);
+ free(l);
+}
+
static node *gen_load(node *addr)
{
load *l = calloc(1, sizeof(load));
@@ -93,6 +113,7 @@ static node *gen_load(node *addr)
return NULL;
l->node.run = (node_run_t)run_load;
+ l->node.destroy = (node_destroy_t)destroy_load;
l->addr = addr;
return &l->node;
}
@@ -111,6 +132,13 @@ static intptr_t run_store(store *s)
return 0;
}
+static void destroy_store(store *s)
+{
+ destroy(s->addr);
+ destroy(s->val);
+ free(s);
+}
+
static node *gen_store(node *addr, node *val)
{
store *s = calloc(1, sizeof(store));
@@ -118,6 +146,7 @@ static node *gen_store(node *addr, node *val)
return NULL;
s->node.run = (node_run_t)run_store;
+ s->node.destroy = (node_destroy_t)destroy_store;
s->addr = addr;
s->val = val;
return &s->node;
@@ -136,6 +165,13 @@ static intptr_t run_add(add *a)
return l + r;
}
+static void destroy_add(add *a)
+{
+ destroy(a->l);
+ destroy(a->r);
+ free(a);
+}
+
static node *gen_add(node *l, node *r)
{
add *a = calloc(1, sizeof(add));
@@ -143,6 +179,7 @@ static node *gen_add(node *l, node *r)
return NULL;
a->node.run = (node_run_t)run_add;
+ a->node.destroy = (node_destroy_t)destroy_add;
a->l = l;
a->r = r;
return &a->node;
@@ -161,6 +198,13 @@ static intptr_t run_mul(mul *m)
return l * r;
}
+static void destroy_mul(mul *m)
+{
+ destroy(m->l);
+ destroy(m->r);
+ free(m);
+}
+
static node *gen_mul(node *l, node *r)
{
mul *m = calloc(1, sizeof(mul));
@@ -168,6 +212,7 @@ static node *gen_mul(node *l, node *r)
return NULL;
m->node.run = (node_run_t)run_mul;
+ m->node.destroy = (node_destroy_t)destroy_mul;
m->l = l;
m->r = r;
return &m->node;
@@ -183,6 +228,11 @@ static intptr_t run_var(var_n *v)
return *v->varref;
}
+static void destroy_var(var_n *v)
+{
+ free(v);
+}
+
static node *gen_var(proc *proc, char *name)
{
assert(proc && name);
@@ -192,6 +242,7 @@ static node *gen_var(proc *proc, char *name)
return NULL;
v->node.run = (node_run_t)run_var;
+ v->node.destroy = (node_destroy_t)destroy_var;
foreach(vars, var, &proc->vars) {
if (strcmp(var->name, name) != 0)
continue;
@@ -214,6 +265,11 @@ static intptr_t run_varref(varref *v)
return (intptr_t)v->varref;
}
+static void destroy_varref(varref *v)
+{
+ free(v);
+}
+
static node *gen_varref(proc *proc, char *name)
{
assert(proc && name);
@@ -226,6 +282,7 @@ static node *gen_varref(proc *proc, char *name)
return NULL;
v->node.run = (node_run_t)run_varref;
+ v->node.destroy = (node_destroy_t)destroy_varref;
foreach(vars, var, &proc->vars) {
if (strcmp(var->name, name) != 0)
continue;
@@ -248,6 +305,11 @@ static intptr_t run_const_n(const_n *c)
return c->v;
}
+static void destroy_const_n(const_n *c)
+{
+ free(c);
+}
+
static node *gen_const(intptr_t v)
{
const_n *c = calloc(1, sizeof(const_n));
@@ -255,6 +317,7 @@ static node *gen_const(intptr_t v)
return NULL;
c->node.run = (node_run_t)run_const_n;
+ c->node.destroy = (node_destroy_t)destroy_const_n;
c->v = v;
return &c->node;
}
@@ -273,6 +336,13 @@ static intptr_t run_assign(assign *a)
return 0;
}
+static void destroy_assign(assign *a)
+{
+ destroy(a->dst);
+ destroy(a->src);
+ free(a);
+}
+
static node *gen_assign(node *dst, node *src)
{
assign *a = calloc(1, sizeof(assign));
@@ -280,6 +350,7 @@ static node *gen_assign(node *dst, node *src)
return NULL;
a->node.run = (node_run_t)run_assign;
+ a->node.destroy = (node_destroy_t)destroy_assign;
a->dst = dst;
a->src = src;
return &a->node;
@@ -301,6 +372,15 @@ static intptr_t run_for(for_n *f)
return 0;
}
+static void destroy_for(for_n *f)
+{
+ destroy(f->init);
+ destroy(f->cond);
+ destroy(f->post);
+ destroy(f->body);
+ free(f);
+}
+
static node *gen_for(node *init, node *cond, node *post, node *body)
{
for_n *f = calloc(1, sizeof(for_n));
@@ -308,6 +388,7 @@ static node *gen_for(node *init, node *cond, node *post, node *body)
return NULL;
f->node.run = (node_run_t)run_for;
+ f->node.destroy = (node_destroy_t)destroy_for;
f->init = init;
f->cond = cond;
f->post = post;
@@ -328,6 +409,13 @@ static intptr_t run_lt(lt *c)
return l < r;
}
+static void destroy_lt(lt *c)
+{
+ destroy(c->l);
+ destroy(c->r);
+ free(c);
+}
+
static node *gen_lt(node *l, node *r)
{
lt *c = calloc(1, sizeof(lt));
@@ -335,6 +423,7 @@ static node *gen_lt(node *l, node *r)
return NULL;
c->node.run = (node_run_t)run_lt;
+ c->node.destroy = (node_destroy_t)destroy_lt;
c->l = l;
c->r = r;
return &c->node;
@@ -359,6 +448,16 @@ static intptr_t run_body(body *b)
return 0;
}
+static void destroy_body(body *b)
+{
+ foreach(nodes, node, &b->list) {
+ destroy(*node);
+ }
+
+ nodes_destroy(&b->list);
+ free(b);
+}
+
static node *gen_body(int header, ...)
{
(void)header;
@@ -367,6 +466,7 @@ static node *gen_body(int header, ...)
return NULL;
b->node.run = (node_run_t)run_body;
+ b->node.destroy = (node_destroy_t)destroy_body;
b->list = nodes_create(0);
va_list args;
diff --git a/fptr_ast/main.c b/fptr_ast/main.c
index 84d0b17..c33bd72 100644
--- a/fptr_ast/main.c
+++ b/fptr_ast/main.c
@@ -152,4 +152,10 @@ int main()
run(outer_loop);
printf("%zu\n", hash(C));
+
+ destroy(outer_loop);
+ destroy_proc(proc);
+ free(A);
+ free(B);
+ free(C);
}
diff --git a/fptr_ast/ref.c b/fptr_ast/ref.c
index 6169a25..c428566 100644
--- a/fptr_ast/ref.c
+++ b/fptr_ast/ref.c
@@ -30,4 +30,8 @@ int main()
init_matrices(A, B);
matrix_mult(A, B, C);
printf("%zu\n", hash(C));
+
+ free(A);
+ free(B);
+ free(C);
}
diff --git a/fptr_ast/struct.c b/fptr_ast/struct.c
index f22fae4..f33044a 100644
--- a/fptr_ast/struct.c
+++ b/fptr_ast/struct.c
@@ -51,6 +51,12 @@ static proc *gen_proc(int header, ...)
#define gen_proc(...) gen_proc(0, __VA_ARGS__, NULL)
+static void destroy_proc(proc *proc)
+{
+ vars_destroy(&proc->vars);
+ free(proc);
+}
+
static void proc_set(proc *proc, char *name, intptr_t v)
{
foreach(vars, var, &proc->vars) {
@@ -328,3 +334,24 @@ static intptr_t run(node *n)
return 0;
}
+
+/* destruction is easy as hell */
+static void destroy(node *n)
+{
+ if (!n)
+ return;
+
+ destroy(n->n0);
+ destroy(n->n1);
+ destroy(n->n2);
+ destroy(n->n3);
+
+ foreach(nodes, node, &n->nodes) {
+ destroy(*node);
+ }
+
+ nodes_destroy(&n->nodes);
+ free(n);
+
+ /* strings are not owned, nor are references to variables */
+}