Panda3D

Loading...
Searching...
No Matches
simpleLru.h
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.h
10 * @author drose
11 * @date 2007-05-11
12 */
13
14#ifndef SIMPLELRU_H
15#define SIMPLELRU_H
16
17#include "pandabase.h"
18#include "linkedListNode.h"
19#include "namable.h"
20#include "lightMutex.h"
21#include "lightMutexHolder.h"
22
23 class SimpleLruPage;
24
25/**
26 * An implementation of a very simple LRU algorithm. Also see AdaptiveLru.
27 */
28 class EXPCL_PANDA_GOBJ SimpleLru : public LinkedListNode, public Namable {
29PUBLISHED:
30 explicit SimpleLru(const std::string &name, size_t max_size);
31 ~SimpleLru();
32
33 INLINE size_t get_total_size() const;
34 INLINE size_t get_max_size() const;
35 INLINE void set_max_size(size_t max_size);
36 size_t count_active_size() const;
37
38 INLINE void consider_evict();
39 INLINE void evict_to(size_t target_size);
40 INLINE void begin_epoch();
41
42 INLINE bool validate();
43
44 void output(std::ostream &out) const;
45 void write(std::ostream &out, int indent_level) const;
46
47 public:
48 static LightMutex &_global_lock;
49
50 private:
51 void do_evict_to(size_t target_size, bool hard_evict);
52 bool do_validate();
53
54 size_t _total_size;
55 size_t _max_size;
56 SimpleLruPage *_active_marker;
57
58 friend class SimpleLruPage;
59};
60
61/**
62 * One atomic piece that may be managed by a SimpleLru chain. To use this
63 * class, inherit from it and override evict_lru().
64 */
65 class EXPCL_PANDA_GOBJ SimpleLruPage : public LinkedListNode {
66PUBLISHED:
67 INLINE explicit SimpleLruPage(size_t lru_size);
68 INLINE SimpleLruPage(const SimpleLruPage &copy);
69 INLINE void operator = (const SimpleLruPage &copy);
70
71 virtual ~SimpleLruPage();
72
73 INLINE SimpleLru *get_lru() const;
74
75 void enqueue_lru(SimpleLru *lru);
76 INLINE void dequeue_lru();
77
78 INLINE void mark_used_lru() const;
79 INLINE void mark_used_lru(SimpleLru *lru);
80
81 INLINE size_t get_lru_size() const;
82 INLINE void set_lru_size(size_t lru_size);
83
84 virtual void evict_lru();
85
86 virtual void output(std::ostream &out) const;
87 virtual void write(std::ostream &out, int indent_level) const;
88
89 private:
90 SimpleLru *_lru;
91
92 size_t _lru_size;
93
94 friend class SimpleLru;
95};
96
97 inline std::ostream &operator << (std::ostream &out, const SimpleLru &lru) {
98 lru.output(out);
99 return out;
100}
101
102 inline std::ostream &operator << (std::ostream &out, const SimpleLruPage &page) {
103 page.output(out);
104 return out;
105}
106
107#include "simpleLru.I"
108
109#endif
LightMutex
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition lightMutex.h:41
Namable::output
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
SimpleLruPage
One atomic piece that may be managed by a SimpleLru chain.
Definition simpleLru.h:65
SimpleLruPage::get_lru_size
size_t get_lru_size() const
Returns the size of this page as reported to the LRU, presumably in bytes.
Definition simpleLru.I:171
SimpleLruPage::dequeue_lru
void dequeue_lru()
Removes the page from its SimpleLru.
Definition simpleLru.I:134
SimpleLruPage::mark_used_lru
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
SimpleLruPage::get_lru
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
SimpleLruPage::set_lru_size
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
SimpleLruPage::enqueue_lru
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
SimpleLruPage::evict_lru
virtual void evict_lru()
Evicts the page from the LRU.
Definition simpleLru.cxx:229
SimpleLru
An implementation of a very simple LRU algorithm.
Definition simpleLru.h:28
SimpleLru::begin_epoch
void begin_epoch()
Marks the end of the previous epoch and the beginning of the next one.
Definition simpleLru.I:77
SimpleLru::evict_to
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
SimpleLru::consider_evict
void consider_evict()
Evicts a sequence of objects if the queue is full.
Definition simpleLru.I:52
SimpleLru::get_max_size
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::count_active_size
size_t count_active_size() const
Returns the total size of the pages that were enqueued since the last call to begin_epoch().
Definition simpleLru.cxx:97
SimpleLru::set_max_size
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
SimpleLru::validate
bool validate()
Checks that the LRU is internally self-consistent.
Definition simpleLru.I:87
SimpleLru::get_total_size
size_t get_total_size() const
Returns the total size of all objects currently active on the LRU.
Definition simpleLru.I:18
lightMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lightMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
linkedListNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
namable.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
simpleLru.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.