mie: implement value cleanup
This commit is contained in:
25
mie/block.c
25
mie/block.c
@@ -78,7 +78,32 @@ static struct mie_type *get_type(struct mie_value *v)
|
|||||||
return &label_type;
|
return &label_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cleanup(struct mie_value *value)
|
||||||
|
{
|
||||||
|
struct mie_block *block = MIE_BLOCK(value);
|
||||||
|
|
||||||
|
b_queue_iterator it;
|
||||||
|
b_queue_iterator_begin(&block->b_phi, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator_begin(&block->b_instr, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block->b_terminator) {
|
||||||
|
mie_value_destroy(MIE_VALUE(block->b_terminator));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const struct mie_value_type block_value_type = {
|
const struct mie_value_type block_value_type = {
|
||||||
.t_id = MIE_VALUE_BLOCK,
|
.t_id = MIE_VALUE_BLOCK,
|
||||||
.t_get_type = get_type,
|
.t_get_type = get_type,
|
||||||
|
.t_cleanup = cleanup,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,15 +55,8 @@ struct mie_ctx *mie_ctx_create(void)
|
|||||||
|
|
||||||
void mie_ctx_destroy(struct mie_ctx *ctx)
|
void mie_ctx_destroy(struct mie_ctx *ctx)
|
||||||
{
|
{
|
||||||
if (ctx->ctx_true) {
|
ctx->ctx_true = NULL;
|
||||||
mie_value_destroy(MIE_VALUE(ctx->ctx_true));
|
ctx->ctx_false = NULL;
|
||||||
ctx->ctx_true = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx->ctx_false) {
|
|
||||||
mie_value_destroy(MIE_VALUE(ctx->ctx_false));
|
|
||||||
ctx->ctx_false = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
b_btree_iterator it = {};
|
b_btree_iterator it = {};
|
||||||
b_btree_iterator_begin(&ctx->ctx_int_cache, &it);
|
b_btree_iterator_begin(&ctx->ctx_int_cache, &it);
|
||||||
@@ -84,6 +77,12 @@ void mie_ctx_destroy(struct mie_ctx *ctx)
|
|||||||
free(entry);
|
free(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const size_t nr_types = sizeof ctx->ctx_types / sizeof ctx->ctx_types[0];
|
||||||
|
for (size_t i = 0; i < nr_types; i++) {
|
||||||
|
mie_value_destroy(MIE_VALUE(ctx->ctx_types[i]));
|
||||||
|
ctx->ctx_types[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
free(ctx);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
mie/func.c
23
mie/func.c
@@ -86,7 +86,30 @@ static struct mie_type *get_type(struct mie_value *v)
|
|||||||
return f->f_ret;
|
return f->f_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cleanup(struct mie_value *value)
|
||||||
|
{
|
||||||
|
struct mie_func *func = MIE_FUNC(value);
|
||||||
|
|
||||||
|
b_queue_iterator it;
|
||||||
|
b_queue_iterator_begin(&func->f_args, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator_begin(&func->f_blocks, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
mie_name_map_destroy(func->f_names);
|
||||||
|
}
|
||||||
|
|
||||||
const struct mie_value_type func_value_type = {
|
const struct mie_value_type func_value_type = {
|
||||||
.t_id = MIE_VALUE_FUNC,
|
.t_id = MIE_VALUE_FUNC,
|
||||||
.t_get_type = get_type,
|
.t_get_type = get_type,
|
||||||
|
.t_cleanup = cleanup,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ enum mie_value_flags {
|
|||||||
struct mie_value_type {
|
struct mie_value_type {
|
||||||
enum mie_value_type_id t_id;
|
enum mie_value_type_id t_id;
|
||||||
struct mie_type *(*t_get_type)(struct mie_value *);
|
struct mie_type *(*t_get_type)(struct mie_value *);
|
||||||
|
void (*t_cleanup)(struct mie_value *);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mie_value {
|
struct mie_value {
|
||||||
|
|||||||
35
mie/module.c
35
mie/module.c
@@ -22,6 +22,41 @@ void mie_module_add_function(struct mie_module *mod, struct mie_func *func)
|
|||||||
b_queue_push_back(&mod->m_func, &func->f_base.v_entry);
|
b_queue_push_back(&mod->m_func, &func->f_base.v_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cleanup(struct mie_value *value)
|
||||||
|
{
|
||||||
|
struct mie_module *module = MIE_MODULE(value);
|
||||||
|
|
||||||
|
b_queue_iterator it;
|
||||||
|
b_queue_iterator_begin(&module->m_records, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator_begin(&module->m_data, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator_begin(&module->m_types, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator_begin(&module->m_func, &it);
|
||||||
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
|
struct mie_value *v = b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
b_queue_iterator_erase(&it);
|
||||||
|
mie_value_destroy(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const struct mie_value_type module_value_type = {
|
const struct mie_value_type module_value_type = {
|
||||||
.t_id = MIE_VALUE_MODULE,
|
.t_id = MIE_VALUE_MODULE,
|
||||||
|
.t_cleanup = cleanup,
|
||||||
};
|
};
|
||||||
|
|||||||
11
mie/value.c
11
mie/value.c
@@ -1,5 +1,6 @@
|
|||||||
#include <mie/const.h>
|
#include <mie/const.h>
|
||||||
#include <mie/value.h>
|
#include <mie/value.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
extern const struct mie_value_type module_value_type;
|
extern const struct mie_value_type module_value_type;
|
||||||
extern const struct mie_value_type type_value_type;
|
extern const struct mie_value_type type_value_type;
|
||||||
@@ -32,7 +33,15 @@ void mie_value_init(struct mie_value *val, enum mie_value_type_id type)
|
|||||||
|
|
||||||
void mie_value_destroy(struct mie_value *val)
|
void mie_value_destroy(struct mie_value *val)
|
||||||
{
|
{
|
||||||
/* TODO cleanup value */
|
if (val->v_type && val->v_type->t_cleanup) {
|
||||||
|
val->v_type->t_cleanup(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val->v_name.n_str) {
|
||||||
|
free(val->v_name.n_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mie_type *mie_value_get_type(struct mie_value *val)
|
struct mie_type *mie_value_get_type(struct mie_value *val)
|
||||||
|
|||||||
Reference in New Issue
Block a user