2014-11-21 11:39:01 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
var bloc uintptr
|
|
|
|
var memlock mutex
|
|
|
|
|
2015-01-30 04:36:12 -07:00
|
|
|
func memRound(p uintptr) uintptr {
|
|
|
|
return (p + _PAGESIZE - 1) &^ (_PAGESIZE - 1)
|
|
|
|
}
|
2014-11-21 11:39:01 -07:00
|
|
|
|
|
|
|
func initBloc() {
|
2015-01-30 04:36:12 -07:00
|
|
|
bloc = memRound(uintptr(unsafe.Pointer(&end)))
|
2014-11-21 11:39:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sbrk(n uintptr) unsafe.Pointer {
|
|
|
|
lock(&memlock)
|
|
|
|
// Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c
|
2015-01-30 04:36:12 -07:00
|
|
|
bl := bloc
|
|
|
|
n = memRound(n)
|
2014-11-21 11:39:01 -07:00
|
|
|
if brk_(unsafe.Pointer(bl+n)) < 0 {
|
|
|
|
unlock(&memlock)
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-30 04:36:12 -07:00
|
|
|
bloc += n
|
2014-11-21 11:39:01 -07:00
|
|
|
unlock(&memlock)
|
|
|
|
return unsafe.Pointer(bl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer {
|
|
|
|
p := sbrk(n)
|
|
|
|
if p != nil {
|
|
|
|
xadd64(stat, int64(n))
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) {
|
|
|
|
xadd64(stat, -int64(n))
|
|
|
|
lock(&memlock)
|
|
|
|
// from tiny/mem.c
|
|
|
|
// Push pointer back if this is a free
|
|
|
|
// of the most recent sysAlloc.
|
2015-01-30 04:36:12 -07:00
|
|
|
n = memRound(n)
|
2014-11-21 11:39:01 -07:00
|
|
|
if bloc == uintptr(v)+n {
|
|
|
|
bloc -= n
|
|
|
|
}
|
|
|
|
unlock(&memlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysUnused(v unsafe.Pointer, n uintptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysUsed(v unsafe.Pointer, n uintptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysMap(v unsafe.Pointer, n uintptr, reserved bool, stat *uint64) {
|
|
|
|
// sysReserve has already allocated all heap memory,
|
|
|
|
// but has not adjusted stats.
|
|
|
|
xadd64(stat, int64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysFault(v unsafe.Pointer, n uintptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
|
|
|
|
*reserved = true
|
|
|
|
return sbrk(n)
|
|
|
|
}
|