00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "memoryInfo.h"
00016
00017 #ifdef DO_MEMORY_USAGE
00018
00019 #include "typedReferenceCount.h"
00020 #include "typeHandle.h"
00021
00022
00023
00024
00025
00026
00027 MemoryInfo::
00028 MemoryInfo() {
00029 _void_ptr = (void *)NULL;
00030 _ref_ptr = (ReferenceCount *)NULL;
00031 _typed_ptr = (TypedObject *)NULL;
00032 _size = 0;
00033 _static_type = TypeHandle::none();
00034 _dynamic_type = TypeHandle::none();
00035
00036 _flags = 0;
00037 }
00038
00039
00040
00041
00042
00043
00044
00045 TypeHandle MemoryInfo::
00046 get_type() {
00047
00048
00049 if ((_flags & F_reconsider_dynamic_type) == 0) {
00050 if (_dynamic_type == TypeHandle::none()) {
00051 return _static_type;
00052 }
00053 return _dynamic_type;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 determine_dynamic_type();
00063
00064
00065 TypeHandle type = _static_type;
00066 update_type_handle(type, _dynamic_type);
00067
00068 if (type != _static_type) {
00069 if (express_cat.is_spam()) {
00070 express_cat.spam()
00071 << "Pointer " << get_void_ptr() << " has static type "
00072 << _static_type << " and dynamic type " << _dynamic_type << "\n";
00073 }
00074 }
00075
00076 return type;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 void MemoryInfo::
00086 determine_dynamic_type() {
00087 if ((_flags & F_reconsider_dynamic_type) != 0 &&
00088 _static_type != TypeHandle::none()) {
00089
00090
00091
00092 if (_typed_ptr == (TypedObject *)NULL) {
00093
00094
00095
00096 if (_static_type.is_derived_from(TypedReferenceCount::get_class_type())) {
00097 _typed_ptr = (TypedReferenceCount *)_ref_ptr;
00098 }
00099 }
00100
00101 if (_typed_ptr != (TypedObject *)NULL) {
00102
00103
00104
00105
00106 TypeHandle got_type = _typed_ptr->get_type();
00107
00108 if (got_type == TypeHandle::none()) {
00109 express_cat.warning()
00110 << "Found an unregistered type in a " << _static_type
00111 << " pointer:\n"
00112 << "Check derived types of " << _static_type
00113 << " and make sure that all are being initialized.\n";
00114 _dynamic_type = _static_type;
00115 _flags &= ~F_reconsider_dynamic_type;
00116
00117 if (ConfigVariableBool("raise-unregistered-type", false).get_value()) {
00118 nassert_raise("Unregistered type");
00119 }
00120 return;
00121 }
00122
00123 TypeHandle orig_type = _dynamic_type;
00124 update_type_handle(_dynamic_type, got_type);
00125 }
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 bool MemoryInfo::
00140 update_type_handle(TypeHandle &destination, TypeHandle refined) {
00141 if (refined == TypeHandle::none()) {
00142 express_cat.error()
00143 << "Attempt to update type of " << get_void_ptr()
00144 << "(type is " << get_type()
00145 << ") to an undefined type!\n";
00146
00147 } else if (destination == refined) {
00148
00149
00150 } else if (destination.is_derived_from(refined)) {
00151
00152
00153 } else if (destination == TypeHandle::none() ||
00154 refined.is_derived_from(destination)) {
00155
00156 if (express_cat.is_spam()) {
00157 express_cat.spam()
00158 << "Updating " << get_void_ptr() << " from type "
00159 << destination << " to type " << refined << "\n";
00160 }
00161 destination = refined;
00162
00163 } else {
00164
00165 express_cat.warning()
00166 << "Pointer " << get_void_ptr() << " previously indicated as type "
00167 << destination << " is now type " << refined << "!\n";
00168 return false;
00169 }
00170
00171 return true;
00172 }
00173
00174 #endif // DO_MEMORY_USAGE