Skip to content

Commit 3b78f38

Browse files
authored
Merge pull request #38438 from glessard/umbp-allocate-deduplication
[stdlib] remove duplicated code
2 parents 8fd1cd9 + e946cd0 commit 3b78f38

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -599,26 +599,8 @@ extension Unsafe${Mutable}BufferPointer {
599599
@inlinable // unsafe-performance
600600
public static func allocate(capacity count: Int)
601601
-> UnsafeMutableBufferPointer<Element> {
602-
let size = MemoryLayout<Element>.stride * count
603-
// For any alignment <= _minAllocationAlignment, force alignment = 0.
604-
// This forces the runtime's "aligned" allocation path so that
605-
// deallocation does not require the original alignment.
606-
//
607-
// The runtime guarantees:
608-
//
609-
// align == 0 || align > _minAllocationAlignment:
610-
// Runtime uses "aligned allocation".
611-
//
612-
// 0 < align <= _minAllocationAlignment:
613-
// Runtime may use either malloc or "aligned allocation".
614-
var align = Builtin.alignof(Element.self)
615-
if Int(align) <= _minAllocationAlignment() {
616-
align = (0)._builtinWordValue
617-
}
618-
let raw = Builtin.allocRaw(size._builtinWordValue, align)
619-
Builtin.bindMemory(raw, count._builtinWordValue, Element.self)
620-
return UnsafeMutableBufferPointer(
621-
start: UnsafeMutablePointer(raw), count: count)
602+
let base = UnsafeMutablePointer<Element>.allocate(capacity: count)
603+
return UnsafeMutableBufferPointer(start: base, count: count)
622604
}
623605

624606
/// Initializes every element in this buffer's memory to a copy of the given value.

0 commit comments

Comments
 (0)