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