File: mmap_unix.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.0.0~beta10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,120 kB
  • sloc: perl: 557; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 814 bytes parent folder | download
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
//go:build !windows && !appengine && !plan9 && !js && !wasip1 && !wasi

package maxminddb

import (
	"errors"
	"os"

	"golang.org/x/sys/unix"
)

type mmapENODEVError struct{}

func (mmapENODEVError) Error() string {
	return "mmap: the underlying filesystem of the specified file does not support memory mapping"
}

func (mmapENODEVError) Is(target error) bool {
	return target == errors.ErrUnsupported
}

func mmap(fd, length int) (data []byte, err error) {
	data, err = unix.Mmap(fd, 0, length, unix.PROT_READ, unix.MAP_SHARED)
	if err != nil {
		if err == unix.ENODEV {
			return nil, mmapENODEVError{}
		}
		return nil, os.NewSyscallError("mmap", err)
	}
	return data, nil
}

func munmap(b []byte) (err error) {
	if err = unix.Munmap(b); err != nil {
		return os.NewSyscallError("munmap", err)
	}
	return nil
}