Panda3D

geomCacheEntry.cxx

00001 // Filename: geomCacheEntry.cxx
00002 // Created by:  drose (21Mar05)
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 "geomCacheEntry.h"
00016 #include "geomCacheManager.h"
00017 #include "lightMutexHolder.h"
00018 #include "config_gobj.h"
00019 #include "clockObject.h"
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: GeomCacheEntry::Destructor
00023 //       Access: Public, Virtual
00024 //  Description: 
00025 ////////////////////////////////////////////////////////////////////
00026 GeomCacheEntry::
00027 ~GeomCacheEntry() {
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: GeomCacheEntry::record
00032 //       Access: Public
00033 //  Description: Records the entry in the global cache for the first
00034 //               time.
00035 ////////////////////////////////////////////////////////////////////
00036 PT(GeomCacheEntry) GeomCacheEntry::
00037 record(Thread *current_thread) {
00038   nassertr(_next == (GeomCacheEntry *)NULL && _prev == (GeomCacheEntry *)NULL, NULL);
00039   PT(GeomCacheEntry) keepme = this;
00040 
00041   GeomCacheManager *cache_mgr = GeomCacheManager::get_global_ptr();
00042   LightMutexHolder holder(cache_mgr->_lock);
00043 
00044   if (gobj_cat.is_debug()) {
00045     gobj_cat.debug()
00046       << "recording cache entry: " << *this << ", total_size = "
00047       << cache_mgr->_total_size + 1 << "\n";
00048   }
00049 
00050   insert_before(cache_mgr->_list);
00051   ++cache_mgr->_total_size;
00052   cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
00053   cache_mgr->_geom_cache_record_pcollector.add_level(1);
00054   _last_frame_used = ClockObject::get_global_clock()->get_frame_count(current_thread);
00055 
00056   if (PStatClient::is_connected()) {
00057     GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
00058   }
00059 
00060   // Increment our own reference count while we're in the queue, just
00061   // so we don't have to play games with it later--this is inner-loop
00062   // stuff.
00063   ref();
00064 
00065   // Now remove any old entries if our cache is over the limit.  This may
00066   // also remove the entry we just added, especially if our cache size
00067   // is set to 0.  This may actually remove this very object.
00068   cache_mgr->evict_old_entries();
00069 
00070   return this;
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //     Function: GeomCacheEntry::refresh
00075 //       Access: Public
00076 //  Description: Marks the cache entry recently used, so it will not
00077 //               be evicted for a while.
00078 ////////////////////////////////////////////////////////////////////
00079 void GeomCacheEntry::
00080 refresh(Thread *current_thread) {
00081   GeomCacheManager *cache_mgr = GeomCacheManager::get_global_ptr();
00082   LightMutexHolder holder(cache_mgr->_lock);
00083   nassertv(_next != (GeomCacheEntry *)NULL && _prev != (GeomCacheEntry *)NULL);
00084 
00085   remove_from_list();
00086   insert_before(cache_mgr->_list);
00087 
00088   int current_frame = ClockObject::get_global_clock()->get_frame_count(current_thread);
00089   if (PStatClient::is_connected()) {
00090     if (_last_frame_used != current_frame) {
00091       GeomCacheManager::_geom_cache_active_pcollector.add_level(1);
00092     }
00093   }
00094 
00095   _last_frame_used = current_frame;
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: GeomCacheEntry::erase
00100 //       Access: Public
00101 //  Description: Removes the entry from the queue, returning a pointer
00102 //               to the entry.  Does not call evict_callback().
00103 ////////////////////////////////////////////////////////////////////
00104 PT(GeomCacheEntry) GeomCacheEntry::
00105 erase() {
00106   nassertr(_next != (GeomCacheEntry *)NULL && _prev != (GeomCacheEntry *)NULL, NULL);
00107 
00108   PT(GeomCacheEntry) keepme = this;
00109   unref();
00110 
00111   if (gobj_cat.is_debug()) {
00112     gobj_cat.debug()
00113       << "remove_entry(" << *this << ")\n";
00114   }
00115 
00116   GeomCacheManager *cache_mgr = GeomCacheManager::get_global_ptr();
00117   LightMutexHolder holder(cache_mgr->_lock);
00118 
00119   remove_from_list();
00120   --cache_mgr->_total_size;
00121   cache_mgr->_geom_cache_size_pcollector.set_level(cache_mgr->_total_size);
00122   cache_mgr->_geom_cache_erase_pcollector.add_level(1);
00123 
00124   if (PStatClient::is_connected()) {
00125     int current_frame = ClockObject::get_global_clock()->get_frame_count();
00126     if (_last_frame_used == current_frame) {
00127       GeomCacheManager::_geom_cache_active_pcollector.sub_level(1);
00128     }
00129   }
00130 
00131   return this;
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: GeomCacheEntry::evict_callback
00136 //       Access: Public, Virtual
00137 //  Description: Called when the entry is evicted from the cache, this
00138 //               should clean up the owning object appropriately.
00139 ////////////////////////////////////////////////////////////////////
00140 void GeomCacheEntry::
00141 evict_callback() {
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: GeomCacheEntry::output
00146 //       Access: Public, Virtual
00147 //  Description: 
00148 ////////////////////////////////////////////////////////////////////
00149 void GeomCacheEntry::
00150 output(ostream &out) const {
00151   out << "[ unknown ]";
00152 }
 All Classes Functions Variables Enumerations