Panda3D
typeHandle.cxx
1 // Filename: typeHandle.cxx
2 // Created by: drose (23Oct98)
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 "typeHandle.h"
16 #include "typeRegistryNode.h"
17 #include "atomicAdjust.h"
18 
19 // This is initialized to zero by static initialization.
20 TypeHandle TypeHandle::_none;
21 
22 #ifdef DO_MEMORY_USAGE
23 ////////////////////////////////////////////////////////////////////
24 // Function: TypeHandle::get_memory_usage
25 // Access: Published
26 // Description: Returns the total allocated memory used by objects of
27 // this type, for the indicated memory class. This is
28 // only updated if track-memory-usage is set true in
29 // your Config.prc file.
30 ////////////////////////////////////////////////////////////////////
31 int TypeHandle::
32 get_memory_usage(MemoryClass memory_class) const {
33  assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
34  if ((*this) == TypeHandle::none()) {
35  return 0;
36  } else {
37  TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
38  assert(rnode != (TypeRegistryNode *)NULL);
39  return (size_t)AtomicAdjust::get(rnode->_memory_usage[memory_class]);
40  }
41 }
42 #endif // DO_MEMORY_USAGE
43 
44 #ifdef DO_MEMORY_USAGE
45 ////////////////////////////////////////////////////////////////////
46 // Function: TypeHandle::inc_memory_usage
47 // Access: Published
48 // Description: Adds the indicated amount to the record for the total
49 // allocated memory for objects of this type.
50 ////////////////////////////////////////////////////////////////////
51 void TypeHandle::
52 inc_memory_usage(MemoryClass memory_class, int size) {
53  assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
54  if ((*this) != TypeHandle::none()) {
55  TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
56  assert(rnode != (TypeRegistryNode *)NULL);
57  AtomicAdjust::add(rnode->_memory_usage[memory_class], (AtomicAdjust::Integer)size);
58  //cerr << *this << ".inc(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
59  if (rnode->_memory_usage[memory_class] < 0) {
60  cerr << "Memory usage overflow for type " << *this << ".\n";
61  abort();
62  }
63  }
64 }
65 #endif // DO_MEMORY_USAGE
66 
67 #ifdef DO_MEMORY_USAGE
68 ////////////////////////////////////////////////////////////////////
69 // Function: TypeHandle::dec_memory_usage
70 // Access: Published
71 // Description: Subtracts the indicated amount from the record for
72 // the total allocated memory for objects of this type.
73 ////////////////////////////////////////////////////////////////////
74 void TypeHandle::
75 dec_memory_usage(MemoryClass memory_class, int size) {
76  assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
77  if ((*this) != TypeHandle::none()) {
78  TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
79  assert(rnode != (TypeRegistryNode *)NULL);
80  AtomicAdjust::add(rnode->_memory_usage[memory_class], -(AtomicAdjust::Integer)size);
81  //cerr << *this << ".dec(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
82  assert(rnode->_memory_usage[memory_class] >= 0);
83  }
84 }
85 #endif // DO_MEMORY_USAGE
86 
87 ostream &
88 operator << (ostream &out, TypeHandle::MemoryClass mem_class) {
89  switch (mem_class) {
90  case TypeHandle::MC_singleton:
91  return out << "singleton";
92 
93  case TypeHandle::MC_array:
94  return out << "array";
95 
96  case TypeHandle::MC_deleted_chain_active:
97  return out << "deleted_chain_active";
98 
99  case TypeHandle::MC_deleted_chain_inactive:
100  return out << "deleted_chain_inactive";
101 
102  case TypeHandle::MC_limit:
103  return out << "limit";
104  }
105 
106  return out
107  << "**invalid TypeHandle::MemoryClass (" << (int)mem_class
108  << ")**\n";
109 }
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
static void add(Integer &var, Integer delta)
Atomically computes var += delta.
This is a single entry in the TypeRegistry.
static Integer get(const Integer &var)
Atomically retrieves the snapshot value of the indicated variable.
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85