-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuffer_pool.go
123 lines (103 loc) · 2.07 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package secureio
import (
"fmt"
"sync"
"sync/atomic"
)
type buffer struct {
locker lockerRWMutex
pool *bufferPool
isMonopolized uint32
isBusy bool
lockID uint64
refCount int32
Bytes []byte
Offset uint
MetadataVariableUInt uint
}
func (buf *buffer) LockDo(fn func()) {
if !buf.isBusy {
panic(`should not happen`)
}
buf.locker.LockDo(fn)
}
func (buf *buffer) Lock() {
if !buf.isBusy {
panic(`should not happen`)
}
buf.locker.Lock()
}
func (buf *buffer) Unlock() {
buf.locker.Unlock()
}
func (buf *buffer) Read(b []byte) (int, error) {
copy(b, buf.Bytes[buf.Offset:])
buf.Offset += uint(len(b))
return len(b), nil
}
func (buf *buffer) Reset() {
buf.Offset = 0
buf.Bytes = buf.Bytes[:0]
buf.MetadataVariableUInt = 0
}
func (buf *buffer) Grow(size uint) {
if uint(cap(buf.Bytes)) < size {
buf.Bytes = make([]byte, size)
return
}
buf.Bytes = buf.Bytes[:size]
}
func (buf *buffer) Len() uint {
return uint(len(buf.Bytes)) - buf.Offset
}
func (buf *buffer) Cap() uint {
return uint(cap(buf.Bytes))
}
type bufferPool struct {
storage sync.Pool
}
func newBufferPool(maxSize uint) *bufferPool {
pool := &bufferPool{
storage: sync.Pool{},
}
pool.storage.New = func() interface{} {
buf := &buffer{
pool: pool,
}
buf.Grow(maxSize)
return buf
}
return pool
}
func (pool *bufferPool) AcquireBuffer() *buffer {
buf := pool.storage.Get().(*buffer)
if buf.isBusy {
panic(`should not happened`)
}
buf.isBusy = true
atomic.AddInt32(&buf.refCount, 1)
return buf
}
func (buf *buffer) incRefCount() bool {
if atomic.AddInt32(&buf.refCount, 1) == 1 {
// this item was already fully released, we cannot reuse it :(
atomic.AddInt32(&buf.refCount, -1)
return false
}
return true
}
func (buf *buffer) Release() {
refCount := atomic.AddInt32(&buf.refCount, -1)
if refCount > 0 {
return
}
if refCount < 0 || !buf.isBusy {
panic(fmt.Sprintf(`should not happened (refCount == %v; isBusy == %v)`,
refCount, buf.isBusy))
}
buf.Reset()
buf.isBusy = false
buf.isMonopolized = 0
buf.lockID = 0
buf.pool.storage.Put(buf)
}