source: trunk/libdjvu/DjVuFileCache.cpp @ 81

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

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

File size: 7.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.cpp,v 1.9 2004/08/06 15:11:29 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include "DjVuFileCache.h"
65#include "debug.h"
66
67#include <stdlib.h>
68
69
70#ifdef HAVE_NAMESPACES
71namespace DJVU {
72# ifdef NOT_DEFINED // Just to fool emacs c++ mode
73}
74#endif
75#endif
76
77
78DjVuFileCache::~DjVuFileCache(void) {}
79
80int
81DjVuFileCache::Item::qsort_func(const void * el1, const void * el2)
82{
83   const Item * item1=*(Item **) el1;
84   const Item * item2=*(Item **) el2;
85   time_t time1=item1->get_time();
86   time_t time2=item2->get_time();
87   return time1<time2 ? -1 : time1>time2 ? 1 : 0;
88}
89
90void
91DjVuFileCache::set_max_size(int xmax_size)
92{
93   DEBUG_MSG("DjVuFileCache::set_max_size(): resizing to " << xmax_size << "\n");
94   DEBUG_MAKE_INDENT(3);
95
96   GCriticalSectionLock lock(&class_lock);
97   
98   max_size=xmax_size;
99   cur_size=calculate_size();
100
101   if (max_size>=0) clear_to_size(enabled ? max_size : 0);
102}
103
104void
105DjVuFileCache::enable(bool en)
106{
107   enabled=en;
108   set_max_size(max_size);
109}
110
111void
112DjVuFileCache::add_file(const GP<DjVuFile> & file)
113{
114   DEBUG_MSG("DjVuFileCache::add_file(): trying to add a new item\n");
115   DEBUG_MAKE_INDENT(3);
116
117   GCriticalSectionLock lock(&class_lock);
118
119      // See if the file is already cached
120   GPosition pos;
121   for(pos=list;pos;++pos)
122      if (list[pos]->get_file()==file) break;
123   
124   if (pos) list[pos]->refresh();       // Refresh the timestamp
125   else
126   {
127         // Doesn't exist in the list yet
128      int _max_size=enabled ? max_size : 0;
129      if (max_size<0) _max_size=max_size;
130
131      int add_size=file->get_memory_usage();
132   
133      if (_max_size>=0 && add_size>_max_size)
134      {
135         DEBUG_MSG("but this item is way too large => doing nothing\n");
136         return;
137      }
138
139      if (_max_size>=0) clear_to_size(_max_size-add_size);
140
141      list.append(new Item(file));
142      cur_size+=add_size;
143      file_added(file);
144   }
145}
146
147void
148DjVuFileCache::clear_to_size(int size)
149{
150   DEBUG_MSG("DjVuFileCache::clear_to_size(): dropping cache size to " << size << "\n");
151   DEBUG_MAKE_INDENT(3);
152
153   GCriticalSectionLock lock(&class_lock);
154   
155   if (size==0)
156   {
157      list.empty();
158      cur_size=0;
159   } else
160      if (list.size()>20)
161      {
162            // More than 20 elements in the cache: use qsort to
163            // sort them before picking up the oldest
164         GTArray<void *> item_arr(list.size()-1);
165         GPosition pos;
166         int i;
167         for(pos=list, i=0;pos;++pos, i++)
168         {
169            GP<Item> item=list[pos];
170            item->list_pos=pos;
171            item_arr[i]=item;
172         }
173
174         qsort(&item_arr[0], item_arr.size(), sizeof(item_arr[0]), Item::qsort_func);
175
176         for(i=0;i<item_arr.size() && cur_size>(int) size;i++)
177         {
178            Item * item=(Item *) item_arr[i];
179            cur_size-=item->get_size();
180            GP<DjVuFile> file=item->file;
181            list.del(item->list_pos);
182            file_cleared(file);
183            if (cur_size<=0) cur_size=calculate_size();
184         }
185      } else
186      {
187            // Less than 20 elements: no reason to presort
188         while(cur_size>(int) size)
189         {
190            if (!list.size())
191            {
192                  // Oops. Discrepancy due to an item changed its size
193               cur_size=0;
194               break;
195            }
196
197               // Remove the oldest cache item
198            GPosition oldest_pos=list;
199            GPosition pos=list;
200            for(++pos;pos;++pos)
201               if (list[pos]->get_time()<list[oldest_pos]->get_time())
202                  oldest_pos=pos;
203            cur_size-=list[oldest_pos]->get_size();
204            GP<DjVuFile> file=list[oldest_pos]->file;
205            list.del(oldest_pos);
206            file_cleared(file);
207
208               // cur_size *may* become negative because items may change their
209               // size after they've been added to the cache
210            if (cur_size<=0) cur_size=calculate_size();
211         }
212      }
213   
214   DEBUG_MSG("done: current cache size=" << cur_size << "\n");
215}
216
217int
218DjVuFileCache::calculate_size(void)
219{
220   GCriticalSectionLock lock(&class_lock);
221   
222   int size=0;
223   for(GPosition pos=list;pos;++pos)
224      size+=list[pos]->get_size();
225   return size;
226}
227
228void
229DjVuFileCache::del_file(const DjVuFile * file)
230{
231   DEBUG_MSG("DjVuFileCache::del_file(): Removing an item from cache\n");
232   DEBUG_MAKE_INDENT(3);
233
234   GCriticalSectionLock lock(&class_lock);
235
236   for(GPosition pos=list;pos;++pos)
237      if (list[pos]->get_file()==file)
238      {
239         GP<DjVuFile> file=list[pos]->get_file();
240         cur_size-=list[pos]->get_size();
241         list.del(pos);
242         file_deleted(file);
243         break;
244      }
245   if (cur_size<0) cur_size=calculate_size();
246   DEBUG_MSG("current cache size=" << cur_size << "\n");
247}
248
249GPList<DjVuFileCache::Item>
250DjVuFileCache::get_items(void)
251{
252   GCriticalSectionLock lock(&class_lock);
253
254   return list;
255}
256
257void
258DjVuFileCache::file_added(const GP<DjVuFile> &) {}
259
260void
261DjVuFileCache::file_deleted(const GP<DjVuFile> &) {}
262
263void
264DjVuFileCache::file_cleared(const GP<DjVuFile> &) {}
265
266#ifdef HAVE_NAMESPACES
267}
268# ifndef NOT_USING_DJVU_NAMESPACE
269using namespace DJVU;
270# endif
271#endif
272
Note: See TracBrowser for help on using the repository browser.