Source file
test/escape_unsafe.go
1
2
3
4
5
6
7
8
9 package escape
10
11 import (
12 "reflect"
13 "unsafe"
14 )
15
16
17
18 func convert(p *float64) *uint64 {
19 return (*uint64)(unsafe.Pointer(p))
20 }
21
22
23
24 func arithAdd() unsafe.Pointer {
25 var x [2]byte
26 return unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + 1)
27 }
28
29 func arithSub() unsafe.Pointer {
30 var x [2]byte
31 return unsafe.Pointer(uintptr(unsafe.Pointer(&x[1])) - 1)
32 }
33
34 func arithMask() unsafe.Pointer {
35 var x [2]byte
36 return unsafe.Pointer(uintptr(unsafe.Pointer(&x[1])) &^ 1)
37 }
38
39
40
41
42
43 func valuePointer(p *int) unsafe.Pointer {
44 return unsafe.Pointer(reflect.ValueOf(p).Pointer())
45 }
46
47
48 func valueUnsafeAddr(p *int) unsafe.Pointer {
49 return unsafe.Pointer(reflect.ValueOf(p).Elem().UnsafeAddr())
50 }
51
52
53
54
55 func fromSliceData(s []int) unsafe.Pointer {
56 return unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data)
57 }
58
59 func fromStringData(s string) unsafe.Pointer {
60 return unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
61 }
62
63 func toSliceData(s *[]int, p unsafe.Pointer) {
64 (*reflect.SliceHeader)(unsafe.Pointer(s)).Data = uintptr(p)
65 }
66
67 func toStringData(s *string, p unsafe.Pointer) {
68 (*reflect.StringHeader)(unsafe.Pointer(s)).Data = uintptr(p)
69 }
70
View as plain text