-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmap.go
52 lines (40 loc) · 1.08 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
// mmap.go -- mmap a slice of ints/uints from a file
//
// Author: Sudhi Herle <sudhi@herle.net>
//
// 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 bbhash
import (
"reflect"
"syscall"
"unsafe"
)
// map 'n' uint64s at offset 'off'
func mmapUint64(fd int, off uint64, n int, prot, flags int) ([]uint64, error) {
sz := n * 8
// XXX Will this grow the file if needed?
ba, err := syscall.Mmap(fd, int64(off), sz, prot, flags)
if err != nil {
return nil, err
}
bh := (*reflect.SliceHeader)(unsafe.Pointer(&ba))
var v []uint64
// XXX Will addr get garbage collected? It shouldn't!
sh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sh.Data = bh.Data
sh.Len = n
sh.Cap = n
return v, nil
}
// unmap a previously mapped u64 array
func munmapUint64(fd int, v []uint64) error {
var a []byte
vh := (*reflect.SliceHeader)(unsafe.Pointer(&v))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&a))
bh.Data = vh.Data
bh.Len = vh.Len * 8
bh.Cap = bh.Len
return syscall.Munmap(a)
}