-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunsafeslice.go
218 lines (188 loc) · 5.32 KB
/
unsafeslice.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Package unsafeslice contains functions for zero-copy casting between typed slices and byte slices.
package unsafeslice
import (
"reflect"
"unsafe"
)
// Useful constants.
const (
Uint64Size = 8
Uint32Size = 4
Uint16Size = 2
Uint8Size = 1
Float32Size = 4
Float64Size = 8
)
func newRawSliceHeader(sh *reflect.SliceHeader, b []byte, stride int) *reflect.SliceHeader {
if len(b) == 0 {
sh.Len = 0
sh.Cap = 0
sh.Data = 0
} else {
sh.Len = len(b) / stride
sh.Cap = len(b) / stride
sh.Data = (uintptr)(unsafe.Pointer(&b[0]))
}
return sh
}
func newSliceHeaderFromBytes(b []byte, stride int) unsafe.Pointer {
sh := &reflect.SliceHeader{}
return unsafe.Pointer(newRawSliceHeader(sh, b, stride))
}
func newSliceHeader(p unsafe.Pointer, size int) unsafe.Pointer {
return unsafe.Pointer(&reflect.SliceHeader{
Len: size,
Cap: size,
Data: uintptr(p),
})
}
func Uint64SliceFromByteSlice(b []byte) []uint64 {
return *(*[]uint64)(newSliceHeaderFromBytes(b, Uint64Size))
}
func ByteSliceFromUint64Slice(b []uint64) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint64Size))
}
func Int64SliceFromByteSlice(b []byte) []int64 {
return *(*[]int64)(newSliceHeaderFromBytes(b, Uint64Size))
}
func ByteSliceFromInt64Slice(b []int64) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint64Size))
}
func Uint32SliceFromByteSlice(b []byte) []uint32 {
return *(*[]uint32)(newSliceHeaderFromBytes(b, Uint32Size))
}
func ByteSliceFromUint32Slice(b []uint32) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint32Size))
}
func Int32SliceFromByteSlice(b []byte) []int32 {
return *(*[]int32)(newSliceHeaderFromBytes(b, Uint32Size))
}
func ByteSliceFromInt32Slice(b []int32) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint32Size))
}
func Uint16SliceFromByteSlice(b []byte) []uint16 {
return *(*[]uint16)(newSliceHeaderFromBytes(b, Uint16Size))
}
func ByteSliceFromUint16Slice(b []uint16) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint16Size))
}
func Int16SliceFromByteSlice(b []byte) []int16 {
return *(*[]int16)(newSliceHeaderFromBytes(b, Uint16Size))
}
func ByteSliceFromInt16Slice(b []int16) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint16Size))
}
func Uint8SliceFromByteSlice(b []byte) []uint8 {
return b
}
func ByteSliceFromUint8Slice(b []uint8) []byte {
return b
}
func Int8SliceFromByteSlice(b []byte) []int8 {
return *(*[]int8)(newSliceHeaderFromBytes(b, Uint8Size))
}
func ByteSliceFromInt8Slice(b []int8) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Uint8Size))
}
func Float32SliceFromByteSlice(b []byte) []float32 {
return *(*[]float32)(newSliceHeaderFromBytes(b, Float32Size))
}
func ByteSliceFromFloat32Slice(b []float32) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Float32Size))
}
func Float64SliceFromByteSlice(b []byte) []float64 {
return *(*[]float64)(newSliceHeaderFromBytes(b, Float64Size))
}
func ByteSliceFromFloat64Slice(b []float64) []byte {
if len(b) == 0 {
return nil
}
return *(*[]byte)(newSliceHeader(unsafe.Pointer(&b[0]), len(b)*Float64Size))
}
func ByteSliceFromString(s string) []byte {
h := (*reflect.StringHeader)(unsafe.Pointer(&s))
return *(*[]byte)(newSliceHeader(unsafe.Pointer(h.Data), len(s)*Uint8Size))
}
func StringFromByteSlice(b []byte) string {
if len(b) == 0 {
return ""
}
h := &reflect.StringHeader{
Data: uintptr(unsafe.Pointer(&b[0])),
Len: len(b),
}
return *(*string)(unsafe.Pointer(h))
}
// Create a slice of structs from a slice of bytes.
//
// var v []Struct
// StructSliceFromByteSlice(bytes, &v)
//
// Elements in the byte array must be padded correctly. See unsafe.AlignOf, et al.
//
// Note that this is slower than the scalar primitives above as it uses reflection.
func StructSliceFromByteSlice(b []byte, out interface{}) {
ptr := reflect.ValueOf(out)
if ptr.Kind() != reflect.Ptr {
panic("expected pointer to a slice of structs (*[]X)")
}
slice := ptr.Elem()
if slice.Kind() != reflect.Slice {
panic("expected pointer to a slice of structs (*[]X)")
}
// TODO: More checks, such as ensuring that:
// - elements are NOT pointers
// - structs do not contain pointers, slices or maps
stride := int(slice.Type().Elem().Size())
if len(b)%stride != 0 {
panic("size of byte buffer is not a multiple of struct size")
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(slice.UnsafeAddr()))
newRawSliceHeader(sh, b, stride)
}
// ByteSliceFromStructSlice does what you would expect.
//
// Note that this is slower than the scalar primitives above as it uses reflection.
func ByteSliceFromStructSlice(s interface{}) []byte {
slice := reflect.ValueOf(s)
if slice.Kind() != reflect.Slice {
panic("expected a slice of structs (*[]X)")
}
var length int
var data uintptr
if slice.Len() != 0 {
elem := slice.Index(0)
length = int(elem.Type().Size()) * slice.Len()
data = elem.UnsafeAddr()
}
out := &reflect.SliceHeader{
Len: length,
Cap: length,
Data: data,
}
return *(*[]byte)(unsafe.Pointer(out))
}