From 5fe238773ec92e3588b1420126d15fade7872973 Mon Sep 17 00:00:00 2001 From: Jake Hughes Date: Mon, 9 Sep 2024 17:56:42 +0100 Subject: [PATCH] Allow external use of keep-alive API The current implementation is a macro which means it cannot be used directly in Alloy. --- include/gc/gc.h | 7 +++++++ mark.c | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/include/gc/gc.h b/include/gc/gc.h index 8a5c5ad3c..0b80d0f27 100644 --- a/include/gc/gc.h +++ b/include/gc/gc.h @@ -1518,6 +1518,13 @@ GC_API int GC_CALL GC_invoke_finalizers(void); # define GC_reachable_here(ptr) GC_noop1((GC_word)(ptr)) #endif +/* Explicitly tell the collector that an object is reachable */ +/* at a particular program point. This prevents the argument */ +/* pointer from being optimized away, even it is otherwise no */ +/* longer needed. Used to prevent finalizers from running when */ +/* the associated object is still in use. */ +GC_API void GC_CALL GC_keep_alive(GC_word); + /* Make the argument appear live to compiler. Should be robust against */ /* the whole program analysis. */ GC_API void GC_CALL GC_noop1(GC_word); diff --git a/mark.c b/mark.c index 988c186f2..b6c4f95bf 100644 --- a/mark.c +++ b/mark.c @@ -54,6 +54,11 @@ GC_API void GC_CALL GC_noop1(GC_word x) # endif } +GC_API void GC_keep_alive(GC_word x) +{ + GC_reachable_here(x); +} + /* Initialize GC_obj_kinds properly and standard free lists properly. */ /* This must be done statically since they may be accessed before */ /* GC_init is called. */