@@ -209,9 +209,41 @@ internal final class __DataStorage : @unchecked Sendable {
209
209
return _bytes? . advanced ( by: - _offset)
210
210
}
211
211
212
+ @inlinable
213
+ static var copyWillRetainMask : Int {
214
+ #if _pointerBitWidth(_64)
215
+ return Int ( bitPattern: 0x8000000000000000 )
216
+ #elseif _pointerBitWidth(_32)
217
+ return Int ( bitPattern: 0x80000000 )
218
+ #endif
219
+ }
220
+
221
+ @inlinable
222
+ static var capacityMask : Int {
223
+ #if _pointerBitWidth(_64)
224
+ return Int ( bitPattern: 0x7FFFFFFFFFFFFFFF )
225
+ #elseif _pointerBitWidth(_32)
226
+ return Int ( bitPattern: 0x7FFFFFFF )
227
+ #endif
228
+ }
229
+
212
230
@inlinable // This is @inlinable as trivially computable.
213
231
var capacity : Int {
214
- return _capacity
232
+ return _capacity & __DataStorage. capacityMask
233
+ }
234
+
235
+ @inlinable
236
+ var _copyWillRetain : Bool {
237
+ get {
238
+ return _capacity & __DataStorage. copyWillRetainMask == 0
239
+ }
240
+ set {
241
+ if !newValue {
242
+ _capacity |= __DataStorage. copyWillRetainMask
243
+ } else {
244
+ _capacity &= __DataStorage. capacityMask
245
+ }
246
+ }
215
247
}
216
248
217
249
@inlinable // This is @inlinable as trivially computable.
@@ -355,7 +387,7 @@ internal final class __DataStorage : @unchecked Sendable {
355
387
func setLength( _ length: Int ) {
356
388
let origLength = _length
357
389
let newLength = length
358
- if _capacity < newLength || _bytes == nil {
390
+ if capacity < newLength || _bytes == nil {
359
391
ensureUniqueBufferReference ( growingTo: newLength, clear: true )
360
392
} else if origLength < newLength && _needToZero {
361
393
_ = memset ( _bytes! + origLength, 0 , newLength - origLength)
@@ -370,7 +402,7 @@ internal final class __DataStorage : @unchecked Sendable {
370
402
precondition ( length >= 0 , " Length of appending bytes must not be negative " )
371
403
let origLength = _length
372
404
let newLength = origLength + length
373
- if _capacity < newLength || _bytes == nil {
405
+ if capacity < newLength || _bytes == nil {
374
406
ensureUniqueBufferReference ( growingTo: newLength, clear: false )
375
407
}
376
408
_length = newLength
@@ -437,7 +469,7 @@ internal final class __DataStorage : @unchecked Sendable {
437
469
let range = range_. lowerBound - _offset ..< range_. upperBound - _offset
438
470
if range. upperBound - range. lowerBound == 0 { return }
439
471
if _length < range. upperBound {
440
- if _capacity <= range. upperBound {
472
+ if capacity <= range. upperBound {
441
473
ensureUniqueBufferReference ( growingTo: range. upperBound, clear: false )
442
474
}
443
475
_length = range. upperBound
@@ -1962,7 +1994,17 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
1962
1994
deallocator. _deallocator ( bytes, count)
1963
1995
_representation = . empty
1964
1996
} else {
1965
- _representation = _Representation ( __DataStorage ( bytes: bytes, length: count, copy: false , deallocator: whichDeallocator, offset: 0 ) , count: count)
1997
+ let storage = __DataStorage ( bytes: bytes, length: count, copy: false , deallocator: whichDeallocator, offset: 0 )
1998
+ switch deallocator {
1999
+ // technically .custom can potential cause this too but there is a potential chance this is expected behavior
2000
+ // commented out for now... revisit later
2001
+ // case .custom: fallthrough
2002
+ case . none:
2003
+ storage. _copyWillRetain = false
2004
+ default :
2005
+ break
2006
+ }
2007
+ _representation = _Representation ( storage, count: count)
1966
2008
}
1967
2009
}
1968
2010
0 commit comments