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.
5 // +build dragonfly freebsd nacl netbsd openbsd solaris
10 "runtime/internal/sys"
14 // Don't split the stack as this function may be invoked without a valid G,
15 // which prevents us from allocating more stack.
17 func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
18 v := unsafe.Pointer(mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
19 if uintptr(v) < 4096 {
22 mSysStatInc(sysStat, n)
26 func sysUnused(v unsafe.Pointer, n uintptr) {
27 madvise(v, n, _MADV_FREE)
30 func sysUsed(v unsafe.Pointer, n uintptr) {
33 // Don't split the stack as this function may be invoked without a valid G,
34 // which prevents us from allocating more stack.
36 func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
37 mSysStatDec(sysStat, n)
41 func sysFault(v unsafe.Pointer, n uintptr) {
42 mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
45 func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
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 sys.PtrSize == 8 && uint64(n) > 1<<32 || sys.GoosNacl != 0 {
54 p := unsafe.Pointer(mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
55 if uintptr(p) < 4096 {
62 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
65 mSysStatInc(sysStat, n)
67 // On 64-bit, we don't actually have v reserved, so tread carefully.
69 flags := int32(_MAP_ANON | _MAP_PRIVATE)
70 if GOOS == "dragonfly" {
71 // TODO(jsing): For some reason DragonFly seems to return
72 // memory at a different address than we requested, even when
73 // there should be no reason for it to do so. This can be
74 // avoided by using MAP_FIXED, but I'm not sure we should need
75 // to do this - we do not on other platforms.
78 p := mmap(v, n, _PROT_READ|_PROT_WRITE, flags, -1, 0)
79 if uintptr(p) == _ENOMEM {
80 throw("runtime: out of memory")
83 print("runtime: address space conflict: map(", v, ") = ", p, "\n")
84 throw("runtime: address space conflict")
89 p := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
90 if uintptr(p) == _ENOMEM {
91 throw("runtime: out of memory")
94 throw("runtime: cannot map pages in arena address space")