From e2aeef3a863f8ed24248065932b471f2611d29b1 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 27 Sep 2025 11:24:50 +0800 Subject: [PATCH] refactor(synk): replace runtime weak pointer functions with weak package and simplify buffer handling --- internal/utils/synk/pool.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/internal/utils/synk/pool.go b/internal/utils/synk/pool.go index 30ffaa0a..8774d9dd 100644 --- a/internal/utils/synk/pool.go +++ b/internal/utils/synk/pool.go @@ -1,34 +1,25 @@ package synk import ( - "runtime" "sync/atomic" "unsafe" + "weak" ) -type weakBuf = unsafe.Pointer +type weakBuf = weak.Pointer[[]byte] func makeWeak(b *[]byte) weakBuf { - ptr := runtime_registerWeakPointer(unsafe.Pointer(b)) - addCleanup(b, addGCed, cap(*b)) - runtime.KeepAlive(ptr) - return weakBuf(ptr) + return weak.Make(b) } func getBufFromWeak(w weakBuf) []byte { - ptr := (*[]byte)(runtime_makeStrongFromWeak(w)) - if ptr == nil { - return nil + ptr := w.Value() + if ptr != nil { + return *ptr } - return *ptr + return nil } -//go:linkname runtime_registerWeakPointer weak.runtime_registerWeakPointer -func runtime_registerWeakPointer(unsafe.Pointer) unsafe.Pointer - -//go:linkname runtime_makeStrongFromWeak weak.runtime_makeStrongFromWeak -func runtime_makeStrongFromWeak(unsafe.Pointer) unsafe.Pointer - type BytesPool struct { sizedPool chan weakBuf unsizedPool chan weakBuf