github.com/Schaudge/hts@v0.0.0-20240223063651-737b4d69d68c/bam/pool.go (about) 1 package bam 2 3 import ( 4 "sync" 5 "reflect" 6 "unsafe" 7 ) 8 9 var bufPool = sync.Pool{ 10 New: func() interface{} { 11 return []byte{} 12 }, 13 } 14 15 func resizeScratch(buf *[]byte, n int) { 16 if *buf == nil || cap(*buf) < n { 17 // Allocate slightly more memory than needed to prevent frequent 18 // reallocation. 19 size := (n/16 + 1) * 16 20 *buf = make([]byte, n, size) 21 } else { 22 *buf = (*buf)[:n] 23 } 24 } 25 26 const sizeofSliceHeader = int(unsafe.Sizeof(reflect.SliceHeader{})) 27 28 // Round "off" up so that it is a multiple of 8. Used when storing a pointer in 29 // []byte. 8-byte alignment is sufficient for all CPUs we care about. 30 func alignOffset(off int) int { 31 const pointerSize = 8 32 return ((off-1)/pointerSize + 1) * pointerSize 33 }