~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Source/NSAutoreleasePool.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/** Implementation of auto release pool for delayed disposal
2
2
   Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3
 
   
 
3
 
4
4
   Written by:  Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
 
5
   Written by:  Richard Frith-Macdonald <rfm@gnu.org>
5
6
   Date: January 1995
6
 
   
 
7
 
7
8
   This file is part of the GNUstep Base Library.
8
9
 
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.
13
 
   
 
14
 
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.
18
 
   
 
19
 
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.
22
23
 
23
24
   <title>NSAutoreleasePool class reference</title>
24
 
   $Date: 2002/01/30 15:28:50 $ $Revision: 1.46 $
25
 
   */ 
 
25
   $Date: 2005/02/22 11:22:43 $ $Revision: 1.56 $
 
26
   */
26
27
 
27
 
#include <config.h>
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>
 
28
#include "config.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>
34
35
 
35
 
/*
36
 
 * Set to 1 to count all autoreleases
37
 
 */
38
 
#define COUNT_ALL       0
39
 
 
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... */
55
51
 
56
52
@interface NSAutoreleasePool (Private)
57
53
- (id) _parentAutoreleasePool;
58
 
- (unsigned) autoreleaseCount;
59
 
- (unsigned) autoreleaseCountForObject: (id)anObject;
60
54
+ (unsigned) autoreleaseCountForObject: (id)anObject;
61
 
+ (id) currentPool;
62
55
- (void) _reallyDealloc;
63
56
- (void) _setChildPool: (id)pool;
64
57
@end
65
58
 
66
59
 
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. */
70
63
 
71
64
static id pop_pool_from_cache (struct autorelease_thread_vars *tv);
84
77
    {
85
78
      NSZoneFree(NSDefaultMallocZone(), tv->pool_cache);
86
79
      tv->pool_cache = 0;
 
80
      tv->pool_cache_size = 0;
87
81
    }
88
82
}
89
83
 
135
129
 
136
130
+ (id) allocWithZone: (NSZone*)zone
137
131
{
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);
202
194
  return _parent;
203
195
}
204
196
 
205
 
/* This method not in OpenStep */
206
197
- (unsigned) autoreleaseCount
207
198
{
208
199
  unsigned count = 0;
215
206
  return count;
216
207
}
217
208
 
218
 
/* This method not in OpenStep */
219
209
- (unsigned) autoreleaseCountForObject: (id)anObject
220
210
{
221
211
  unsigned count = 0;
222
212
  struct autorelease_array_list *released = _released_head;
223
 
  int i;
 
213
  unsigned int i;
224
214
 
225
215
  while (released && released->count)
226
216
    {
232
222
  return count;
233
223
}
234
224
 
235
 
/* This method not in OpenStep */
236
 
/* xxx This count should be made for *all* threads, but currently is 
237
 
   only madefor the current thread! */
238
225
+ (unsigned) autoreleaseCountForObject: (id)anObject
239
226
{
240
227
  unsigned count = 0;
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;
306
 
          
 
293
        
307
294
          new_released = (struct autorelease_array_list*)
308
295
            NSZoneMalloc(NSDefaultMallocZone(),
309
296
            sizeof(struct autorelease_array_list) + (new_size * sizeof(id)));
319
306
  _released->objects[_released->count] = anObj;
320
307
  (_released->count)++;
321
308
 
322
 
#if     COUNT_ALL
323
 
  /* Keep track of the total number of objects autoreleased across all
324
 
     pools. */
325
 
  ARP_THREAD_VARS->total_objects_count++;
326
 
#endif
327
 
 
328
309
  /* Keep track of the total number of objects autoreleased in this pool */
329
310
  _released_count++;
330
311
}
360
341
     releasing. */
361
342
  {
362
343
    struct autorelease_array_list *released = _released_head;
363
 
    int i;
 
344
    unsigned int i;
364
345
 
365
346
    while (released)
366
347
      {
367
348
        for (i = 0; i < released->count; i++)
368
349
          {
369
350
            id anObject = released->objects[i];
370
 
#if 0
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
374
 
               out. */
375
 
            if (!NSZoneMemInUse(anObject))
376
 
              [NSException 
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."];
382
 
#endif
383
351
            released->objects[i] = nil;
384
352
            [anObject release];
385
353
          }
404
372
  }
405
373
}
406
374
 
 
375
- (void) emptyPool
 
376
{
 
377
  struct autorelease_array_list *released;
 
378
 
 
379
  if (_child)
 
380
    {
 
381
      [_child dealloc];
 
382
    }
 
383
 
 
384
  released = _released_head;
 
385
  while (released)
 
386
    {
 
387
      unsigned int      i;
 
388
 
 
389
      for (i = 0; i < released->count; i++)
 
390
        {
 
391
          id anObject = released->objects[i];
 
392
          released->objects[i] = nil;
 
393
          [anObject release];
 
394
        }
 
395
      released->count = 0;
 
396
      released = released->next;
 
397
    }
 
398
  _released = _released_head;
 
399
}
 
400
 
407
401
- (void) _reallyDealloc
408
402
{
409
403
  struct autorelease_array_list *a;
410
 
  for (a = _released_head; a; )
 
404
  for (a = _released_head; a;)
411
405
    {
412
406
      void *n = a->next;
413
407
      NSZoneFree(NSDefaultMallocZone(), a);
439
433
  free_pool_cache(tv);
440
434
}
441
435
 
442
 
+ (void) resetTotalAutoreleasedObjects
443
 
{
444
 
  ARP_THREAD_VARS->total_objects_count = 0;
445
 
}
446
 
 
447
 
+ (unsigned) totalAutoreleasedObjects
448
 
{
449
 
  return ARP_THREAD_VARS->total_objects_count;
450
 
}
451
 
 
452
436
+ (void) enableRelease: (BOOL)enable
453
437
{
454
438
  autorelease_enabled = enable;
465
449
}
466
450
 
467
451
@end
 
452