mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 17:28:31 +02:00
refactor(synk): replace runtime weak pointer functions with weak package and simplify buffer handling
This commit is contained in:
@@ -1,34 +1,25 @@
|
|||||||
package synk
|
package synk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
"weak"
|
||||||
)
|
)
|
||||||
|
|
||||||
type weakBuf = unsafe.Pointer
|
type weakBuf = weak.Pointer[[]byte]
|
||||||
|
|
||||||
func makeWeak(b *[]byte) weakBuf {
|
func makeWeak(b *[]byte) weakBuf {
|
||||||
ptr := runtime_registerWeakPointer(unsafe.Pointer(b))
|
return weak.Make(b)
|
||||||
addCleanup(b, addGCed, cap(*b))
|
|
||||||
runtime.KeepAlive(ptr)
|
|
||||||
return weakBuf(ptr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBufFromWeak(w weakBuf) []byte {
|
func getBufFromWeak(w weakBuf) []byte {
|
||||||
ptr := (*[]byte)(runtime_makeStrongFromWeak(w))
|
ptr := w.Value()
|
||||||
if ptr == nil {
|
if ptr != nil {
|
||||||
return 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 {
|
type BytesPool struct {
|
||||||
sizedPool chan weakBuf
|
sizedPool chan weakBuf
|
||||||
unsizedPool chan weakBuf
|
unsizedPool chan weakBuf
|
||||||
|
|||||||
Reference in New Issue
Block a user