@@ -181,12 +181,14 @@ fileprivate func _allocate<T: AnyObject>(
181
181
) -> ( T , realNumTailBytes: Int ) {
182
182
_internalInvariant ( getSwiftClassInstanceExtents ( T . self) . 1 == numHeaderBytes)
183
183
184
+ func roundUp( _ x: Int ) -> Int { ( x + 15 ) & ~ 15 }
185
+
184
186
let numBytes = numHeaderBytes + numTailBytes
185
187
186
188
let linearBucketThreshold = 128
187
189
if _fastPath ( numBytes < linearBucketThreshold) {
188
190
// Allocate up to the nearest bucket of 16
189
- let realNumBytes = ( numBytes+ 15 ) & ~ 15
191
+ let realNumBytes = roundUp ( numBytes)
190
192
let realNumTailBytes = realNumBytes - numHeaderBytes
191
193
_internalInvariant ( realNumTailBytes >= numTailBytes)
192
194
let object = tailAllocator ( realNumTailBytes)
@@ -200,9 +202,14 @@ fileprivate func _allocate<T: AnyObject>(
200
202
growTailBytes = numTailBytes
201
203
}
202
204
203
- let object = tailAllocator ( growTailBytes)
205
+ let total = roundUp ( numHeaderBytes + growTailBytes)
206
+ let totalTailBytes = total - numHeaderBytes
207
+
208
+ let object = tailAllocator ( totalTailBytes)
204
209
let mallocSize = _swift_stdlib_malloc_size (
205
210
UnsafeRawPointer ( Builtin . bridgeToRawPointer ( object) ) )
211
+ _internalInvariant ( mallocSize % MemoryLayout< Int> . stride == 0 )
212
+
206
213
let realNumTailBytes = mallocSize - numHeaderBytes
207
214
_internalInvariant ( realNumTailBytes >= numTailBytes)
208
215
return ( object, realNumTailBytes)
@@ -447,6 +454,8 @@ extension __StringStorage {
447
454
// required nul-terminator.
448
455
//
449
456
// NOTE: Callers who wish to mutate this storage should enfore nul-termination
457
+ //
458
+ // TODO: Refactoring or removing. Excluding the last byte is awkward.
450
459
@inline ( __always)
451
460
private var unusedStorage : UnsafeMutableBufferPointer < UInt8 > {
452
461
UnsafeMutableBufferPointer (
@@ -485,8 +494,8 @@ extension __StringStorage {
485
494
486
495
// Check that capacity end matches our notion of unused storage, and also
487
496
// checks that breadcrumbs were dutifully aligned.
488
- // _internalInvariant(UnsafeMutablePointer<UInt8>(_realCapacityEnd)
489
- // == unusedStorage.baseAddress! + (unusedStorage.count + 1))
497
+ _internalInvariant ( UnsafeMutablePointer < UInt8 > ( _realCapacityEnd)
498
+ == unusedStorage. baseAddress! + ( unusedStorage. count + 1 ) )
490
499
}
491
500
#endif // INTERNAL_CHECKS_ENABLED
492
501
}
0 commit comments