package pool

// Inspired by https://github.com/xtaci/smux/blob/master/alloc.go

import (
	"math/bits"
	"sync"
)

var defaultAllocator = NewAllocator()

// Allocator for incoming frames, optimized to prevent overwriting after zeroing
type Allocator struct {
	buffers []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 {
	alloc := new(Allocator)
	alloc.buffers = make([]sync.Pool, 17) // 1B -> 64K
	for k := range alloc.buffers {
		i := k
		alloc.buffers[k].New = func() any {
			return make([]byte, 0, 1<<uint32(i))
		}
	}
	return alloc
}

// AcquireByteArr a []byte from pool with most appropriate cap
func (alloc *Allocator) Get(size int) []byte {
	if size <= 0 || size > 65536 {
		return nil
	}

	bit := msb(size)
	if size == 1<<bit {
		return alloc.buffers[bit].Get().([]byte)[:size]
	}

	return alloc.buffers[bit+1].Get().([]byte)[:size]
}

// ReleaseByteArr returns a []byte to pool for future use,
// which the cap must be exactly 2^n
func (alloc *Allocator) Put(buf []byte) {
	alloc.buffers[msb(cap(buf))].Put(buf)
}

// msb return the pos of most significant bit
func msb(size int) uint16 {
	return uint16(bits.Len32(uint32(size)) - 1)
}
