mirror of
https://github.com/xtaci/smux.git
synced 2024-04-21 10:51:48 +00:00
add comments to alloc
This commit is contained in:
@@ -12,12 +12,14 @@ func init() {
|
||||
defaultAllocator = NewAllocator()
|
||||
}
|
||||
|
||||
// allocator for incoming frames
|
||||
// to prevent write after zeroing
|
||||
// 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
|
||||
@@ -30,6 +32,7 @@ func NewAllocator() *Allocator {
|
||||
return alloc
|
||||
}
|
||||
|
||||
// Get a []byte from pool with most appropriate cap
|
||||
func (alloc *Allocator) Get(size int) []byte {
|
||||
if size <= 0 || size > 65536 {
|
||||
return nil
|
||||
@@ -43,6 +46,8 @@ func (alloc *Allocator) Get(size int) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// Put returns a []byte to pool for future use,
|
||||
// which the cap must be exactly 2^n
|
||||
func (alloc *Allocator) Put(buf []byte) error {
|
||||
bits := msb(cap(buf))
|
||||
if cap(buf) == 0 || cap(buf) > 65536 || cap(buf) != 1<<bits {
|
||||
|
||||
Reference in New Issue
Block a user