github.com/outcaste-io/ristretto@v0.2.3/contrib/memtest/nojemalloc.go (about)

     1  //+build !jemalloc
     2  
     3  package main
     4  
     5  // #include <stdlib.h>
     6  import "C"
     7  import (
     8  	"reflect"
     9  	"sync/atomic"
    10  	"unsafe"
    11  )
    12  
    13  func Calloc(size int) []byte {
    14  	if size == 0 {
    15  		return make([]byte, 0)
    16  	}
    17  	ptr := C.calloc(C.size_t(size), 1)
    18  	if ptr == nil {
    19  		panic("OOM")
    20  	}
    21  	hdr := reflect.SliceHeader{Data: uintptr(ptr), Len: size, Cap: size}
    22  	atomic.AddInt64(&numbytes, int64(size))
    23  	return *(*[]byte)(unsafe.Pointer(&hdr))
    24  }
    25  
    26  func Free(bs []byte) {
    27  	if len(bs) == 0 {
    28  		return
    29  	}
    30  
    31  	if sz := cap(bs); sz != 0 {
    32  		bs = bs[:cap(bs)]
    33  		C.free(unsafe.Pointer(&bs[0]))
    34  		atomic.AddInt64(&numbytes, -int64(sz))
    35  	}
    36  }
    37  
    38  func NumAllocBytes() int64 { return atomic.LoadInt64(&numbytes) }
    39  
    40  func check() {}