Panda3D
|
00001 // Filename: referenceCount.cxx 00002 // Created by: drose (23Oct98) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "referenceCount.h" 00016 #include "atomicAdjust.h" 00017 #include "mutexImpl.h" 00018 00019 TypeHandle ReferenceCount::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: ReferenceCount::do_test_ref_count_integrity 00023 // Access: Protected 00024 // Description: Does some easy checks to make sure that the reference 00025 // count isn't completely bogus. Returns true if ok, 00026 // false otherwise. 00027 //////////////////////////////////////////////////////////////////// 00028 bool ReferenceCount:: 00029 do_test_ref_count_integrity() const { 00030 nassertr(this != NULL, false); 00031 00032 // If this assertion fails, we're trying to delete an object that 00033 // was just deleted. Possibly you used a real pointer instead of a 00034 // PointerTo at some point, and the object was deleted when the 00035 // PointerTo went out of scope. Maybe you tried to create an 00036 // automatic (local variable) instance of a class that derives from 00037 // ReferenceCount. Or maybe your headers are out of sync, and you 00038 // need to make clean in direct or some higher tree. 00039 nassertr(_ref_count != deleted_ref_count, false); 00040 00041 // If this assertion fails, the reference counts are all screwed 00042 // up altogether. Maybe some errant code stomped all over memory 00043 // somewhere. 00044 nassertr(_ref_count >= 0, false); 00045 00046 return true; 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: ReferenceCount::do_test_ref_count_nonzero 00051 // Access: Protected 00052 // Description: Returns true if the reference count is nonzero, false 00053 // otherwise. 00054 //////////////////////////////////////////////////////////////////// 00055 bool ReferenceCount:: 00056 do_test_ref_count_nonzero() const { 00057 nassertr(do_test_ref_count_integrity(), false); 00058 nassertr(_ref_count > 0, false); 00059 00060 return true; 00061 } 00062 00063 //////////////////////////////////////////////////////////////////// 00064 // Function: ReferenceCount::create_weak_list 00065 // Access: Private 00066 // Description: Allocates a new WeakReferenceList structure and 00067 // stores it on the object. 00068 //////////////////////////////////////////////////////////////////// 00069 void ReferenceCount:: 00070 create_weak_list() { 00071 WeakReferenceList *weak_list = new WeakReferenceList; 00072 void *orig = 00073 AtomicAdjust::compare_and_exchange_ptr(_weak_list, NULL, weak_list); 00074 if (orig != (void *)NULL) { 00075 // Someone else created it first. 00076 delete weak_list; 00077 } 00078 } 00079