Skip to content

Added protocol to support CVarArg objects that need to be retained #32311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ protocol _CVarArgAligned: CVarArg {
var _cVarArgAlignment: Int { get }
}

#if !_runtime(_ObjC)
/// Some pointers require an alternate object to be retained. The object
/// that is returned will be used with _cVarArgEncoding and held until
/// the closure is complete. This is required since autoreleased storage
/// is not available on all platforms.
public protocol _CVarArgObject: CVarArg {
/// Returns the alternate object that should be encoded.
var _cVarArgObject: CVarArg { get }
}
#endif

#if arch(x86_64)
@usableFromInline
internal let _countGPRegisters = 6
Expand Down Expand Up @@ -462,6 +473,11 @@ final internal class __VaListBuilder {
@usableFromInline // c-abi
internal var storage: ContiguousArray<Int>

#if !_runtime(_ObjC)
@usableFromInline // c-abi
internal var retainer = [CVarArg]()
#endif

@inlinable // c-abi
internal init() {
// prepare the register save area
Expand All @@ -473,6 +489,16 @@ final internal class __VaListBuilder {

@inlinable // c-abi
internal func append(_ arg: CVarArg) {
#if !_runtime(_ObjC)
var arg = arg

// We may need to retain an object that provides a pointer value.
if let obj = arg as? _CVarArgObject {
arg = obj._cVarArgObject
retainer.append(arg)
}
#endif

var encoded = arg._cVarArgEncoding

#if arch(x86_64) || arch(arm64)
Expand Down Expand Up @@ -560,6 +586,16 @@ final internal class __VaListBuilder {

@inlinable // c-abi
internal func append(_ arg: CVarArg) {
#if !_runtime(_ObjC)
var arg = arg

// We may need to retain an object that provides a pointer value.
if let obj = arg as? _CVarArgObject {
arg = obj._cVarArgObject
retainer.append(arg)
}
#endif

// Write alignment padding if necessary.
// This is needed on architectures where the ABI alignment of some
// supported vararg type is greater than the alignment of Int, such
Expand Down Expand Up @@ -665,6 +701,11 @@ final internal class __VaListBuilder {
@usableFromInline // c-abi
internal var storage: UnsafeMutablePointer<Int>?

#if !_runtime(_ObjC)
@usableFromInline // c-abi
internal var retainer = [CVarArg]()
#endif

internal static var alignedStorageForEmptyVaLists: Double = 0
}

Expand Down