github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/runtime/mem_js.go (about)

     1  // Copyright 2018 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 js,wasm
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  // Don't split the stack as this function may be invoked without a valid G,
    15  // which prevents us from allocating more stack.
    16  //go:nosplit
    17  func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
    18  	p := sysReserve(nil, n)
    19  	sysMap(p, n, sysStat)
    20  	return p
    21  }
    22  
    23  func sysUnused(v unsafe.Pointer, n uintptr) {
    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  }
    35  
    36  func sysFault(v unsafe.Pointer, n uintptr) {
    37  }
    38  
    39  var reserveEnd uintptr
    40  
    41  func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    42  	// TODO(neelance): maybe unify with mem_plan9.go, depending on how https://github.com/WebAssembly/design/blob/master/FutureFeatures.md#finer-grained-control-over-memory turns out
    43  
    44  	if reserveEnd < lastmoduledatap.end {
    45  		reserveEnd = lastmoduledatap.end
    46  	}
    47  	if uintptr(v) < reserveEnd {
    48  		v = unsafe.Pointer(reserveEnd)
    49  	}
    50  	reserveEnd = uintptr(v) + n
    51  
    52  	current := currentMemory()
    53  	needed := int32(reserveEnd/sys.DefaultPhysPageSize + 1)
    54  	if current < needed {
    55  		if growMemory(needed-current) == -1 {
    56  			return nil
    57  		}
    58  	}
    59  
    60  	return v
    61  }
    62  
    63  func currentMemory() int32
    64  func growMemory(pages int32) int32
    65  
    66  func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    67  	mSysStatInc(sysStat, n)
    68  }