clash/common/pool/alloc.go

74 lines
1.6 KiB
Go
Raw Normal View History

2020-04-25 00:30:40 +08:00
package pool
// Inspired by https://github.com/xtaci/smux/blob/master/alloc.go
import (
"errors"
"math/bits"
"sync"
)
var defaultAllocator = NewAllocator()
2020-04-25 00:30:40 +08:00
// 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
2022-03-16 12:10:13 +08:00
alloc.buffers[k].New = func() any {
2020-04-25 00:30:40 +08:00
return make([]byte, 1<<uint32(i))
}
}
return alloc
}
// 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:
2020-04-25 00:30:40 +08:00
return nil
case size > 65536:
return make([]byte, size)
default:
bits := msb(size)
if size == 1<<bits {
return alloc.buffers[bits].Get().([]byte)[:size]
}
2020-04-25 00:30:40 +08:00
return alloc.buffers[bits+1].Get().([]byte)[:size]
2020-04-25 00:30:40 +08:00
}
}
// Put returns a []byte to pool for future use,
// which the cap must be exactly 2^n
func (alloc *Allocator) Put(buf []byte) error {
if cap(buf) == 0 || cap(buf) > 65536 {
return nil
}
2020-04-25 00:30:40 +08:00
bits := msb(cap(buf))
if cap(buf) != 1<<bits {
2020-04-25 00:30:40 +08:00
return errors.New("allocator Put() incorrect buffer size")
}
2020-08-25 22:19:59 +08:00
2021-10-10 23:44:09 +08:00
//nolint
2022-05-17 19:58:33 +08:00
//lint:ignore SA6002 ignore temporarily
2020-04-25 00:30:40 +08:00
alloc.buffers[bits].Put(buf)
return nil
}
2020-10-14 19:56:02 +08:00
// msb return the pos of most significant bit
2020-04-25 00:30:40 +08:00
func msb(size int) uint16 {
return uint16(bits.Len32(uint32(size)) - 1)
}