Panda3D
geomCacheEntry.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 geomCacheEntry.cxx
10  * @author drose
11  * @date 2005-03-21
12  */
13 
14 #include "geomCacheEntry.h"
15 #include "geomCacheManager.h"
16 #include "lightMutexHolder.h"
17 #include "config_gobj.h"
18 #include "clockObject.h"
19 
20 TypeHandle GeomCacheEntry::_type_handle;
21 
22 /**
23  *
24  */
25 GeomCacheEntry::
26 ~GeomCacheEntry() {
27 }
28 
29 /**
30  * Records the entry in the global cache for the first time.
31  */
32 PT(GeomCacheEntry) GeomCacheEntry::
33 record(Thread *current_thread) {
34  nassertr(_next == nullptr && _prev == nullptr, nullptr);
35  PT(GeomCacheEntry) keepme = this;
36 
38  LightMutexHolder holder(cache_mgr->_lock);
39 
40  if (gobj_cat.is_debug()) {
41  gobj_cat.debug()
42  << "recording cache entry: " << *this << ", total_size = "
43  << cache_mgr->_total_size + 1 << "\n";
44  }
45 
46  insert_before(cache_mgr->_list);
47  ++cache_mgr->_total_size;
48  cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
49  cache_mgr->_geom_cache_record_pcollector.add_level(1);
50  _last_frame_used = ClockObject::get_global_clock()->get_frame_count(current_thread);
51 
53  GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
54  }
55 
56  // Increment our own reference count while we're in the queue, just so we
57  // don't have to play games with it later--this is inner-loop stuff.
58  ref();
59 
60  // Now remove any old entries if our cache is over the limit. This may also
61  // remove the entry we just added, especially if our cache size is set to 0.
62  // This may actually remove this very object.
63  cache_mgr->evict_old_entries();
64 
65  return this;
66 }
67 
68 /**
69  * Marks the cache entry recently used, so it will not be evicted for a while.
70  */
72 refresh(Thread *current_thread) {
74  LightMutexHolder holder(cache_mgr->_lock);
75  nassertv(_next != nullptr && _prev != nullptr);
76 
77  remove_from_list();
78  insert_before(cache_mgr->_list);
79 
80  int current_frame = ClockObject::get_global_clock()->get_frame_count(current_thread);
82  if (_last_frame_used != current_frame) {
83  GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
84  }
85  }
86 
87  _last_frame_used = current_frame;
88 }
89 
90 /**
91  * Removes the entry from the queue, returning a pointer to the entry. Does
92  * not call evict_callback().
93  */
94 PT(GeomCacheEntry) GeomCacheEntry::
95 erase() {
96  nassertr(_next != nullptr && _prev != nullptr, nullptr);
97 
98  PT(GeomCacheEntry) keepme;
99  keepme.cheat() = this;
100 
101  if (gobj_cat.is_debug()) {
102  gobj_cat.debug()
103  << "remove_entry(" << *this << ")\n";
104  }
105 
107  LightMutexHolder holder(cache_mgr->_lock);
108 
109  remove_from_list();
110  --cache_mgr->_total_size;
111  cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
112  cache_mgr->_geom_cache_erase_pcollector.add_level(1);
113 
115  int current_frame = ClockObject::get_global_clock()->get_frame_count();
116  if (_last_frame_used == current_frame) {
117  GeomCacheManager::_geom_cache_active_pcollector.sub_level(1);
118  }
119  }
120 
121  return this;
122 }
123 
124 /**
125  * Called when the entry is evicted from the cache, this should clean up the
126  * owning object appropriately.
127  */
128 void GeomCacheEntry::
130 }
131 
132 /**
133  *
134  */
135 void GeomCacheEntry::
136 output(std::ostream &out) const {
137  out << "[ unknown ]";
138 }
LightMutexHolder
Similar to MutexHolder, but for a light mutex.
Definition: lightMutexHolder.h:25
config_gobj.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
clockObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
ClockObject::get_global_clock
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:215
GeomCacheEntry
This object contains a single cache entry in the GeomCacheManager.
Definition: geomCacheEntry.h:31
GeomCacheManager
This is used to keep track of, and limit the size of, the cache of munged vertices,...
Definition: geomCacheManager.h:37
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
lightMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomCacheEntry::evict_callback
virtual void evict_callback()
Called when the entry is evicted from the cache, this should clean up the owning object appropriately...
Definition: geomCacheEntry.cxx:129
PStatClient::is_connected
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise.
Definition: pStatClient.h:291
ReferenceCount::ref
void ref() const
Explicitly increments the reference count.
Definition: referenceCount.I:151
ClockObject::get_frame_count
get_frame_count
Returns the number of times tick() has been called since the ClockObject was created,...
Definition: clockObject.h:94
GeomCacheManager::evict_old_entries
void evict_old_entries()
Trims the cache size down to get_max_size() by evicting old cache entries as needed.
Definition: geomCacheManager.I:53
geomCacheEntry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PT
PT(GeomCacheEntry) GeomCacheEntry
Records the entry in the global cache for the first time.
Definition: geomCacheEntry.cxx:32
GeomCacheEntry::refresh
void refresh(Thread *current_thread)
Marks the cache entry recently used, so it will not be evicted for a while.
Definition: geomCacheEntry.cxx:72
geomCacheManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomCacheManager::get_global_ptr
static GeomCacheManager * get_global_ptr()
Returns the global cache manager pointer.
Definition: geomCacheManager.cxx:69
Thread
A thread; that is, a lightweight process.
Definition: thread.h:46