Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added protocol to support CVarArg objects that need to be retained
  • Loading branch information
Molanda committed Jun 11, 2020
1 parent 122deaa commit 3425a6d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions stdlib/public/core/VarArgs.swift
Expand Up @@ -63,6 +63,15 @@ protocol _CVarArgAligned: CVarArg {
var _cVarArgAlignment: Int { get }
}

/// 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 available on all platforms.
public protocol _CVarArgObject: CVarArg {
/// Returns the alternate object that should be encoded.
var _cVarArgObject: CVarArg { get }
}

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

@usableFromInline // c-abi
internal var retainer = [CVarArg]()

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

@inlinable // c-abi
internal func append(_ arg: CVarArg) {
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)
}

var encoded = arg._cVarArgEncoding

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

@inlinable // c-abi
internal func append(_ arg: CVarArg) {
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)
}

// 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 +693,9 @@ final internal class __VaListBuilder {
@usableFromInline // c-abi
internal var storage: UnsafeMutablePointer<Int>?

@usableFromInline // c-abi
internal var retainer = [CVarArg]()

internal static var alignedStorageForEmptyVaLists: Double = 0
}

Expand Down

0 comments on commit 3425a6d

Please sign in to comment.