Panda3D
 All Classes Functions Variables Enumerations
simpleLru.I
00001 // Filename: simpleLru.I
00002 // Created by:  drose (11May07)
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 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: SimpleLru::get_total_size
00018 //       Access: Published
00019 //  Description: Returns the total size of all objects currently
00020 //               active on the LRU.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE size_t SimpleLru::
00023 get_total_size() const {
00024   LightMutexHolder holder(_global_lock);
00025   return _total_size;
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: SimpleLru::get_max_size
00030 //       Access: Published
00031 //  Description: Returns the max size of all objects that are allowed
00032 //               to be active on the LRU.
00033 ////////////////////////////////////////////////////////////////////
00034 INLINE size_t SimpleLru::
00035 get_max_size() const {
00036   LightMutexHolder holder(_global_lock);
00037   return _max_size;
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: SimpleLru::set_max_size
00042 //       Access: Published
00043 //  Description: Changes the max size of all objects that are allowed
00044 //               to be active on the LRU.
00045 //
00046 //               If the size is (size_t)-1, there is no limit.
00047 ////////////////////////////////////////////////////////////////////
00048 INLINE void SimpleLru::
00049 set_max_size(size_t max_size) {
00050   LightMutexHolder holder(_global_lock);
00051   _max_size = max_size;
00052   if (_total_size > _max_size) {
00053     do_evict_to(_max_size, false);
00054   }
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: SimpleLru::consider_evict
00059 //       Access: Published
00060 //  Description: Evicts a sequence of objects if the queue is full.
00061 ////////////////////////////////////////////////////////////////////
00062 INLINE void SimpleLru::
00063 consider_evict() {
00064   LightMutexHolder holder(_global_lock);
00065   if (_total_size > _max_size) {
00066     do_evict_to(_max_size, false);
00067   }
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: SimpleLru::evict_to
00072 //       Access: Published
00073 //  Description: Evicts a sequence of objects until the queue fits
00074 //               within the indicated target size, regardless of its
00075 //               normal max size.
00076 ////////////////////////////////////////////////////////////////////
00077 INLINE void SimpleLru::
00078 evict_to(size_t target_size) {
00079   LightMutexHolder holder(_global_lock);
00080   if (_total_size > target_size) {
00081     do_evict_to(target_size, true);
00082   }
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: SimpleLru::begin_epoch
00087 //       Access: Published
00088 //  Description: Marks the end of the previous epoch and the beginning
00089 //               of the next one.  This will evict any objects that
00090 //               are pending eviction, and also update any internal
00091 //               bookkeeping.
00092 ////////////////////////////////////////////////////////////////////
00093 INLINE void SimpleLru::
00094 begin_epoch() {
00095   consider_evict();
00096   _active_marker->enqueue_lru(this);
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: SimpleLru::validate
00101 //       Access: Published
00102 //  Description: Checks that the LRU is internally self-consistent.
00103 //               Returns true if successful, false if there is some
00104 //               problem.
00105 ////////////////////////////////////////////////////////////////////
00106 INLINE bool SimpleLru::
00107 validate() {
00108   LightMutexHolder holder(_global_lock);
00109   return do_validate();
00110 }
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //     Function: SimpleLruPage::Constructor
00114 //       Access: Protected
00115 //  Description: 
00116 ////////////////////////////////////////////////////////////////////
00117 INLINE SimpleLruPage::
00118 SimpleLruPage(size_t lru_size) :
00119   _lru(NULL),
00120   _lru_size(lru_size)
00121 {
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: SimpleLruPage::Copy Constructor
00126 //       Access: Protected
00127 //  Description: 
00128 ////////////////////////////////////////////////////////////////////
00129 INLINE SimpleLruPage::
00130 SimpleLruPage(const SimpleLruPage &copy) :
00131   _lru(NULL),
00132   _lru_size(copy._lru_size)
00133 {
00134 }
00135 
00136 ////////////////////////////////////////////////////////////////////
00137 //     Function: SimpleLruPage::Copy Assignment Operator
00138 //       Access: Protected
00139 //  Description: 
00140 ////////////////////////////////////////////////////////////////////
00141 INLINE void SimpleLruPage::
00142 operator = (const SimpleLruPage &copy) {
00143   set_lru_size(copy.get_lru_size());
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: SimpleLruPage::get_lru
00148 //       Access: Published
00149 //  Description: Returns the LRU that manages this page, or NULL if it
00150 //               is not currently managed by any LRU.
00151 ////////////////////////////////////////////////////////////////////
00152 INLINE SimpleLru *SimpleLruPage::
00153 get_lru() const {
00154   LightMutexHolder holder(SimpleLru::_global_lock);
00155   return _lru;
00156 }
00157 
00158 ////////////////////////////////////////////////////////////////////
00159 //     Function: SimpleLruPage::dequeue_lru
00160 //       Access: Published
00161 //  Description: Removes the page from its SimpleLru.
00162 ////////////////////////////////////////////////////////////////////
00163 INLINE void SimpleLruPage::
00164 dequeue_lru() {
00165   LightMutexHolder holder(SimpleLru::_global_lock);
00166 
00167   if (_lru != (SimpleLru *)NULL) {
00168     remove_from_list();
00169     _lru->_total_size -= _lru_size;
00170     _lru = NULL;
00171   }
00172 }
00173 
00174 ////////////////////////////////////////////////////////////////////
00175 //     Function: SimpleLruPage::mark_used_lru
00176 //       Access: Published
00177 //  Description: To be called when the page is used; this will move it
00178 //               to the tail of the SimpleLru queue it is already on.
00179 //
00180 //               This method is const because it's not technically
00181 //               modifying the contents of the page itself.
00182 ////////////////////////////////////////////////////////////////////
00183 INLINE void SimpleLruPage::
00184 mark_used_lru() const {
00185   if (_lru != (SimpleLru *)NULL) {
00186     ((SimpleLruPage *)this)->mark_used_lru(_lru);
00187   }
00188 }
00189 
00190 ////////////////////////////////////////////////////////////////////
00191 //     Function: SimpleLruPage::mark_used_lru
00192 //       Access: Published
00193 //  Description: To be called when the page is used; this will move it
00194 //               to the tail of the specified SimpleLru queue.
00195 ////////////////////////////////////////////////////////////////////
00196 INLINE void SimpleLruPage::
00197 mark_used_lru(SimpleLru *lru) {
00198   enqueue_lru(lru);
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 //     Function: SimpleLruPage::get_lru_size
00203 //       Access: Published
00204 //  Description: Returns the size of this page as reported to the LRU,
00205 //               presumably in bytes.
00206 ////////////////////////////////////////////////////////////////////
00207 INLINE size_t SimpleLruPage::
00208 get_lru_size() const {
00209   return _lru_size;
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: SimpleLruPage::set_lru_size
00214 //       Access: Published
00215 //  Description: Specifies the size of this page, presumably in bytes,
00216 //               although any unit is possible.
00217 ////////////////////////////////////////////////////////////////////
00218 INLINE void SimpleLruPage::
00219 set_lru_size(size_t lru_size) {
00220   LightMutexHolder holder(SimpleLru::_global_lock);
00221   if (_lru != (SimpleLru *)NULL) {
00222     _lru->_total_size -= _lru_size;
00223     _lru->_total_size += lru_size;
00224     _lru_size = lru_size;
00225   } else {
00226     _lru_size = lru_size;
00227   }
00228 }
 All Classes Functions Variables Enumerations