source: trunk/libdjvu/DjVuFileCache.h @ 101

Last change on this file since 101 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

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