github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/mem_dragonfly.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  enum
    13  {
    14  	ENOMEM = 12,
    15  };
    16  
    17  #pragma textflag NOSPLIT
    18  void*
    19  runtime·sysAlloc(uintptr n, uint64 *stat)
    20  {
    21  	void *v;
    22  
    23  	v = runtime·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
    24  	if(v < (void*)4096)
    25  		return nil;
    26  	runtime·xadd64(stat, n);
    27  	return v;
    28  }
    29  
    30  void
    31  runtime·SysUnused(void *v, uintptr n)
    32  {
    33  	runtime·madvise(v, n, MADV_FREE);
    34  }
    35  
    36  void
    37  runtime·SysUsed(void *v, uintptr n)
    38  {
    39  	USED(v);
    40  	USED(n);
    41  }
    42  
    43  void
    44  runtime·SysFree(void *v, uintptr n, uint64 *stat)
    45  {
    46  	runtime·xadd64(stat, -(uint64)n);
    47  	runtime·munmap(v, n);
    48  }
    49  
    50  void
    51  runtime·SysFault(void *v, uintptr n)
    52  {
    53  	runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
    54  }
    55  
    56  void*
    57  runtime·SysReserve(void *v, uintptr n, bool *reserved)
    58  {
    59  	void *p;
    60  
    61  	// On 64-bit, people with ulimit -v set complain if we reserve too
    62  	// much address space.  Instead, assume that the reservation is okay
    63  	// and check the assumption in SysMap.
    64  	if(sizeof(void*) == 8 && n > 1LL<<32) {
    65  		*reserved = false;
    66  		return v;
    67  	}
    68  
    69  	*reserved = true;
    70  	p = runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
    71  	if(p < (void*)4096)
    72  		return nil;
    73  	return p;
    74  }
    75  
    76  void
    77  runtime·SysMap(void *v, uintptr n, bool reserved, uint64 *stat)
    78  {
    79  	void *p;
    80  	
    81  	runtime·xadd64(stat, n);
    82  
    83  	// On 64-bit, we don't actually have v reserved, so tread carefully.
    84  	if(!reserved) {
    85  		// TODO(jsing): For some reason DragonFly seems to return
    86  		// memory at a different address than we requested, even when
    87  		// there should be no reason for it to do so. This can be
    88  		// avoided by using MAP_FIXED, but I'm not sure we should need
    89  		// to do this - we do not on other platforms.
    90  		p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0);
    91  		if(p == (void*)ENOMEM)
    92  			runtime·throw("runtime: out of memory");
    93  		if(p != v) {
    94  			runtime·printf("runtime: address space conflict: map(%p) = %p\n", v, p);
    95  			runtime·throw("runtime: address space conflict");
    96  		}
    97  		return;
    98  	}
    99  
   100  	p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0);
   101  	if(p == (void*)ENOMEM)
   102  		runtime·throw("runtime: out of memory");
   103  	if(p != v)
   104  		runtime·throw("runtime: cannot map pages in arena address space");
   105  }