DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (fbecf6c8a86f)

VCS Links

Line Code
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
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [Open Source Virtual Machine.].
 *
 * The Initial Developer of the Original Code is
 * Adobe System Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2004-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adobe AS3 Team
 *   leon.sha@sun.com
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#ifndef __GCObject__
#define __GCObject__

#ifdef __GNUC__
	#define GNUC_ONLY(x) x
#else
	#define GNUC_ONLY(x)
#endif

// VC++ wants these declared
//void *operator new(size_t size);
//void *operator new[] (size_t size);

// Sun Studio doesn't support default parameters for operator new, so break up as two functions
REALLY_INLINE void *operator new(size_t size, MMgc::GC *gc)
{
	return gc->AllocPtrZero(size);
}

REALLY_INLINE void *operator new(size_t size, MMgc::GC *gc, int flags)
{
	return gc->Alloc(size, flags);
}

namespace MMgc
{
	/**
	 * Baseclass for GC managed objects that aren't finalized
	 */
	class GCObject
	{
	public:
		// 'throw()' annotation to avoid GCC warning: 'operator new' must not return NULL unless it is declared 'throw()' (or -fcheck-new is in effect)
		static void *operator new(size_t size, GC *gc, size_t extra) GNUC_ONLY(throw());

		static void *operator new(size_t size, GC *gc) GNUC_ONLY(throw());
		
		static void operator delete(void *gcObject);
		
		GCWeakRef *GetWeakRef() const;
	};

	/**
	 *	Baseclass for GC managed objects that are finalized 
	 */
	class GCFinalizedObject 
	//: public GCObject can't do this, get weird compile errors in AVM plus, I think it has to do with
	// the most base class (GCObject) not having any virtual methods)
	{
	public:
		GCWeakRef *GetWeakRef() const;
		
		virtual ~GCFinalizedObject();
		static void* operator new(size_t size, GC *gc, size_t extra);
		static void* operator new(size_t size, GC *gc);
		static void operator delete (void *gcObject);
	};

	REALLY_INLINE void *GCObject::operator new(size_t size, GC *gc, size_t extra) GNUC_ONLY(throw())
	{
		return gc->AllocExtraPtrZero(size, extra);
	}
	
	REALLY_INLINE void *GCObject::operator new(size_t size, GC *gc) GNUC_ONLY(throw())
	{
		return gc->AllocPtrZero(size);
	}
	
	REALLY_INLINE void GCObject::operator delete(void *gcObject)
	{
		GC::GetGC(gcObject)->FreeNotNull(gcObject);
	}
	
	REALLY_INLINE GCWeakRef* GCObject::GetWeakRef() const
	{
		return GC::GetWeakRef(this);
	}

	REALLY_INLINE GCFinalizedObject::~GCFinalizedObject()
	{
		// Nothing
	}

	REALLY_INLINE GCWeakRef* GCFinalizedObject::GetWeakRef() const
	{
		return GC::GetWeakRef(this);
	}
	
	REALLY_INLINE void* GCFinalizedObject::operator new(size_t size, GC *gc, size_t extra)
	{
		return gc->AllocExtraPtrZeroFinalized(size, extra);
	}
	
	REALLY_INLINE void* GCFinalizedObject::operator new(size_t size, GC *gc)
	{
		return gc->AllocPtrZeroFinalized(size);
	}
	
	REALLY_INLINE void GCFinalizedObject::operator delete (void *gcObject)
	{
		GC::GetGC(gcObject)->FreeNotNull(gcObject);
	}		
	
	/**
	 * Base class for reference counted objects.
	 *
	 * This object always has a finalizer (the C++ destructor).  The C++ destructor /must/
	 * leave the object with all-fields-zero.
	 *
	 * Reference counting is deferred: when an object's reference count drops to zero it
	 * is inserted into the zero-count table (the ZCT), see ZCT.h etc.  If the reference 
	 * count grows above zero the object is removed from the ZCT again.  Every new object
	 * is added to the ZCT initially - reference counts start at zero.
	 *
	 * Occasionally the ZCT is reaped: objects in the ZCT that are not referenced from stack
	 * memory or special ZCT roots, and that are not explicitly pinned by client code, are
	 * deleted by calling their finalizers and reclaiming their memory.   A finalizer may
	 * make the reference counts of more referenced objects drop to zero, whereupon they
	 * too are entered into the ZCT (and may be deleted by the ongoing reap, or a later
	 * reap).
	 *
	 * (Under complicated scenarios it is possible for an object allocated during reaping
	 * to be erroneously deleted, see https://bugzilla.mozilla.org/show_bug.cgi?id=506644,
	 * so client code may want to take note of that.)
	 */

