github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/mem_windows.c (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  #include "runtime.h"
     6  #include "arch_GOARCH.h"
     7  #include "os_GOOS.h"
     8  #include "defs_GOOS_GOARCH.h"
     9  #include "malloc.h"
    10  
    11  enum {
    12  	MEM_COMMIT = 0x1000,
    13  	MEM_RESERVE = 0x2000,
    14  	MEM_RELEASE = 0x8000,
    15  	
    16  	PAGE_EXECUTE_READWRITE = 0x40,
    17  };
    18  
    19  #pragma dynimport runtime·VirtualAlloc VirtualAlloc "kernel32.dll"
    20  #pragma dynimport runtime·VirtualFree VirtualFree "kernel32.dll"
    21  extern void *runtime·VirtualAlloc;
    22  extern void *runtime·VirtualFree;
    23  
    24  void*
    25  runtime·SysAlloc(uintptr n)
    26  {
    27  	mstats.sys += n;
    28  	return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, (uintptr)(MEM_COMMIT|MEM_RESERVE), (uintptr)PAGE_EXECUTE_READWRITE);
    29  }
    30  
    31  void
    32  runtime·SysUnused(void *v, uintptr n)
    33  {
    34  	USED(v);
    35  	USED(n);
    36  }
    37  
    38  void
    39  runtime·SysFree(void *v, uintptr n)
    40  {
    41  	uintptr r;
    42  
    43  	mstats.sys -= n;
    44  	r = (uintptr)runtime·stdcall(runtime·VirtualFree, 3, v, (uintptr)0, (uintptr)MEM_RELEASE);
    45  	if(r == 0)
    46  		runtime·throw("runtime: failed to release pages");
    47  }
    48  
    49  void*
    50  runtime·SysReserve(void *v, uintptr n)
    51  {
    52  	// v is just a hint.
    53  	// First try at v.
    54  	v = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, (uintptr)MEM_RESERVE, (uintptr)PAGE_EXECUTE_READWRITE);
    55  	if(v != nil)
    56  		return v;
    57  	
    58  	// Next let the kernel choose the address.
    59  	return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, (uintptr)MEM_RESERVE, (uintptr)PAGE_EXECUTE_READWRITE);
    60  }
    61  
    62  void
    63  runtime·SysMap(void *v, uintptr n)
    64  {
    65  	void *p;
    66  	
    67  	mstats.sys += n;
    68  	p = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, (uintptr)MEM_COMMIT, (uintptr)PAGE_EXECUTE_READWRITE);
    69  	if(p != v)
    70  		runtime·throw("runtime: cannot map pages in arena address space");
    71  }