Home | History | Annotate | Download | only in runtime
      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 package runtime
      6 
      7 import "unsafe"
      8 
      9 // Don't split the stack as this function may be invoked without a valid G,
     10 // which prevents us from allocating more stack.
     11 //go:nosplit
     12 func sysAlloc(n uintptr, sysStat *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 	mSysStatInc(sysStat, n)
     18 	return v
     19 }
     20 
     21 func sysUnused(v unsafe.Pointer, n uintptr) {
     22 	// Linux's MADV_DONTNEED is like BSD's MADV_FREE.
     23 	madvise(v, n, _MADV_FREE)
     24 }
     25 
     26 func sysUsed(v unsafe.Pointer, n uintptr) {
     27 }
     28 
     29 // Don't split the stack as this function may be invoked without a valid G,
     30 // which prevents us from allocating more stack.
     31 //go:nosplit
     32 func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
     33 	mSysStatDec(sysStat, n)
     34 	munmap(v, n)
     35 }
     36 
     37 func sysFault(v unsafe.Pointer, n uintptr) {
     38 	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
     39 }
     40 
     41 func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
     42 	*reserved = true
     43 	p := (unsafe.Pointer)(mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0))
     44 	if uintptr(p) < 4096 {
     45 		return nil
     46 	}
     47 	return p
     48 }
     49 
     50 const (
     51 	_ENOMEM = 12
     52 )
     53 
     54 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
     55 	mSysStatInc(sysStat, n)
     56 	p := (unsafe.Pointer)(mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0))
     57 	if uintptr(p) == _ENOMEM {
     58 		throw("runtime: out of memory")
     59 	}
     60 	if p != v {
     61 		throw("runtime: cannot map pages in arena address space")
     62 	}
     63 }
     64