Panda3D
 All Classes Functions Variables Enumerations
geomCacheEntry.cxx
1 // Filename: geomCacheEntry.cxx
2 // Created by: drose (21Mar05)
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 "geomCacheEntry.h"
16 #include "geomCacheManager.h"
17 #include "lightMutexHolder.h"
18 #include "config_gobj.h"
19 #include "clockObject.h"
20 
21 TypeHandle GeomCacheEntry::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: GeomCacheEntry::Destructor
25 // Access: Public, Virtual
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 GeomCacheEntry::
29 ~GeomCacheEntry() {
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: GeomCacheEntry::record
34 // Access: Public
35 // Description: Records the entry in the global cache for the first
36 // time.
37 ////////////////////////////////////////////////////////////////////
39 record(Thread *current_thread) {
40  nassertr(_next == (GeomCacheEntry *)NULL && _prev == (GeomCacheEntry *)NULL, NULL);
41  PT(GeomCacheEntry) keepme = this;
42 
43  GeomCacheManager *cache_mgr = GeomCacheManager::get_global_ptr();
44  LightMutexHolder holder(cache_mgr->_lock);
45 
46  if (gobj_cat.is_debug()) {
47  gobj_cat.debug()
48  << "recording cache entry: " << *this << ", total_size = "
49  << cache_mgr->_total_size + 1 << "\n";
50  }
51 
52  insert_before(cache_mgr->_list);
53  ++cache_mgr->_total_size;
54  cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
55  cache_mgr->_geom_cache_record_pcollector.add_level(1);
56  _last_frame_used = ClockObject::get_global_clock()->get_frame_count(current_thread);
57 
59  GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
60  }
61 
62  // Increment our own reference count while we're in the queue, just
63  // so we don't have to play games with it later--this is inner-loop
64  // stuff.
65  ref();
66 
67  // Now remove any old entries if our cache is over the limit. This may
68  // also remove the entry we just added, especially if our cache size
69  // is set to 0. This may actually remove this very object.
70  cache_mgr->evict_old_entries();
71 
72  return this;
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: GeomCacheEntry::refresh
77 // Access: Public
78 // Description: Marks the cache entry recently used, so it will not
79 // be evicted for a while.
80 ////////////////////////////////////////////////////////////////////
82 refresh(Thread *current_thread) {
84  LightMutexHolder holder(cache_mgr->_lock);
85  nassertv(_next != (GeomCacheEntry *)NULL && _prev != (GeomCacheEntry *)NULL);
86 
87  remove_from_list();
88  insert_before(cache_mgr->_list);
89 
90  int current_frame = ClockObject::get_global_clock()->get_frame_count(current_thread);
92  if (_last_frame_used != current_frame) {
93  GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
94  }
95  }
96 
97  _last_frame_used = current_frame;
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: GeomCacheEntry::erase
102 // Access: Public
103 // Description: Removes the entry from the queue, returning a pointer
104 // to the entry. Does not call evict_callback().
105 ////////////////////////////////////////////////////////////////////
107 erase() {
108  nassertr(_next != (GeomCacheEntry *)NULL && _prev != (GeomCacheEntry *)NULL, NULL);
109 
110  PT(GeomCacheEntry) keepme = this;
111  unref();
112 
113  if (gobj_cat.is_debug()) {
114  gobj_cat.debug()
115  << "remove_entry(" << *this << ")\n";
116  }
117 
119  LightMutexHolder holder(cache_mgr->_lock);
120 
121  remove_from_list();
122  --cache_mgr->_total_size;
123  cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
124  cache_mgr->_geom_cache_erase_pcollector.add_level(1);
125 
127  int current_frame = ClockObject::get_global_clock()->get_frame_count();
128  if (_last_frame_used == current_frame) {
129  GeomCacheManager::_geom_cache_active_pcollector.sub_level(1);
130  }
131  }
132 
133  return this;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: GeomCacheEntry::evict_callback
138 // Access: Public, Virtual
139 // Description: Called when the entry is evicted from the cache, this
140 // should clean up the owning object appropriately.
141 ////////////////////////////////////////////////////////////////////
142 void GeomCacheEntry::
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: GeomCacheEntry::output
148 // Access: Public, Virtual
149 // Description:
150 ////////////////////////////////////////////////////////////////////
151 void GeomCacheEntry::
152 output(ostream &out) const {
153  out << "[ unknown ]";
154 }
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:271
Similar to MutexHolder, but for a light mutex.
This is used to keep track of, and limit the size of, the cache of munged vertices, which would otherwise be distributed through all of the GeomVertexData objects in the system.
int get_frame_count(Thread *current_thread=Thread::get_current_thread()) const
Returns the number of times tick() has been called since the ClockObject was created, or since it was last reset.
Definition: clockObject.I:113
virtual void evict_callback()
Called when the entry is evicted from the cache, this should clean up the owning object appropriately...
void refresh(Thread *current_thread)
Marks the cache entry recently used, so it will not be evicted for a while.
A thread; that is, a lightweight process.
Definition: thread.h:51
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise...
Definition: pStatClient.h:269
This object contains a single cache entry in the GeomCacheManager.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
static GeomCacheManager * get_global_ptr()
Returns the global cache manager pointer.