Skip to content

Commit 0843891

Browse files
authored
Switch to malloc_good_size instead of malloc_size (#70532)
Switch to malloc_good_size instead of malloc_size
1 parent afb042d commit 0843891

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

stdlib/public/SwiftShims/swift/shims/LibcShims.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ static inline __swift_bool _swift_stdlib_has_malloc_size() {
129129
return HAS_MALLOC_SIZE != 0;
130130
}
131131

132+
static inline __swift_size_t _swift_stdlib_malloc_good_size(__swift_size_t sz) {
133+
#if defined(__APPLE__)
134+
extern __swift_size_t malloc_good_size(__swift_size_t);
135+
return malloc_good_size(sz);
136+
#else
137+
return (sz + 15) & ~15; //round up to the nearest 16 byte alignment, at least
138+
#endif
139+
}
140+
132141
// Math library functions
133142
static inline SWIFT_ALWAYS_INLINE
134143
float _stdlib_remainderf(float _self, float _other) {

stdlib/public/core/Shims.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,31 @@ internal let _fastEnumerationStorageMutationsPtr =
4141
internal func _mallocSize(ofAllocation ptr: UnsafeRawPointer) -> Int? {
4242
return _swift_stdlib_has_malloc_size() ? _swift_stdlib_malloc_size(ptr) : nil
4343
}
44+
45+
/*
46+
Invariant:
47+
malloc_size(malloc(malloc_good_size(size))) >= malloc_good_size(size)
48+
49+
Usually:
50+
malloc_size(malloc(malloc_good_size(size))) == malloc_good_size(size)
51+
*/
52+
@_effects(readnone) @inline(__always)
53+
internal func _mallocGoodSize(for size: Int) -> Int {
54+
precondition(size >= 0)
55+
// Not all allocators will see benefits from rounding up to 16/32 byte aligned
56+
// but it'll never cause misbehavior, and many reasonable ones will benefit
57+
if (size <= 128) {
58+
return (size &+ 15) & ~15;
59+
}
60+
if (size <= 256) {
61+
return (size &+ 31) & ~31;
62+
}
63+
return _mallocGoodSizeLarge(for: size)
64+
}
65+
66+
@_effects(readnone)
67+
internal func _mallocGoodSizeLarge(for size: Int) -> Int {
68+
let goodSize = _swift_stdlib_malloc_good_size(size)
69+
precondition(goodSize >= size)
70+
return goodSize
71+
}

stdlib/public/core/StringStorage.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,12 @@ fileprivate func _allocate<T: AnyObject>(
181181
) -> (T, realNumTailBytes: Int) {
182182
_internalInvariant(getSwiftClassInstanceExtents(T.self).1 == numHeaderBytes)
183183

184-
func roundUp(_ x: Int) -> Int { (x + 15) & ~15 }
185-
186184
let numBytes = numHeaderBytes + numTailBytes
187185

188186
let linearBucketThreshold = 128
189187
if _fastPath(numBytes < linearBucketThreshold) {
190188
// Allocate up to the nearest bucket of 16
191-
let realNumBytes = roundUp(numBytes)
189+
let realNumBytes = _mallocGoodSize(for: numBytes)
192190
let realNumTailBytes = realNumBytes - numHeaderBytes
193191
_internalInvariant(realNumTailBytes >= numTailBytes)
194192
let object = tailAllocator(realNumTailBytes)
@@ -202,21 +200,10 @@ fileprivate func _allocate<T: AnyObject>(
202200
growTailBytes = numTailBytes
203201
}
204202

205-
let total = roundUp(numHeaderBytes + growTailBytes)
203+
let total = _mallocGoodSize(for: numHeaderBytes + growTailBytes)
206204
let totalTailBytes = total - numHeaderBytes
207205

208-
let object = tailAllocator(totalTailBytes)
209-
if let allocSize = _mallocSize(ofAllocation:
210-
UnsafeRawPointer(Builtin.bridgeToRawPointer(object))) {
211-
_internalInvariant(allocSize % MemoryLayout<Int>.stride == 0)
212-
213-
let realNumTailBytes = allocSize - numHeaderBytes
214-
_internalInvariant(realNumTailBytes >= numTailBytes)
215-
216-
return (object, realNumTailBytes)
217-
} else {
218-
return (object, totalTailBytes)
219-
}
206+
return (tailAllocator(totalTailBytes), totalTailBytes)
220207
}
221208

222209
fileprivate func _allocateStringStorage(

0 commit comments

Comments
 (0)