File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ type Pinner struct {
2424// contains pointers to Go objects, these objects must be pinned separately if they
2525// are going to be accessed from C code.
2626//
27- // The argument must be a pointer of any type or an [unsafe.Pointer].
27+ // The argument must be a pointer of any type or an [unsafe.Pointer] or string or slice .
2828// It's safe to call Pin on non-Go pointers, in which case Pin will do nothing.
2929func (p * Pinner ) Pin (pointer any ) {
3030 if p .pinner == nil {
@@ -114,6 +114,8 @@ func pinnerGetPtr(i *any) unsafe.Pointer {
114114 data = e .data
115115 case kindString :
116116 data = unsafe .Pointer (unsafe .StringData (* (* string )(e .data )))
117+ case kindSlice :
118+ data = unsafe .Pointer (unsafe .SliceData (* (* []byte )(e .data )))
117119 default :
118120 panic (errorString ("runtime.Pinner: argument is not a pointer or string: " + toRType (etyp ).string ()))
119121 }
Original file line number Diff line number Diff line change @@ -559,3 +559,20 @@ func TestPinnerPinString(t *testing.T) {
559559func getHeapStr () string {
560560 return string (byte (fastrand ()))
561561}
562+
563+ func TestPinnerPinSlice (t * testing.T ) {
564+ var pinner runtime.Pinner
565+ s := make ([]* int , 10 )
566+ pinner .Pin (s )
567+ addr := unsafe .Pointer (unsafe .SliceData (s ))
568+ if ! runtime .IsPinned (addr ) {
569+ t .Fatal ("not marked as pinned" )
570+ }
571+ if runtime .GetPinCounter (addr ) != nil {
572+ t .Fatal ("pin counter should not exist" )
573+ }
574+ pinner .Unpin ()
575+ if runtime .IsPinned (addr ) {
576+ t .Fatal ("still marked as pinned" )
577+ }
578+ }
You can’t perform that action at this time.
0 commit comments