1 /* Interface for NSAutoreleasePool for GNUStep
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
7 This file is part of the GNUstep Base Library.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 #ifndef __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE
26 #define __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE
27 #import "../GNUstepBase/GSVersionMacros.h"
31 #if defined(__cplusplus)
35 @
class NSAutoreleasePool
;
40 * Each thread has its own copy of these variables.
43 NSAutoreleasePool *current_pool; // current pool for thread
44 unsigned total_objects_count; // total #/autoreleased objects over thread's lifetime
45 id *pool_cache; // cache of previously-allocated pools,
46 int pool_cache_size; // used internally for recycling
51 typedef struct autorelease_thread_vars
53 /* The current, default NSAutoreleasePool for the calling thread;
54 the one that will hold objects that are arguments to
55 [NSAutoreleasePool +addObject:]. */
56 __unsafe_unretained NSAutoreleasePool
*current_pool
;
58 /* The total number of objects autoreleased since the thread was
59 started, or since -resetTotalAutoreleasedObjects was called
60 in this thread. (if compiled in) */
61 unsigned total_objects_count
;
63 /* A cache of NSAutoreleasePool's already alloc'ed. Caching old pools
64 instead of deallocating and re-allocating them will save time. */
65 __unsafe_unretained id
*pool_cache
;
70 /* Initialize an autorelease_thread_vars structure for a new thread.
71 This function is called in NSThread each time an NSThread is created.
72 TV should be of type `struct autorelease_thread_vars *' */
73 #define init_autorelease_thread_vars(TV) \
74 memset (TV, 0, sizeof (__typeof__ (*TV)))
79 * Each pool holds its objects-to-be-released in a linked-list of
83 struct autorelease_array_list *next;
90 typedef struct autorelease_array_list
92 struct autorelease_array_list
*next
;
95 __unsafe_unretained id objects
[0];
102 * The standard OpenStep system of memory management employs retain counts.
103 * When an object is created, it has a retain count of 1. When an object
104 * is retained, the retain count is incremented. When it is released the
105 * retain count is decremented, and when the retain count goes to zero the
106 * object gets deallocated.
109 * A simple retain/release mechanism has problems with passing objects
110 * from one scope to another,
111 * so it's augmented with autorelease pools. You can use the
112 * AUTORELEASE() macro to call the [NSObject-autorelease]
113 * method, which adds an object to the current autorelease pool by
114 * calling [NSAutoreleasePool+addObject:].<br />
115 * An autorelease pool simply maintains a reference to each object
116 * added to it, and for each addition, the autorelease pool will
117 * call the [NSObject-release] method of the object when the pool
118 * is released. So doing an AUTORELEASE() is just the same as
119 * doing a RELEASE(), but deferred until the current autorelease
120 * pool is deallocated.
123 * The NSAutoreleasePool class maintains a separate stack of
124 * autorelease pools objects in each thread.
127 * When an autorelease pool is created, it is automatically
128 * added to the stack of pools in the thread.
131 * When a pool is destroyed, it (and any pool later in
132 * the stack) is removed from the stack.
135 * This mechanism provides a simple but controllable and reasonably
136 * efficient way of managing temporary objects. An object can be
137 * autoreleased and then passed around and used until the topmost
138 * pool in the stack is destroyed.
141 * Most methods return objects which are either owned by autorelease
142 * pools or by the receiver of the method, so the lifetime of the
143 * returned object can be assumed to be the shorter of the lifetime
144 * of the current autorelease pool, or that of the receiver on which
145 * the method was called.<br />
146 * The exceptions to this are those object returned by -
149 * <term>[NSObject+alloc], [NSObject+allocWithZone:]</term>
151 * Methods whose names begin with alloc return an uninitialised
152 * object, owned by the caller.
154 * <term>[NSObject-init]</term>
156 * Methods whose names begin with init return an initialised
157 * version of the receiving object, owned by the caller.<br />
158 * NB. The returned object may not actually be the same as the
159 * receiver ... sometimes an init method releases the original
160 * receiver and returns an alternative.
162 * <term>[NSObject+new]</term>
164 * Methods whose names begin with new combine the effects of
165 * allocation and initialisation.
167 * <term>[NSObject-copy], [(NSCopying)-copyWithZone:]</term>
169 * Methods whose names begin with copy create a copy of the receiver
170 * which is owned by the caller.
173 * [NSObject-mutableCopy], [(NSMutableCopying)-mutableCopyWithZone:]
176 * Methods whose names begin with mutableCopy create a copy of the receiver
177 * which is owned by the caller.
181 NS_AUTOMATED_REFCOUNT_UNAVAILABLE
182 @interface NSAutoreleasePool
: NSObject
184 #if GS_EXPOSE(NSAutoreleasePool) && !__has_feature(objc_arc)
185 /* For re-setting the current pool when we are dealloc'ed. */
186 NSAutoreleasePool
*_parent
;
187 /* This pointer to our child pool is necessary for co-existing
189 NSAutoreleasePool
*_child
;
190 /* A collection of the objects to be released. */
191 struct autorelease_array_list
*_released
;
192 struct autorelease_array_list
*_released_head
;
193 /* The total number of objects autoreleased in this pool. */
194 unsigned _released_count
;
195 /* The method to add an object to this pool */
196 void (*_addImp
)(id
, SEL
, id
);
200 /* Pointer to private additional data used to avoid breaking ABI
201 * when we don't have the non-fragile ABI available.
202 * Use this mechanism rather than changing the instance variable
203 * layout (see Source/GSInternal.h for details).
205 @
private id _internal GS_UNUSED_IVAR
;
210 * Adds anObj to the current autorelease pool.<br />
211 * If there is no autorelease pool in the thread,
212 * a warning is logged and the object is leaked (ie it will not be released).
214 + (void) addObject
: (id
)anObj
;
217 * Allocate and return an autorelease pool instance.<br />
218 * If there is an already-allocated NSAutoreleasePool available,
219 * save time by just returning that, rather than allocating a new one.<br />
220 * The pool instance becomes the current autorelease pool for this thread.
222 + (id
) allocWithZone
: (NSZone
*)zone
;
225 * Adds anObj to this autorelease pool.
227 - (void) addObject
: (id
)anObj
;
230 * Raises an exception - pools should not be autoreleased.
234 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
236 * Intended to trigger a garbage collection run (if needed) when called in
237 * a garbage collected environment.<br />
238 * In a non-garbage collected environment, this method implements the
239 * undocumented MacOS-X behavior, and releases the receiver.
245 * Destroys the receiver (calls -dealloc).
247 - (oneway
void) release
;
250 * Raises an exception ... pools should not be retained.
254 #if OS_API_VERSION(GS_API_NONE, GS_API_NONE)
257 * Counts the number of times that the specified object occurs
258 * in autorelease pools in the current thread.
261 * This method is <em>slow</em> and should probably only be
262 * used for debugging purposes.
265 + (unsigned) autoreleaseCountForObject
: (id
)anObject
;
268 * Return the currently active autorelease pool.
274 * Specifies whether objects contained in autorelease pools are to
275 * be released when the pools are deallocated (by default YES).
278 * You can set this to NO for debugging purposes.
281 + (void) enableRelease
: (BOOL
)enable
;
285 * When autorelease pools are deallocated, the memory they used
286 * is retained in a cache for re-use so that new polls can be
287 * created very quickly.
290 * This method may be used to empty that cache, ensuring that
291 * the minimum memory is used by the application.
298 * Specifies a limit to the number of objects that may be added to
299 * an autorelease pool. When this limit is reached an exception is
303 * You can set this to a smallish value to catch problems with code
304 * that autoreleases too many objects to operate efficiently.
307 * Default value is maxint.
310 + (void) setPoolCountThreshold
: (unsigned)c
;
313 * Return the number of objects in this pool.
315 - (unsigned) autoreleaseCount
;
318 * Empties the current pool by releasing all the autoreleased objects
319 * in it. Also destroys any child pools (ones created after
320 * the receiver in the same thread) causing any objects in those pools
321 * to be released.<br />
322 * This is a low cost (efficient) method which may be used to get rid of
323 * autoreleased objects in the pool, but carry on using the pool.
329 #if defined(__cplusplus)
333 #endif /* __NSAutoreleasePool_h_GNUSTEP_BASE_INCLUDE */