Panda3D
 All Classes Functions Variables Enumerations
referenceCount.cxx
1 // Filename: referenceCount.cxx
2 // Created by: drose (23Oct98)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "referenceCount.h"
16 #include "atomicAdjust.h"
17 #include "mutexImpl.h"
18 
19 TypeHandle ReferenceCount::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: ReferenceCount::do_test_ref_count_integrity
23 // Access: Protected
24 // Description: Does some easy checks to make sure that the reference
25 // count isn't completely bogus. Returns true if ok,
26 // false otherwise.
27 ////////////////////////////////////////////////////////////////////
28 bool ReferenceCount::
29 do_test_ref_count_integrity() const {
30  nassertr(this != NULL, false);
31 
32  // If this assertion fails, we're trying to delete an object that
33  // was just deleted. Possibly you used a real pointer instead of a
34  // PointerTo at some point, and the object was deleted when the
35  // PointerTo went out of scope. Maybe you tried to create an
36  // automatic (local variable) instance of a class that derives from
37  // ReferenceCount. Or maybe your headers are out of sync, and you
38  // need to make clean in direct or some higher tree.
39  nassertr(_ref_count != deleted_ref_count, false);
40 
41  // If this assertion fails, the reference counts are all screwed
42  // up altogether. Maybe some errant code stomped all over memory
43  // somewhere.
44  nassertr(_ref_count >= 0, false);
45 
46  return true;
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: ReferenceCount::do_test_ref_count_nonzero
51 // Access: Protected
52 // Description: Returns true if the reference count is nonzero, false
53 // otherwise.
54 ////////////////////////////////////////////////////////////////////
55 bool ReferenceCount::
56 do_test_ref_count_nonzero() const {
57  nassertr(do_test_ref_count_integrity(), false);
58  nassertr(_ref_count > 0, false);
59 
60  return true;
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: ReferenceCount::create_weak_list
65 // Access: Private
66 // Description: Allocates a new WeakReferenceList structure and
67 // stores it on the object.
68 ////////////////////////////////////////////////////////////////////
69 void ReferenceCount::
70 create_weak_list() {
71  WeakReferenceList *weak_list = new WeakReferenceList;
72  void *orig =
73  AtomicAdjust::compare_and_exchange_ptr(_weak_list, NULL, weak_list);
74  if (orig != (void *)NULL) {
75  // Someone else created it first.
76  delete weak_list;
77  }
78 }
79 
This is a list of WeakPointerTo's that share a reference to a given ReferenceCount object...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
static Pointer compare_and_exchange_ptr(Pointer &mem, Pointer old_value, Pointer new_value)
Atomic compare and exchange.