File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ type Pinner struct {
24
24
// contains pointers to Go objects, these objects must be pinned separately if they
25
25
// are going to be accessed from C code.
26
26
//
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 .
28
28
// It's safe to call Pin on non-Go pointers, in which case Pin will do nothing.
29
29
func (p * Pinner ) Pin (pointer any ) {
30
30
if p .pinner == nil {
@@ -114,6 +114,8 @@ func pinnerGetPtr(i *any) unsafe.Pointer {
114
114
data = e .data
115
115
case kindString :
116
116
data = unsafe .Pointer (unsafe .StringData (* (* string )(e .data )))
117
+ case kindSlice :
118
+ data = unsafe .Pointer (unsafe .SliceData (* (* []byte )(e .data )))
117
119
default :
118
120
panic (errorString ("runtime.Pinner: argument is not a pointer or string: " + toRType (etyp ).string ()))
119
121
}
Original file line number Diff line number Diff line change @@ -559,3 +559,20 @@ func TestPinnerPinString(t *testing.T) {
559
559
func getHeapStr () string {
560
560
return string (byte (fastrand ()))
561
561
}
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