rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/mem_bsd.go (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  // +build dragonfly freebsd nacl netbsd openbsd solaris
     6  
     7  package runtime
     8  
     9  import "unsafe"
    10  
    11  //go:nosplit
    12  func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer {
    13  	v := unsafe.Pointer(mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
    14  	if uintptr(v) < 4096 {
    15  		return nil
    16  	}
    17  	xadd64(stat, int64(n))
    18  	return v
    19  }
    20  
    21  func sysUnused(v unsafe.Pointer, n uintptr) {
    22  	madvise(v, n, _MADV_FREE)
    23  }
    24  
    25  func sysUsed(v unsafe.Pointer, n uintptr) {
    26  }
    27  
    28  func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) {
    29  	xadd64(stat, -int64(n))
    30  	munmap(v, n)
    31  }
    32  
    33  func sysFault(v unsafe.Pointer, n uintptr) {
    34  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    35  }
    36  
    37  func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
    38  	// On 64-bit, people with ulimit -v set complain if we reserve too
    39  	// much address space.  Instead, assume that the reservation is okay
    40  	// and check the assumption in SysMap.
    41  	if ptrSize == 8 && uint64(n) > 1<<32 || goos_nacl != 0 {
    42  		*reserved = false
    43  		return v
    44  	}
    45  
    46  	p := unsafe.Pointer(mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
    47  	if uintptr(p) < 4096 {
    48  		return nil
    49  	}
    50  	*reserved = true
    51  	return p
    52  }
    53  
    54  func sysMap(v unsafe.Pointer, n uintptr, reserved bool, stat *uint64) {
    55  	const _ENOMEM = 12
    56  
    57  	xadd64(stat, int64(n))
    58  
    59  	// On 64-bit, we don't actually have v reserved, so tread carefully.
    60  	if !reserved {
    61  		flags := int32(_MAP_ANON | _MAP_PRIVATE)
    62  		if GOOS == "dragonfly" {
    63  			// TODO(jsing): For some reason DragonFly seems to return
    64  			// memory at a different address than we requested, even when
    65  			// there should be no reason for it to do so. This can be
    66  			// avoided by using MAP_FIXED, but I'm not sure we should need
    67  			// to do this - we do not on other platforms.
    68  			flags |= _MAP_FIXED
    69  		}
    70  		p := mmap(v, n, _PROT_READ|_PROT_WRITE, flags, -1, 0)
    71  		if uintptr(p) == _ENOMEM {
    72  			throw("runtime: out of memory")
    73  		}
    74  		if p != v {
    75  			print("runtime: address space conflict: map(", v, ") = ", p, "\n")
    76  			throw("runtime: address space conflict")
    77  		}
    78  		return
    79  	}
    80  
    81  	p := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    82  	if uintptr(p) == _ENOMEM {
    83  		throw("runtime: out of memory")
    84  	}
    85  	if p != v {
    86  		throw("runtime: cannot map pages in arena address space")
    87  	}
    88  }