-
Notifications
You must be signed in to change notification settings - Fork 0
/
offheap.go
155 lines (133 loc) · 3.43 KB
/
offheap.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
package offheap
import (
"reflect"
"syscall"
"unsafe"
)
var b byte
// Mmap is a helper method for allocating slices via mmap. It returns a uinptr
// which then can be reflected to a slice.
func Mmap(count, size int) (uintptr, int, error) {
var pages, fd int
pageSize := syscall.Getpagesize()
pages = (size * count) / pageSize
if pages%pageSize != 0 {
pages = pageSize*pages + pageSize
}
fd = -1
ptr, uFd, errno := syscall.Syscall6(
syscall.SYS_MMAP,
uintptr(0),
uintptr(pages),
syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC,
syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE|syscall.MAP_POPULATE,
uintptr(fd),
uintptr(0),
)
var err error
if errno != 0 {
err = errno
return ptr, -1, err
}
return ptr, int(uFd), err
}
// Bytes returns a slice of bytes that are allocated off heap using mmap as
// well as an associated file descriptor.
func Bytes(count int) ([]byte, int, error) {
var bs []byte
byteSize := int(reflect.TypeOf(b).Size())
ptr, fd, err := Mmap(count, byteSize)
if err != nil {
return bs, fd, err
}
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// IntSlice returns a slice of ints that are allocated off heap using mmap as
// well as an associated file descriptor.
func IntSlice(count int) ([]int, int, error) {
var i []int
byteSize := int(reflect.TypeOf(count).Size())
ptr, fd, err := Mmap(count, byteSize)
if err != nil {
return i, fd, err
}
return *(*[]int)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// IntPSlice returns a slice of ints that are allocated off heap using mmap as
// well as an associated file descriptor.
func IntPSlice(count int) ([]*int, int, error) {
var i []*int
ptr, fd, err := Mmap(count, 8)
if err != nil {
return i, fd, err
}
return *(*[]*int)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// Int32Slice returns a slice of int32s that are allocated off heap using mmap as
// well as an associated file descriptor.
func Int32Slice(count int) ([]int32, int, error) {
var i []int32
ptr, fd, err := Mmap(count, 32)
if err != nil {
return i, fd, err
}
return *(*[]int32)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// Uint32Slice returns a slice of uint32s that are allocated off heap using
// mmap as well as an associated file descriptor.
func Uint32Slice(count int) ([]uint32, int, error) {
var i []uint32
ptr, fd, err := Mmap(count, 32)
if err != nil {
return i, fd, err
}
return *(*[]uint32)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// Int64Slice returns a slice of int64s that are allocated off heap using mmap as
// well as an associated file descriptor.
func Int64Slice(count int) ([]int64, int, error) {
var i []int64
ptr, fd, err := Mmap(count, 64)
if err != nil {
return i, fd, err
}
return *(*[]int64)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}
// Uint64Slice returns a slice of uint64s that are allocated off heap using
// mmap as well as an associated file descriptor.
func Uint64Slice(count int) ([]uint64, int, error) {
var i []uint64
ptr, fd, err := Mmap(count, 64)
if err != nil {
return i, fd, err
}
return *(*[]uint64)(unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: count,
Cap: count,
})), fd, nil
}