Skip to content

Commit 4c5380d

Browse files
committed
vinumc/eval: Track scope namespace and childs alocations with arenas
We need to free the child ptrs and areanas in the end of compilation. To be able to that, add arenas to the evaluation context.
1 parent c961030 commit 4c5380d

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

subprojects/vinumc/eval.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7+
#include <vutils/arena_allocator.h>
78
#include <vutils/str.h>
89
#include <vutils/system_allocator.h>
910
#include <vutils/vec.h>
@@ -26,32 +27,45 @@ struct eval_ctx eval_ctx_new(struct vut_allocator allocator) {
2627
.scopes = VUT_VEC_INIT(struct eval_ctx_scopes_t, allocator),
2728
};
2829

30+
struct vut_arena *arena = vut_allocator_malloc(allocator, sizeof(*arena), 1);
31+
*arena = vut_arena_new(allocator, 10 * 1024);
32+
ret.scopes_childs_arena = vut_arena_to_vut_allocator(arena);
33+
34+
arena = vut_allocator_malloc(allocator, sizeof(*arena), 1);
35+
*arena = vut_arena_new(allocator, 10 * 1024);
36+
ret.scopes_namespace_arena = vut_arena_to_vut_allocator(arena);
37+
2938
return ret;
3039
}
3140

3241
void eval_ctx_free(struct eval_ctx *ctx) {
42+
vut_arena_free_all(ctx->scopes_namespace_arena.base_allocator);
43+
vut_allocator_free(ctx->allocator, ctx->scopes_namespace_arena.base_allocator);
44+
45+
vut_arena_free_all(ctx->scopes_childs_arena.base_allocator);
46+
vut_allocator_free(ctx->allocator, ctx->scopes_childs_arena.base_allocator);
47+
3348
VUT_VEC_FREE(&ctx->scopes);
3449

3550
*ctx = (struct eval_ctx){ 0 };
3651
}
3752

38-
static struct scope scope_new(ast_node_id_t node, int father, struct vut_allocator allocator) {
53+
static struct scope scope_new(struct eval_ctx *ctx, ast_node_id_t node, int father) {
3954
struct scope new_scope = {
4055
.father = father,
4156
.node = node,
42-
.childs = VUT_VEC_INIT(struct scope_childs_t, allocator),
43-
.namespace = VUT_VEC_INIT(struct scope_namespace_t, allocator),
57+
.childs = VUT_VEC_INIT(struct scope_childs_t, ctx->scopes_childs_arena),
58+
.namespace = VUT_VEC_INIT(struct scope_namespace_t, ctx->scopes_namespace_arena),
4459
};
4560
return new_scope;
4661
}
4762

48-
static size_t add_scope_child(struct eval_ctx_scopes_t *scope_array, size_t scope_id,
49-
ast_node_id_t node, struct vut_allocator allocator) {
50-
size_t new_scope_id = scope_array->len;
51-
struct scope new_scope = scope_new(node, scope_id, allocator);
63+
static size_t add_scope_child(struct eval_ctx *ctx, size_t father, ast_node_id_t node) {
64+
size_t new_scope_id = ctx->scopes.len;
65+
struct scope new_scope = scope_new(ctx, node, father);
5266

53-
VUT_VEC_PUT(scope_array, new_scope);
54-
struct scope *scope = &scope_array->base[scope_id];
67+
VUT_VEC_PUT(&ctx->scopes, new_scope);
68+
struct scope *scope = &ctx->scopes.base[father];
5569
VUT_VEC_PUT(&scope->childs, new_scope_id);
5670

5771
return new_scope_id;
@@ -130,8 +144,7 @@ RESOLVE_FUNC_SIGNATURE(resolve_symbols) {
130144
resolve_symbols_assignment(ctx, ast, curr_scope_id, ast_node_id);
131145
} else {
132146
if (ast_get_type(ast, ast_node_id) == CALL)
133-
curr_scope_id = add_scope_child(&ctx->scopes, curr_scope_id, ast_node_id,
134-
ctx->allocator);
147+
curr_scope_id = add_scope_child(ctx, curr_scope_id, ast_node_id);
135148
resolve_symbols_descent(ctx, ast, curr_scope_id, ast_node_id);
136149
}
137150
}
@@ -386,7 +399,7 @@ void unload_libs(struct loaded_lib *loaded_libs, struct vut_allocator alloc) {
386399
}
387400

388401
struct vut_str eval(struct eval_ctx *ctx, struct ast *ast, struct sv_vec *libraries) {
389-
struct scope base_scope = scope_new(0, -1, ctx->allocator);
402+
struct scope base_scope = scope_new(ctx, 0, -1);
390403
VUT_VEC_PUT(&ctx->scopes, base_scope);
391404

392405
struct loaded_lib *loaded_libs = load_libs(ctx, libraries);

subprojects/vinumc/eval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ struct eval_ctx_scopes_t VUT_VEC_DEF(struct scope);
3939

4040
struct eval_ctx {
4141
struct eval_ctx_scopes_t scopes;
42+
4243
struct vut_allocator allocator;
44+
struct vut_allocator scopes_childs_arena;
45+
struct vut_allocator scopes_namespace_arena;
4346
};
4447

4548
struct eval_ctx eval_ctx_new(struct vut_allocator allocator);

0 commit comments

Comments
 (0)