-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.go
101 lines (81 loc) · 1.87 KB
/
mmap.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
// mmap.go -- mmap a slice of ints/uints from a file
//
//
// (c) Sudhi Herle 2018
//
// Author: Sudhi Herle <[email protected]>
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package chd
import (
"reflect"
"unsafe"
)
// byte-slice to uint16 slice
func bsToUint16Slice(b []byte) []uint16 {
n := len(b) / 2
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []uint16
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n
sh.Cap = n
return v
}
// uint32 slice to byte-slice
func u16sToByteSlice(b []uint16) []byte {
n := len(b)
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n * 2
sh.Cap = n * 2
return v
}
// byte-slice to uint32 slice
func bsToUint32Slice(b []byte) []uint32 {
n := len(b) / 4
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []uint32
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n
sh.Cap = n
return v
}
// uint32 slice to byte-slice
func u32sToByteSlice(b []uint32) []byte {
n := len(b)
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n * 4
sh.Cap = n * 4
return v
}
// byte-slice to uint64 slice
func bsToUint64Slice(b []byte) []uint64 {
n := len(b) / 8
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []uint64
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n
sh.Cap = n
return v
}
// uint64 slice to byte-slice
func u64sToByteSlice(b []uint64) []byte {
n := len(b)
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var v []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n * 8
sh.Cap = n * 8
return v
}