	class RCObject : public GCFinalizedObject
	{
		friend class GC;
		friend class ZCT;
	public:
		REALLY_INLINE static void *operator new(size_t size, GC *gc, size_t extra)
		{
			return gc->AllocExtraRCObject(size, extra);
		}
		
		REALLY_INLINE static void *operator new(size_t size, GC *gc)
		{
			return gc->AllocRCObject(size);
		}
		
		REALLY_INLINE RCObject()
		{
			// composite == 0 is special, it means a deleted object in Release builds
			// b/c RCObjects have a vtable we "know" composite isn't the first word and thus
			// won't be trampled by the freelist
			composite = 1;
			GC::GetGC(this)->AddToZCT(this REFCOUNT_PROFILING_ARG(true));
		}

		REALLY_INLINE ~RCObject()
		{
			// for explicit deletion
			if (InZCT())
				GC::GetGC(this)->RemoveFromZCT(this REFCOUNT_PROFILING_ARG(true));
			composite = 0;
		}

		/**
		 * @return true if the object is currently pinned (explicitly or by
		 *         the ZCT's stack pinner).
		 */
		REALLY_INLINE bool IsPinned()
		{
			return (composite & STACK_PIN) != 0;
		}

		/**
		 * Explicitly pin the object, protecting it from ZCT reaping.  The pin
		 * flag /will/ be cleared if the object is subsequently added to the
		 * ZCT and reaping is not ongoing.  It is not advised to call Pin()
		 * except from prereap() callback handlers.
		 */
		void Pin()
		{
#ifdef _DEBUG
			// this is a deleted object but 0xca indicates the InZCT flag so we
			// might erroneously get here for a deleted RCObject
			if(composite == 0xcacacaca || composite == 0xbabababa)
				return;
#endif

			// In Release builds, a deleted object is indicated by
			// composite == 0.  We must not set the STACK_PIN bit
			// on a deleted object, because if we do, it transforms
			// from a deleted object into a zero-ref count live object,
			// causing nasty crashes down the line.
			if (composite == 0)
				return;
			
			composite |= STACK_PIN;
		}

		/**
		 * Explicitly unpin the object, allowing it to be reaped by the ZCT.
		 * It is not advised to unpin objects that weren't pinned explicitly
		 * by a call to Pin(), and calls to Unpin() should come from postreap()
		 * callback handlers.
		 */
		void Unpin()
		{
			composite &= ~STACK_PIN;
		}

		/**
		 * @return the object's current reference count.  The value is not
		 * valid unless Sticky() returns 0.
		 */
		REALLY_INLINE uint32_t RefCount() const
		{
			return (composite & RCBITS) - 1;
		}
		
		/**
		 * @return non-zero if the object is sticky (RC operations have no effect because
		 *         the RC field is invalid, either as a consequence of an RC overflow or
		 *         because the sticky bit was set explicitly).
		 */
		REALLY_INLINE uint32_t Sticky() const
		{
			return composite & STICKYFLAG;
		}
		
		/**
		 * Make RC operations on the object be no-ops.
		 */
		REALLY_INLINE void Stick()
		{ 
			if (InZCT())
				GC::GetGC(this)->RemoveFromZCT(this REFCOUNT_PROFILING_ARG(true));
			composite |= STICKYFLAG;
		}

