summaryrefslogtreecommitdiffstats
path: root/src/expr.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-12-20 20:18:35 -0500
committerTavian Barnes <tavianator@tavianator.com>2023-12-20 20:37:44 -0500
commit70092ae5c8f83a99fbc98dc8e2ca2eaab676a5a8 (patch)
tree3c1ff27c143258cdaa4218972b2960ef4653212a /src/expr.c
parent9c6e4ce18304c395338c7c5b2bac9eb89583a568 (diff)
downloadbfs-70092ae5c8f83a99fbc98dc8e2ca2eaab676a5a8.tar.xz
expr: Arena-allocate expressions
Diffstat (limited to 'src/expr.c')
-rw-r--r--src/expr.c22
1 files changed, 8 insertions, 14 deletions
diff --git a/src/expr.c b/src/expr.c
index 380038c..2002fc7 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -3,24 +3,27 @@
#include "expr.h"
#include "alloc.h"
+#include "ctx.h"
#include "eval.h"
#include "exec.h"
#include "printf.h"
#include "xregex.h"
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-struct bfs_expr *bfs_expr_new(bfs_eval_fn *eval_fn, size_t argc, char **argv) {
- struct bfs_expr *expr = ZALLOC(struct bfs_expr);
+struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval_fn, size_t argc, char **argv) {
+ struct bfs_expr *expr = arena_alloc(&ctx->expr_arena);
if (!expr) {
- perror("zalloc()");
return NULL;
}
+ memset(expr, 0, sizeof(*expr));
expr->eval_fn = eval_fn;
expr->argc = argc;
expr->argv = argv;
expr->probability = 0.5;
+ SLIST_PREPEND(&ctx->expr_list, expr);
return expr;
}
@@ -36,21 +39,12 @@ bool bfs_expr_never_returns(const struct bfs_expr *expr) {
return expr->always_true && expr->always_false;
}
-void bfs_expr_free(struct bfs_expr *expr) {
- if (!expr) {
- return;
- }
-
- if (bfs_expr_is_parent(expr)) {
- bfs_expr_free(expr->rhs);
- bfs_expr_free(expr->lhs);
- } else if (expr->eval_fn == eval_exec) {
+void bfs_expr_clear(struct bfs_expr *expr) {
+ if (expr->eval_fn == eval_exec) {
bfs_exec_free(expr->exec);
} else if (expr->eval_fn == eval_fprintf) {
bfs_printf_free(expr->printf);
} else if (expr->eval_fn == eval_regex) {
bfs_regfree(expr->regex);
}
-
- free(expr);
}