1
1
/** Implementation of auto release pool for delayed disposal
2
2
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4
4
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
5
Written by: Richard Frith-Macdonald <rfm@gnu.org>
7
8
This file is part of the GNUstep Base Library.
9
10
This library is free software; you can redistribute it and/or
10
11
modify it under the terms of the GNU Library General Public
11
12
License as published by the Free Software Foundation; either
12
13
version 2 of the License, or (at your option) any later version.
14
15
This library is distributed in the hope that it will be useful,
15
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
18
Library General Public License for more details.
19
20
You should have received a copy of the GNU Library General Public
20
21
License along with this library; if not, write to the Free
21
22
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
23
24
<title>NSAutoreleasePool class reference</title>
24
$Date: 2002/01/30 15:28:50 $ $Revision: 1.46 $
25
$Date: 2005/02/22 11:22:43 $ $Revision: 1.56 $
28
#include <base/preface.h>
29
#include <Foundation/NSAutoreleasePool.h>
30
#include <Foundation/NSException.h>
31
#include <Foundation/NSThread.h>
32
#include <Foundation/NSZone.h>
29
#include "GNUstepBase/preface.h"
30
#include "Foundation/NSAutoreleasePool.h"
31
#include "Foundation/NSException.h"
32
#include "Foundation/NSThread.h"
33
#include "Foundation/NSZone.h"
33
34
#include <limits.h>
36
* Set to 1 to count all autoreleases
40
36
/* When this is `NO', autoreleased objects are never actually recorded
41
37
in an NSAutoreleasePool, and are not sent a `release' message.
42
38
Thus memory for objects use grows, and grows, and... */
56
52
@interface NSAutoreleasePool (Private)
57
53
- (id) _parentAutoreleasePool;
58
- (unsigned) autoreleaseCount;
59
- (unsigned) autoreleaseCountForObject: (id)anObject;
60
54
+ (unsigned) autoreleaseCountForObject: (id)anObject;
62
55
- (void) _reallyDealloc;
63
56
- (void) _setChildPool: (id)pool;
67
60
/* Functions for managing a per-thread cache of NSAutoreleasedPool's
68
already alloc'ed. The cache is kept in the autorelease_thread_var
61
already alloc'ed. The cache is kept in the autorelease_thread_var
69
62
structure, which is an ivar of NSThread. */
71
64
static id pop_pool_from_cache (struct autorelease_thread_vars *tv);
136
130
+ (id) allocWithZone: (NSZone*)zone
138
/* If there is an already-allocated NSAutoreleasePool available,
139
save time by just returning that, rather than allocating a new one. */
140
132
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
141
133
if (tv->pool_cache_count)
142
134
return pop_pool_from_cache (tv);
303
290
/* We are at the end of the chain, and need to allocate a new one. */
304
291
struct autorelease_array_list *new_released;
305
292
unsigned new_size = _released->size * 2;
307
294
new_released = (struct autorelease_array_list*)
308
295
NSZoneMalloc(NSDefaultMallocZone(),
309
296
sizeof(struct autorelease_array_list) + (new_size * sizeof(id)));
362
343
struct autorelease_array_list *released = _released_head;
367
348
for (i = 0; i < released->count; i++)
369
350
id anObject = released->objects[i];
371
/* There is no general method to find out whether a memory
372
chunk has been deallocated or not, especially when
373
custom zone functions might be used. So we #if this
375
if (!NSZoneMemInUse(anObject))
377
raise: NSGenericException
378
format: @"Autoreleasing deallocated object.\n"
379
@"Suggest you debug after setting [NSObject "
380
@"enableDoubleReleaseCheck:YES]\n"
381
@"to check for release errors."];
383
351
released->objects[i] = nil;
384
352
[anObject release];
377
struct autorelease_array_list *released;
384
released = _released_head;
389
for (i = 0; i < released->count; i++)
391
id anObject = released->objects[i];
392
released->objects[i] = nil;
396
released = released->next;
398
_released = _released_head;
407
401
- (void) _reallyDealloc
409
403
struct autorelease_array_list *a;
410
for (a = _released_head; a; )
404
for (a = _released_head; a;)
412
406
void *n = a->next;
413
407
NSZoneFree(NSDefaultMallocZone(), a);
439
433
free_pool_cache(tv);
442
+ (void) resetTotalAutoreleasedObjects
444
ARP_THREAD_VARS->total_objects_count = 0;
447
+ (unsigned) totalAutoreleasedObjects
449
return ARP_THREAD_VARS->total_objects_count;
452
436
+ (void) enableRelease: (BOOL)enable
454
438
autorelease_enabled = enable;