You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB

  1. // Copyright 2011 Evan Shaw. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd linux openbsd solaris netbsd
  5. package mmap
  6. import (
  7. "syscall"
  8. )
  9. func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
  10. flags := syscall.MAP_SHARED
  11. prot := syscall.PROT_READ
  12. switch {
  13. case inprot&COPY != 0:
  14. prot |= syscall.PROT_WRITE
  15. flags = syscall.MAP_PRIVATE
  16. case inprot&RDWR != 0:
  17. prot |= syscall.PROT_WRITE
  18. }
  19. if inprot&EXEC != 0 {
  20. prot |= syscall.PROT_EXEC
  21. }
  22. if inflags&ANON != 0 {
  23. flags |= syscall.MAP_ANON
  24. }
  25. b, err := syscall.Mmap(int(fd), off, len, prot, flags)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return b, nil
  30. }
  31. func flush(addr, len uintptr) error {
  32. _, _, errno := syscall.Syscall(_SYS_MSYNC, addr, len, _MS_SYNC)
  33. if errno != 0 {
  34. return syscall.Errno(errno)
  35. }
  36. return nil
  37. }
  38. func lock(addr, len uintptr) error {
  39. _, _, errno := syscall.Syscall(syscall.SYS_MLOCK, addr, len, 0)
  40. if errno != 0 {
  41. return syscall.Errno(errno)
  42. }
  43. return nil
  44. }
  45. func unlock(addr, len uintptr) error {
  46. _, _, errno := syscall.Syscall(syscall.SYS_MUNLOCK, addr, len, 0)
  47. if errno != 0 {
  48. return syscall.Errno(errno)
  49. }
  50. return nil
  51. }
  52. func unmap(addr, len uintptr) error {
  53. _, _, errno := syscall.Syscall(syscall.SYS_MUNMAP, addr, len, 0)
  54. if errno != 0 {
  55. return syscall.Errno(errno)
  56. }
  57. return nil
  58. }