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