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