		/**
		 * Increment the object's reference count.
		 *
		 * OPTIMIZEME: this is too expensive and too large.  It should be
		 * possible to do better if we play around with the positive/negative
		 * boundary (eg, operations that don't cross that boundary are
		 * cheap, and those that do aren't), but the special-casing of
		 * composite==0 and invariants throughtout the system relying on
		 * composite==0 make it hard.  We need profiling data on the breakdown
		 * of the frequency of various paths through this function.
		 */
		REALLY_INLINE void IncrementRef() 
		{
			REFCOUNT_PROFILING_ONLY( GC::GetGC(this)->policy.signalIncrementRef(); )
			if(Sticky() || composite == 0)
				return;
#ifdef _DEBUG
			GC* gc = GC::GetGC(this);
			GCAssert(gc->IsRCObject(this));
			GCAssert(this == gc->FindBeginningGuarded(this));
			// don't touch swept objects
			if(composite == 0xcacacaca || composite == 0xbabababa)
				return;
#endif

			composite++;
			if((composite&RCBITS) == RCBITS) {
				composite |= STICKYFLAG;
			} else if(InZCT()) {
				GCAssert(RefCount() == 1);
				GC::GetGC(this)->RemoveFromZCT(this);
			}
			
#ifdef MMGC_RC_HISTORY
			if(GC::GetGC(this)->keepDRCHistory)
				history.Add(GCHeap::GetGCHeap()->GetProfiler()->GetStackTrace());
#endif
		}

		/**
		 * Decrement the object's reference count.
		 *
		 * OPTIMIZEME: this is too expensive and too large.  It should be
		 * possible to do better if we play around with the positive/negative
		 * boundary (eg, operations that don't cross that boundary are
		 * cheap, and those that do aren't), but the special-casing of
		 * composite==0 and invariants throughtout the system relying on
		 * composite==0 make it hard.  We need profiling data on the breakdown
		 * of the frequency of various paths through this function.
		 */
		REALLY_INLINE void DecrementRef() 
		{ 
			REFCOUNT_PROFILING_ONLY( GC::GetGC(this)->policy.signalDecrementRef(); )
			if(Sticky() || composite == 0)
				return;

#ifdef _DEBUG
			GC* gc = GC::GetGC(this);
			GCAssert(gc->IsRCObject(this));
			GCAssert(this == gc->FindBeginningGuarded(this));
			// don't touch swept objects
			if(composite == 0xcacacaca || composite == 0xbabababa)
				return;
		
			if(gc->Destroying())
				return;

			if(RefCount() == 0) {
#ifdef MMGC_RC_HISTORY
				DumpHistory();
#endif
				GCAssert(false);
			}
#endif

			if (RefCount() == 0)
			{
				// This is a defensive measure.  If DecrementRef is
				// ever called on a zero ref-count object, composite--
				// will cause an underflow, flipping all kinds of bits
				// in bad ways and resulting in a crash later.  Often,
				// such a DecrementRef bug would be caught by the
				// _DEBUG asserts above, but sometimes we have
				// release-only crashers like this.  Better to fail
				// gracefully at the point of failure, rather than
				// push the failure to some later point.
				return;
			}
			
			composite--; 

#ifdef MMGC_RC_HISTORY
			if(GC::GetGC(this)->keepDRCHistory)
				history.Add(GCHeap::GetGCHeap()->GetProfiler()->GetStackTrace());
#endif
			// ??? unclear what the following comment pertains to -- lars, 2009-06-09
			
			// the delete flag works around the fact that DecrementRef
			// may be called after ~RCObject since all dtors are called
			// in one pass.  For example a FunctionScriptObject may be
			// the sole reference to a ScopeChain and dec its ref in 
			// ~FunctionScriptObject during a sweep, but since ScopeChain's
			// are smaller the ScopeChain was already finalized, thus the
			// push crashes b/c the history object has been destructed.
			
			// composite == 1 is the same as (rc == 1 && !notSticky && !notInZCT)
			if(RefCount() == 0) {
				GC::GetGC(this)->AddToZCT(this);
			}
		}
		
#ifdef MMGC_RC_HISTORY
		void DumpHistory();
#endif

	private:

		// @return non-zero if the object is in the ZCT
		uint32_t InZCT() const
		{
			return composite & ZCTFLAG;
		}
		
		// Clear the ZCT flag and the ZCT index
		void ClearZCTFlag() 
		{
			composite &= ~(ZCTFLAG|ZCT_INDEX);
		}

