-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
46 lines (39 loc) · 857 Bytes
/
pool.go
File metadata and controls
46 lines (39 loc) · 857 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
42
43
44
45
46
package cbytebuf
import (
"sync"
)
// Pool is a pool implementation based on sync.Pool.
type Pool struct {
p sync.Pool
}
// P is a default instance of native pool for simple cases.
// Just call cbytebuf.Acquire() and cbytebuf.Release().
var P Pool
// Get old byte buffer from the pool or create a new byte buffer.
func (p *Pool) Get() *CByteBuf {
v := p.p.Get()
if v != nil {
if b, ok := v.(*CByteBuf); ok {
return b
}
}
return &CByteBuf{}
}
// Put byte buffer back to the pool.
//
// Using data returned from the buffer after putting is unsafe.
func (p *Pool) Put(b *CByteBuf) {
if b.h.Data == 0 {
return
}
b.Reset()
p.p.Put(b)
}
// Acquire gets byte buffer from default pool instance.
func Acquire() *CByteBuf {
return P.Get()
}
// Release puts byte buffer back to default pool instance.
func Release(b *CByteBuf) {
P.Put(b)
}