Swift autoreleasepool
2021-08-18 本文已影响0人
Ian_
Swift 对 runtime 的 autoreleasepool 的实现
public func autoreleasepool<Result>(
invoking body: () throws -> Result
) rethrows -> Result {
let pool = _swift_objc_autoreleasePoolPush()
defer {
_swift_objc_autoreleasePoolPop(pool)
}
return try body()
}
关于
/// Executes the `body` in an autorelease pool if the platform does not
/// implement the return-autoreleased optimization.
///
/// (Currently, only the i386 iOS and watchOS simulators don't implement the
/// return-autoreleased optimization.)
@inline(never)
public func autoreleasepoolIfUnoptimizedReturnAutoreleased(
invoking body: () -> Void
) {
#if targetEnvironment(simulator) && arch(i386) && (os(iOS) || os(watchOS))
autoreleasepool(invoking: body)
#else
body()
#endif
}