本站源代码
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.

52 lines
1019B

  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. "golang.org/x/sys/unix"
  8. )
  9. func mmap(len int, inprot, inflags, fd uintptr, off int64) ([]byte, error) {
  10. flags := unix.MAP_SHARED
  11. prot := unix.PROT_READ
  12. switch {
  13. case inprot&COPY != 0:
  14. prot |= unix.PROT_WRITE
  15. flags = unix.MAP_PRIVATE
  16. case inprot&RDWR != 0:
  17. prot |= unix.PROT_WRITE
  18. }
  19. if inprot&EXEC != 0 {
  20. prot |= unix.PROT_EXEC
  21. }
  22. if inflags&ANON != 0 {
  23. flags |= unix.MAP_ANON
  24. }
  25. b, err := unix.Mmap(int(fd), off, len, prot, flags)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return b, nil
  30. }
  31. func (m MMap) flush() error {
  32. return unix.Msync([]byte(m), unix.MS_SYNC)
  33. }
  34. func (m MMap) lock() error {
  35. return unix.Mlock([]byte(m))
  36. }
  37. func (m MMap) unlock() error {
  38. return unix.Munlock([]byte(m))
  39. }
  40. func (m MMap) unmap() error {
  41. return unix.Munmap([]byte(m))
  42. }
上海开阖软件有限公司 沪ICP备12045867号-1