github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/mem_plan9.go (about) 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 var bloc uintptr 10 var memlock mutex 11 12 const memRound = _PAGESIZE - 1 13 14 func initBloc() { 15 bloc = uintptr(unsafe.Pointer(&end)) 16 } 17 18 func sbrk(n uintptr) unsafe.Pointer { 19 lock(&memlock) 20 // Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c 21 bl := (bloc + memRound) &^ memRound 22 if brk_(unsafe.Pointer(bl+n)) < 0 { 23 unlock(&memlock) 24 return nil 25 } 26 bloc = bl + n 27 unlock(&memlock) 28 return unsafe.Pointer(bl) 29 } 30 31 func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer { 32 p := sbrk(n) 33 if p != nil { 34 xadd64(stat, int64(n)) 35 } 36 return p 37 } 38 39 func sysFree(v unsafe.Pointer, n uintptr, stat *uint64) { 40 xadd64(stat, -int64(n)) 41 lock(&memlock) 42 // from tiny/mem.c 43 // Push pointer back if this is a free 44 // of the most recent sysAlloc. 45 n += (n + memRound) &^ memRound 46 if bloc == uintptr(v)+n { 47 bloc -= n 48 } 49 unlock(&memlock) 50 } 51 52 func sysUnused(v unsafe.Pointer, n uintptr) { 53 } 54 55 func sysUsed(v unsafe.Pointer, n uintptr) { 56 } 57 58 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, stat *uint64) { 59 // sysReserve has already allocated all heap memory, 60 // but has not adjusted stats. 61 xadd64(stat, int64(n)) 62 } 63 64 func sysFault(v unsafe.Pointer, n uintptr) { 65 } 66 67 func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer { 68 *reserved = true 69 return sbrk(n) 70 }