github.com/dgraph-io/ristretto@v0.1.2-0.20240116140435-c67e07994f91/contrib/memtest/nojemalloc.go (about) 1 //go:build !jemalloc 2 // +build !jemalloc 3 4 package main 5 6 // #include <stdlib.h> 7 import "C" 8 import ( 9 "log" 10 "reflect" 11 "sync/atomic" 12 "unsafe" 13 ) 14 15 func Calloc(size int) []byte { 16 if size == 0 { 17 return make([]byte, 0) 18 } 19 ptr := C.calloc(C.size_t(size), 1) 20 if ptr == nil { 21 panic("OOM") 22 } 23 hdr := reflect.SliceHeader{Data: uintptr(ptr), Len: size, Cap: size} 24 atomic.AddInt64(&numbytes, int64(size)) 25 //nolint:govet 26 return *(*[]byte)(unsafe.Pointer(&hdr)) 27 } 28 29 func Free(bs []byte) { 30 if len(bs) == 0 { 31 return 32 } 33 34 if sz := cap(bs); sz != 0 { 35 bs = bs[:cap(bs)] 36 C.free(unsafe.Pointer(&bs[0])) 37 atomic.AddInt64(&numbytes, -int64(sz)) 38 } 39 } 40 41 func NumAllocBytes() int64 { return atomic.LoadInt64(&numbytes) } 42 43 func check() {} 44 45 func init() { 46 log.Println("USING CALLOC") 47 }