-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuffer_pool.go
52 lines (42 loc) · 947 Bytes
/
buffer_pool.go
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
47
48
49
50
51
52
package log
import (
"bytes"
"sync"
"sync/atomic"
"unsafe"
)
type BytesBufferPool interface {
Get() *bytes.Buffer
Put(*bytes.Buffer)
}
var _BytesBufferPoolPtr unsafe.Pointer = unsafe.Pointer(&_defaultBytesBufferPool) // *BytesBufferPool
func getBytesBufferPool() BytesBufferPool {
ptr := (*BytesBufferPool)(atomic.LoadPointer(&_BytesBufferPoolPtr))
return *ptr
}
func SetBytesBufferPool(pool BytesBufferPool) {
if pool == nil {
return
}
atomic.StorePointer(&_BytesBufferPoolPtr, unsafe.Pointer(&pool))
}
var _defaultBytesBufferPool BytesBufferPool = &bytesBufferPool{
pool: sync.Pool{
New: syncPoolNew,
},
}
func syncPoolNew() interface{} {
return bytes.NewBuffer(make([]byte, 0, 16<<10))
}
type bytesBufferPool struct {
pool sync.Pool
}
func (p *bytesBufferPool) Get() *bytes.Buffer {
return p.pool.Get().(*bytes.Buffer)
}
func (p *bytesBufferPool) Put(x *bytes.Buffer) {
if x == nil {
return
}
p.pool.Put(x)
}