From f6df2761187f1975e35eb43461d735d6e325df85 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 17 Feb 2023 08:51:51 +0900 Subject: [PATCH] runtime: allow custom-gc SetFinalizer and clarify KeepAlive --- src/runtime/gc_blocks.go | 4 ++++ src/runtime/gc_custom.go | 4 ++++ src/runtime/gc_leaking.go | 4 ++++ src/runtime/gc_none.go | 4 ++++ src/runtime/runtime.go | 7 +++---- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/runtime/gc_blocks.go b/src/runtime/gc_blocks.go index 59aebb2e..71e88e4a 100644 --- a/src/runtime/gc_blocks.go +++ b/src/runtime/gc_blocks.go @@ -687,3 +687,7 @@ func ReadMemStats(m *MemStats) { m.Frees = gcFrees m.Sys = uint64(heapEnd - heapStart) } + +func SetFinalizer(obj interface{}, finalizer interface{}) { + // Unimplemented. +} diff --git a/src/runtime/gc_custom.go b/src/runtime/gc_custom.go index 45857338..a34b7dce 100644 --- a/src/runtime/gc_custom.go +++ b/src/runtime/gc_custom.go @@ -20,6 +20,7 @@ package runtime // - func free(ptr unsafe.Pointer) // - func markRoots(start, end uintptr) // - func GC() +// - func SetFinalizer(obj interface{}, finalizer interface{}) // - func ReadMemStats(ms *runtime.MemStats) // // @@ -51,6 +52,9 @@ func markRoots(start, end uintptr) // GC is called to explicitly run garbage collection. func GC() +// SetFinalizer registers a finalizer. +func SetFinalizer(obj interface{}, finalizer interface{}) + // ReadMemStats populates m with memory statistics. func ReadMemStats(ms *MemStats) diff --git a/src/runtime/gc_leaking.go b/src/runtime/gc_leaking.go index 2f2bdff1..d99b8d12 100644 --- a/src/runtime/gc_leaking.go +++ b/src/runtime/gc_leaking.go @@ -85,6 +85,10 @@ func GC() { // No-op. } +func SetFinalizer(obj interface{}, finalizer interface{}) { + // No-op. +} + func initHeap() { // preinit() may have moved heapStart; reset heapptr heapptr = heapStart diff --git a/src/runtime/gc_none.go b/src/runtime/gc_none.go index 859c0b5d..98636f5c 100644 --- a/src/runtime/gc_none.go +++ b/src/runtime/gc_none.go @@ -26,6 +26,10 @@ func GC() { // Unimplemented. } +func SetFinalizer(obj interface{}, finalizer interface{}) { + // Unimplemented. +} + func initHeap() { // Nothing to initialize. } diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go index a9778eae..586a610a 100644 --- a/src/runtime/runtime.go +++ b/src/runtime/runtime.go @@ -89,11 +89,10 @@ func UnlockOSThread() { } func KeepAlive(x interface{}) { - // Unimplemented. Only required with SetFinalizer(). -} - -func SetFinalizer(obj interface{}, finalizer interface{}) { // Unimplemented. + // TODO: This function needs to be implemented in a way that LLVM doesn't optimize away the x + // parameter. This will likely need either a volatile operation or calling an assembly stub + // that simply returns. } var godebugUpdate func(string, string)