From 752675bc0c9ba6082038bc25cefe838c1f58b5de Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 31 Aug 2025 14:45:52 +0300 Subject: free resources --- fptr_ast/fptr.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'fptr_ast/fptr.c') 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; -- cgit v1.2.3