mirror of
https://github.com/ceph/go-ceph
synced 2024-12-24 07:03:00 +00:00
780f7014a2
Since Go 1.21 there is a runtime.Pinner API that allows to safely pass structures with embedded Go pointers to C code. In earlier Go version we know that the garbage collector is non-moving, so it is safe to pass Go pointers to C as well. This change adds two implementations of PtrGuard, one for pre 1.21 that is basically a no-op, and one for 1.21+ that uses runtime.Pinner. Signed-off-by: Sven Anderson <sven@redhat.com>
38 lines
868 B
Go
38 lines
868 B
Go
//go:build !go1.21
|
|
// +build !go1.21
|
|
|
|
// This code assumes a non-moving garbage collector, which is the case until at
|
|
// least go 1.20
|
|
|
|
package cutil
|
|
|
|
import (
|
|
"unsafe"
|
|
)
|
|
|
|
// PtrGuard respresents a guarded Go pointer (pointing to memory allocated by Go
|
|
// runtime) stored in C memory (allocated by C)
|
|
type PtrGuard struct {
|
|
cPtr CPtr
|
|
goPtr unsafe.Pointer
|
|
}
|
|
|
|
// NewPtrGuard writes the goPtr (pointing to Go memory) into C memory at the
|
|
// position cPtr, and returns a PtrGuard object.
|
|
func NewPtrGuard(cPtr CPtr, goPtr unsafe.Pointer) *PtrGuard {
|
|
var v PtrGuard
|
|
v.cPtr = cPtr
|
|
v.goPtr = goPtr
|
|
p := (*unsafe.Pointer)(unsafe.Pointer(cPtr))
|
|
*p = goPtr
|
|
return &v
|
|
}
|
|
|
|
// Release removes the guarded Go pointer from the C memory by overwriting it
|
|
// with NULL.
|
|
func (v *PtrGuard) Release() {
|
|
p := (*unsafe.Pointer)(unsafe.Pointer(v.cPtr))
|
|
*p = nil
|
|
v.goPtr = nil
|
|
}
|