github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/mem_plan9.c (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 #include "runtime.h" 6 #include "defs_GOOS_GOARCH.h" 7 #include "arch_GOARCH.h" 8 #include "malloc.h" 9 #include "os_GOOS.h" 10 11 extern byte end[]; 12 static byte *bloc = { end }; 13 static Lock memlock; 14 15 enum 16 { 17 Round = PAGESIZE-1 18 }; 19 20 void* 21 runtime·SysAlloc(uintptr nbytes) 22 { 23 uintptr bl; 24 25 runtime·lock(&memlock); 26 mstats.sys += nbytes; 27 // Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c 28 bl = ((uintptr)bloc + Round) & ~Round; 29 if(runtime·brk_((void*)(bl + nbytes)) < 0) { 30 runtime·unlock(&memlock); 31 return nil; 32 } 33 bloc = (byte*)bl + nbytes; 34 runtime·unlock(&memlock); 35 return (void*)bl; 36 } 37 38 void 39 runtime·SysFree(void *v, uintptr nbytes) 40 { 41 runtime·lock(&memlock); 42 mstats.sys -= nbytes; 43 // from tiny/mem.c 44 // Push pointer back if this is a free 45 // of the most recent SysAlloc. 46 nbytes += (nbytes + Round) & ~Round; 47 if(bloc == (byte*)v+nbytes) 48 bloc -= nbytes; 49 runtime·unlock(&memlock); 50 } 51 52 void 53 runtime·SysUnused(void *v, uintptr nbytes) 54 { 55 USED(v, nbytes); 56 } 57 58 void 59 runtime·SysMap(void *v, uintptr nbytes) 60 { 61 USED(v, nbytes); 62 } 63 64 void* 65 runtime·SysReserve(void *v, uintptr nbytes) 66 { 67 USED(v); 68 return runtime·SysAlloc(nbytes); 69 }