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"
|
|
|
|
|
2015-02-26 14:41:33 -07:00
|
|
|
const memDebug = false
|
|
|
|
|
2014-11-21 11:39:01 -07:00
|
|
|
var bloc uintptr
|
|
|
|
var memlock mutex
|
|
|
|
|
2015-02-26 14:41:33 -07:00
|
|
|
type memHdr struct {
|
|
|
|
next *memHdr
|
|
|
|
size uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
var memFreelist *memHdr // sorted in ascending order
|
|
|
|
|
|
|
|
func memAlloc(n uintptr) unsafe.Pointer {
|
|
|
|
n = memRound(n)
|
|
|
|
var prevp *memHdr
|
|
|
|
for p := memFreelist; p != nil; p = p.next {
|
|
|
|
if p.size >= n {
|
|
|
|
if p.size == n {
|
|
|
|
if prevp != nil {
|
|
|
|
prevp.next = p.next
|
|
|
|
} else {
|
|
|
|
memFreelist = p.next
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.size -= n
|
|
|
|
p = (*memHdr)(add(unsafe.Pointer(p), p.size))
|
|
|
|
}
|
|
|
|
memclr(unsafe.Pointer(p), unsafe.Sizeof(memHdr{}))
|
|
|
|
return unsafe.Pointer(p)
|
|
|
|
}
|
|
|
|
prevp = p
|
|
|
|
}
|
|
|
|
return sbrk(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func memFree(ap unsafe.Pointer, n uintptr) {
|
|
|
|
n = memRound(n)
|
|
|
|
memclr(ap, n)
|
|
|
|
bp := (*memHdr)(ap)
|
|
|
|
bp.size = n
|
|
|
|
bpn := uintptr(ap)
|
|
|
|
if memFreelist == nil {
|
|
|
|
bp.next = nil
|
|
|
|
memFreelist = bp
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := memFreelist
|
|
|
|
if bpn < uintptr(unsafe.Pointer(p)) {
|
|
|
|
memFreelist = bp
|
|
|
|
if bpn+bp.size == uintptr(unsafe.Pointer(p)) {
|
|
|
|
bp.size += p.size
|
|
|
|
bp.next = p.next
|
|
|
|
memclr(unsafe.Pointer(p), unsafe.Sizeof(memHdr{}))
|
|
|
|
} else {
|
|
|
|
bp.next = p
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for ; p.next != nil; p = p.next {
|
|
|
|
if bpn > uintptr(unsafe.Pointer(p)) && bpn < uintptr(unsafe.Pointer(p.next)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bpn+bp.size == uintptr(unsafe.Pointer(p.next)) {
|
|
|
|
bp.size += p.next.size
|
|
|
|
bp.next = p.next.next
|
|
|
|
memclr(unsafe.Pointer(p.next), unsafe.Sizeof(memHdr{}))
|
|
|
|
} else {
|
|
|
|
bp.next = p.next
|
|
|
|
}
|
|
|
|
if uintptr(unsafe.Pointer(p))+p.size == bpn {
|
|
|
|
p.size += bp.size
|
|
|
|
p.next = bp.next
|
|
|
|
memclr(unsafe.Pointer(bp), unsafe.Sizeof(memHdr{}))
|
|
|
|
} else {
|
|
|
|
p.next = bp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func memCheck() {
|
|
|
|
if memDebug == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for p := memFreelist; p != nil && p.next != nil; p = p.next {
|
|
|
|
if uintptr(unsafe.Pointer(p)) == uintptr(unsafe.Pointer(p.next)) {
|
|
|
|
print("runtime: ", unsafe.Pointer(p), " == ", unsafe.Pointer(p.next), "\n")
|
|
|
|
throw("mem: infinite loop")
|
|
|
|
}
|
|
|
|
if uintptr(unsafe.Pointer(p)) > uintptr(unsafe.Pointer(p.next)) {
|
|
|
|
print("runtime: ", unsafe.Pointer(p), " > ", unsafe.Pointer(p.next), "\n")
|
|
|
|
throw("mem: unordered list")
|
|
|
|
}
|
|
|
|
if uintptr(unsafe.Pointer(p))+p.size > uintptr(unsafe.Pointer(p.next)) {
|
|
|
|
print("runtime: ", unsafe.Pointer(p), "+", p.size, " > ", unsafe.Pointer(p.next), "\n")
|
|
|
|
throw("mem: overlapping blocks")
|
|
|
|
}
|
|
|
|
for b := add(unsafe.Pointer(p), unsafe.Sizeof(memHdr{})); uintptr(b) < uintptr(unsafe.Pointer(p))+p.size; b = add(b, 1) {
|
|
|
|
if *(*byte)(b) != 0 {
|
|
|
|
print("runtime: value at addr ", b, " with offset ", uintptr(b)-uintptr(unsafe.Pointer(p)), " in block ", p, " of size ", p.size, " is not zero\n")
|
|
|
|
throw("mem: uninitialised memory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-06 18:55:02 -06:00
|
|
|
bloc = memRound(firstmoduledata.end)
|
2014-11-21 11:39:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sbrk(n uintptr) unsafe.Pointer {
|
|
|
|
// 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 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-30 04:36:12 -07:00
|
|
|
bloc += n
|
2014-11-21 11:39:01 -07:00
|
|
|
return unsafe.Pointer(bl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer {
|
2015-02-26 14:41:33 -07:00
|
|
|
lock(&memlock)
|
|
|
|
p := memAlloc(n)
|
|
|
|
memCheck()
|
|
|
|
unlock(&memlock)
|
2014-11-21 11:39:01 -07:00
|
|
|
if p != nil {
|
|
|
|
xadd64(stat, int64(n))
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) {
|
|
|
|
xadd64(stat, -int64(n))
|
|
|
|
lock(&memlock)
|
2015-02-26 14:41:33 -07:00
|
|
|
memFree(v, n)
|
|
|
|
memCheck()
|
2014-11-21 11:39:01 -07:00
|
|
|
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
|
2015-02-26 14:41:33 -07:00
|
|
|
lock(&memlock)
|
|
|
|
p := memAlloc(n)
|
|
|
|
memCheck()
|
|
|
|
unlock(&memlock)
|
|
|
|
return p
|
2014-11-21 11:39:01 -07:00
|
|
|
}
|