1995-03-17 23:53:43 +00:00
|
|
|
|
/* Interface for NSAutoreleasePool for GNUStep
|
1997-01-05 22:49:30 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
|
1995-07-01 19:01:11 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-07-01 19:01:11 +00:00
|
|
|
|
Date: 1995
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-03-17 23:53:43 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-03-17 23:53:43 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-08-09 12:54:57 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
1995-03-17 23:53:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
|
#ifndef __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE
|
|
|
|
|
#define __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE
|
2006-10-31 07:05:46 +00:00
|
|
|
|
#import <GNUstepBase/GSVersionMacros.h>
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
|
#import <Foundation/NSObject.h>
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
|
@class NSAutoreleasePool;
|
2000-11-22 08:41:07 +00:00
|
|
|
|
@class NSThread;
|
1996-07-15 18:41:44 +00:00
|
|
|
|
|
|
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
|
/**
|
|
|
|
|
* Each thread has its own copy of these variables.
|
|
|
|
|
<example>
|
|
|
|
|
{
|
|
|
|
|
NSAutoreleasePool *current_pool; // current pool for thread
|
|
|
|
|
unsigned total_objects_count; // total #/autoreleased objects over thread's lifetime
|
|
|
|
|
id *pool_cache; // cache of previously-allocated pools,
|
|
|
|
|
int pool_cache_size; // used internally for recycling
|
|
|
|
|
int pool_cache_count;
|
|
|
|
|
}
|
|
|
|
|
</example>
|
|
|
|
|
*/
|
2002-06-06 14:02:59 +00:00
|
|
|
|
typedef struct autorelease_thread_vars
|
1996-07-15 18:41:44 +00:00
|
|
|
|
{
|
|
|
|
|
/* The current, default NSAutoreleasePool for the calling thread;
|
|
|
|
|
the one that will hold objects that are arguments to
|
|
|
|
|
[NSAutoreleasePool +addObject:]. */
|
|
|
|
|
NSAutoreleasePool *current_pool;
|
|
|
|
|
|
|
|
|
|
/* The total number of objects autoreleased since the thread was
|
|
|
|
|
started, or since -resetTotalAutoreleasedObjects was called
|
1999-04-19 14:29:52 +00:00
|
|
|
|
in this thread. (if compiled in) */
|
1996-07-15 18:41:44 +00:00
|
|
|
|
unsigned total_objects_count;
|
|
|
|
|
|
|
|
|
|
/* A cache of NSAutoreleasePool's already alloc'ed. Caching old pools
|
|
|
|
|
instead of deallocating and re-allocating them will save time. */
|
|
|
|
|
id *pool_cache;
|
|
|
|
|
int pool_cache_size;
|
|
|
|
|
int pool_cache_count;
|
2002-06-06 14:02:59 +00:00
|
|
|
|
} thread_vars_struct;
|
1996-07-15 18:41:44 +00:00
|
|
|
|
|
|
|
|
|
/* Initialize an autorelease_thread_vars structure for a new thread.
|
1997-01-05 22:49:30 +00:00
|
|
|
|
This function is called in NSThread each time an NSThread is created.
|
|
|
|
|
TV should be of type `struct autorelease_thread_vars *' */
|
2007-07-31 12:09:42 +00:00
|
|
|
|
#define init_autorelease_thread_vars(TV) \
|
|
|
|
|
memset (TV, 0, sizeof (__typeof__ (*TV)))
|
1997-01-05 22:49:30 +00:00
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
|
|
|
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
|
/**
|
|
|
|
|
* Each pool holds its objects-to-be-released in a linked-list of
|
|
|
|
|
these structures.
|
|
|
|
|
<example>
|
|
|
|
|
{
|
|
|
|
|
struct autorelease_array_list *next;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count;
|
|
|
|
|
id objects[0];
|
|
|
|
|
}
|
|
|
|
|
</example>
|
|
|
|
|
*/
|
2002-06-06 14:02:59 +00:00
|
|
|
|
typedef struct autorelease_array_list
|
1995-03-17 23:53:43 +00:00
|
|
|
|
{
|
1996-03-30 22:20:43 +00:00
|
|
|
|
struct autorelease_array_list *next;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count;
|
|
|
|
|
id objects[0];
|
2002-06-06 14:02:59 +00:00
|
|
|
|
} array_list_struct;
|
1996-03-30 22:20:43 +00:00
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
|
|
2004-05-14 16:22:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* The standard OpenStep system of memory management employs retain counts.
|
|
|
|
|
* When an object is created, it has a retain count of 1. When an object
|
|
|
|
|
* is retained, the retain count is incremented. When it is released the
|
|
|
|
|
* retain count is decremented, and when the retain count goes to zero the
|
|
|
|
|
* object gets deallocated.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
2004-06-22 22:27:39 +00:00
|
|
|
|
* A simple retain/release mechanism has problems with passing objects
|
|
|
|
|
* from one scope to another,
|
|
|
|
|
* so it's augmented with autorelease pools. You can use the
|
2004-05-14 16:22:49 +00:00
|
|
|
|
* AUTORELEASE() macro to call the [NSObject-autorelease]
|
|
|
|
|
* method, which adds an object to the current autorelease pool by
|
|
|
|
|
* calling [NSAutoreleasePool+addObject:].<br />
|
|
|
|
|
* An autorelease pool simply maintains a reference to each object
|
|
|
|
|
* added to it, and for each addition, the autorelease pool will
|
|
|
|
|
* call the [NSObject-release] method of the object when the pool
|
|
|
|
|
* is released. So doing an AUTORELEASE() is just the same as
|
|
|
|
|
* doing a RELEASE(), but deferred until the current autorelease
|
|
|
|
|
* pool is deallocated.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* The NSAutoreleasePool class maintains a separate stack of
|
|
|
|
|
* autorelease pools objects in each thread.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* When an autorelease pool is created, it is automatically
|
|
|
|
|
* added to the stack of pools in the thread.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* When a pool is destroyed, it (and any pool later in
|
|
|
|
|
* the stack) is removed from the stack.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* This mechanism provides a simple but controllable and reasonably
|
|
|
|
|
* efficient way of managing temporary objects. An object can be
|
|
|
|
|
* autoreleased and then passed around and used until the topmost
|
|
|
|
|
* pool in the stack is destroyed.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Most methods return objects which are either owned by autorelease
|
|
|
|
|
* pools or by the receiver of the method, so the lifetime of the
|
|
|
|
|
* returned object can be assumed to be the shorter of the lifetime
|
|
|
|
|
* of the current autorelease pool, or that of the receiver on which
|
|
|
|
|
* the method was called.<br />
|
|
|
|
|
* The exceptions to this are those object returned by -
|
|
|
|
|
* </p>
|
|
|
|
|
* <deflist>
|
|
|
|
|
* <term>[NSObject+alloc], [NSObject+allocWithZone:]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Methods whose names begin with alloc return an uninitialised
|
|
|
|
|
* object, owned by the caller.
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSObject-init]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Methods whose names begin with init return an initialised
|
|
|
|
|
* version of the receiving object, owned by the caller.<br />
|
2004-06-22 22:27:39 +00:00
|
|
|
|
* NB. The returned object may not actually be the same as the
|
2004-05-14 16:22:49 +00:00
|
|
|
|
* receiver ... sometimes an init method releases the original
|
|
|
|
|
* receiver and returns an alternative.
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSObject+new]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Methods whose names begin with new combine the effects of
|
|
|
|
|
* allocation and initialisation.
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSObject-copy], [(NSCopying)-copyWithZone:]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Methods whose names begin with copy create a copy of the receiver
|
|
|
|
|
* which is owned by the caller.
|
|
|
|
|
* </desc>
|
2006-03-08 11:28:59 +00:00
|
|
|
|
* <term>
|
|
|
|
|
* [NSObject-mutableCopy], [(NSMutableCopying)-mutableCopyWithZone:]
|
|
|
|
|
* </term>
|
2004-05-14 16:22:49 +00:00
|
|
|
|
* <desc>
|
|
|
|
|
* Methods whose names begin with mutableCopy create a copy of the receiver
|
|
|
|
|
* which is owned by the caller.
|
|
|
|
|
* </desc>
|
|
|
|
|
* </deflist>
|
|
|
|
|
*/
|
1996-03-30 22:20:43 +00:00
|
|
|
|
@interface NSAutoreleasePool : NSObject
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
#if GS_EXPOSE(NSAutoreleasePool)
|
1996-03-30 22:20:43 +00:00
|
|
|
|
/* For re-setting the current pool when we are dealloc'ed. */
|
|
|
|
|
NSAutoreleasePool *_parent;
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* This pointer to our child pool is necessary for co-existing
|
|
|
|
|
with exceptions. */
|
1996-03-30 22:20:43 +00:00
|
|
|
|
NSAutoreleasePool *_child;
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* A collection of the objects to be released. */
|
1996-03-30 22:20:43 +00:00
|
|
|
|
struct autorelease_array_list *_released;
|
|
|
|
|
struct autorelease_array_list *_released_head;
|
|
|
|
|
/* The total number of objects autoreleased in this pool. */
|
|
|
|
|
unsigned _released_count;
|
1999-05-06 20:24:50 +00:00
|
|
|
|
/* The method to add an object to this pool */
|
1999-09-16 07:21:34 +00:00
|
|
|
|
void (*_addImp)(id, SEL, id);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
void *_unused;
|
|
|
|
|
#endif
|
1995-03-17 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-05-14 16:22:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Adds anObj to the current autorelease pool.<br />
|
|
|
|
|
* If there is no autorelease pool in the thread,
|
|
|
|
|
* a warning is logged and the object is leaked (ie it will not be released).
|
|
|
|
|
*/
|
2001-12-17 14:31:42 +00:00
|
|
|
|
+ (void) addObject: (id)anObj;
|
2004-05-14 16:22:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocate and return an autorelease pool instance.<br />
|
|
|
|
|
* If there is an already-allocated NSAutoreleasePool available,
|
|
|
|
|
* save time by just returning that, rather than allocating a new one.<br />
|
|
|
|
|
* The pool instance becomes the current autorelease pool for this thread.
|
|
|
|
|
*/
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)zone;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds anObj to this autorelease pool.
|
|
|
|
|
*/
|
2001-12-17 14:31:42 +00:00
|
|
|
|
- (void) addObject: (id)anObj;
|
1995-03-17 23:53:43 +00:00
|
|
|
|
|
2004-05-14 16:22:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Raises an exception - pools should not be autoreleased.
|
|
|
|
|
*/
|
|
|
|
|
- (id) autorelease;
|
|
|
|
|
|
2008-06-06 13:57:06 +00:00
|
|
|
|
#if OS_API_VERSION(100400, GS_API_LATEST)
|
2009-01-22 15:59:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Intended to trigger a garbage collection run (if needed) when called in
|
2009-11-04 05:51:34 +00:00
|
|
|
|
* a garbage collected environment.<br />
|
|
|
|
|
* In a non-garbage collected environment, this method implements the
|
|
|
|
|
* undocumented MacOS-X behavior, and releases the receiver.
|
2008-06-06 13:57:06 +00:00
|
|
|
|
*/
|
|
|
|
|
- (void) drain;
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-05-14 16:22:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Destroys the receiver (calls -dealloc).
|
|
|
|
|
*/
|
|
|
|
|
- (oneway void) release;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raises an exception ... pools should not be retained.
|
|
|
|
|
*/
|
|
|
|
|
- (id) retain;
|
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
|
#if OS_API_VERSION(GS_API_NONE, GS_API_NONE)
|
2004-05-14 16:22:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* Counts the number of times that the specified object occurs
|
|
|
|
|
* in autorelease pools in the current thread.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* This method is <em>slow</em> and should probably only be
|
|
|
|
|
* used for debugging purposes.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
|
|
|
|
+ (unsigned) autoreleaseCountForObject: (id)anObject;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the currently active autorelease pool.
|
|
|
|
|
*/
|
|
|
|
|
+ (id) currentPool;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* Specifies whether objects contained in autorelease pools are to
|
|
|
|
|
* be released when the pools are deallocated (by default YES).
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* You can set this to NO for debugging purposes.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1995-04-14 15:01:37 +00:00
|
|
|
|
+ (void) enableRelease: (BOOL)enable;
|
2004-05-14 16:22:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* When autorelease pools are deallocated, the memory they used
|
|
|
|
|
* is retained in a cache for re-use so that new polls can be
|
|
|
|
|
* created very quickly.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* This method may be used to empty that cache, ensuring that
|
|
|
|
|
* the minimum memory is used by the application.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
|
|
|
|
+ (void) freeCache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* Specifies a limit to the number of objects that may be added to
|
|
|
|
|
* an autorelease pool. When this limit is reached an exception is
|
|
|
|
|
* raised.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* You can set this to a smallish value to catch problems with code
|
|
|
|
|
* that autoreleases too many objects to operate efficiently.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Default value is maxint.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1995-04-14 15:01:37 +00:00
|
|
|
|
+ (void) setPoolCountThreshhold: (unsigned)c;
|
2004-05-14 16:22:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the number of objects in this pool.
|
|
|
|
|
*/
|
|
|
|
|
- (unsigned) autoreleaseCount;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Empties the current pool by releasing all the autoreleased objects
|
|
|
|
|
* in it. Also destroys any child pools (ones created after
|
|
|
|
|
* the receiver in the same thread) causing any objects in those pools
|
|
|
|
|
* to be released.<br />
|
|
|
|
|
* This is a low cost (efficient) method which may be used to get rid of
|
|
|
|
|
* autoreleased objects in the pool, but carry on using the pool.
|
1999-04-19 14:29:52 +00:00
|
|
|
|
*/
|
2004-05-14 16:22:49 +00:00
|
|
|
|
- (void) emptyPool;
|
1998-11-10 20:16:33 +00:00
|
|
|
|
#endif
|
1995-03-17 23:53:43 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
|
#endif /* __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE */
|