Debian Codegraph @

mmap_windows.go

raw symbols SCIP
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 syscall "golang.org/x/sys/windows"
9 "os"
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 // canCarve is false on Windows: VirtualFree with MEM_RELEASE frees
25 // only whole VirtualAlloc allocations, never a sub-range.
26 const canCarve = false
27
28 // mmapGranularity is the granularity mmap in this file actually maps at:
29 // VirtualAlloc reserves at 64 KiB granularity, so mmap rounds every request
30 // up to a pageSize multiple and page.size records that.
31 const mmapGranularity = pageSize
32
33 var (
34 modkernel32 = syscall.NewLazySystemDLL("kernel32.dll")
35 osPageMask = osPageSize - 1
36 osPageSize = os.Getpagesize()
37 procVirtualAlloc = modkernel32.NewProc("VirtualAlloc")
38 procVirtualFree = modkernel32.NewProc("VirtualFree")
39 )
40
41 // pageSize aligned.
42 func mmap(size int) (uintptr, int, error) {
43 size = roundup(size, pageSize)
44 addr, _, err := procVirtualAlloc.Call(0, uintptr(size), _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE)
45 if err.(syscall.Errno) != 0 || addr == 0 {
46 return addr, size, err
47 }
48 return addr, size, nil
49 }
50
51 func unmap(addr uintptr, size int) error {
52 r, _, err := procVirtualFree.Call(addr, 0, _MEM_RELEASE)
53 if r == 0 {
54 return err
55 }
56
57 return nil
58 }