aboutsummaryrefslogtreecommitdiff
path: root/test/fuse_integration/mmap_unix.go
blob: 702a1c0324fcdb044ad19dc08e6248cd504336e9 (plain)
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
//go:build unix

package fuse

import (
	"syscall"
	"unsafe"
)

// Memory mapping support for Unix-like systems

func mmapFile(fd int, offset int64, length int, prot int, flags int) ([]byte, error) {
	addr, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
		0, // addr (let kernel choose)
		uintptr(length),
		uintptr(prot),
		uintptr(flags),
		uintptr(fd),
		uintptr(offset))

	if errno != 0 {
		return nil, errno
	}

	// Convert the address to a byte slice
	return (*[1 << 30]byte)(unsafe.Pointer(addr))[:length:length], nil
}

func munmapFile(data []byte) error {
	if len(data) == 0 {
		return nil
	}

	_, _, errno := syscall.Syscall(syscall.SYS_MUNMAP,
		uintptr(unsafe.Pointer(&data[0])),
		uintptr(len(data)),
		0)

	if errno != 0 {
		return errno
	}
	return nil
}

func msyncFile(data []byte, flags int) error {
	if len(data) == 0 {
		return nil
	}

	_, _, errno := syscall.Syscall(syscall.SYS_MSYNC,
		uintptr(unsafe.Pointer(&data[0])),
		uintptr(len(data)),
		uintptr(flags))

	if errno != 0 {
		return errno
	}
	return nil
}

func isMmapSupported() bool {
	return true
}

// Memory protection flags
const (
	PROT_READ  = syscall.PROT_READ
	PROT_WRITE = syscall.PROT_WRITE
	PROT_EXEC  = syscall.PROT_EXEC
	PROT_NONE  = syscall.PROT_NONE
)

// Memory mapping flags
const (
	MAP_SHARED    = syscall.MAP_SHARED
	MAP_PRIVATE   = syscall.MAP_PRIVATE
	MAP_ANONYMOUS = syscall.MAP_ANON
)

// Memory sync flags
const (
	MS_ASYNC      = syscall.MS_ASYNC
	MS_SYNC       = syscall.MS_SYNC
	MS_INVALIDATE = syscall.MS_INVALIDATE
)