1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#import <Foundation/NSAutoreleasePool.h> #import <objc/objc-arc.h> @interface NSAutoreleasePool () { void* _opaqueAutoreleasePool; } @end @implementation NSAutoreleasePool /* * _ARCCompatibleAutoreleasePool signals to the runtime that, if necessary, * this autorelease pool class may be entirely avoided. This allows for fast-path * object management. * * The original methods remain as mere shims for the runtime's management functions. */ - (void)_ARCCompatibleAutoreleasePool { } /** @Status Interoperable */ + (void)addObject:(id)object { objc_autorelease(object); } /** @Status Interoperable */ - (id)init { if (self = [super init]) { _opaqueAutoreleasePool = objc_autoreleasePoolPush(); } return self; } /** @Status Interoperable */ - (void)dealloc { objc_autoreleasePoolPop(_opaqueAutoreleasePool); [super dealloc]; } /** @Status Interoperable @Notes On non-GC platforms like WinObjC, -drain is identical to -release. */ - (void)drain { [self release]; } @end |
while中autoreleasepool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
– (id)findMatchingObject:(id)anObject { id match; while (match == nil) { @autoreleasepool { /* Do a search that creates a lot of temporary objects. */ match = [self expensiveSearchForObject:anObject]; if (match != nil) { [match retain]; /* Keep match around. */ } } } return [match autorelease]; /* Let match go and return it. */ } |