Panda3D

simpleLru.h

00001 // Filename: simpleLru.h
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 #ifndef SIMPLELRU_H
00016 #define SIMPLELRU_H
00017 
00018 #include "pandabase.h"
00019 #include "linkedListNode.h"
00020 #include "namable.h"
00021 #include "lightMutex.h"
00022 #include "lightMutexHolder.h"
00023 
00024 class SimpleLruPage;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : SimpleLru
00028 // Description : An implementation of a very simple LRU algorithm.
00029 //               Also see AdaptiveLru.
00030 ////////////////////////////////////////////////////////////////////
00031 class EXPCL_PANDA_GOBJ SimpleLru : public LinkedListNode, public Namable {
00032 PUBLISHED:
00033   SimpleLru(const string &name, size_t max_size);
00034   ~SimpleLru();
00035 
00036   INLINE size_t get_total_size() const;
00037   INLINE size_t get_max_size() const;
00038   INLINE void set_max_size(size_t max_size);
00039   size_t count_active_size() const;
00040 
00041   INLINE void consider_evict();
00042   INLINE void evict_to(size_t target_size);
00043   INLINE void begin_epoch();
00044 
00045   INLINE bool validate();
00046 
00047   void output(ostream &out) const;
00048   void write(ostream &out, int indent_level) const;
00049 
00050 public:
00051   static LightMutex &_global_lock;
00052 
00053 private:
00054   void do_evict_to(size_t target_size, bool hard_evict);
00055   bool do_validate();
00056 
00057   size_t _total_size;
00058   size_t _max_size;
00059   SimpleLruPage *_active_marker;
00060 
00061   friend class SimpleLruPage;
00062 };
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //       Class : SimpleLruPage
00066 // Description : One atomic piece that may be managed by a SimpleLru
00067 //               chain.  To use this class, inherit from it and
00068 //               override evict_lru().
00069 ////////////////////////////////////////////////////////////////////
00070 class EXPCL_PANDA_GOBJ SimpleLruPage : public LinkedListNode {
00071 PUBLISHED:
00072   INLINE SimpleLruPage(size_t lru_size);
00073   INLINE SimpleLruPage(const SimpleLruPage &copy);
00074   INLINE void operator = (const SimpleLruPage &copy);
00075 
00076   virtual ~SimpleLruPage();
00077 
00078   INLINE SimpleLru *get_lru() const;
00079 
00080   void enqueue_lru(SimpleLru *lru);
00081   INLINE void dequeue_lru();
00082 
00083   INLINE void mark_used_lru() const;
00084   INLINE void mark_used_lru(SimpleLru *lru);
00085 
00086   INLINE size_t get_lru_size() const;
00087   INLINE void set_lru_size(size_t lru_size);
00088 
00089   virtual void evict_lru();
00090 
00091   virtual void output(ostream &out) const;
00092   virtual void write(ostream &out, int indent_level) const;
00093 
00094 private:
00095   SimpleLru *_lru;
00096 
00097   size_t _lru_size;
00098 
00099   friend class SimpleLru;
00100 };
00101 
00102 inline ostream &operator << (ostream &out, const SimpleLru &lru) {
00103   lru.output(out);
00104   return out;
00105 }
00106 
00107 inline ostream &operator << (ostream &out, const SimpleLruPage &page) {
00108   page.output(out);
00109   return out;
00110 }
00111 
00112 #include "simpleLru.I"
00113 
00114 #endif
 All Classes Functions Variables Enumerations