package runtime
import (
"runtime/internal/atomic"
"unsafe"
)
const (
_EACCES = 13
_EINVAL = 22
)
func sysAllocOS (n uintptr ) unsafe .Pointer {
p , err := mmap (nil , n , _PROT_READ |_PROT_WRITE , _MAP_ANON |_MAP_PRIVATE , -1 , 0 )
if err != 0 {
if err == _EACCES {
print ("runtime: mmap: access denied\n" )
exit (2 )
}
if err == _EAGAIN {
print ("runtime: mmap: too much locked memory (check 'ulimit -l').\n" )
exit (2 )
}
return nil
}
return p
}
var adviseUnused = uint32 (_MADV_FREE )
const madviseUnsupported = 0
func sysUnusedOS (v unsafe .Pointer , n uintptr ) {
if uintptr (v )&(physPageSize -1 ) != 0 || n &(physPageSize -1 ) != 0 {
throw ("unaligned sysUnused" )
}
advise := atomic .Load (&adviseUnused )
if debug .madvdontneed != 0 && advise != madviseUnsupported {
advise = _MADV_DONTNEED
}
switch advise {
case _MADV_FREE :
if madvise (v , n , _MADV_FREE ) == 0 {
break
}
atomic .Store (&adviseUnused , _MADV_DONTNEED )
fallthrough
case _MADV_DONTNEED :
if madvise (v , n , _MADV_DONTNEED ) == 0 {
break
}
atomic .Store (&adviseUnused , madviseUnsupported )
fallthrough
case madviseUnsupported :
mmap (v , n , _PROT_READ |_PROT_WRITE , _MAP_ANON |_MAP_FIXED |_MAP_PRIVATE , -1 , 0 )
}
if debug .harddecommit > 0 {
p , err := mmap (v , n , _PROT_NONE , _MAP_ANON |_MAP_FIXED |_MAP_PRIVATE , -1 , 0 )
if p != v || err != 0 {
throw ("runtime: cannot disable permissions in address space" )
}
}
}
func sysUsedOS (v unsafe .Pointer , n uintptr ) {
if debug .harddecommit > 0 {
p , err := mmap (v , n , _PROT_READ |_PROT_WRITE , _MAP_ANON |_MAP_FIXED |_MAP_PRIVATE , -1 , 0 )
if err == _ENOMEM {
throw ("runtime: out of memory" )
}
if p != v || err != 0 {
throw ("runtime: cannot remap pages in address space" )
}
return
}
}
func sysHugePageOS (v unsafe .Pointer , n uintptr ) {
if physHugePageSize != 0 {
beg := alignUp (uintptr (v ), physHugePageSize )
end := alignDown (uintptr (v )+n , physHugePageSize )
if beg < end {
madvise (unsafe .Pointer (beg ), end -beg , _MADV_HUGEPAGE )
}
}
}
func sysNoHugePageOS (v unsafe .Pointer , n uintptr ) {
if uintptr (v )&(physPageSize -1 ) != 0 {
throw ("unaligned sysNoHugePageOS" )
}
madvise (v , n , _MADV_NOHUGEPAGE )
}
func sysHugePageCollapseOS (v unsafe .Pointer , n uintptr ) {
if uintptr (v )&(physPageSize -1 ) != 0 {
throw ("unaligned sysHugePageCollapseOS" )
}
if physHugePageSize == 0 {
return
}
madvise (v , n , _MADV_COLLAPSE )
}
func sysFreeOS (v unsafe .Pointer , n uintptr ) {
munmap (v , n )
}
func sysFaultOS (v unsafe .Pointer , n uintptr ) {
mmap (v , n , _PROT_NONE , _MAP_ANON |_MAP_PRIVATE |_MAP_FIXED , -1 , 0 )
}
func sysReserveOS (v unsafe .Pointer , n uintptr ) unsafe .Pointer {
p , err := mmap (v , n , _PROT_NONE , _MAP_ANON |_MAP_PRIVATE , -1 , 0 )
if err != 0 {
return nil
}
return p
}
func sysMapOS (v unsafe .Pointer , n uintptr ) {
p , err := mmap (v , n , _PROT_READ |_PROT_WRITE , _MAP_ANON |_MAP_FIXED |_MAP_PRIVATE , -1 , 0 )
if err == _ENOMEM {
throw ("runtime: out of memory" )
}
if p != v || err != 0 {
print ("runtime: mmap(" , v , ", " , n , ") returned " , p , ", " , err , "\n" )
throw ("runtime: cannot map pages in arena address space" )
}
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .