Panda3D
Loading...
Searching...
No Matches
typeHandle.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 typeHandle.cxx
10 * @author drose
11 * @date 1998-10-23
12 */
13
14#include "typeHandle.h"
15#include "typeRegistryNode.h"
16#include "atomicAdjust.h"
17
18/**
19 * Returns the total allocated memory used by objects of this type, for the
20 * indicated memory class. This is only updated if track-memory-usage is set
21 * true in your Config.prc file.
22 */
24get_memory_usage(MemoryClass memory_class) const {
25#ifdef DO_MEMORY_USAGE
26 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
27 if ((*this) == TypeHandle::none()) {
28 return 0;
29 } else {
30 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
31 assert(rnode != nullptr);
32 return (size_t)AtomicAdjust::get(rnode->_memory_usage[memory_class]);
33 }
34#endif // DO_MEMORY_USAGE
35 return 0;
36}
37
38/**
39 * Adds the indicated amount to the record for the total allocated memory for
40 * objects of this type.
41 */
43inc_memory_usage(MemoryClass memory_class, size_t size) {
44#ifdef DO_MEMORY_USAGE
45#ifdef _DEBUG
46 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
47#endif
48 if ((*this) != TypeHandle::none()) {
49 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
50 assert(rnode != nullptr);
51 AtomicAdjust::add(rnode->_memory_usage[memory_class], (AtomicAdjust::Integer)size);
52 // cerr << *this << ".inc(" << memory_class << ", " << size << ") -> " <<
53 // rnode->_memory_usage[memory_class] << "\n";
54 if (rnode->_memory_usage[memory_class] < 0) {
55 std::cerr << "Memory usage overflow for type " << rnode->_name << ".\n";
56 abort();
57 }
58 }
59#endif // DO_MEMORY_USAGE
60}
61
62/**
63 * Subtracts the indicated amount from the record for the total allocated
64 * memory for objects of this type.
65 */
67dec_memory_usage(MemoryClass memory_class, size_t size) {
68#ifdef DO_MEMORY_USAGE
69#ifdef _DEBUG
70 assert((int)memory_class >= 0 && (int)memory_class < (int)MC_limit);
71#endif
72 if ((*this) != TypeHandle::none()) {
73 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
74 assert(rnode != nullptr);
75 AtomicAdjust::add(rnode->_memory_usage[memory_class], -(AtomicAdjust::Integer)size);
76 // cerr << *this << ".dec(" << memory_class << ", " << size << ") -> " <<
77 // rnode->_memory_usage[memory_class] << "\n";
78 assert(rnode->_memory_usage[memory_class] >= 0);
79 }
80#endif // DO_MEMORY_USAGE
81}
82
83/**
84 * Allocates memory, adding it to the total amount of memory allocated for
85 * this type.
86 */
88allocate_array(size_t size) {
89 TAU_PROFILE("TypeHandle:allocate_array()", " ", TAU_USER);
90
91 void *ptr = PANDA_MALLOC_ARRAY(size);
92#ifdef DO_MEMORY_USAGE
93 if ((*this) != TypeHandle::none()) {
94 size_t alloc_size = MemoryHook::get_ptr_size(ptr);
95#ifdef _DEBUG
96 assert(size <= alloc_size);
97#endif
98 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
99 assert(rnode != nullptr);
100 AtomicAdjust::add(rnode->_memory_usage[MC_array], (AtomicAdjust::Integer)alloc_size);
101 if (rnode->_memory_usage[MC_array] < 0) {
102 std::cerr << "Memory usage overflow for type " << rnode->_name << ".\n";
103 abort();
104 }
105 }
106#endif // DO_MEMORY_USAGE
107 return ptr;
108}
109
110/**
111 * Reallocates memory, adjusting the total amount of memory allocated for this
112 * type.
113 */
115reallocate_array(void *old_ptr, size_t size) {
116 TAU_PROFILE("TypeHandle:reallocate_array()", " ", TAU_USER);
117
118#ifdef DO_MEMORY_USAGE
119 size_t old_size = MemoryHook::get_ptr_size(old_ptr);
120 void *new_ptr = PANDA_REALLOC_ARRAY(old_ptr, size);
121
122 if ((*this) != TypeHandle::none()) {
123 size_t new_size = MemoryHook::get_ptr_size(new_ptr);
124
125 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
126 assert(rnode != nullptr);
127 AtomicAdjust::add(rnode->_memory_usage[MC_array], (AtomicAdjust::Integer)new_size - (AtomicAdjust::Integer)old_size);
128 assert(rnode->_memory_usage[MC_array] >= 0);
129 }
130#else
131 void *new_ptr = PANDA_REALLOC_ARRAY(old_ptr, size);
132#endif
133 return new_ptr;
134}
135
136/**
137 * Deallocates memory, subtracting it from the total amount of memory
138 * allocated for this type.
139 */
141deallocate_array(void *ptr) {
142 TAU_PROFILE("TypeHandle:deallocate_array()", " ", TAU_USER);
143
144#ifdef DO_MEMORY_USAGE
145 size_t alloc_size = MemoryHook::get_ptr_size(ptr);
146 if ((*this) != TypeHandle::none()) {
147 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
148 assert(rnode != nullptr);
149 AtomicAdjust::add(rnode->_memory_usage[MC_array], -(AtomicAdjust::Integer)alloc_size);
150 assert(rnode->_memory_usage[MC_array] >= 0);
151 }
152#endif // DO_MEMORY_USAGE
153 PANDA_FREE_ARRAY(ptr);
154}
155
156#ifdef HAVE_PYTHON
157/**
158 * Returns the internal void pointer that is stored for interrogate's benefit.
159 */
160PyObject *TypeHandle::
161get_python_type() const {
162 TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
163 if (rnode != nullptr) {
164 return rnode->get_python_type();
165 } else {
166 return nullptr;
167 }
168}
169#endif
170
171/**
172 * Return the Index of the BEst fit Classs from a set
173 */
175get_best_parent_from_Set(const std::set< int > &legal_vals) const {
176 if (legal_vals.find(_index) != legal_vals.end()) {
177 return _index;
178 }
179
180 for (int pi = 0; pi < get_num_parent_classes(); ++pi) {
182 int val = ph.get_best_parent_from_Set(legal_vals);
183 if (val > 0) {
184 return val;
185 }
186 }
187 return -1;
188}
189
190std::ostream &
191operator << (std::ostream &out, TypeHandle::MemoryClass mem_class) {
192 switch (mem_class) {
193 case TypeHandle::MC_singleton:
194 return out << "singleton";
195
196 case TypeHandle::MC_array:
197 return out << "array";
198
199 case TypeHandle::MC_deleted_chain_active:
200 return out << "deleted_chain_active";
201
202 case TypeHandle::MC_deleted_chain_inactive:
203 return out << "deleted_chain_inactive";
204
205 case TypeHandle::MC_limit:
206 return out << "limit";
207 }
208
209 return out
210 << "**invalid TypeHandle::MemoryClass (" << (int)mem_class
211 << ")**\n";
212}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static Integer add(Integer &var, Integer delta)
Atomically computes var += delta.
static Integer get(const Integer &var)
Atomically retrieves the snapshot value of the indicated variable.
static size_t get_ptr_size(void *ptr)
Given a pointer that was returned by a MemoryHook allocation, returns the number of bytes that were a...
Definition memoryHook.I:67
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
void dec_memory_usage(MemoryClass memory_class, size_t size)
Subtracts the indicated amount from the record for the total allocated memory for objects of this typ...
get_parent_class
Returns the nth parent class of this type.
Definition typeHandle.h:137
size_t get_memory_usage(MemoryClass memory_class) const
Returns the total allocated memory used by objects of this type, for the indicated memory class.
void deallocate_array(void *ptr)
Deallocates memory, subtracting it from the total amount of memory allocated for this type.
get_num_parent_classes
Returns the number of parent classes that this type is known to have.
Definition typeHandle.h:137
int get_best_parent_from_Set(const std::set< int > &legal_vals) const
Return the Index of the BEst fit Classs from a set.
void * reallocate_array(void *ptr, size_t size)
Reallocates memory, adjusting the total amount of memory allocated for this type.
void inc_memory_usage(MemoryClass memory_class, size_t size)
Adds the indicated amount to the record for the total allocated memory for objects of this type.
void * allocate_array(size_t size)
Allocates memory, adding it to the total amount of memory allocated for this type.
This is a single entry in the TypeRegistry.
PyObject * get_python_type() const
Returns the Python type object associated with this node.
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.