@@ -25,10 +25,8 @@ type Pinner struct {
25
25
// objects, these objects must be pinned separately if they are going to be
26
26
// accessed from C code.
27
27
//
28
- // The argument must be a pointer of any type or an
29
- // unsafe.Pointer. It must be the result of calling new,
30
- // taking the address of a composite literal, or taking the address of a
31
- // local variable. If one of these conditions is not met, Pin will panic.
28
+ // The argument must be a pointer of any type or an unsafe.Pointer.
29
+ // It's safe to call Pin on non-Go pointers, in which case Pin will do nothing.
32
30
func (p * Pinner ) Pin (pointer any ) {
33
31
if p .pinner == nil {
34
32
// Check the pinner cache first.
@@ -144,14 +142,17 @@ func isPinned(ptr unsafe.Pointer) bool {
144
142
return pinState .isPinned ()
145
143
}
146
144
147
- // setPinned marks or unmarks a Go pointer as pinned.
145
+ // setPinned marks or unmarks a Go pointer as pinned, when the ptr is a Go pointer.
146
+ // It will be ignored while try to pin a non-Go pointer,
147
+ // and it will be panic while try to unpin a non-Go pointer,
148
+ // which should not happen in normal usage.
148
149
func setPinned (ptr unsafe.Pointer , pin bool ) bool {
149
150
span := spanOfHeap (uintptr (ptr ))
150
151
if span == nil {
151
152
if ! pin {
152
- panic (errorString ("runtime.Pinner.Unpin: unexpected non Go pointer" ))
153
+ panic (errorString ("tried to unpin non- Go pointer" ))
153
154
}
154
- // this is a linker-allocated, zero size object or other object,
155
+ // This is a linker-allocated, zero size object or other object,
155
156
// nothing to do, silently ignore it.
156
157
return false
157
158
}
0 commit comments