github.com/cznic/memory@v0.0.0-20181122101858-44f9dcde99e8/mmap_windows.go (about)

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