Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions sync_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ func NewSyncPool(minSize, maxSize, factor int) *SyncPool {
}

// Alloc try alloc a []byte from internal slab class if no free chunk in slab class Alloc will make one.
func (pool *SyncPool) Alloc(size int) []byte {
// 1st returned value indicate a slice caller can use
// 2nd returned value indicate the capacity of the slice
// 3rd returned value indicate whether the slice is allocated from this pool or not
func (pool *SyncPool) Alloc(size int) ([]byte, int, bool) {
if size <= pool.maxSize {
for i := 0; i < len(pool.classesSize); i++ {
if pool.classesSize[i] >= size {
mem := pool.classes[i].Get().(*[]byte)
return (*mem)[:size]
return (*mem)[:size], pool.classesSize[i], true
}
}
}
return make([]byte, size)
return make([]byte, size), size, false
}

// Free release a []byte that alloc from Pool.Alloc.
Expand Down