Panda3D
memoryUsagePointers.cxx
1 // Filename: memoryUsagePointers.cxx
2 // Created by: drose (25May00)
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 "memoryUsagePointers.h"
16 
17 #ifdef DO_MEMORY_USAGE
18 
19 #include "config_express.h"
20 #include "referenceCount.h"
21 #include "typedReferenceCount.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: MemoryUsagePointers::Constructor
25 // Access: Published
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 MemoryUsagePointers::
29 MemoryUsagePointers() {
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: MemoryUsagePointers::Destructor
34 // Access: Published
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 MemoryUsagePointers::
38 ~MemoryUsagePointers() {
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: MemoryUsagePointers::get_num_pointers
43 // Access: Published
44 // Description: Returns the number of pointers in the set.
45 ////////////////////////////////////////////////////////////////////
46 int MemoryUsagePointers::
47 get_num_pointers() const {
48  return _entries.size();
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: MemoryUsagePointers::get_pointer
53 // Access: Published
54 // Description: Returns the nth pointer of the set.
55 ////////////////////////////////////////////////////////////////////
56 ReferenceCount *MemoryUsagePointers::
57 get_pointer(int n) const {
58  nassertr(n >= 0 && n < get_num_pointers(), NULL);
59  return _entries[n]._ref_ptr;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: MemoryUsagePointers::get_typed_pointer
64 // Access: Published
65 // Description: Returns the nth pointer of the set, typecast to a
66 // TypedObject if possible. If the pointer is not a
67 // TypedObject or if the cast cannot be made, returns
68 // NULL.
69 ////////////////////////////////////////////////////////////////////
70 TypedObject *MemoryUsagePointers::
71 get_typed_pointer(int n) const {
72  nassertr(n >= 0 && n < get_num_pointers(), NULL);
73  TypedObject *typed_ptr = _entries[n]._typed_ptr;
74 
75  if (typed_ptr != (TypedObject *)NULL) {
76  return typed_ptr;
77  }
78 
79  ReferenceCount *ref_ptr = _entries[n]._ref_ptr;
80 
81  TypeHandle type = _entries[n]._type;
82 
83  // We can only cast-across to a TypedObject when we explicitly know
84  // the inheritance path. Most of the time, this will be via
85  // TypedReferenceCount. There are classes defined in other packages
86  // that inherit from TypedObject and ReferenceCount separately (like
87  // Node), but we can't do anything about that here without knowing
88  // about the particular class. (Actually, we couldn't do anything
89  // about Node anyway, because it inherits virtually from
90  // ReferenceCount.)
91 
92  // RTTI can't help us here, because ReferenceCount has no virtual
93  // functions, so we can't use C++'s new dynamic_cast feature.
94 
95  if (type != TypeHandle::none() &&
96  type.is_derived_from(TypedReferenceCount::get_class_type())) {
97  return (TypedReferenceCount *)ref_ptr;
98  }
99  return NULL;
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: MemoryUsagePointers::get_type
104 // Access: Published
105 // Description: Returns the actual type of the nth pointer, if it is
106 // known.
107 ////////////////////////////////////////////////////////////////////
108 TypeHandle MemoryUsagePointers::
109 get_type(int n) const {
110  nassertr(n >= 0 && n < get_num_pointers(), TypeHandle::none());
111  return _entries[n]._type;
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: MemoryUsagePointers::get_type_name
116 // Access: Published
117 // Description: Returns the type name of the nth pointer, if it is
118 // known.
119 ////////////////////////////////////////////////////////////////////
120 string MemoryUsagePointers::
121 get_type_name(int n) const {
122  nassertr(n >= 0 && n < get_num_pointers(), "");
123  return get_type(n).get_name();
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: MemoryUsagePointers::get_age
128 // Access: Published
129 // Description: Returns the age of the nth pointer: the number of
130 // seconds elapsed between the time it was allocated and
131 // the time it was added to this set via a call to
132 // MemoryUsage::get_pointers().
133 ////////////////////////////////////////////////////////////////////
134 double MemoryUsagePointers::
135 get_age(int n) const {
136  nassertr(n >= 0 && n < get_num_pointers(), 0.0);
137  return _entries[n]._age;
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: MemoryUsagePointers::clear
142 // Access: Published
143 // Description: Empties the set of pointers.
144 ////////////////////////////////////////////////////////////////////
145 void MemoryUsagePointers::
146 clear() {
147  _entries.clear();
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: MemoryUsagePointers::output
152 // Access: Published
153 // Description:
154 ////////////////////////////////////////////////////////////////////
155 void MemoryUsagePointers::
156 output(ostream &out) const {
157  out << _entries.size() << " pointers.";
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: MemoryUsagePointers::clear
162 // Access: Private
163 // Description: Adds a new entry to the set. Intended to be called
164 // only by MemoryUsage.
165 ////////////////////////////////////////////////////////////////////
166 void MemoryUsagePointers::
167 add_entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr,
168  TypeHandle type, double age) {
169  // We can't safely add pointers with a zero reference count. They
170  // might be statically-allocated or something, and if we try to add
171  // them they'll try to destruct when the PointerTo later goes away.
172  if (ref_ptr->get_ref_count() != 0) {
173  _entries.push_back(Entry(ref_ptr, typed_ptr, type, age));
174  }
175 }
176 
177 
178 #endif // DO_MEMORY_USAGE
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
bool is_derived_from(TypeHandle parent, TypedObject *object=(TypedObject *) NULL) const
Returns true if this type is derived from the indicated type, false otherwise.
Definition: typeHandle.I:152
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
int get_ref_count() const
Returns the current reference count.
A base class for all things that want to be reference-counted.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85