Artifact bb48a7e60492f5538ce69acfa1b8e615b03feab63bbb57427e8fecfb37b4589a:
- File
src/OFAutoreleasePool.m
— part of check-in
[e6f6113b5c]
at
2009-06-02 20:15:34
on branch trunk
— No need to override -[release] in OFAutoreleasePool.
As we can't retain a pool, -[dealloc] will always be called when -[release]
is called. (user: js, size: 3041) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008 - 2009 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #include "config.h" #include <stdlib.h> #import "OFAutoreleasePool.h" #import "OFList.h" #import "OFThread.h" #import "OFExceptions.h" static OFTLSKey *pool_list_key; static void release_list(void *list) { of_list_object_t *first, *iter; IMP release; if ((first = [(OFList*)list first]) != NULL) release = [first->object methodFor: @selector(release)]; for (iter = first; iter != NULL; iter = iter->next) release(iter->object, @selector(release)); [(OFList*)list release]; } @implementation OFAutoreleasePool + (void)initialize { if (self != [OFAutoreleasePool class]) return; pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list]; } + (void)addObjectToTopmostPool: (OFObject*)obj { OFList *pool_list = [OFThread objectForTLSKey: pool_list_key]; if (pool_list == nil || [pool_list last] == NULL) { @try { [[self alloc] init]; pool_list = [OFThread objectForTLSKey: pool_list_key]; } @catch (OFException *e) { [obj release]; @throw e; } } if (pool_list == nil || [pool_list last] == NULL) { [obj release]; @throw [OFInitializationFailedException newWithClass: self]; } @try { [[pool_list last]->object addObject: obj]; } @catch (OFException *e) { [obj release]; @throw e; } } - init { OFList *pool_list; self = [super init]; if ((pool_list = [OFThread objectForTLSKey: pool_list_key]) == nil) { @try { pool_list = [[OFList alloc] initWithoutRetainAndRelease]; } @catch (OFException *e) { [self dealloc]; @throw e; } @try { [OFThread setObject: pool_list forTLSKey: pool_list_key]; } @catch (OFException *e) { [self dealloc]; @throw e; } @finally { [pool_list release]; } } @try { listobj = [pool_list append: self]; } @catch (OFException *e) { [self dealloc]; @throw e; } return self; } - (void)dealloc { /* * FIXME: * Maybe we should get the objects ourself, release them and then * release the pool without calling its release method? This way, * there wouldn't be a recursion. */ if (listobj->next != NULL) [listobj->next->object release]; [self releaseObjects]; [[OFThread objectForTLSKey: pool_list_key] remove: listobj]; [super dealloc]; } - addObject: (OFObject*)obj { if (objects == nil) objects = [[OFMutableArray alloc] init]; [objects addObject: obj]; [obj release]; return self; } - releaseObjects { if (listobj->next != NULL) [listobj->next->object releaseObjects]; if (objects == nil) return self; [objects release]; objects = nil; return self; } - retain { @throw [OFNotImplementedException newWithClass: isa andSelector: _cmd]; } - autorelease { @throw [OFNotImplementedException newWithClass: isa andSelector: _cmd]; } @end