Panda3D
|
00001 // Filename: typeHandle.cxx 00002 // Created by: drose (23Oct98) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "typeHandle.h" 00016 #include "typeRegistryNode.h" 00017 #include "atomicAdjust.h" 00018 00019 // This is initialized to zero by static initialization. 00020 TypeHandle TypeHandle::_none; 00021 00022 #ifdef HAVE_PYTHON 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: TypeHandle::make 00025 // Access: Published 00026 // Description: This special method allows coercion to a TypeHandle 00027 // from a Python class object or instance. It simply 00028 // attempts to call classobj.getClassType(), and returns 00029 // that value (or raises an exception if that method 00030 // doesn't work). 00031 // 00032 // This method allows a Python class object to be used 00033 // anywhere a TypeHandle is expected by the C++ 00034 // interface. 00035 //////////////////////////////////////////////////////////////////// 00036 PyObject *TypeHandle:: 00037 make(PyObject *classobj) { 00038 return PyObject_CallMethod(classobj, (char *)"getClassType", (char *)""); 00039 } 00040 #endif // HAVE_PYTHON 00041 00042 #ifdef DO_MEMORY_USAGE 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: TypeHandle::get_memory_usage 00045 // Access: Published 00046 // Description: Returns the total allocated memory used by objects of 00047 // this type, for the indicated memory class. This is 00048 // only updated if track-memory-usage is set true in 00049 // your Config.prc file. 00050 //////////////////////////////////////////////////////////////////// 00051 int TypeHandle:: 00052 get_memory_usage(MemoryClass memory_class) const { 00053 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit); 00054 if ((*this) == TypeHandle::none()) { 00055 return 0; 00056 } else { 00057 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL); 00058 assert(rnode != (TypeRegistryNode *)NULL); 00059 return (size_t)AtomicAdjust::get(rnode->_memory_usage[memory_class]); 00060 } 00061 } 00062 #endif // DO_MEMORY_USAGE 00063 00064 #ifdef DO_MEMORY_USAGE 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: TypeHandle::inc_memory_usage 00067 // Access: Published 00068 // Description: Adds the indicated amount to the record for the total 00069 // allocated memory for objects of this type. 00070 //////////////////////////////////////////////////////////////////// 00071 void TypeHandle:: 00072 inc_memory_usage(MemoryClass memory_class, int size) { 00073 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit); 00074 if ((*this) != TypeHandle::none()) { 00075 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL); 00076 assert(rnode != (TypeRegistryNode *)NULL); 00077 AtomicAdjust::add(rnode->_memory_usage[memory_class], (AtomicAdjust::Integer)size); 00078 //cerr << *this << ".inc(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n"; 00079 assert(rnode->_memory_usage[memory_class] >= 0); 00080 } 00081 } 00082 #endif // DO_MEMORY_USAGE 00083 00084 #ifdef DO_MEMORY_USAGE 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: TypeHandle::dec_memory_usage 00087 // Access: Published 00088 // Description: Subtracts the indicated amount from the record for 00089 // the total allocated memory for objects of this type. 00090 //////////////////////////////////////////////////////////////////// 00091 void TypeHandle:: 00092 dec_memory_usage(MemoryClass memory_class, int size) { 00093 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit); 00094 if ((*this) != TypeHandle::none()) { 00095 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL); 00096 assert(rnode != (TypeRegistryNode *)NULL); 00097 AtomicAdjust::add(rnode->_memory_usage[memory_class], -(AtomicAdjust::Integer)size); 00098 //cerr << *this << ".dec(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n"; 00099 assert(rnode->_memory_usage[memory_class] >= 0); 00100 } 00101 } 00102 #endif // DO_MEMORY_USAGE 00103 00104 ostream & 00105 operator << (ostream &out, TypeHandle::MemoryClass mem_class) { 00106 switch (mem_class) { 00107 case TypeHandle::MC_singleton: 00108 return out << "singleton"; 00109 00110 case TypeHandle::MC_array: 00111 return out << "array"; 00112 00113 case TypeHandle::MC_deleted_chain_active: 00114 return out << "deleted_chain_active"; 00115 00116 case TypeHandle::MC_deleted_chain_inactive: 00117 return out << "deleted_chain_inactive"; 00118 00119 case TypeHandle::MC_limit: 00120 return out << "limit"; 00121 } 00122 00123 return out 00124 << "**invalid TypeHandle::MemoryClass (" << (int)mem_class 00125 << ")**\n"; 00126 }