github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/mem_netbsd.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 11 enum 12 { 13 ENOMEM = 12, 14 }; 15 16 void* 17 runtime·SysAlloc(uintptr n) 18 { 19 void *v; 20 21 mstats.sys += n; 22 v = runtime·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 23 if(v < (void*)4096) 24 return nil; 25 return v; 26 } 27 28 void 29 runtime·SysUnused(void *v, uintptr n) 30 { 31 runtime·madvise(v, n, MADV_FREE); 32 } 33 34 void 35 runtime·SysFree(void *v, uintptr n) 36 { 37 mstats.sys -= n; 38 runtime·munmap(v, n); 39 } 40 41 void* 42 runtime·SysReserve(void *v, uintptr n) 43 { 44 void *p; 45 46 // On 64-bit, people with ulimit -v set complain if we reserve too 47 // much address space. Instead, assume that the reservation is okay 48 // and check the assumption in SysMap. 49 if(sizeof(void*) == 8) 50 return v; 51 52 p = runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0); 53 if(p < (void*)4096) 54 return nil; 55 return p; 56 } 57 58 void 59 runtime·SysMap(void *v, uintptr n) 60 { 61 void *p; 62 63 mstats.sys += n; 64 65 // On 64-bit, we don't actually have v reserved, so tread carefully. 66 if(sizeof(void*) == 8) { 67 p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 68 if(p == (void*)ENOMEM) 69 runtime·throw("runtime: out of memory"); 70 if(p != v) { 71 runtime·printf("runtime: address space conflict: map(%p) = %p\n", v, p); 72 runtime·throw("runtime: address space conflict"); 73 } 74 return; 75 } 76 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 }