github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/runtime/mem_windows.go (about)

     1  // Copyright 2010 The Go 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 runtime
     6  
     7  import (
     8  	"unsafe"
     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  // Don't split the stack as this function may be invoked without a valid G,
    22  // which prevents us from allocating more stack.
    23  //go:nosplit
    24  func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
    25  	mSysStatInc(sysStat, n)
    26  	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
    27  }
    28  
    29  func sysUnused(v unsafe.Pointer, n uintptr) {
    30  	r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
    31  	if r != 0 {
    32  		return
    33  	}
    34  
    35  	// Decommit failed. Usual reason is that we've merged memory from two different
    36  	// VirtualAlloc calls, and Windows will only let each VirtualFree handle pages from
    37  	// a single VirtualAlloc. It is okay to specify a subset of the pages from a single alloc,
    38  	// just not pages from multiple allocs. This is a rare case, arising only when we're
    39  	// trying to give memory back to the operating system, which happens on a time
    40  	// scale of minutes. It doesn't have to be terribly fast. Instead of extra bookkeeping
    41  	// on all our VirtualAlloc calls, try freeing successively smaller pieces until
    42  	// we manage to free something, and then repeat. This ends up being O(n log n)
    43  	// in the worst case, but that's fast enough.
    44  	for n > 0 {
    45  		small := n
    46  		for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
    47  			small /= 2
    48  			small &^= 4096 - 1
    49  		}
    50  		if small < 4096 {
    51  			print("runtime: VirtualFree of ", small, " bytes failed with errno=", getlasterror(), "\n")
    52  			throw("runtime: failed to decommit pages")
    53  		}
    54  		v = add(v, small)
    55  		n -= small
    56  	}
    57  }
    58  
    59  func sysUsed(v unsafe.Pointer, n uintptr) {
    60  	r := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
    61  	if r != uintptr(v) {
    62  		print("runtime: VirtualAlloc of ", n, " bytes failed with errno=", getlasterror(), "\n")
    63  		throw("runtime: failed to commit pages")
    64  	}
    65  
    66  	// Commit failed. See SysUnused.
    67  	for n > 0 {
    68  		small := n
    69  		for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
    70  			small /= 2
    71  			small &^= 4096 - 1
    72  		}
    73  		if small < 4096 {
    74  			print("runtime: VirtualAlloc of ", small, " bytes failed with errno=", getlasterror(), "\n")
    75  			throw("runtime: failed to commit pages")
    76  		}
    77  		v = add(v, small)
    78  		n -= small
    79  	}
    80  }
    81  
    82  // Don't split the stack as this function may be invoked without a valid G,
    83  // which prevents us from allocating more stack.
    84  //go:nosplit
    85  func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    86  	mSysStatDec(sysStat, n)
    87  	r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
    88  	if r == 0 {
    89  		print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
    90  		throw("runtime: failed to release pages")
    91  	}
    92  }
    93  
    94  func sysFault(v unsafe.Pointer, n uintptr) {
    95  	// SysUnused makes the memory inaccessible and prevents its reuse
    96  	sysUnused(v, n)
    97  }
    98  
    99  func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
   100  	*reserved = true
   101  	// v is just a hint.
   102  	// First try at v.
   103  	v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
   104  	if v != nil {
   105  		return v
   106  	}
   107  
   108  	// Next let the kernel choose the address.
   109  	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
   110  }
   111  
   112  func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
   113  	mSysStatInc(sysStat, n)
   114  	p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
   115  	if p != uintptr(v) {
   116  		print("runtime: VirtualAlloc of ", n, " bytes failed with errno=", getlasterror(), "\n")
   117  		throw("runtime: cannot map pages in arena address space")
   118  	}
   119  }