Panda3D

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