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