// }
//
// Some C functions accept a void* argument that points to an arbitrary
-// data value supplied by the caller. It is not safe to coerce a cgo.Handle
-// (an integer) to a Go unsafe.Pointer, but instead we can pass the address
+// data value supplied by the caller. It is not safe to coerce a [cgo.Handle]
+// (an integer) to a Go [unsafe.Pointer], but instead we can pass the address
// of the cgo.Handle to the void* parameter, as in this variant of the
// previous example:
//
// The intended use is to pass the returned handle to C code, which
// passes it back to Go, which calls Value.
func NewHandle(v any) Handle {
- h := atomic.AddUintptr(&handleIdx, 1)
+ h := handleIdx.Add(1)
if h == 0 {
panic("runtime/cgo: ran out of handle space")
}
var (
handles = sync.Map{} // map[Handle]interface{}
- handleIdx uintptr // atomic
+ handleIdx atomic.Uintptr
)