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
|
// Copyright 2011 Evan Shaw. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE-MMAP-GO file.
// Modifications (c) 2024 The Memory Authors.
// Copyright 2024 The Memory Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build openbsd && (386 || amd64 || arm64)
package memory
import (
"fmt"
"os"
"sync"
"unsafe"
"golang.org/x/sys/unix"
)
// track what can be unmapped
var allocmap map[uintptr][]byte
var m sync.Mutex
const pageSizeLog = 20
var (
osPageMask = osPageSize - 1
osPageSize = os.Getpagesize()
)
func init() {
allocmap = make(map[uintptr][]byte)
}
func unmap(addr uintptr, size int) error {
if trace {
fmt.Fprintf(os.Stderr, "unmap %#x\n", addr)
}
a, ok := allocmap[addr]
if !ok {
if trace {
fmt.Fprintf(os.Stderr, "unmap %#x: not found\n", addr)
}
// panic("unmap called on unknown mapping")
return nil
}
if err := unix.Munmap(a); err != nil {
if trace {
fmt.Fprintf(os.Stderr, "unmap: %s\n", err.Error())
}
// panic(err.Error())
return err
}
m.Lock()
delete(allocmap, addr)
m.Unlock()
return nil
}
func mmap(size int) (uintptr, int, error) {
roundsize := roundup(size, osPageSize) + pageSize
b, err := unix.Mmap(-1, 0, roundsize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_PRIVATE|unix.MAP_ANON)
if err != nil {
return 0, 0, err
}
p := uintptr(unsafe.Pointer(&b[0]))
if trace {
fmt.Fprintf(os.Stderr, "mmap actual @%#x size: %#x\n", p, roundsize)
}
// waste all the space until the next page
r := (p + uintptr(pageSize)) &^ uintptr(pageMask)
nsize := (roundsize) - int((r - p))
if nsize < size {
panic("didn't allocate enough to meet initial request!")
}
if trace {
fmt.Fprintf(os.Stderr, "mmap page-rounded @%#x size: %#x\n", r, nsize)
}
m.Lock()
allocmap[r] = b
m.Unlock()
return r, nsize, nil
}
|