You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-6477 UnsafeBufferPointer should have a stack allocating version (or at least a conditional stack allocation)
Issue Description:
LLVM promotes malloc/free pairs to stack allocations if it can see that the free happens on all paths. The same should happen for UnsafeMutable*Pointer.allocate, as in this code:
public protocol SizedInBytes {
associatedtype ByteStorage
}
@propertyWrapper
public struct Unaligned<Value: SizedInBytes> {
private var storage: Value.ByteStorage
public init(wrappedValue: Value) {
precondition(MemoryLayout<Value.ByteStorage>.size == MemoryLayout<Value>.size, "byte storage has a different size")
precondition(MemoryLayout<Value.ByteStorage>.alignment == 1, "byte storage must have byte alignment")
self.storage = withUnsafeBytes(of: wrappedValue) {
$0.load(as: Value.ByteStorage.self)
}
}
public var wrappedValue: Value {
get {
// This allocation should get stack-promoted but doesn't.
let result = UnsafeMutablePointer<Value>.allocate(capacity: 1)
defer { result.deinitialize(count: 1); result.deallocate() }
result.withMemoryRebound(to: Value.ByteStorage.self, capacity: 1) {
$0.initialize(to: self.storage)
}
return result.pointee
}
set {
self = .init(wrappedValue: newValue)
}
}
}
extension Int32: SizedInBytes {
public typealias ByteStorage = (UInt8, UInt8, UInt8, UInt8)
}
public struct Test {
@Unaligned public var x: Int32 = 0
}
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: a4eed787067b0f855cb1ab8cd8082a29
relates to:
Issue Description:
LLVM promotes malloc/free pairs to stack allocations if it can see that the free happens on all paths. The same should happen for UnsafeMutable*Pointer.allocate, as in this code:
The text was updated successfully, but these errors were encountered: