github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/mem_darwin.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 "defs_GOOS_GOARCH.h"
     8  #include "os_GOOS.h"
     9  #include "malloc.h"
    10  #include "textflag.h"
    11  
    12  #pragma textflag NOSPLIT
    13  void*
    14  runtime·sysAlloc(uintptr n, uint64 *stat)
    15  {
    16  	void *v;
    17  
    18  	v = runtime·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
    19  	if(v < (void*)4096)
    20  		return nil;
    21  	runtime·xadd64(stat, n);
    22  	return v;
    23  }
    24  
    25  void
    26  runtime·SysUnused(void *v, uintptr n)
    27  {
    28  	// Linux's MADV_DONTNEED is like BSD's MADV_FREE.
    29  	runtime·madvise(v, n, MADV_FREE);
    30  }
    31  
    32  void
    33  runtime·SysUsed(void *v, uintptr n)
    34  {
    35  	USED(v);
    36  	USED(n);
    37  }
    38  
    39  void
    40  runtime·SysFree(void *v, uintptr n, uint64 *stat)
    41  {
    42  	runtime·xadd64(stat, -(uint64)n);
    43  	runtime·munmap(v, n);
    44  }
    45  
    46  void
    47  runtime·SysFault(void *v, uintptr n)
    48  {
    49  	runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
    50  }
    51  
    52  void*
    53  runtime·SysReserve(void *v, uintptr n, bool *reserved)
    54  {
    55  	void *p;
    56  
    57  	*reserved = true;
    58  	p = runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
    59  	if(p < (void*)4096)
    60  		return nil;
    61  	return p;
    62  }
    63  
    64  enum
    65  {
    66  	ENOMEM = 12,
    67  };
    68  
    69  void
    70  runtime·SysMap(void *v, uintptr n, bool reserved, uint64 *stat)
    71  {
    72  	void *p;
    73  	
    74  	USED(reserved);
    75  
    76  	runtime·xadd64(stat, n);
    77  	p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0);
    78  	if(p == (void*)ENOMEM)
    79  		runtime·throw("runtime: out of memory");
    80  	if(p != v)
    81  		runtime·throw("runtime: cannot map pages in arena address space");
    82  }