aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2023-08-04 21:31:59 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2023-08-04 21:31:59 +0300
commit7cc838733848c58ea1c88fb477cfeefc941e3f32 (patch)
tree9126a797f62ef69278ee9502c043b7e59e0f2fc1
parentbf993be4a04bc7279f7d49ae18f185e1dca36479 (diff)
downloadcopyjit-7cc838733848c58ea1c88fb477cfeefc941e3f32.tar.gz
copyjit-7cc838733848c58ea1c88fb477cfeefc941e3f32.zip
add compile_destroy as cleanup
-rw-r--r--.gitignore1
-rw-r--r--Makefile4
-rw-r--r--examples/main.c1
-rw-r--r--src/copyjit.c5
-rw-r--r--src/copyjit.h2
-rw-r--r--tests/check.c10
6 files changed, 21 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 5cc42f1..bbdf58f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,5 +13,6 @@ build
copyjit
libcopyjit.a
examples/copyjit
+tests/check
!include/copyjit
!lib/gen/.gitkeep
diff --git a/Makefile b/Makefile
index 7a207a7..477ef64 100644
--- a/Makefile
+++ b/Makefile
@@ -120,9 +120,9 @@ libcopyjit.a: $(OBJS)
# could probably make this a bit prettier
.PHONY: clean
clean:
- @$(RM) -r build copyjit lib/gen/* lib/*.bin lib/*.d lib/empty lib/prune deps.mk
+ @$(RM) -r build lib/gen/* lib/*.bin lib/*.d lib/empty lib/prune deps.mk
@$(RM) -r lib/*decls.h lib/*defns.h lib/ops.h lib/imm.h
- @$(RM) tests/check
+ @$(RM) tests/check examples/copyjit libcopyjit.a
.PHONY: clean_docs
clean_docs:
diff --git a/examples/main.c b/examples/main.c
index 94e372b..90fb21d 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -32,4 +32,5 @@ int main()
compile_finish(&ctx);
printf("%lu\n", run(&ctx));
+ compile_destroy(&ctx);
}
diff --git a/src/copyjit.c b/src/copyjit.c
index 0de85d3..96616e3 100644
--- a/src/copyjit.c
+++ b/src/copyjit.c
@@ -471,6 +471,11 @@ void compile_start(ctx_t *ctx)
ctx->pc = ctx->buf;
}
+void compile_destroy(ctx_t *ctx)
+{
+ munmap(ctx->buf, ctx->size);
+}
+
reloc_t compile_placeholder(ctx_t *ctx, compile_callback_t call)
{
reloc_t r = {0};
diff --git a/src/copyjit.h b/src/copyjit.h
index 8170204..6104800 100644
--- a/src/copyjit.h
+++ b/src/copyjit.h
@@ -44,6 +44,8 @@ void compile_patch(reloc_t reloc, void *addr);
void compile_start(ctx_t *ctx);
void compile_finish(ctx_t *ctx);
+void compile_destroy(ctx_t *ctx);
+
unsigned long run(ctx_t *ctx);
#endif /* HMMJIT_H */
diff --git a/tests/check.c b/tests/check.c
index f6e775c..168f02f 100644
--- a/tests/check.c
+++ b/tests/check.c
@@ -16,6 +16,7 @@ static void check_li()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// y
@@ -34,6 +35,7 @@ static void check_li()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// o
@@ -50,6 +52,7 @@ static void check_li()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// a
@@ -66,6 +69,7 @@ static void check_li()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
}
@@ -83,6 +87,7 @@ static void check_add()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// y
@@ -101,6 +106,7 @@ static void check_add()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// o
@@ -117,6 +123,7 @@ static void check_add()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
// a
@@ -133,6 +140,7 @@ static void check_add()
compile_finish(&ctx);
assert(run(&ctx) == i);
+ compile_destroy(&ctx);
}
}
@@ -156,6 +164,7 @@ static void check_branch()
compile_finish(&ctx);
assert(run(&ctx) == (i != 0));
+ compile_destroy(&ctx);
}
for (size_t i = 0; i < 2; ++i) {
@@ -176,6 +185,7 @@ static void check_branch()
compile_finish(&ctx);
assert(run(&ctx) == (i == 0));
+ compile_destroy(&ctx);
}
}