package pool // Inspired by https://github.com/xtaci/smux/blob/master/alloc.go import ( "errors" "math/bits" "sync" ) var defaultAllocator = NewAllocator() // Allocator for incoming frames, optimized to prevent overwriting after zeroing type Allocator struct { buffers [17]sync.Pool } // NewAllocator initiates a []byte allocator for frames less than 65536 bytes, // the waste(memory fragmentation) of space allocation is guaranteed to be // no more than 50%. func NewAllocator() *Allocator { return &Allocator{ buffers: [...]sync.Pool{ // 1B -> 64K {New: func() any { return new([1]byte) }}, {New: func() any { return new([1 << 1]byte) }}, {New: func() any { return new([1 << 2]byte) }}, {New: func() any { return new([1 << 3]byte) }}, {New: func() any { return new([1 << 4]byte) }}, {New: func() any { return new([1 << 5]byte) }}, {New: func() any { return new([1 << 6]byte) }}, {New: func() any { return new([1 << 7]byte) }}, {New: func() any { return new([1 << 8]byte) }}, {New: func() any { return new([1 << 9]byte) }}, {New: func() any { return new([1 << 10]byte) }}, {New: func() any { return new([1 << 11]byte) }}, {New: func() any { return new([1 << 12]byte) }}, {New: func() any { return new([1 << 13]byte) }}, {New: func() any { return new([1 << 14]byte) }}, {New: func() any { return new([1 << 15]byte) }}, {New: func() any { return new([1 << 16]byte) }}, }, } } // Get a []byte from pool with most appropriate cap func (alloc *Allocator) Get(size int) []byte { switch { case size < 0: panic("alloc.Get: len out of range") case size == 0: return nil case size > 65536: return make([]byte, size) default: index := msb(size) if size != 1< 65536 { return nil } bits := msb(cap(buf)) if cap(buf) != 1<