-
Notifications
You must be signed in to change notification settings - Fork 13
/
unsafe.go
67 lines (58 loc) · 1.27 KB
/
unsafe.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
55
56
57
58
59
60
61
62
63
64
65
66
67
package jettison
import (
"reflect"
"unsafe"
)
// eface is the runtime representation of
// the empty interface.
type eface struct {
rtype unsafe.Pointer
word unsafe.Pointer
}
// sliceHeader is the runtime representation
// of a slice.
type sliceHeader struct {
Data unsafe.Pointer
Len int
Cap int
}
// stringHeader is the runtime representation
// of a string.
type stringHeader struct {
Data unsafe.Pointer
Len int
}
//nolint:staticcheck
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)
}
func unpackEface(i interface{}) *eface {
return (*eface)(unsafe.Pointer(&i))
}
func packEface(p unsafe.Pointer, t reflect.Type, ptr bool) interface{} {
var i interface{}
e := (*eface)(unsafe.Pointer(&i))
e.rtype = unpackEface(t).word
if ptr {
// Value is indirect, but interface is
// direct. We need to load the data at
// p into the interface data word.
e.word = *(*unsafe.Pointer)(p)
} else {
// Value is direct, and so is the interface.
e.word = p
}
return i
}
// sp2b converts a string pointer to a byte slice.
//go:nosplit
func sp2b(p unsafe.Pointer) []byte {
shdr := (*stringHeader)(p)
return *(*[]byte)(unsafe.Pointer(&sliceHeader{
Data: shdr.Data,
Len: shdr.Len,
Cap: shdr.Len,
}))
}