Source file
test/uintptrescapes3.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "runtime"
14 "unsafe"
15 )
16
17 var callback func()
18
19
20
21 func F(ptr uintptr) { callback() }
22
23
24
25 func Fv(ptrs ...uintptr) { callback() }
26
27 type T struct{}
28
29
30
31 func (T) M(ptr uintptr) { callback() }
32
33
34
35 func (T) Mv(ptrs ...uintptr) { callback() }
36
37
38
39 var tests = []func(ptr unsafe.Pointer){
40 func(ptr unsafe.Pointer) { F(uintptr(ptr)) },
41 func(ptr unsafe.Pointer) { Fv(uintptr(ptr)) },
42 func(ptr unsafe.Pointer) { T{}.M(uintptr(ptr)) },
43 func(ptr unsafe.Pointer) { T{}.Mv(uintptr(ptr)) },
44 }
45
46 func main() {
47 for i, test := range tests {
48 finalized := false
49
50 ptr := new([64]byte)
51 runtime.SetFinalizer(ptr, func(*[64]byte) {
52 finalized = true
53 })
54
55 callback = func() {
56 runtime.GC()
57 if finalized {
58 fmt.Printf("test #%d failed\n", i)
59 }
60 }
61 test(unsafe.Pointer(ptr))
62 }
63 }
64
View as plain text