		// @return the ZCT index.  This is only valid if InZCT returns non-zero.
		uint32_t getZCTIndex() const
		{
			return (composite & ZCT_INDEX) >> 8;
		}
		
		// Set the ZCT index and the ZCT flag and clear the pinned flag.
		void setZCTIndexAndUnpin(uint32_t index) 
		{
			GCAssert(index <= (ZCT_INDEX>>8));
			composite = (composite&~(ZCT_INDEX|STACK_PIN)) | ((index<<8)|ZCTFLAG);
		}

		// Set the ZCT index and the ZCT flag.  If reaping==0 then clear the pinned flag, 
		// otherwise preserve the pinned flag.
		void setZCTIndexAndMaybeUnpin(uint32_t index, uint32_t reaping)
		{
			GCAssert(reaping == 0 || reaping == 1);
			GCAssert(index <= (ZCT_INDEX>>8));
			composite = (composite&~(ZCT_INDEX|((~reaping&1)<<STACK_PIN_SHIFT))) | ((index<<8)|ZCTFLAG);
		}

		// Fields in 'composite'
		static const uint32_t ZCTFLAG	         = 0x80000000;			// The object is in the ZCT
		static const uint32_t STICKYFLAG         = 0x40000000;			// The object is sticky (RC overflow)
		static const uint32_t STACK_PIN          = 0x20000000;			// The object has been pinned
		static const uint32_t STACK_PIN_SHIFT    = 29;
		static const uint32_t RCBITS	         = 0x000000FF;			// 8 bits for the reference count
		static const uint32_t ZCT_INDEX          = 0x0FFFFF00;			// 20 bits for the ZCT index
		static const uint32_t ZCT_CAPACITY       = (ZCT_INDEX>>8) + 1;
		
		uint32_t composite;
#ifdef MMGC_RC_HISTORY
		// addref/decref stack traces
		BasicList<StackTrace*> history;
#endif // MMGC_MEMORY_INFO
	};

	template<class T> 
	class ZeroPtr
	{
	public:
		ZeroPtr() { t = NULL; }
		ZeroPtr(T _t) : t(_t) { }
		~ZeroPtr() 
		{
			t = NULL;
		}
		
		operator T() { return t; }
		bool operator!=(T other) { return other != t; }
		T operator->() const { return t; }
	private:
		T t;
	};

	template<class T> 
	class RCPtr
	{
	public:
		RCPtr() { t = NULL; }
		RCPtr(T _t) : t(_t) 
		{ 
			if(valid()) 
				t->IncrementRef(); 
		}
		~RCPtr() 
		{
			if(valid())
				t->DecrementRef();

			// 02may06 grandma : I want to enable 
			//	class DataIOBase { DRC(PlayerToplevel *) const m_toplevel; }
			//
			// DataIOBase is a virtual base class, so we don't know if the
			// subclass is GCObject or not. We need it to be const, or
			// a GCObject would require a DWB(), and if it's const, we
			// cannot zero it out during ~DataIOBase. The simplest solution
			// seemed to be zeroing out the member here.

			t = NULL; 
		}

		T operator=(T tNew)
		{
			if(valid())
				t->DecrementRef();
			t = tNew;
			if(valid())
				t->IncrementRef();
			// this cast is safe b/c other wise compilation would fail
			return (T) t;
		}

		RCPtr<T>& operator=(const RCPtr<T>& other)
		{
			if(valid())
				t->DecrementRef();
			t = other.t;
			if(valid())
				t->IncrementRef();
			return *this;
		}
		
		operator T() const
		{
			return (T) t;
		}

		operator ZeroPtr<T>() const { return t; }

		bool operator!=(T other) { return other != t; }

		T operator->() const
		{
			return (T) t;
		}

		void Clear() { t = NULL; }

	private:
		// Hidden and meant not to be used at all.
		RCPtr(const RCPtr<T>& other);

		inline bool valid() { return (uintptr_t)t > 1; }
		T t;
	};


// put spaces around the template arg to avoid possible digraph warnings
#define DRC(_type) MMgc::RCPtr< _type >

#undef GNUC_ONLY

}


#endif /* __GCObject__ */