Last active
November 2, 2016 19:52
-
-
Save phausler/4281c94a462eb35a92ddb3d8ea6e5cbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class NSAutoreleasePool { | |
fileprivate static var _current = NSThreadSpecific<NSAutoreleasePool>() | |
internal static var current: NSAutoreleasePool { | |
return _current.get() { | |
return NSAutoreleasePool() | |
} | |
} | |
var depth: Int = 0 | |
var objects = [[AnyObject]]() | |
fileprivate override init() { } | |
fileprivate func push() { | |
objects.append([AnyObject]()) | |
depth += 1 | |
} | |
fileprivate func pop() { | |
objects.removeLast() | |
depth -= 1 | |
} | |
func add(_ object: AnyObject) { | |
objects[depth - 1].append(object) | |
} | |
} | |
public func autoreleasepool(_ code: () -> ()) { | |
NSAutoreleasePool.current.push() | |
code() | |
NSAutoreleasePool.current.pop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment