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

     1  // +build jemalloc,allocator
     2  
     3  package main
     4  
     5  import (
     6  	"unsafe"
     7  
     8  	"github.com/outcaste-io/ristretto/z"
     9  )
    10  
    11  // Defined in node.go.
    12  func init() {
    13  	alloc = z.NewAllocator(10 << 20, "demo")
    14  }
    15  
    16  func newNode(val int) *node {
    17  	// b := alloc.Allocate(nodeSz)
    18  	b := alloc.AllocateAligned(nodeSz)
    19  	n := (*node)(unsafe.Pointer(&b[0]))
    20  	n.val = val
    21  	alloc.Allocate(1) // Extra allocate just to demonstrate AllocateAligned is working as expected.
    22  	return n
    23  }
    24  
    25  func freeNode(n *node) {
    26  	// buf := (*[z.MaxArrayLen]byte)(unsafe.Pointer(n))[:nodeSz:nodeSz]
    27  	// z.Free(buf)
    28  }