-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyteSyncPool.go
More file actions
41 lines (36 loc) · 805 Bytes
/
byteSyncPool.go
File metadata and controls
41 lines (36 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pool
import (
"sync"
)
//BytePool is a simple pool of []byte
type ByteSyncPool struct {
sliceSize int
pool *sync.Pool
}
//NewBytePool...
func NewByteSyncPool(sliceSize int) *ByteSyncPool {
return &ByteSyncPool{
sliceSize: sliceSize,
pool: &sync.Pool{
New: func() interface{} {
return make([]byte, sliceSize)
},
},
}
}
//Get returns a cleared []byte
func (b *ByteSyncPool) Get() (value []byte) {
v := b.pool.Get().([]byte)
//clear the slice before returning it
for i, _ := range v {
v[i] = 0
}
//resize the slice in case we have been returned a partial slice
//if the []byte given doesn't have the correct capacity we will panic!
v = v[:b.sliceSize]
return v
}
//Put adds a slice to the pool
func (b ByteSyncPool) Put(values []byte) {
b.pool.Put(values)
}