-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcast_helpers.go
54 lines (39 loc) · 1.28 KB
/
cast_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"reflect"
"unsafe"
)
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
func s2b(str string) (bs []byte) {
bsoff := unsafe.Offsetof(reflect.SliceHeader{}.Data)
stroff := unsafe.Offsetof(reflect.StringHeader{}.Data)
*(*unsafe.Pointer)(add(unsafe.Pointer(&bs), bsoff)) = *(*unsafe.Pointer)(add(unsafe.Pointer(&str), stroff))
(*reflect.SliceHeader)(unsafe.Pointer(&bs)).Len = len(str)
(*reflect.SliceHeader)(unsafe.Pointer(&bs)).Cap = len(str)
return
}
func b2s(bs []byte) (str string) {
stroff := unsafe.Offsetof(reflect.StringHeader{}.Data)
bsoff := unsafe.Offsetof(reflect.SliceHeader{}.Data)
*(*unsafe.Pointer)(add(unsafe.Pointer(&str), stroff)) = *(*unsafe.Pointer)(add(unsafe.Pointer(&bs), bsoff))
(*reflect.StringHeader)(unsafe.Pointer(&str)).Len = len(bs)
return
}
func p2b(p unsafe.Pointer) (bs []byte) {
bsoff := unsafe.Offsetof(reflect.SliceHeader{}.Data)
*(*unsafe.Pointer)(add(unsafe.Pointer(&bs), bsoff)) = p
return
}
var efacePtrOff uintptr
func i2p(p interface{}) unsafe.Pointer {
return *(*unsafe.Pointer)(add(unsafe.Pointer(&p), efacePtrOff))
}
func init() {
f, ok := reflect.ValueOf(reflect.Value{}).Type().FieldByName("ptr")
if !ok {
panic("unable to find the ptr")
}
efacePtrOff = f.Offset
}