Panda3D

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 {
29 PUBLISHED:
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 {
66 PUBLISHED:
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
LinkedListNode
This just stores the pointers to implement a doubly-linked list of some kind of object.
Definition: linkedListNode.h:31
Namable
A base class for all things which can have a name.
Definition: namable.h:26
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
SimpleLru
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:28
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.