Panda3D
simpleLru.I
1 // Filename: simpleLru.I
2 // Created by: drose (11May07)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: SimpleLru::get_total_size
18 // Access: Published
19 // Description: Returns the total size of all objects currently
20 // active on the LRU.
21 ////////////////////////////////////////////////////////////////////
22 INLINE size_t SimpleLru::
23 get_total_size() const {
24  LightMutexHolder holder(_global_lock);
25  return _total_size;
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: SimpleLru::get_max_size
30 // Access: Published
31 // Description: Returns the max size of all objects that are allowed
32 // to be active on the LRU.
33 ////////////////////////////////////////////////////////////////////
34 INLINE size_t SimpleLru::
35 get_max_size() const {
36  LightMutexHolder holder(_global_lock);
37  return _max_size;
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: SimpleLru::set_max_size
42 // Access: Published
43 // Description: Changes the max size of all objects that are allowed
44 // to be active on the LRU.
45 //
46 // If the size is (size_t)-1, there is no limit.
47 ////////////////////////////////////////////////////////////////////
48 INLINE void SimpleLru::
49 set_max_size(size_t max_size) {
50  LightMutexHolder holder(_global_lock);
51  _max_size = max_size;
52  if (_total_size > _max_size) {
53  do_evict_to(_max_size, false);
54  }
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: SimpleLru::consider_evict
59 // Access: Published
60 // Description: Evicts a sequence of objects if the queue is full.
61 ////////////////////////////////////////////////////////////////////
62 INLINE void SimpleLru::
64  LightMutexHolder holder(_global_lock);
65  if (_total_size > _max_size) {
66  do_evict_to(_max_size, false);
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: SimpleLru::evict_to
72 // Access: Published
73 // Description: Evicts a sequence of objects until the queue fits
74 // within the indicated target size, regardless of its
75 // normal max size.
76 ////////////////////////////////////////////////////////////////////
77 INLINE void SimpleLru::
78 evict_to(size_t target_size) {
79  LightMutexHolder holder(_global_lock);
80  if (_total_size > target_size) {
81  do_evict_to(target_size, true);
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: SimpleLru::begin_epoch
87 // Access: Published
88 // Description: Marks the end of the previous epoch and the beginning
89 // of the next one. This will evict any objects that
90 // are pending eviction, and also update any internal
91 // bookkeeping.
92 ////////////////////////////////////////////////////////////////////
93 INLINE void SimpleLru::
96  _active_marker->enqueue_lru(this);
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: SimpleLru::validate
101 // Access: Published
102 // Description: Checks that the LRU is internally self-consistent.
103 // Returns true if successful, false if there is some
104 // problem.
105 ////////////////////////////////////////////////////////////////////
106 INLINE bool SimpleLru::
108  LightMutexHolder holder(_global_lock);
109  return do_validate();
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: SimpleLruPage::Constructor
114 // Access: Protected
115 // Description:
116 ////////////////////////////////////////////////////////////////////
117 INLINE SimpleLruPage::
118 SimpleLruPage(size_t lru_size) :
119  _lru(NULL),
120  _lru_size(lru_size)
121 {
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: SimpleLruPage::Copy Constructor
126 // Access: Protected
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 INLINE SimpleLruPage::
130 SimpleLruPage(const SimpleLruPage &copy) :
131  _lru(NULL),
132  _lru_size(copy._lru_size)
133 {
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: SimpleLruPage::Copy Assignment Operator
138 // Access: Protected
139 // Description:
140 ////////////////////////////////////////////////////////////////////
141 INLINE void SimpleLruPage::
142 operator = (const SimpleLruPage &copy) {
143  set_lru_size(copy.get_lru_size());
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: SimpleLruPage::get_lru
148 // Access: Published
149 // Description: Returns the LRU that manages this page, or NULL if it
150 // is not currently managed by any LRU.
151 ////////////////////////////////////////////////////////////////////
153 get_lru() const {
154  LightMutexHolder holder(SimpleLru::_global_lock);
155  return _lru;
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: SimpleLruPage::dequeue_lru
160 // Access: Published
161 // Description: Removes the page from its SimpleLru.
162 ////////////////////////////////////////////////////////////////////
163 INLINE void SimpleLruPage::
165  LightMutexHolder holder(SimpleLru::_global_lock);
166 
167  if (_lru != (SimpleLru *)NULL) {
168  remove_from_list();
169  _lru->_total_size -= _lru_size;
170  _lru = NULL;
171  }
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: SimpleLruPage::mark_used_lru
176 // Access: Published
177 // Description: To be called when the page is used; this will move it
178 // to the tail of the SimpleLru queue it is already on.
179 //
180 // This method is const because it's not technically
181 // modifying the contents of the page itself.
182 ////////////////////////////////////////////////////////////////////
183 INLINE void SimpleLruPage::
184 mark_used_lru() const {
185  if (_lru != (SimpleLru *)NULL) {
186  ((SimpleLruPage *)this)->mark_used_lru(_lru);
187  }
188 }
189 
190 ////////////////////////////////////////////////////////////////////
191 // Function: SimpleLruPage::mark_used_lru
192 // Access: Published
193 // Description: To be called when the page is used; this will move it
194 // to the tail of the specified SimpleLru queue.
195 ////////////////////////////////////////////////////////////////////
196 INLINE void SimpleLruPage::
198  enqueue_lru(lru);
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function: SimpleLruPage::get_lru_size
203 // Access: Published
204 // Description: Returns the size of this page as reported to the LRU,
205 // presumably in bytes.
206 ////////////////////////////////////////////////////////////////////
207 INLINE size_t SimpleLruPage::
208 get_lru_size() const {
209  return _lru_size;
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: SimpleLruPage::set_lru_size
214 // Access: Published
215 // Description: Specifies the size of this page, presumably in bytes,
216 // although any unit is possible.
217 ////////////////////////////////////////////////////////////////////
218 INLINE void SimpleLruPage::
219 set_lru_size(size_t lru_size) {
220  LightMutexHolder holder(SimpleLru::_global_lock);
221  if (_lru != (SimpleLru *)NULL) {
222  _lru->_total_size -= _lru_size;
223  _lru->_total_size += lru_size;
224  _lru_size = lru_size;
225  } else {
226  _lru_size = lru_size;
227  }
228 }
size_t get_total_size() const
Returns the total size of all objects currently active on the LRU.
Definition: simpleLru.I:23
void consider_evict()
Evicts a sequence of objects if the queue is full.
Definition: simpleLru.I:63
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:31
void mark_used_lru() const
To be called when the page is used; this will move it to the tail of the SimpleLru queue it is alread...
Definition: simpleLru.I:184
void enqueue_lru(SimpleLru *lru)
Adds the page to the LRU for the first time, or marks it recently-accessed if it has already been add...
Definition: simpleLru.cxx:70
void dequeue_lru()
Removes the page from its SimpleLru.
Definition: simpleLru.I:164
void begin_epoch()
Marks the end of the previous epoch and the beginning of the next one.
Definition: simpleLru.I:94
size_t get_lru_size() const
Returns the size of this page as reported to the LRU, presumably in bytes.
Definition: simpleLru.I:208
void set_lru_size(size_t lru_size)
Specifies the size of this page, presumably in bytes, although any unit is possible.
Definition: simpleLru.I:219
Similar to MutexHolder, but for a light mutex.
One atomic piece that may be managed by a SimpleLru chain.
Definition: simpleLru.h:70
void evict_to(size_t target_size)
Evicts a sequence of objects until the queue fits within the indicated target size, regardless of its normal max size.
Definition: simpleLru.I:78
void set_max_size(size_t max_size)
Changes the max size of all objects that are allowed to be active on the LRU.
Definition: simpleLru.I:49
size_t get_max_size() const
Returns the max size of all objects that are allowed to be active on the LRU.
Definition: simpleLru.I:35
SimpleLru * get_lru() const
Returns the LRU that manages this page, or NULL if it is not currently managed by any LRU...
Definition: simpleLru.I:153
bool validate()
Checks that the LRU is internally self-consistent.
Definition: simpleLru.I:107