github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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, uint64 *stat)
    22  {
    23  	uintptr bl;
    24  
    25  	runtime·lock(&memlock);
    26  	// Plan 9 sbrk from /sys/src/libc/9sys/sbrk.c
    27  	bl = ((uintptr)bloc + Round) & ~Round;
    28  	if(runtime·brk_((void*)(bl + nbytes)) < 0) {
    29  		runtime·unlock(&memlock);
    30  		return nil;
    31  	}
    32  	bloc = (byte*)bl + nbytes;
    33  	runtime·unlock(&memlock);
    34  	runtime·xadd64(stat, nbytes);
    35  	return (void*)bl;
    36  }
    37  
    38  void
    39  runtime·SysFree(void *v, uintptr nbytes, uint64 *stat)
    40  {
    41  	runtime·xadd64(stat, -(uint64)nbytes);
    42  	runtime·lock(&memlock);
    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·SysUsed(void *v, uintptr nbytes)
    60  {
    61  	USED(v, nbytes);
    62  }
    63  
    64  void
    65  runtime·SysMap(void *v, uintptr nbytes, uint64 *stat)
    66  {
    67  	USED(v, nbytes, stat);
    68  }
    69  
    70  void*
    71  runtime·SysReserve(void *v, uintptr nbytes)
    72  {
    73  	USED(v);
    74  	return runtime·SysAlloc(nbytes, &mstats.heap_sys);
    75  }