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
|
/* Implementation of garbage collecting classe framework
Copyright (C) 2002 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Inspired by gc classes of Ovidiu Predescu and Mircea Oancea
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
Library 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., 31 Milk Street #960789 Boston, MA 02196 USA.
AutogsdocSource: Additions/GCObject.m
AutogsdocSource: Additions/GCArray.m
AutogsdocSource: Additions/GCDictionary.m
*/
#import "common.h"
#ifndef NeXT_Foundation_LIBRARY
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSThread.h"
#endif
#import "GNUstepBase/GCObject.h"
#import "../GSPThread.h"
/*
* The head of a linked list of all garbage collecting objects is a
* special object which is never deallocated.
*/
@interface _GCObjectList : GCObject
@end
@implementation _GCObjectList
- (void) dealloc
{
GSNOSUPERDEALLOC;
}
@end
/**
* <p>The GCObject class is both the base class for all garbage collected
* objects, and an infrastructure for handling garbage collection.
* </p>
* <p>It maintains a list of all garbage collectable objects and provides
* a method to run a garbage collection pass on those objects.
* </p>
*/
@implementation GCObject
static GCObject *allObjects = nil;
static BOOL isCollecting = NO;
static gs_mutex_t *allocationLock = NULL;
+ (void) _becomeMultiThreaded: (NSNotification *)aNotification
{
if (allocationLock == NULL)
{
# ifndef NeXT_RUNTIME
allocationLock = malloc(sizeof(gs_mutex_t));
if (allocationLock == NULL)
{
abort();
}
GS_MUTEX_INIT(*allocationLock);
# endif
}
}
/**
* Allocates an instance of the class and links it into the list of
* all garbage collectable objects. Returns the new instance.<br />
*/
+ (id) allocWithZone: (NSZone*)zone
{
GCObject *o = [super allocWithZone: zone];
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
o->gc.next = allObjects;
o->gc.previous = allObjects->gc.previous;
allObjects->gc.previous->gc.next = o;
allObjects->gc.previous = o;
o->gc.flags.refCount = 1;
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
return o;
}
/**
* <p>This method runs a garbage collection, causing unreferenced objects to
* be deallocated. This is done using a simple three pass algorithm -
* </p>
* <deflist>
* <term>Pass 1</term>
* <desc>
* All the garbage collectable objects are sent a
* -gcDecrementRefCountOfContainedObjects message.
* </desc>
* <term>Pass 2</term>
* <desc>
* All objects having a refCount greater than 0 are sent an
* -gcIncrementRefCountOfContainedObjects message.
* </desc>
* <term>Pass 3</term>
* <desc>
* All the objects that still have the refCount of 0
* are part of cyclic graphs and none of the objects from this graph
* are held by some object outside graph. These objects receive the
* -dealloc message. In this method they should send the -dealloc message
* to any garbage collectable (GCObject and subclass) instances they
* contain.
* </desc>
* </deflist>
* <p>During garbage collection, the +gcIsCollecting method returns YES.
* </p>
*/
+ (void) gcCollectGarbage
{
GCObject *object;
GCObject *last;
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
if (isCollecting == YES)
{
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
return; // Don't allow recursion.
}
isCollecting = YES;
// Pass 1
object = allObjects->gc.next;
while (object != allObjects)
{
[object gcDecrementRefCountOfContainedObjects];
// object->gc.flags.visited = 0;
// object = object->gc.next;
[object gcSetVisited: NO];
object = [object gcNextObject];
}
// Pass 2
object = allObjects->gc.next;
while (object != allObjects)
{
if ([object retainCount] > 0)
{
[object gcIncrementRefCountOfContainedObjects];
}
// object = object->gc.next;
object = [object gcNextObject];
}
last = allObjects;
object = last->gc.next;
while (object != allObjects)
{
if ([object retainCount] == 0)
{
GCObject *next;
// next = object->gc.next;
// next->gc.previous = last;
// last->gc.next = next;
// object->gc.next = object;
// object->gc.previous = object;
next = [object gcNextObject];
[next gcSetPreviousObject: last];
[last gcSetNextObject: next];
[object gcSetNextObject: object];
[object gcSetPreviousObject: object];
[object dealloc];
object = next;
}
else
{
last = object;
// object = object->gc.next;
object = [object gcNextObject];
}
}
isCollecting = NO;
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
}
+ (void) initialize
{
if (self == [GCObject class])
{
allObjects = (_GCObjectList*)
NSAllocateObject([_GCObjectList class], 0, NSDefaultMallocZone());
[[NSObject leakAt: &allObjects] release];
allObjects->gc.next = allObjects;
allObjects->gc.previous = allObjects;
if ([NSThread isMultiThreaded] == YES)
{
[self _becomeMultiThreaded: nil];
}
else
{
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_becomeMultiThreaded:)
name: NSWillBecomeMultiThreadedNotification
object: nil];
}
}
}
/**
* Returns a flag to indicate whether a garbage collection is in progress.
*/
+ (BOOL) gcIsCollecting
{
return isCollecting;
}
/**
* Called to remove anObject from the list of garbage collectable objects.<br />
* This method is provided so that classes which are not subclasses of
* GCObject (but which have the same initial instance variable layout) can
* use multiple inheritance (behaviors) to act as GCObject instances, but
* can have their own -dealloc methods.<br />
* These classes should call this in their own -dealloc methods.
*/
+ (void) gcObjectWillBeDeallocated: (GCObject*)anObject
{
GCObject *p;
GCObject *n;
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
// p = anObject->gc.previous;
// n = anObject->gc.next;
// p->gc.next = n;
// n->gc.previous = p;
p = [anObject gcPreviousObject];
n = [anObject gcNextObject];
[p gcSetNextObject: n];
[n gcSetPreviousObject: p];
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
}
/**
* Copies the receiver (using the NSCopyObject() function) and links
* the resulting object into the list of all garbage collactable
* objects. Returns the newly created object.
*/
- (id) copyWithZone: (NSZone*)zone
{
GCObject *o = (GCObject*)NSCopyObject(self, 0, zone);
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
o->gc.next = allObjects;
o->gc.previous = allObjects->gc.previous;
allObjects->gc.previous->gc.next = o;
allObjects->gc.previous = o;
o->gc.flags.refCount = 1;
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
return o;
}
/**
* Removes the receiver from the list of garbage collectable objects and
* then calls the superclass implementation to complete deallocation of
* th receiver and freeing of the memory it uses.<br />
* Subclasses should call this at the end of their -dealloc methods as usual.
*/
- (void) dealloc
{
GCObject *p;
GCObject *n;
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
// p = anObject->gc.previous;
// n = anObject->gc.next;
// p->gc.next = n;
// n->gc.previous = p;
p = [self gcPreviousObject];
n = [self gcNextObject];
[p gcSetNextObject: n];
[n gcSetPreviousObject: p];
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
[super dealloc];
}
/**
* Decrements the garbage collection reference count for the receiver.<br />
*/
- (void) gcDecrementRefCount
{
/*
* No locking needed since this is only called when garbage collecting
* and the collection method handles locking.
*/
gc.flags.refCount--;
}
/**
* <p>Marks the receiver as not having been visited in the current garbage
* collection process (first pass of collection).
* </p>
* <p>All container subclasses should override this method to call the super
* implementation then decrement the ref counts of their contents as well as
* sending the -gcDecrementRefCountOfContainedObjects
* message to each of them.
* </p>
*/
- (void) gcDecrementRefCountOfContainedObjects
{
/*
* No locking needed since this is only called when garbage collecting
* and the collection method handles locking.
*/
gc.flags.visited = 0;
}
/**
* Increments the garbage collection reference count for the receiver.<br />
*/
- (void) gcIncrementRefCount
{
/*
* No locking needed since this is only called when garbage collecting
* and the collection method handles locking.
*/
gc.flags.refCount++;
}
/**
* <p>Checks to see if the receiver has already been visited in the
* current garbage collection process, and either marks the receiver as
* visited (and returns YES) or returns NO to indicate that it had already
* been visited.
* </p>
* <p>All container subclasses should override this method to call the super
* implementation then, if the method returns YES, increment the reference
* count of any contained objects and send the
* -gcIncrementRefCountOfContainedObjects
* to each of the contained objects too.
* </p>
*/
- (BOOL) gcIncrementRefCountOfContainedObjects
{
/*
* No locking needed since this is only called when garbage collecting
* and the collection method handles locking.
*/
if (gc.flags.visited == 1)
{
return NO;
}
gc.flags.visited = 1;
return YES;
}
/**
* Decrements the receivers reference count, and if zero, rmoveis it
* from the list of garbage collectable objects and deallocates it.
*/
- (oneway void) release
{
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
if (gc.flags.refCount > 0 && gc.flags.refCount-- == 1)
{
[GCObject gcObjectWillBeDeallocated: self];
[self dealloc];
}
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
}
/**
* Increments the receivers reference count and returns the receiver.
*/
- (id) retain
{
if (allocationLock != 0)
{
GS_MUTEX_LOCK(*allocationLock);
}
gc.flags.refCount++;
if (allocationLock != 0)
{
GS_MUTEX_UNLOCK(*allocationLock);
}
return self;
}
/**
* Returns the receivers reference count.
*/
- (NSUInteger) retainCount
{
return gc.flags.refCount;
}
@end
/**
* This category implements accessor methods for the instance variables
* used for garbage collecting. If/when we can ensure that all garbage
* collecting classes use the same initial ivar layout, we can remove
* these methods and the garbage collector can access the ivars directly,
* making a pretty big performance improvement during collecting.<br />
* NB. These methods must *only* be used by the garbage collecting process
* or in methods called from the garbage collector. Anything else is not
* thread-safe.
*/
@implementation GCObject (Extra)
- (BOOL) gcAlreadyVisited
{
if (gc.flags.visited == 1)
{
return YES;
}
else
{
return NO;
}
}
- (GCObject*) gcNextObject
{
return gc.next;
}
- (GCObject*) gcPreviousObject
{
return gc.previous;
}
- (GCObject*) gcSetNextObject: (GCObject*)anObject
{
gc.next = anObject;
return self;
}
- (GCObject*) gcSetPreviousObject: (GCObject*)anObject
{
gc.previous = anObject;
return self;
}
- (void) gcSetVisited: (BOOL)flag
{
if (flag == YES)
{
gc.flags.visited = 1;
}
else
{
gc.flags.visited = 0;
}
}
@end
|