Skip to content

Instantly share code, notes, and snippets.

@phausler
Last active November 2, 2016 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phausler/4281c94a462eb35a92ddb3d8ea6e5cbe to your computer and use it in GitHub Desktop.
Save phausler/4281c94a462eb35a92ddb3d8ea6e5cbe to your computer and use it in GitHub Desktop.
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