package allocator import ( "errors" "math/bits" "github.com/xjasonlyu/tun2socks/v2/internal/pool" ) // Allocator for incoming frames, optimized to prevent overwriting // after zeroing. type Allocator struct { buffers []*pool.Pool[[]byte] } // New 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 New() *Allocator { alloc := &Allocator{} alloc.buffers = make([]*pool.Pool[[]byte], 17) // 1B -> 64K for k := range alloc.buffers { i := k alloc.buffers[k] = pool.New(func() []byte { return make([]byte, 1< 65536 { return nil } b := msb(size) if size == 1< 65536 || cap(buf) != 1<