-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsafe.go
138 lines (122 loc) · 3.93 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package unsafe
import (
"reflect"
"unsafe"
)
// UintptrToSlice returns a slice with len and cap of sz.
func UintptrToSlice(ptr uintptr, sz uint64) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr),
Len: int(sz),
Cap: int(sz),
}))
}
// UnsafeToSlice returns a slice with len and cap of sz.
func UnsafeToSlice(ptr unsafe.Pointer, sz uint64) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr),
Len: int(sz),
Cap: int(sz),
}))
}
// SliceToUintptr returns the pointer to which data data points.
func SliceToUintptr(data []byte) uintptr {
return uintptr(unsafe.Pointer(&data[0]))
}
// SliceToUnsafe returns the pointer to which data data points.
func SliceToUnsafe(data []byte) unsafe.Pointer {
return unsafe.Pointer(&data[0])
}
//UnsafeToUint64Slice returns a uint64 slice with len and cap of sz.
func UnsafeToUint64Slice(ptr unsafe.Pointer, sz uint64) []uint64 {
return *(*[]uint64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr),
Len: int(sz),
Cap: int(sz),
}))
}
//UnsafeToInt64Slice returns a int64 slice with len and cap of sz.
func UnsafeToInt64Slice(ptr unsafe.Pointer, sz uint64) []int64 {
return *(*[]int64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr),
Len: int(sz),
Cap: int(sz),
}))
}
// ByteSliceFromUint64 returns a byte slice with the length of 8
// and val copied to it without considering byte order.
func ByteSliceFromUint64(val uint64) []byte {
data := make([]byte, 8)
*(*uint64)(unsafe.Pointer(&data[0])) = val
return data
}
// Int64SliceToUint64Slice converts a int64 slice to a uint64 slice unsafe.
func Int64SliceToUint64Slice(vals []int64) []uint64 {
return *(*[]uint64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals),
Cap: cap(vals),
}))
}
// Uint64SliceToInt64Slice converts a uint64 slice to a int64 slice unsafe.
func Uint64SliceToInt64Slice(vals []uint64) []int64 {
return *(*[]int64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals),
Cap: cap(vals),
}))
}
// Uint64SliceToByteSlice converts a uint64 slice to a byte slice unsafe.
// Length of vals is multiplied by 8.
func Uint64SliceToByteSlice(vals []uint64) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) * 8,
Cap: cap(vals) * 8,
}))
}
// ByteSliceToUint64Slice converts a byte slice to a uint64 slice unsafe.
// Length of vals is divided by 8.
func ByteSliceToUint64Slice(vals []byte) []uint64 {
return *(*[]uint64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) / 8,
Cap: cap(vals) / 8,
}))
}
// Int64SliceToByteSlice converts a int64 slice to a byte slice unsafe.
// Length of vals is multiplied by 8.
func Int64SliceToByteSlice(vals []int64) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) * 8,
Cap: cap(vals) * 8,
}))
}
// ByteSliceToInt64Slice converts a byte slice to a int64 slice unsafe.
// Length of vals is divided by 8.
func ByteSliceToInt64Slice(vals []byte) []int64 {
return *(*[]int64)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) / 8,
Cap: cap(vals) / 8,
}))
}
// Uint32SliceToByteSlice converts a uint32 slice to a byte slice unsafe.
// Length of vals is multiplied by 4.
func Uint32SliceToByteSlice(vals []uint32) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) * 4,
Cap: cap(vals) * 4,
}))
}
// ByteSliceToUint32Slice converts a byte slice to a uint32 slice unsafe.
// Length of vals is divided by 4.
func ByteSliceToUint32Slice(vals []byte) []uint32 {
return *(*[]uint32)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&vals[0])),
Len: len(vals) / 4,
Cap: cap(vals) / 4,
}))
}