-
Notifications
You must be signed in to change notification settings - Fork 350
/
gather_write_buffer.go
176 lines (133 loc) · 3.57 KB
/
gather_write_buffer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gather
import (
"io"
"sync"
"github.com/kopia/kopia/repo/logging"
)
var log = logging.Module("gather") // +checklocksignore
// WriteBuffer is a write buffer for content of unknown size that manages
// data in a series of byte slices of uniform size.
type WriteBuffer struct {
alloc *chunkAllocator
mu sync.Mutex
inner Bytes
}
// Close releases all memory allocated by this buffer.
func (b *WriteBuffer) Close() {
b.mu.Lock()
defer b.mu.Unlock()
if b.alloc != nil {
for _, s := range b.inner.Slices {
b.alloc.releaseChunk(s)
}
b.alloc = nil
}
b.inner.invalidate()
}
// MakeContiguous ensures the write buffer consists of exactly one contiguous single slice of the provided length
// and returns the slice.
func (b *WriteBuffer) MakeContiguous(length int) []byte {
b.Reset()
b.mu.Lock()
defer b.mu.Unlock()
var v []byte
switch {
case length <= typicalContiguousAllocator.chunkSize:
// most commonly used allocator for default chunk size with max 8MB
b.alloc = typicalContiguousAllocator
v = b.allocChunk()[0:length]
case length <= maxContiguousAllocator.chunkSize:
b.alloc = maxContiguousAllocator
v = b.allocChunk()[0:length]
default:
v = make([]byte, length)
}
b.inner.Slices = [][]byte{v}
return v
}
// Reset resets buffer back to empty.
func (b *WriteBuffer) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
if b.alloc != nil {
for _, s := range b.inner.Slices {
b.alloc.releaseChunk(s)
}
}
b.inner.invalidate()
b.alloc = nil
b.inner = Bytes{}
}
// Write implements io.Writer for appending to the buffer.
func (b *WriteBuffer) Write(data []byte) (n int, err error) {
b.Append(data)
return len(data), nil
}
// AppendSectionTo appends the section of the buffer to the provided slice and returns it.
func (b *WriteBuffer) AppendSectionTo(w io.Writer, offset, size int) error {
b.mu.Lock()
defer b.mu.Unlock()
return b.inner.AppendSectionTo(w, offset, size)
}
// Length returns the combined length of all slices.
func (b *WriteBuffer) Length() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.inner.Length()
}
// ToByteSlice appends all bytes to the provided slice and returns it.
func (b *WriteBuffer) ToByteSlice() []byte {
b.mu.Lock()
defer b.mu.Unlock()
return b.inner.ToByteSlice()
}
// Bytes returns inner gather.Bytes.
func (b *WriteBuffer) Bytes() Bytes {
b.mu.Lock()
defer b.mu.Unlock()
return b.inner
}
// Append appends the specified slice of bytes to the buffer.
func (b *WriteBuffer) Append(data []byte) {
b.mu.Lock()
defer b.mu.Unlock()
b.inner.assertValid()
if len(b.inner.Slices) == 0 {
b.inner.sliceBuf[0] = b.allocChunk()
b.inner.Slices = b.inner.sliceBuf[0:1]
}
for len(data) > 0 {
ndx := len(b.inner.Slices) - 1
remaining := cap(b.inner.Slices[ndx]) - len(b.inner.Slices[ndx])
if remaining == 0 {
b.inner.Slices = append(b.inner.Slices, b.allocChunk())
ndx = len(b.inner.Slices) - 1
remaining = cap(b.inner.Slices[ndx]) - len(b.inner.Slices[ndx])
}
chunkSize := remaining
if chunkSize > len(data) {
chunkSize = len(data)
}
b.inner.Slices[ndx] = append(b.inner.Slices[ndx], data[0:chunkSize]...)
data = data[chunkSize:]
}
}
func (b *WriteBuffer) allocChunk() []byte {
if b.alloc == nil {
b.alloc = defaultAllocator
}
return b.alloc.allocChunk()
}
// Dup creates a clone of the WriteBuffer.
func (b *WriteBuffer) Dup() *WriteBuffer {
dup := &WriteBuffer{}
b.mu.Lock()
defer b.mu.Unlock()
dup.alloc = b.alloc
dup.inner = FromSlice(b.inner.ToByteSlice())
return dup
}
// NewWriteBuffer creates new write buffer.
func NewWriteBuffer() *WriteBuffer {
return &WriteBuffer{}
}