-
Notifications
You must be signed in to change notification settings - Fork 2
/
offheap.go
39 lines (33 loc) · 980 Bytes
/
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
// Copyright 2015 Seth Hoenig. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package offheap provides a way to use system memory directly.
package offheap
import (
"github.com/edsrzf/mmap-go"
)
// Memory represents a private anonymous mmap in-memory-only
// file that your program can use to read and write bytes.
// This allows your program to use very large amounts of memory
// without the Go runtime trying to garbage collect it.
type Memory []byte
// New creates an offheap Memory slice of the specified number of bytes.
func New(bytes int64) (Memory, error) {
mapped, e := mmap.MapRegion(
nil,
int(bytes),
mmap.COPY,
mmap.ANON,
0,
)
if e != nil {
return nil, e
}
return Memory(mapped), nil
}
// Unmap will delete the offheap Memory slice. Any usage of the Memory
// afterwords will cause a panic.
func (m Memory) Unmap() error {
p := mmap.MMap(m)
return p.Unmap()
}