mmap_windows.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 The Memory Authors. 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. package memory // import "modernc.org/memory"
  5. import (
  6. "os"
  7. "syscall"
  8. )
  9. const (
  10. _MEM_COMMIT = 0x1000
  11. _MEM_RESERVE = 0x2000
  12. _MEM_DECOMMIT = 0x4000
  13. _MEM_RELEASE = 0x8000
  14. _PAGE_READWRITE = 0x0004
  15. _PAGE_NOACCESS = 0x0001
  16. )
  17. const pageSizeLog = 16
  18. var (
  19. modkernel32 = syscall.NewLazyDLL("kernel32.dll")
  20. osPageMask = osPageSize - 1
  21. osPageSize = os.Getpagesize()
  22. procVirtualAlloc = modkernel32.NewProc("VirtualAlloc")
  23. procVirtualFree = modkernel32.NewProc("VirtualFree")
  24. )
  25. // pageSize aligned.
  26. func mmap(size int) (uintptr, int, error) {
  27. size = roundup(size, pageSize)
  28. addr, _, err := procVirtualAlloc.Call(0, uintptr(size), _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE)
  29. if err.(syscall.Errno) != 0 || addr == 0 {
  30. return addr, size, err
  31. }
  32. return addr, size, nil
  33. }
  34. func unmap(addr uintptr, size int) error {
  35. r, _, err := procVirtualFree.Call(addr, 0, _MEM_RELEASE)
  36. if r == 0 {
  37. return err
  38. }
  39. return nil
  40. }