ref: 0e0e474915d2dcf8193f27c5491cd6c1502fc23b
parent: 6a028566c703adb465acab5e662c3b73d5be32ca
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Nov 4 21:32:37 EST 2024
on exit: call gc(0) and call finalizers on all cvalues This allows to flush all iostreams regardless of buffering mode. Test case is rather simple: (princ "hello")
--- a/builtins.c
+++ b/builtins.c
@@ -125,7 +125,7 @@
{
if(nargs > 1)
argcount(nargs, 1);
- exit(nargs > 0 ? tofixnum(args[0]) : 0);
+ fl_exit(nargs > 0 ? tofixnum(args[0]) : 0);
}
BUILTIN("symbol", symbol)
--- a/cvalues.c
+++ b/cvalues.c
@@ -73,7 +73,7 @@
fltype_t *t = cv_class(tmp);
if(t->vtable != nil && t->vtable->finalize != nil)
t->vtable->finalize(tagptr(tmp, TAG_CVALUE));
- if(!isinlined(tmp) && owned(tmp)){
+ if(!isinlined(tmp) && owned(tmp) && !fl_exiting){
memset(cv_data(tmp), 0xbb, cv_len(tmp));
LLT_FREE(cv_data(tmp));
}
@@ -744,6 +744,8 @@
if(t->vtable != nil && t->vtable->relocate != nil)
t->vtable->relocate(v, ncv);
forward(v, ncv);
+ if(fl_exiting)
+ cv_autorelease(ptr(ncv));
return ncv;
}
--- a/flisp.c
+++ b/flisp.c
@@ -83,6 +83,16 @@
uint32_t fl_throwing_frame = 0; // active frame when exception was thrown
value_t fl_lasterror;
+bool fl_exiting = false;
+
+_Noreturn void
+fl_exit(int status)
+{
+ fl_exiting = true;
+ gc(0);
+ exit(status);
+}
+
#define FL_TRY \
fl_exception_context_t _ctx; int l__tr, l__ca; \
_ctx.sp = SP; _ctx.frame = curr_frame; _ctx.rdst = readstate; _ctx.prev = fl_ctx; \
--- a/flisp.h
+++ b/flisp.h
@@ -195,6 +195,9 @@
void fl_init(size_t initial_heapsize);
int fl_load_system_image(value_t ios);
+extern bool fl_exiting;
+_Noreturn void fl_exit(int status);
+
/* collector */
value_t relocate(value_t v);
void gc(int mustgrow);
--- a/flmain.c
+++ b/flmain.c
@@ -24,7 +24,7 @@
return lst;
}
-int
+_Noreturn void
flmain(const char *boot, int bootsz, int argc, char **argv)
{
D_PNAN = D_NNAN = strtod("+NaN", nil);
@@ -58,5 +58,5 @@
ios_putc('\n', ios_stderr);
break;
}
- return r;
+ fl_exit(r);
}
--- a/llt.h
+++ b/llt.h
@@ -30,4 +30,4 @@
extern double D_PNAN, D_NNAN, D_PINF, D_NINF;
extern float F_PNAN, F_NNAN, F_PINF, F_NINF;
-int flmain(const char *boot, int bootsz, int argc, char **argv);
+_Noreturn void flmain(const char *boot, int bootsz, int argc, char **argv);
--- a/main_plan9.c
+++ b/main_plan9.c
@@ -16,5 +16,5 @@
*(uint32_t*)&F_PINF = 0x7f800000;
*(uint32_t*)&F_NINF = 0xff800000;
- exit(flmain(boot, sizeof(boot), argc, argv));
+ flmain(boot, sizeof(boot), argc, argv);
}
--- a/main_posix.c
+++ b/main_posix.c
@@ -18,5 +18,5 @@
setlocale(LC_NUMERIC, "C");
- return flmain(boot, sizeof(boot), argc, argv);
+ flmain(boot, sizeof(boot), argc, argv);
}