1995-03-12 19:35:17 +00:00
|
|
|
|
/* Implementation of auto release pool for delayed disposal
|
1996-03-18 13:54:29 +00:00
|
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
1995-03-12 19:35:17 +00:00
|
|
|
|
|
|
|
|
|
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
|
Date: January 1995
|
|
|
|
|
|
|
|
|
|
This file is part of the GNU Objective C Class Library.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <objects/stdobjects.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1995-03-12 19:35:17 +00:00
|
|
|
|
#include <objects/objc-malloc.h>
|
1996-03-30 22:39:48 +00:00
|
|
|
|
#include <objects/Array.h>
|
1996-03-26 20:59:42 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
1995-04-14 15:01:24 +00:00
|
|
|
|
#include <limits.h>
|
1995-03-12 19:35:17 +00:00
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* TODO:
|
|
|
|
|
Doesn't work multi-threaded.
|
1995-03-12 19:35:17 +00:00
|
|
|
|
|
|
|
|
|
This class should be made more efficient, especially:
|
1996-03-30 22:39:48 +00:00
|
|
|
|
[[NSAutorelease alloc] init]
|
|
|
|
|
[current_pool addObject:o] (REALLOC-case)
|
1995-03-12 19:35:17 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* The current, default NSAutoreleasePool; the one that will hold
|
|
|
|
|
objects that are arguments to [NSAutoreleasePool +addObject:]. */
|
1995-03-12 19:35:17 +00:00
|
|
|
|
static NSAutoreleasePool *current_pool = nil;
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* When this is `NO', autoreleased objects are never actually recorded
|
|
|
|
|
in an NSAutoreleasePool, and are not sent a `release' message.
|
|
|
|
|
Thus memory for objects use grows, and grows, and... */
|
1995-04-14 15:01:24 +00:00
|
|
|
|
static BOOL autorelease_enabled = YES;
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* When the _released_count of the current pool gets over this value,
|
|
|
|
|
we raise an exception. */
|
1995-04-14 15:01:24 +00:00
|
|
|
|
static unsigned pool_count_warning_threshhold = UINT_MAX;
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* The total number of objects autoreleased since the program was
|
|
|
|
|
started, or since -resetTotalAutoreleasedObjects was called. */
|
|
|
|
|
static unsigned total_autoreleased_objects_count = 0;
|
1995-03-12 19:35:17 +00:00
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* The size of the first _released array. */
|
|
|
|
|
#define BEGINNING_POOL_SIZE 32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface NSAutoreleasePool (Private)
|
|
|
|
|
- _parentAutoreleasePool;
|
|
|
|
|
- (unsigned) autoreleaseCount;
|
|
|
|
|
- (unsigned) autoreleaseCountForObject: anObject;
|
|
|
|
|
+ (unsigned) autoreleaseCountForObject: anObject;
|
|
|
|
|
+ currentPool;
|
|
|
|
|
- (void) _setChildPool: pool;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/* A cache of NSAutoreleasePool's already alloc'ed. Caching old pools
|
|
|
|
|
instead of deallocating and re-allocating them will save time. */
|
|
|
|
|
static id *autorelease_pool_cache;
|
|
|
|
|
static int autorelease_pool_cache_size = 32;
|
|
|
|
|
static int autorelease_pool_cache_count = 0;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
push_pool_to_cache (id p)
|
|
|
|
|
{
|
|
|
|
|
if (autorelease_pool_cache_count == autorelease_pool_cache_size)
|
|
|
|
|
{
|
|
|
|
|
autorelease_pool_cache_size *= 2;
|
|
|
|
|
OBJC_REALLOC (autorelease_pool_cache, id, autorelease_pool_cache_size);
|
|
|
|
|
}
|
|
|
|
|
autorelease_pool_cache[autorelease_pool_cache_count++] = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static id
|
|
|
|
|
pop_pool_from_cache ()
|
|
|
|
|
{
|
|
|
|
|
assert (autorelease_pool_cache_count);
|
|
|
|
|
return autorelease_pool_cache[--autorelease_pool_cache_count];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1995-03-12 19:35:17 +00:00
|
|
|
|
@implementation NSAutoreleasePool
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NSAutoreleasePool class])
|
|
|
|
|
OBJC_MALLOC (autorelease_pool_cache, id, autorelease_pool_cache_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
/* If there is an already-allocated NSAutoreleasePool available,
|
|
|
|
|
save time by just returning that, rather than allocating a new one. */
|
|
|
|
|
if (autorelease_pool_cache_count)
|
|
|
|
|
return pop_pool_from_cache ();
|
|
|
|
|
|
|
|
|
|
return NSAllocateObject (self, 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- init
|
|
|
|
|
{
|
|
|
|
|
if (!_released_head)
|
|
|
|
|
{
|
|
|
|
|
/* Allocate the array that will be the new head of the list of arrays. */
|
|
|
|
|
_released = (struct autorelease_array_list*)
|
|
|
|
|
(*objc_malloc) (sizeof(struct autorelease_array_list) +
|
|
|
|
|
(BEGINNING_POOL_SIZE * sizeof(id)));
|
|
|
|
|
/* Currently no NEXT array in the list, so NEXT == NULL. */
|
|
|
|
|
_released->next = NULL;
|
|
|
|
|
_released->size = BEGINNING_POOL_SIZE;
|
|
|
|
|
_released->count = 0;
|
|
|
|
|
_released_head = _released;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
/* Already initialized; (it came from autorelease_pool_cache);
|
|
|
|
|
we don't have to allocate new array list memory. */
|
|
|
|
|
{
|
|
|
|
|
_released = _released_head;
|
|
|
|
|
_released->count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This NSAutoreleasePool contains no objects yet. */
|
|
|
|
|
_released_count = 0;
|
|
|
|
|
|
|
|
|
|
/* Install ourselves as the current pool. */
|
|
|
|
|
_parent = current_pool;
|
|
|
|
|
_child = nil;
|
|
|
|
|
[current_pool _setChildPool: self];
|
|
|
|
|
current_pool = self;
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _setChildPool: pool
|
|
|
|
|
{
|
|
|
|
|
assert (!_child);
|
|
|
|
|
_child = pool;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-14 15:01:24 +00:00
|
|
|
|
/* This method not in OpenStep */
|
1996-03-30 22:39:48 +00:00
|
|
|
|
- _parentAutoreleasePool
|
1995-04-14 15:01:24 +00:00
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
return _parent;
|
1995-04-14 15:01:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This method not in OpenStep */
|
|
|
|
|
- (unsigned) autoreleaseCount
|
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
unsigned count = 0;
|
|
|
|
|
struct autorelease_array_list *released = _released_head;
|
|
|
|
|
while (released && released->count)
|
|
|
|
|
{
|
|
|
|
|
count += released->count;
|
|
|
|
|
released = released->next;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
1995-04-14 15:01:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This method not in OpenStep */
|
|
|
|
|
- (unsigned) autoreleaseCountForObject: anObject
|
|
|
|
|
{
|
|
|
|
|
unsigned count = 0;
|
1996-03-30 22:39:48 +00:00
|
|
|
|
struct autorelease_array_list *released = _released_head;
|
1995-04-14 15:01:24 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
while (released && released->count)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < released->count; i++)
|
|
|
|
|
if (released->objects[i] == anObject)
|
|
|
|
|
count++;
|
|
|
|
|
released = released->next;
|
|
|
|
|
}
|
1995-04-14 15:01:24 +00:00
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This method not in OpenStep */
|
|
|
|
|
+ (unsigned) autoreleaseCountForObject: anObject
|
|
|
|
|
{
|
1995-08-02 15:40:33 +00:00
|
|
|
|
unsigned count = 0;
|
1995-04-14 15:01:24 +00:00
|
|
|
|
id pool = current_pool;
|
|
|
|
|
while (pool)
|
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
count += [pool autoreleaseCountForObject: anObject];
|
|
|
|
|
pool = [pool _parentAutoreleasePool];
|
1995-04-14 15:01:24 +00:00
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
1995-03-12 19:35:17 +00:00
|
|
|
|
+ currentPool
|
|
|
|
|
{
|
|
|
|
|
return current_pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) addObject: anObj
|
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
[current_pool addObject: anObj];
|
1995-03-12 19:35:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addObject: anObj
|
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* If the global, static variable AUTORELEASE_ENABLED is not set,
|
|
|
|
|
do nothing, just return. */
|
1995-04-14 15:01:24 +00:00
|
|
|
|
if (!autorelease_enabled)
|
|
|
|
|
return;
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
if (_released_count >= pool_count_warning_threshhold)
|
1996-03-26 19:35:47 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
1996-03-26 20:59:42 +00:00
|
|
|
|
format: @"AutoreleasePool count threshhold exceeded."];
|
1995-04-14 15:01:24 +00:00
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* Get a new array for the list, if the current one is full. */
|
|
|
|
|
if (_released->count == _released->size)
|
1995-03-12 19:35:17 +00:00
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
if (_released->next)
|
|
|
|
|
{
|
|
|
|
|
/* There is an already-allocated one in the chain; use it. */
|
|
|
|
|
_released = _released->next;
|
|
|
|
|
_released->count = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* We are at the end of the chain, and need to allocate a new one. */
|
|
|
|
|
struct autorelease_array_list *new_released;
|
|
|
|
|
unsigned new_size = _released->size * 2;
|
|
|
|
|
|
|
|
|
|
new_released = (struct autorelease_array_list*)
|
|
|
|
|
(*objc_malloc) (sizeof(struct autorelease_array_list) +
|
|
|
|
|
(new_size * sizeof(id)));
|
|
|
|
|
new_released->next = NULL;
|
|
|
|
|
new_released->size = new_size;
|
|
|
|
|
new_released->count = 0;
|
|
|
|
|
_released->next = new_released;
|
|
|
|
|
_released = new_released;
|
|
|
|
|
}
|
1995-03-12 19:35:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* Put the object at the end of the list. */
|
|
|
|
|
_released->objects[_released->count] = anObj;
|
|
|
|
|
(_released->count)++;
|
|
|
|
|
|
|
|
|
|
/* Keep track of the total number of objects autoreleased across all
|
|
|
|
|
pools. */
|
|
|
|
|
total_autoreleased_objects_count++;
|
|
|
|
|
|
|
|
|
|
/* Keep track of the total number of objects autoreleased in this pool */
|
|
|
|
|
_released_count++;
|
1995-03-12 19:35:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) retain
|
|
|
|
|
{
|
1996-03-26 19:35:47 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
1996-03-26 20:59:42 +00:00
|
|
|
|
format: @"Don't call `-retain' on a NSAutoreleasePool"];
|
1995-03-12 19:35:17 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (oneway void) release
|
|
|
|
|
{
|
|
|
|
|
[self dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* If there are NSAutoreleasePool below us in the stack of
|
|
|
|
|
NSAutoreleasePools, then deallocate them also. The (only) way we
|
|
|
|
|
could get in this situation (in correctly written programs, that
|
|
|
|
|
don't release NSAutoreleasePools in weird ways), is if an
|
|
|
|
|
exception threw us up the stack. */
|
|
|
|
|
if (_child)
|
|
|
|
|
[_child dealloc];
|
1995-03-12 19:35:17 +00:00
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
/* Make debugging easier by checking to see if the user already
|
|
|
|
|
dealloced the object before trying to release it. Also, take the
|
|
|
|
|
object out of the released list just before releasing it, so if
|
|
|
|
|
we are doing "double_release_check"ing, then
|
|
|
|
|
autoreleaseCountForObject: won't find the object we are currently
|
|
|
|
|
releasing. */
|
|
|
|
|
{
|
|
|
|
|
struct autorelease_array_list *released = _released_head;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
while (released)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < released->count; i++)
|
|
|
|
|
{
|
|
|
|
|
id anObject = released->objects[i];
|
|
|
|
|
if (object_get_class(anObject) == (void*) 0xdeadface)
|
|
|
|
|
[NSException
|
|
|
|
|
raise: NSGenericException
|
|
|
|
|
format: @"Autoreleasing deallocated object.\n"
|
|
|
|
|
@"Suggest you debug after setting [NSObject "
|
|
|
|
|
@"enableDoubleReleaseCheck:YES]\n"
|
|
|
|
|
@"to check for release errors."];
|
|
|
|
|
released->objects[i] = nil;
|
|
|
|
|
[anObject release];
|
|
|
|
|
}
|
|
|
|
|
released = released->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1996-04-10 16:40:25 +00:00
|
|
|
|
/* Uninstall ourselves as the current pool; install our parent pool. */
|
1996-03-30 22:39:48 +00:00
|
|
|
|
current_pool = _parent;
|
1996-04-10 16:40:25 +00:00
|
|
|
|
if (current_pool)
|
|
|
|
|
current_pool->_child = nil;
|
1996-03-30 22:39:48 +00:00
|
|
|
|
|
|
|
|
|
/* Don't deallocate ourself, just save us for later use. */
|
|
|
|
|
push_pool_to_cache (self);
|
1995-03-12 19:35:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- autorelease
|
|
|
|
|
{
|
1996-03-26 19:35:47 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Don't call `-autorelease' on a NSAutoreleasePool"];
|
1995-03-12 19:35:17 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-30 22:39:48 +00:00
|
|
|
|
+ (void) resetTotalAutoreleasedObjects
|
|
|
|
|
{
|
|
|
|
|
total_autoreleased_objects_count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (unsigned) totalAutoreleasedObjects
|
|
|
|
|
{
|
|
|
|
|
return total_autoreleased_objects_count;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-14 15:01:24 +00:00
|
|
|
|
+ (void) enableRelease: (BOOL)enable
|
|
|
|
|
{
|
|
|
|
|
autorelease_enabled = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) setPoolCountThreshhold: (unsigned)c
|
|
|
|
|
{
|
|
|
|
|
pool_count_warning_threshhold = c;
|
|
|
|
|
}
|
|
|
|
|
|
1995-03-12 19:35:17 +00:00
|
|
|
|
@end
|