source: trunk/libdjvu/DjVuFileCache.h @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 8.8 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: DjVuFileCache.h,v 1.9 2007/03/25 20:48:30 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifndef _DJVUFILECACHE_H
60#define _DJVUFILECACHE_H
61#ifdef HAVE_CONFIG_H
62#include "config.h"
63#endif
64#if NEED_GNUG_PRAGMAS
65# pragma interface
66#endif
67
68
69#include "DjVuFile.h"
70
71#ifndef macintosh //MCW can't compile
72# ifndef UNDER_CE
73#  include <sys/types.h>
74#  include <time.h>
75# endif
76#else
77# include <time.h>
78#endif
79
80
81#ifdef HAVE_NAMESPACES
82namespace DJVU {
83# ifdef NOT_DEFINED // Just to fool emacs c++ mode
84}
85#endif
86#endif
87
88/** @name DjVuFileCache.h
89    Files #"DjVuFileCache.h"# and #"DjVuFileCache.cpp"# implement a simple
90    caching mechanism for keeping a given number of \Ref{DjVuFile} instances
91    alive. The cache estimates the size of its elements and gets rid of
92    the oldest ones when necessary.
93
94    See \Ref{DjVuFileCache} for details.
95   
96    @memo Simple DjVuFile caching class.
97    @author Andrei Erofeev <eaf@geocities.com>
98    @version #$Id: DjVuFileCache.h,v 1.9 2007/03/25 20:48:30 leonb Exp $#
99*/
100
101//@{
102
103/** #DjVuFileCache# is a simple list of \Ref{DjVuFile} instances. It keeps
104    track of the total size of all elements and can get rid of the oldest
105    one once the total size becomes over some threshold. Its main purpose
106    is to keep the added \Ref{DjVuFile} instances alive until their size
107    exceeds some given threshold (set by \Ref{set_maximum_size}() function).
108    The user is supposed to use \Ref{DjVuPortcaster::name_to_port}() to
109    find a file corresponding to a given name. The cache provides no
110    naming services */
111#ifdef UNDER_CE
112class DjVuFileCache : public GPEnabled
113{
114protected:
115   DjVuFileCache(const int) {}
116public:
117   static GP<DjVuFileCache> create(const int);
118   virtual ~DjVuFileCache(void);
119   void del_file(const DjVuFile *) {}
120   void add_file(const GP<DjVuFile> &) {}
121   void clear(void) {}
122   void set_max_size(int) {}
123   int get_max_size(void) const {return 0;}
124   void enable(bool en) {}
125   bool is_enabled(void) const {return false;}
126} ;
127#else
128class DjVuFileCache : public GPEnabled
129{
130protected:
131   DjVuFileCache(const int max_size=5*2*1024*1024);
132public:
133      /** Constructs the #DjVuFileCache#
134          @param max_size Maximum allowed size of the cache in bytes. */
135   static GP<DjVuFileCache> create(const int max_size=5*2*1024*1024);
136
137   virtual ~DjVuFileCache(void);
138
139      /** Removes file #file# from the cache */
140   void         del_file(const DjVuFile * file);
141
142      /** Adds the given file to the cache. It it's already there, its
143          timestamp will be refreshed. */
144   void         add_file(const GP<DjVuFile> & file);
145
146      /** Clears the cache. All items will be deleted. */
147   void         clear(void);
148      /** Sets new maximum size. If the total size of all items in the cache
149          is greater than #max_size#, the cache will be deleting the oldest
150          items until the size is OK. */
151   void         set_max_size(int max_size);
152
153      /** Returns the maximum allowed size of the cache. */
154   int          get_max_size(void) const;
155
156      /** Enables or disables the cache. See \Ref{is_enabled}() for details
157          @param en - If {\em en} is TRUE, the cache will be enabled.
158                 Otherwise it will be disabled.
159        */
160   void         enable(bool en);
161
162      /** Returns #TRUE# if the cache is enabled, #FALSE# otherwise.
163          When a cache is disabled, \Ref{add_file}(), and
164          \Ref{del_file}() do nothing. But the {\em maximum size} is preserved
165          inside the class so that next time the cache is enabled, it will
166          be configured the same way. Clearly this "enable/disable" thing is
167          for convenience only. One could easily simulate this behavior by
168          setting the {\em maximum size} of the cache to #ZERO#. */
169   bool         is_enabled(void) const;
170
171public:
172   class Item;
173   
174   class Item : public GPEnabled
175        {
176        public:
177           virtual ~Item(void);
178           time_t               get_time(void) const;
179
180           GP<DjVuFile> get_file(void) const;
181           unsigned int get_size(void) const;
182           
183           void         refresh(void);
184
185        public:
186           GP<DjVuFile> file;
187           time_t               time;
188           GPosition            list_pos;
189           static int   qsort_func(const void * el1, const void * el2);
190
191           Item(void);
192           Item(const GP<DjVuFile> & xfile);
193        };
194
195protected:
196   GCriticalSection     class_lock;
197   
198      /** This function is called right after the given file has been added
199          to the cache for management. */
200   virtual void file_added(const GP<DjVuFile> & file);
201      /** This function is called when the given file is no longer
202          managed by the cache. */
203   virtual void file_deleted(const GP<DjVuFile> & file);
204      /** This function is called when after the cache decides to get rid
205          of the file. */
206   virtual void file_cleared(const GP<DjVuFile> & file);
207
208   GPList<Item> get_items(void);
209private:
210   GPList<Item> list;
211   bool         enabled;
212   int          max_size;
213   int          cur_size;
214
215   int          calculate_size(void);
216   void         clear_to_size(int size);
217};
218
219
220
221//@}
222   
223inline
224DjVuFileCache::Item::Item(void) : time(::time(0)) {};
225
226inline
227DjVuFileCache::Item::Item(const GP<DjVuFile> & xfile) :
228      file(xfile), time(::time(0)) {}
229
230inline
231DjVuFileCache::Item::~Item(void) {}
232
233inline GP<DjVuFile>
234DjVuFileCache::Item::get_file(void) const
235{
236   return file;
237}
238
239inline unsigned int
240DjVuFileCache::Item::get_size(void) const
241{
242   return file->get_memory_usage();
243}
244
245inline time_t
246DjVuFileCache::Item::get_time(void) const
247{
248   return time;
249}
250
251inline void
252DjVuFileCache::Item::refresh(void)
253{
254   time=::time(0);
255}
256
257inline
258DjVuFileCache::DjVuFileCache(const int xmax_size) :
259      enabled(true), max_size(xmax_size), cur_size(0) {}
260
261inline void
262DjVuFileCache::clear(void)
263{
264   clear_to_size(0);
265}
266
267inline bool
268DjVuFileCache::is_enabled(void) const
269{
270   return enabled;
271}
272
273inline int
274DjVuFileCache::get_max_size(void) const
275{
276   return max_size;
277}
278
279#endif
280
281inline GP<DjVuFileCache>
282DjVuFileCache::create(const int max_size)
283{
284  return new DjVuFileCache(max_size);
285}
286
287
288#ifdef HAVE_NAMESPACES
289}
290# ifndef NOT_USING_DJVU_NAMESPACE
291using namespace DJVU;
292# endif
293#endif
294#endif
Note: See TracBrowser for help on using the repository browser.