1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
/** Implementation of auto release pool for delayed disposal
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: January 1995
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSAutoreleasePool class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#define EXPOSE_NSAutoreleasePool_IVARS 1
#define EXPOSE_NSThread_IVARS 1
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSException.h"
#import "Foundation/NSThread.h"
#if __has_include(<objc/capabilities.h>)
# include <objc/capabilities.h>
# ifdef OBJC_ARC_AUTORELEASE_DEBUG
# include <objc/objc-arc.h>
# define ARC_RUNTIME 1
# endif
#endif
/* 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... */
static BOOL autorelease_enabled = YES;
/* When the _released_count of a pool gets over this value, we raise
an exception. This can be adjusted with +setPoolCountThreshold */
static unsigned pool_count_warning_threshold = UINT_MAX-1;
/* When the number of pools in a thread gets over this value, we raise
an exception. This can be adjusted with +setPoolNumberThreshold */
static unsigned pool_number_warning_threshold = 10000;
/* The size of the first _released array. */
#define BEGINNING_POOL_SIZE 32
/* Easy access to the thread variables belonging to NSAutoreleasePool. */
#define ARP_THREAD_VARS (&((GSCurrentThread())->_autorelease_vars))
@interface NSAutoreleasePool (Private)
+ (unsigned) autoreleaseCountForObject: (id)anObject;
- (void) _reallyDealloc;
@end
/* Functions for managing a per-thread cache of NSAutoreleasedPool's
already alloc'ed. The cache is kept in the autorelease_thread_var
structure, which is an ivar of NSThread. */
static id pop_pool_from_cache (struct autorelease_thread_vars *tv);
static inline void
free_pool_cache (struct autorelease_thread_vars *tv)
{
while (tv->pool_cache_count)
{
NSAutoreleasePool *pool = pop_pool_from_cache(tv);
[pool _reallyDealloc];
}
if (tv->pool_cache)
{
NSZoneFree(NSDefaultMallocZone(), tv->pool_cache);
tv->pool_cache = 0;
tv->pool_cache_size = 0;
}
}
static inline void
init_pool_cache (struct autorelease_thread_vars *tv)
{
tv->pool_cache_size = 32;
tv->pool_cache_count = 0;
tv->pool_cache = (id*)NSZoneMalloc(NSDefaultMallocZone(),
sizeof(id) * tv->pool_cache_size);
}
static void
push_pool_to_cache (struct autorelease_thread_vars *tv, id p)
{
if (!tv->pool_cache)
{
init_pool_cache (tv);
}
else if (tv->pool_cache_count == tv->pool_cache_size)
{
tv->pool_cache_size *= 2;
tv->pool_cache = (id*)NSZoneRealloc(NSDefaultMallocZone(),
tv->pool_cache, sizeof(id) * tv->pool_cache_size);
}
tv->pool_cache[tv->pool_cache_count++] = p;
}
static id
pop_pool_from_cache (struct autorelease_thread_vars *tv)
{
return tv->pool_cache[--(tv->pool_cache_count)];
}
@implementation NSAutoreleasePool
+ (void) initialize
{
/* Do nothing here which might interact with ther classes since this
* is called by [NSObject+initialize] !
*/
return;
}
+ (id) allocWithZone: (NSZone*)zone
{
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
if (tv->pool_cache_count)
{
NSAutoreleasePool *p = pop_pool_from_cache (tv);
/* When we cache a 'deallocated' pool, we set its _released_count to
* UINT_MAX, so when we rtrieve it fromm the cache we must increment
* it to start with a count of zero.
*/
if (++(p->_released_count) != 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"NSAutoreleasePool corrupted pool in cache"];
}
return p;
}
return NSAllocateObject (self, 0, zone);
}
+ (id) new
{
static IMP allocImp = 0;
static IMP initImp = 0;
id arp;
if (0 == allocImp)
{
allocImp
= [NSAutoreleasePool methodForSelector: @selector(allocWithZone:)];
initImp
= [NSAutoreleasePool instanceMethodForSelector: @selector(init)];
}
arp = (*allocImp)(self, @selector(allocWithZone:), NSDefaultMallocZone());
return (*initImp)(arp, @selector(init));
}
#ifdef ARC_RUNTIME
- (id) init
{
_released = objc_autoreleasePoolPush();
{
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
unsigned level = 0;
_parent = tv->current_pool;
if (_parent)
{
NSAutoreleasePool *pool = _parent;
while (nil != pool)
{
level++;
pool = pool->_parent;
}
_parent->_child = self;
}
tv->current_pool = self;
if (level > pool_number_warning_threshold)
{
[NSException raise: NSGenericException
format: @"Too many (%u) autorelease pools ... leaking them?", level];
}
}
/* Catch the case where the receiver is a pool still in use (wrongly put in
the pool cache previously). */
NSCAssert(_child != self, @"Invalid child pool");
NSCAssert(_parent != self, @"Invalid parent pool");
return self;
}
- (unsigned) autoreleaseCountForObject: (id)anObject
{
return objc_arc_autorelease_count_for_object_np(anObject);
}
+ (unsigned) autoreleaseCountForObject: (id)anObject
{
return objc_arc_autorelease_count_for_object_np(anObject);
}
- (unsigned) autoreleaseCount
{
return objc_arc_autorelease_count_np();
}
+ (void) addObject: (id)anObj
{
if (autorelease_enabled)
objc_autorelease(anObj);
}
- (void) addObject: (id)anObj
{
if (autorelease_enabled)
objc_autorelease(anObj);
}
- (void) emptyPool
{
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
tv->current_pool = self;
objc_autoreleasePoolPop(_released);
}
/**
* Indicate to the runtime that we have an ARC-compatible implementation of
* NSAutoreleasePool and that it doesn't need to bother creating objects for
* pools.
*/
- (void)_ARCCompatibleAutoreleasePool {}
#else
- (id) init
{
if (!_released_head)
{
_addImp = (void (*)(id, SEL, id))
[self methodForSelector: @selector(addObject:)];
/* Allocate the array that will be the new head of the list of arrays. */
_released = (struct autorelease_array_list*)
NSZoneMalloc(NSDefaultMallocZone(),
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;
_released_count = 0;
}
else
/* Already initialized; (it came from autorelease_pool_cache);
we don't have to allocate new array list memory. */
{
_released = _released_head;
}
/* Install ourselves as the current pool.
* The only other place where the parent/child linked list is modified
* should be in -dealloc
*/
{
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
unsigned level = 0;
_parent = tv->current_pool;
if (_parent)
{
NSAutoreleasePool *pool = _parent;
while (nil != pool)
{
level++;
pool = pool->_parent;
}
_parent->_child = self;
}
tv->current_pool = self;
if (level > pool_number_warning_threshold)
{
[NSException raise: NSGenericException
format: @"Too many (%u) autorelease pools ... leaking them?", level];
}
}
return self;
}
- (unsigned) autoreleaseCount
{
unsigned count = 0;
struct autorelease_array_list *released = _released_head;
while (released != 0)
{
count += released->count;
released = released->next;
}
return count;
}
- (unsigned) autoreleaseCountForObject: (id)anObject
{
unsigned count = 0;
struct autorelease_array_list *released = _released_head;
unsigned int i;
while (released != 0)
{
for (i = 0; i < released->count; i++)
if (released->objects[i] == anObject)
count++;
released = released->next;
}
return count;
}
+ (unsigned) autoreleaseCountForObject: (id)anObject
{
unsigned count = 0;
NSAutoreleasePool *pool = ARP_THREAD_VARS->current_pool;
while (pool)
{
count += [pool autoreleaseCountForObject: anObject];
pool = pool->_parent;
}
return count;
}
+ (void) addObject: (id)anObj
{
NSThread *t = GSCurrentThread();
NSAutoreleasePool *pool;
NSAssert(nil != t, @"Creating autorelease pool on nonexistent thread!");
pool = t->_autorelease_vars.current_pool;
if (pool == nil && t->_active == NO)
{
// Don't leak while exiting thread.
pool = t->_autorelease_vars.current_pool = [self new];
}
if (pool != nil)
{
(*pool->_addImp)(pool, @selector(addObject:), anObj);
}
else
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
if (anObj != nil)
{
NSLog(@"autorelease called without pool for object (%p) "
@"of class %@ in thread %@", anObj,
NSStringFromClass([anObj class]), [NSThread currentThread]);
}
else
{
NSLog(@"autorelease called without pool for nil object.");
}
[arp drain];
}
}
- (void) addObject: (id)anObj
{
/* If the global, static variable AUTORELEASE_ENABLED is not set,
do nothing, just return. */
if (!autorelease_enabled)
return;
if (_released_count >= pool_count_warning_threshold)
{
[NSException raise: NSGenericException
format: @"AutoreleasePool count threshold exceeded."];
}
/* Get a new array for the list, if the current one is full. */
while (_released->count == _released->size)
{
if (_released->next)
{
/* There is an already-allocated one in the chain; use it. */
_released = _released->next;
}
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*)
NSZoneMalloc(NSDefaultMallocZone(),
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;
}
}
/* 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 in this pool */
_released_count++;
}
- (void) emptyPool
{
unsigned i;
Class classes[16];
IMP imps[16];
for (i = 0; i < 16; i++)
{
classes[i] = 0;
imps[i] = 0;
}
/*
* Loop throught the deallocation code repeatedly ... since we deallocate
* objects in the receiver while the receiver remains set as the current
* autorelease pool ... so if any object which is being deallocated adds
* any object to the current autorelease pool, we may need to release it
* again.
*/
while (_child != nil || _released_count > 0)
{
volatile struct autorelease_array_list *released;
/* If there are NSAutoreleasePool below us in the list 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.
* However, if a program has leaked pools we may be deallocating
* a pool with LOTS of children. To avoid stack overflow we
* therefore deallocate children starting with the oldest first.
*/
if (nil != _child)
{
NSAutoreleasePool *pool = _child;
/* Find other end of linked list ... oldest child.
*/
while (nil != pool->_child)
{
pool = pool->_child;
}
/* Deallocate the children in the list.
*/
while (pool != self)
{
pool = pool->_parent;
[pool->_child dealloc];
}
}
/* 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. */
released = _released_head;
while (released != 0)
{
id *objects = (id*)(released->objects);
for (i = 0; i < released->count; i++)
{
id anObject;
Class c;
unsigned hash;
anObject = objects[i];
objects[i] = nil;
if (anObject == nil)
{
fprintf(stderr,
"nil object encountered in autorelease pool\n");
continue;
}
c = object_getClass(anObject);
if (c == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"nul class for object in autorelease pool"];
}
hash = (((unsigned)(uintptr_t)c) >> 3) & 0x0f;
if (classes[hash] != c)
{
/* If anObject was an instance, c is it's class.
* If anObject was a class, c is its metaclass.
* Either way, we should get the appropriate pointer.
* If anObject is a proxy to something,
* the +instanceMethodForSelector: and -methodForSelector:
* methods may not exist, but this will return the
* address of the forwarding method if necessary.
*/
imps[hash]
= class_getMethodImplementation(c, @selector(release));
classes[hash] = c;
}
(imps[hash])(anObject, @selector(release));
}
_released_count -= released->count;
released->count = 0;
released = released->next;
}
}
}
#endif // ARC_RUNTIME
+ (id) currentPool
{
return ARP_THREAD_VARS->current_pool;
}
- (void) drain
{
// Don't call -release, make both -release and -drain have the same cost in
// non-GC mode.
[self dealloc];
}
- (id) retain
{
[NSException raise: NSGenericException
format: @"Don't call `-retain' on a NSAutoreleasePool"];
return self;
}
- (oneway void) release
{
[self dealloc];
}
- (void) dealloc
{
struct autorelease_thread_vars *tv = ARP_THREAD_VARS;
if (UINT_MAX == _released_count)
{
[NSException raise: NSInternalInconsistencyException
format: @"NSAutoreleasePool -dealloc of deallocated pool"];
}
[self emptyPool];
NSAssert(0 == _released_count, NSInternalInconsistencyException);
/* Remove self from the linked list of pools in use.
* We already know that we have deallocated any child (in -emptyPool),
* but we may have a parent which needs to know we have gone.
* The only other place where the parent/child linked list is modified
* should be in -init
*/
if (tv->current_pool == self)
{
tv->current_pool = _parent;
}
if (_parent != nil)
{
_parent->_child = nil;
_parent = nil;
}
/* Mark pool as cached so that any attempt to add an object to use it
* or to deallocate it again will raise an exception.
* We reset to zero when we get i out of the cache as a new allocation.
*/
_released_count = UINT_MAX;
/* Don't deallocate ourself, just save us for later use. */
push_pool_to_cache (tv, self);
GSNOSUPERDEALLOC;
}
- (void) _reallyDealloc
{
struct autorelease_array_list *a;
for (a = _released_head; a;)
{
void *n = a->next;
NSZoneFree(NSDefaultMallocZone(), a);
a = n;
}
_released = _released_head = 0;
[super dealloc];
}
- (id) autorelease
{
[NSException raise: NSGenericException
format: @"Don't call `-autorelease' on a NSAutoreleasePool"];
return self;
}
+ (void) _endThread: (NSThread*)thread
{
struct autorelease_thread_vars *tv;
NSAutoreleasePool *pool;
tv = &((thread)->_autorelease_vars);
/* First release any objects in the pool... bearing in mind that
* releasing any object could cause other objects to be added to
* the pool.
*/
pool = tv->current_pool;
while (pool)
{
[pool emptyPool];
pool = pool->_parent;
}
/* Now free the memory (we have finished usingthe pool).
*/
pool = tv->current_pool;
tv->current_pool = nil;
while (pool)
{
NSAutoreleasePool *p = pool->_parent;
[pool _reallyDealloc];
pool = p;
}
free_pool_cache(tv);
}
+ (void) enableRelease: (BOOL)enable
{
autorelease_enabled = enable;
}
+ (void) freeCache
{
free_pool_cache(ARP_THREAD_VARS);
}
+ (void) setPoolCountThreshold: (unsigned)c
{
if (c >= UINT_MAX) c = UINT_MAX - 1;
pool_count_warning_threshold = c;
}
+ (void) setPoolNumberThreshold: (unsigned)c
{
pool_number_warning_threshold = c;
}
@end
|