mmap_unix.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // +build !windows
  2. /*
  3. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package y
  18. import (
  19. "os"
  20. "syscall"
  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. func Munmap(b []byte) error {
  35. return unix.Munmap(b)
  36. }
  37. // Madvise uses the madvise system call to give advise about the use of memory
  38. // when using a slice that is memory-mapped to a file. Set the readahead flag to
  39. // false if page references are expected in random order.
  40. func Madvise(b []byte, readahead bool) error {
  41. flags := unix.MADV_NORMAL
  42. if !readahead {
  43. flags = unix.MADV_RANDOM
  44. }
  45. return madvise(b, flags)
  46. }
  47. // This is required because the unix package does not support the madvise system call on OS X.
  48. func madvise(b []byte, advice int) (err error) {
  49. _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])),
  50. uintptr(len(b)), uintptr(advice))
  51. if e1 != 0 {
  52. err = e1
  53. }
  54. return
  55. }