ObjFW  Artifact [9b9e08d0f0]

Artifact 9b9e08d0f01c7b742e4bc46d98ee5fdc8859764ecc4e5ad5d72c583e0964e5e1:

  • File src/OFAutoreleasePool.m — part of check-in [f5927f8a84] at 2012-07-14 20:00:11 on branch trunk — New autorelease pools.

    This uses the runtime's autorelease pools and implements autorelease
    pools in the ObjFW runtime. It therefore uses ObjFW's autorelease pools
    when using the ObjFW runtime and Apple's autorelease pools when using
    the Apple runtime.

    These new pools should be ARC-compatible now and finally, it should be
    possible to mix OFAutoreleasePools and NSAutoreleasePools again, even
    @autoreleasepool is allowed in the mix now. This also means the old
    bridge to NSAutoreleasePool should not be required anymore, as both use
    the runtime's autorelease pools now.

    As a bonus, it's significantly faster to use the ObjFW runtime with
    ObjFW's autorelease pools than to use Apple's runtime with Apple's
    autorelease pools, as a quick benchmark using OFXMLParser on large files
    showed. (Note: This is not only due to the different autorelease pools,
    but also due to the fact that even with the same autorelease pools it is
    faster using the ObjFW runtime, as can be seen in versions prior to this
    commit.) (user: js, size: 1924) [annotate] [blame] [check-ins using]


/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>

#import "OFAutoreleasePool.h"
#import "OFArray.h"

#import "OFNotImplementedException.h"

extern id _objc_rootAutorelease(id);
extern void* objc_autoreleasePoolPush(void);
extern void objc_autoreleasePoolPop(void*);

static __thread void *first = NULL;

@implementation OFAutoreleasePool
+ (id)addObject: (id)object
{
	if (first == NULL)
		[[OFAutoreleasePool alloc] init];

	return _objc_rootAutorelease(object);
}

+ (void)_releaseAll
{
	objc_autoreleasePoolPop(first);
}

- init
{
	self = [super init];

	@try {
		pool = objc_autoreleasePoolPush();

		if (first == NULL)
			first = pool;

		_objc_rootAutorelease(self);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)releaseObjects
{
	ignoreRelease = YES;

	objc_autoreleasePoolPop(pool);
	pool = objc_autoreleasePoolPush();

	_objc_rootAutorelease(self);

	ignoreRelease = NO;
}

- (void)release
{
	[self dealloc];
}

- (void)drain
{
	[self dealloc];
}

- (void)dealloc
{
	if (ignoreRelease)
		return;

	ignoreRelease = YES;

	if (first == pool)
		first = NULL;

	objc_autoreleasePoolPop(pool);

	[super dealloc];
}

- retain
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- autorelease
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}
@end