Source file src/runtime/mem_windows.go

     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  	_ERROR_NOT_ENOUGH_MEMORY = 8
    21  	_ERROR_COMMITMENT_LIMIT  = 1455
    22  )
    23  
    24  // Don't split the stack as this function may be invoked without a valid G,
    25  // which prevents us from allocating more stack.
    26  //
    27  //go:nosplit
    28  func sysAllocOS(n uintptr) unsafe.Pointer {
    29  	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
    30  }
    31  
    32  func sysUnusedOS(v unsafe.Pointer, n uintptr) {
    33  	r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
    34  	if r != 0 {
    35  		return
    36  	}
    37  
    38  	// Decommit failed. Usual reason is that we've merged memory from two different
    39  	// VirtualAlloc calls, and Windows will only let each VirtualFree handle pages from
    40  	// a single VirtualAlloc. It is okay to specify a subset of the pages from a single alloc,
    41  	// just not pages from multiple allocs. This is a rare case, arising only when we're
    42  	// trying to give memory back to the operating system, which happens on a time
    43  	// scale of minutes. It doesn't have to be terribly fast. Instead of extra bookkeeping
    44  	// on all our VirtualAlloc calls, try freeing successively smaller pieces until
    45  	// we manage to free something, and then repeat. This ends up being O(n log n)
    46  	// in the worst case, but that's fast enough.
    47  	for n > 0 {
    48  		small := n
    49  		for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
    50  			small /= 2
    51  			small &^= 4096 - 1
    52  		}
    53  		if small < 4096 {
    54  			print("runtime: VirtualFree of ", small, " bytes failed with errno=", getlasterror(), "\n")
    55  			throw("runtime: failed to decommit pages")
    56  		}
    57  		v = add(v, small)
    58  		n -= small
    59  	}
    60  }
    61  
    62  func sysUsedOS(v unsafe.Pointer, n uintptr) {
    63  	p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
    64  	if p == uintptr(v) {
    65  		return
    66  	}
    67  
    68  	// Commit failed. See SysUnused.
    69  	// Hold on to n here so we can give back a better error message
    70  	// for certain cases.
    71  	k := n
    72  	for k > 0 {
    73  		small := k
    74  		for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
    75  			small /= 2
    76  			small &^= 4096 - 1
    77  		}
    78  		if small < 4096 {
    79  			errno := getlasterror()
    80  			switch errno {
    81  			case _ERROR_NOT_ENOUGH_MEMORY, _ERROR_COMMITMENT_LIMIT:
    82  				print("runtime: VirtualAlloc of ", n, " bytes failed with errno=", errno, "\n")
    83  				throw("out of memory")
    84  			default:
    85  				print("runtime: VirtualAlloc of ", small, " bytes failed with errno=", errno, "\n")
    86  				throw("runtime: failed to commit pages")
    87  			}
    88  		}
    89  		v = add(v, small)
    90  		k -= small
    91  	}
    92  }
    93  
    94  func sysHugePageOS(v unsafe.Pointer, n uintptr) {
    95  }
    96  
    97  func sysNoHugePageOS(v unsafe.Pointer, n uintptr) {
    98  }
    99  
   100  func sysHugePageCollapseOS(v unsafe.Pointer, n uintptr) {
   101  }
   102  
   103  // Don't split the stack as this function may be invoked without a valid G,
   104  // which prevents us from allocating more stack.
   105  //
   106  //go:nosplit
   107  func sysFreeOS(v unsafe.Pointer, n uintptr) {
   108  	r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
   109  	if r == 0 {
   110  		print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
   111  		throw("runtime: failed to release pages")
   112  	}
   113  }
   114  
   115  func sysFaultOS(v unsafe.Pointer, n uintptr) {
   116  	// SysUnused makes the memory inaccessible and prevents its reuse
   117  	sysUnusedOS(v, n)
   118  }
   119  
   120  func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
   121  	// v is just a hint.
   122  	// First try at v.
   123  	// This will fail if any of [v, v+n) is already reserved.
   124  	v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
   125  	if v != nil {
   126  		return v
   127  	}
   128  
   129  	// Next let the kernel choose the address.
   130  	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
   131  }
   132  
   133  func sysMapOS(v unsafe.Pointer, n uintptr) {
   134  }
   135  

View as plain text