1994-11-08 17:11:49 +00:00
|
|
|
/* Implementation of release pools for delayed disposal
|
1996-02-22 15:12:41 +00:00
|
|
|
Copyright (C) 1994, 1996 Free Software Foundation, Inc.
|
1994-11-08 17:11:49 +00:00
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1994-11-08 17:11:49 +00:00
|
|
|
Date: May 1993
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1994-11-08 17:11:49 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1996-04-17 15:34:35 +00:00
|
|
|
#include <gnustep/base/preface.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/AutoreleasePool.h>
|
|
|
|
#include <gnustep/base/ObjectRetaining.h>
|
1994-11-08 17:11:49 +00:00
|
|
|
|
|
|
|
/* Doesn't handle multi-threaded stuff.
|
|
|
|
Doesn't handle exceptions. */
|
|
|
|
|
1994-11-08 18:10:31 +00:00
|
|
|
/* Put the stuff from initialize into a runtime init function.
|
1994-11-08 17:11:49 +00:00
|
|
|
This class should be made more efficient, especially:
|
|
|
|
[[Autorelease alloc] init]
|
|
|
|
[current_pool addObject:o] (REALLOC-case)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static AutoreleasePool *current_pool = nil;
|
|
|
|
|
|
|
|
#define DEFAULT_SIZE 64
|
|
|
|
|
|
|
|
@implementation AutoreleasePool
|
|
|
|
|
|
|
|
+ initialize
|
|
|
|
{
|
|
|
|
if (self == [AutoreleasePool class])
|
1994-11-08 20:09:28 +00:00
|
|
|
autorelease_class = self;
|
1994-11-08 17:11:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ currentPool
|
|
|
|
{
|
|
|
|
return current_pool;
|
|
|
|
}
|
|
|
|
|
1994-11-08 20:09:28 +00:00
|
|
|
+ (void) autoreleaseObject: anObj
|
1994-11-08 17:11:49 +00:00
|
|
|
{
|
1994-11-08 20:09:28 +00:00
|
|
|
[current_pool autoreleaseObject:anObj];
|
1994-11-08 17:11:49 +00:00
|
|
|
}
|
|
|
|
|
1994-11-08 20:09:28 +00:00
|
|
|
- (void) autoreleaseObject: anObj
|
1994-11-08 17:11:49 +00:00
|
|
|
{
|
|
|
|
released_count++;
|
|
|
|
if (released_count == released_size)
|
|
|
|
{
|
|
|
|
released_size *= 2;
|
|
|
|
OBJC_REALLOC(released, id, released_size);
|
|
|
|
}
|
|
|
|
released[released_count] = anObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
- init
|
|
|
|
{
|
|
|
|
parent = current_pool;
|
|
|
|
current_pool = self;
|
|
|
|
OBJC_MALLOC(released, id, DEFAULT_SIZE);
|
|
|
|
released_size = DEFAULT_SIZE;
|
|
|
|
released_count = 0;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) retain
|
|
|
|
{
|
|
|
|
[self error:"Don't call `-retain' on a AutoreleasePool"];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (oneway void) release
|
|
|
|
{
|
|
|
|
[self dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (parent)
|
|
|
|
current_pool = parent;
|
|
|
|
else
|
|
|
|
current_pool = [[AutoreleasePool alloc] init];
|
|
|
|
for (i = 0; i < released_count; i++)
|
|
|
|
[released[i] release];
|
|
|
|
OBJC_FREE(released);
|
|
|
|
object_dispose(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
- autorelease
|
|
|
|
{
|
1994-11-08 18:10:31 +00:00
|
|
|
[self error:"Don't call `-autorelease' on a AutoreleasePool"];
|
1994-11-08 17:11:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|