-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_slice.go
More file actions
224 lines (202 loc) · 5.86 KB
/
Copy pathraw_slice.go
File metadata and controls
224 lines (202 loc) · 5.86 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package bytebufferpool
import "unsafe"
const releasedDiagnosticByte = 0xa5
type rawRecord struct {
capacity int
active bool
previousTombstone uintptr
nextTombstone uintptr
}
// AcquireSlice returns a low-level Raw Slice and panics when size is invalid.
// Unlike Lease, a Raw Slice carries no Generation and cannot prevent stale
// aliases from releasing a backing array that has since been reacquired.
func (p *Pool) AcquireSlice(size int) []byte {
buffer, err := p.TryAcquireSlice(size)
if err != nil {
panic(err)
}
return buffer
}
// TryAcquireSlice returns a low-level Raw Slice of size bytes.
func (p *Pool) TryAcquireSlice(size int) ([]byte, error) {
if err := p.validateSize(size); err != nil {
return nil, err
}
if size == 0 {
p.recordAcquire(-1, false)
return nil, nil
}
storage, _ := p.acquireStorage(size)
buffer := storage.buf
storage.buf = nil
storage.class = -1
storage.active.Store(false)
p.rawWrappers.Put(storage)
if p.config.ValidationEnabled {
p.registerRaw(buffer)
}
return buffer, nil
}
// ReleaseSlice releases a Raw Slice to the current Pool Generation.
// Enhanced validation detects observable ownership mistakes, but cannot
// eliminate the ABA ambiguity created by mutable slice aliases.
// An inactive record evicted by the FIFO limit or discarded by Clear is
// reported as RejectedForeign instead of RejectedDuplicate, without mutation.
func (p *Pool) ReleaseSlice(buffer []byte) ReleaseStatus {
if buffer == nil {
return p.recordRelease(IgnoredNil, -1)
}
releaseCapacity := cap(buffer)
if p.config.ValidationEnabled {
status, proceed, owned, originalCapacity := p.validateRawRelease(buffer)
releaseCapacity = originalCapacity
if !proceed {
if owned {
p.prepareRawRelease(buffer, releaseCapacity)
}
return p.recordRelease(status, -1)
}
}
p.prepareRawRelease(buffer, releaseCapacity)
capacity := cap(buffer)
if capacity > p.config.MaxPooledCapacity {
return p.recordRelease(DroppedOversize, -1)
}
class := p.classForCapacity(capacity)
if class < 0 {
if capacity == 0 {
return p.recordRelease(DroppedInvalid, -1)
}
return p.recordRelease(DroppedUnpooled, -1)
}
storage := p.rawWrapper()
storage.buf = buffer
storage.class = class
status := p.release(storage, p.current.Load().id)
if status != Retained {
storage.buf = nil
storage.class = -1
p.rawWrappers.Put(storage)
}
return status
}
func (p *Pool) rawWrapper() *backingStorage {
if value := p.rawWrappers.Get(); value != nil {
return value.(*backingStorage)
}
return &backingStorage{}
}
func (p *Pool) registerRaw(buffer []byte) {
key := rawKey(buffer)
p.validationMu.Lock()
record, exists := p.rawRecords[key]
if record.active {
p.validationMu.Unlock()
panic("bytebufferpool: backing storage handed to two live Raw Slice owners")
}
if exists {
p.removeValidationTombstoneLocked(record)
}
p.rawRecords[key] = rawRecord{capacity: cap(buffer), active: true}
p.activeRawSlices++
p.validationMu.Unlock()
}
func (p *Pool) validateRawRelease(buffer []byte) (status ReleaseStatus, proceed, owned bool, originalCapacity int) {
key := rawKey(buffer)
p.validationMu.Lock()
record, ok := p.rawRecords[key]
if !ok {
p.validationMu.Unlock()
return RejectedForeign, false, false, cap(buffer)
}
if !record.active {
p.validationMu.Unlock()
return RejectedDuplicate, false, false, record.capacity
}
p.activeRawSlices--
p.addValidationTombstoneLocked(key, record)
p.validationMu.Unlock()
if cap(buffer) != record.capacity {
return DroppedInvalid, false, true, record.capacity
}
return Retained, true, true, record.capacity
}
func (p *Pool) addValidationTombstoneLocked(key uintptr, record rawRecord) {
record.active = false
record.previousTombstone = p.validationTombstoneTail
record.nextTombstone = 0
if p.validationTombstoneTail == 0 {
p.validationTombstoneHead = key
} else {
tail := p.rawRecords[p.validationTombstoneTail]
tail.nextTombstone = key
p.rawRecords[p.validationTombstoneTail] = tail
}
p.validationTombstoneTail = key
p.rawRecords[key] = record
p.validationTombstones++
for p.validationTombstones > int64(p.config.MaxValidationTombstones) {
p.evictOldestValidationTombstoneLocked()
}
}
func (p *Pool) removeValidationTombstoneLocked(record rawRecord) {
if record.previousTombstone == 0 {
p.validationTombstoneHead = record.nextTombstone
} else {
previous := p.rawRecords[record.previousTombstone]
previous.nextTombstone = record.nextTombstone
p.rawRecords[record.previousTombstone] = previous
}
if record.nextTombstone == 0 {
p.validationTombstoneTail = record.previousTombstone
} else {
next := p.rawRecords[record.nextTombstone]
next.previousTombstone = record.previousTombstone
p.rawRecords[record.nextTombstone] = next
}
p.validationTombstones--
}
func (p *Pool) evictOldestValidationTombstoneLocked() {
key := p.validationTombstoneHead
record := p.rawRecords[key]
p.removeValidationTombstoneLocked(record)
delete(p.rawRecords, key)
}
func (p *Pool) clearValidationTombstones() {
if !p.config.ValidationEnabled {
return
}
p.validationMu.Lock()
activeRecords := make(map[uintptr]rawRecord)
for key, record := range p.rawRecords {
if record.active {
record.previousTombstone = 0
record.nextTombstone = 0
activeRecords[key] = record
}
}
p.rawRecords = activeRecords
p.validationTombstoneHead = 0
p.validationTombstoneTail = 0
p.validationTombstones = 0
p.validationMu.Unlock()
}
func rawKey(buffer []byte) uintptr {
return uintptr(unsafe.Pointer(unsafe.SliceData(buffer)))
}
func (p *Pool) prepareRawRelease(buffer []byte, originalCapacity int) {
full := buffer[:cap(buffer)]
if originalCapacity > cap(buffer) {
full = unsafe.Slice(unsafe.SliceData(buffer), originalCapacity)
}
if p.config.ZeroOnRelease {
clear(full)
p.recordZeroed(len(full))
return
}
if p.config.ValidationEnabled {
for i := range full {
full[i] = releasedDiagnosticByte
}
}
}