mmap_linux.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build !js
  2. // +build !js
  3. /*
  4. * Copyright 2020 Dgraph Labs, Inc. and Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package z
  19. import (
  20. "os"
  21. "unsafe"
  22. "golang.org/x/sys/unix"
  23. )
  24. // mmap uses the mmap system call to memory-map a file. If writable is true,
  25. // memory protection of the pages is set so that they may be written to as well.
  26. func mmap(fd *os.File, writable bool, size int64) ([]byte, error) {
  27. mtype := unix.PROT_READ
  28. if writable {
  29. mtype |= unix.PROT_WRITE
  30. }
  31. return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED)
  32. }
  33. // munmap unmaps a previously mapped slice.
  34. //
  35. // unix.Munmap maintains an internal list of mmapped addresses, and only calls munmap
  36. // if the address is present in that list. If we use mremap, this list is not updated.
  37. // To bypass this, we call munmap ourselves.
  38. func munmap(data []byte) error {
  39. if len(data) == 0 || len(data) != cap(data) {
  40. return unix.EINVAL
  41. }
  42. _, _, errno := unix.Syscall(
  43. unix.SYS_MUNMAP,
  44. uintptr(unsafe.Pointer(&data[0])),
  45. uintptr(len(data)),
  46. 0,
  47. )
  48. if errno != 0 {
  49. return errno
  50. }
  51. return nil
  52. }
  53. // madvise uses the madvise system call to give advise about the use of memory
  54. // when using a slice that is memory-mapped to a file. Set the readahead flag to
  55. // false if page references are expected in random order.
  56. func madvise(b []byte, readahead bool) error {
  57. flags := unix.MADV_NORMAL
  58. if !readahead {
  59. flags = unix.MADV_RANDOM
  60. }
  61. return unix.Madvise(b, flags)
  62. }
  63. // msync writes any modified data to persistent storage.
  64. func msync(b []byte) error {
  65. return unix.Msync(b, unix.MS_SYNC)
  66. }