00001 // Filename: memoryUsagePointers.cxx 00002 // Created by: drose (25May00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #ifdef DO_MEMORY_USAGE 00020 00021 #include "memoryUsagePointers.h" 00022 #include "config_express.h" 00023 #include "referenceCount.h" 00024 #include "typedReferenceCount.h" 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: MemoryUsagePointers::Constructor 00028 // Access: Public 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 MemoryUsagePointers:: 00032 MemoryUsagePointers() { 00033 } 00034 00035 //////////////////////////////////////////////////////////////////// 00036 // Function: MemoryUsagePointers::Destructor 00037 // Access: Public 00038 // Description: 00039 //////////////////////////////////////////////////////////////////// 00040 MemoryUsagePointers:: 00041 ~MemoryUsagePointers() { 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: MemoryUsagePointers::get_num_pointers 00046 // Access: Public 00047 // Description: Returns the number of pointers in the set. 00048 //////////////////////////////////////////////////////////////////// 00049 int MemoryUsagePointers:: 00050 get_num_pointers() const { 00051 return _entries.size(); 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: MemoryUsagePointers::get_pointer 00056 // Access: Public 00057 // Description: Returns the nth pointer of the set. 00058 //////////////////////////////////////////////////////////////////// 00059 ReferenceCount *MemoryUsagePointers:: 00060 get_pointer(int n) const { 00061 nassertr(n >= 0 && n < get_num_pointers(), NULL); 00062 return _entries[n]._ref_ptr; 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: MemoryUsagePointers::get_typed_pointer 00067 // Access: Public 00068 // Description: Returns the nth pointer of the set, typecast to a 00069 // TypedObject if possible. If the pointer is not a 00070 // TypedObject or if the cast cannot be made, returns 00071 // NULL. 00072 //////////////////////////////////////////////////////////////////// 00073 TypedObject *MemoryUsagePointers:: 00074 get_typed_pointer(int n) const { 00075 nassertr(n >= 0 && n < get_num_pointers(), NULL); 00076 TypedObject *typed_ptr = _entries[n]._typed_ptr; 00077 00078 if (typed_ptr != (TypedObject *)NULL) { 00079 return typed_ptr; 00080 } 00081 00082 ReferenceCount *ref_ptr = _entries[n]._ref_ptr; 00083 00084 TypeHandle type = _entries[n]._type; 00085 00086 // We can only cast-across to a TypedObject when we explicitly know 00087 // the inheritance path. Most of the time, this will be via 00088 // TypedReferenceCount. There are classes defined in other packages 00089 // that inherit from TypedObject and ReferenceCount separately (like 00090 // Node), but we can't do anything about that here without knowing 00091 // about the particular class. (Actually, we couldn't do anything 00092 // about Node anyway, because it inherits virtually from 00093 // ReferenceCount.) 00094 00095 // RTTI can't help us here, because ReferenceCount has no virtual 00096 // functions, so we can't use C++'s new dynamic_cast feature. 00097 00098 if (type != TypeHandle::none() && 00099 type.is_derived_from(TypedReferenceCount::get_class_type())) { 00100 return (TypedReferenceCount *)ref_ptr; 00101 } 00102 return NULL; 00103 } 00104 00105 //////////////////////////////////////////////////////////////////// 00106 // Function: MemoryUsagePointers::get_type 00107 // Access: Public 00108 // Description: Returns the actual type of the nth pointer, if it is 00109 // known. 00110 //////////////////////////////////////////////////////////////////// 00111 TypeHandle MemoryUsagePointers:: 00112 get_type(int n) const { 00113 nassertr(n >= 0 && n < get_num_pointers(), TypeHandle::none()); 00114 return _entries[n]._type; 00115 } 00116 00117 //////////////////////////////////////////////////////////////////// 00118 // Function: MemoryUsagePointers::get_type_name 00119 // Access: Public 00120 // Description: Returns the type name of the nth pointer, if it is 00121 // known. 00122 //////////////////////////////////////////////////////////////////// 00123 string MemoryUsagePointers:: 00124 get_type_name(int n) const { 00125 nassertr(n >= 0 && n < get_num_pointers(), ""); 00126 return get_type(n).get_name(); 00127 } 00128 00129 //////////////////////////////////////////////////////////////////// 00130 // Function: MemoryUsagePointers::get_age 00131 // Access: Public 00132 // Description: Returns the age of the nth pointer: the number of 00133 // seconds elapsed between the time it was allocated and 00134 // the time it was added to this set via a call to 00135 // MemoryUsage::get_pointers(). 00136 //////////////////////////////////////////////////////////////////// 00137 double MemoryUsagePointers:: 00138 get_age(int n) const { 00139 nassertr(n >= 0 && n < get_num_pointers(), 0.0); 00140 return _entries[n]._age; 00141 } 00142 00143 //////////////////////////////////////////////////////////////////// 00144 // Function: MemoryUsagePointers::clear 00145 // Access: Public 00146 // Description: Empties the set of pointers. 00147 //////////////////////////////////////////////////////////////////// 00148 void MemoryUsagePointers:: 00149 clear() { 00150 _entries.clear(); 00151 } 00152 00153 //////////////////////////////////////////////////////////////////// 00154 // Function: MemoryUsagePointers::clear 00155 // Access: Private 00156 // Description: Adds a new entry to the set. Intended to be called 00157 // only by MemoryUsage. 00158 //////////////////////////////////////////////////////////////////// 00159 void MemoryUsagePointers:: 00160 add_entry(ReferenceCount *ref_ptr, TypedObject *typed_ptr, 00161 TypeHandle type, double age) { 00162 // We can't safly add pointers with a zero reference count. They 00163 // might be statically-allocated or something, and if we try to add 00164 // them they'll try to destruct when the PointerTo later goes away. 00165 if (ref_ptr->get_ref_count() != 0) { 00166 _entries.push_back(Entry(ref_ptr, typed_ptr, type, age)); 00167 } 00168 } 00169 00170 00171 #endif // DO_MEMORY_USAGE