|
Packit |
df99a1 |
//C- -*- C++ -*-
|
|
Packit |
df99a1 |
//C- -------------------------------------------------------------------
|
|
Packit |
df99a1 |
//C- DjVuLibre-3.5
|
|
Packit |
df99a1 |
//C- Copyright (c) 2002 Leon Bottou and Yann Le Cun.
|
|
Packit |
df99a1 |
//C- Copyright (c) 2001 AT&T
|
|
Packit |
df99a1 |
//C-
|
|
Packit |
df99a1 |
//C- This software is subject to, and may be distributed under, the
|
|
Packit |
df99a1 |
//C- GNU General Public License, either Version 2 of the license,
|
|
Packit |
df99a1 |
//C- or (at your option) any later version. The license should have
|
|
Packit |
df99a1 |
//C- accompanied the software or you may obtain a copy of the license
|
|
Packit |
df99a1 |
//C- from the Free Software Foundation at http://www.fsf.org .
|
|
Packit |
df99a1 |
//C-
|
|
Packit |
df99a1 |
//C- This program is distributed in the hope that it will be useful,
|
|
Packit |
df99a1 |
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
df99a1 |
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
df99a1 |
//C- GNU General Public License for more details.
|
|
Packit |
df99a1 |
//C-
|
|
Packit |
df99a1 |
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
|
|
Packit |
df99a1 |
//C- Lizardtech Software. Lizardtech Software has authorized us to
|
|
Packit |
df99a1 |
//C- replace the original DjVu(r) Reference Library notice by the following
|
|
Packit |
df99a1 |
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
|
|
Packit |
df99a1 |
//C-
|
|
Packit |
df99a1 |
//C- ------------------------------------------------------------------
|
|
Packit |
df99a1 |
//C- | DjVu (r) Reference Library (v. 3.5)
|
|
Packit |
df99a1 |
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
|
|
Packit |
df99a1 |
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
|
|
Packit |
df99a1 |
//C- | 6,058,214 and patents pending.
|
|
Packit |
df99a1 |
//C- |
|
|
Packit |
df99a1 |
//C- | This software is subject to, and may be distributed under, the
|
|
Packit |
df99a1 |
//C- | GNU General Public License, either Version 2 of the license,
|
|
Packit |
df99a1 |
//C- | or (at your option) any later version. The license should have
|
|
Packit |
df99a1 |
//C- | accompanied the software or you may obtain a copy of the license
|
|
Packit |
df99a1 |
//C- | from the Free Software Foundation at http://www.fsf.org .
|
|
Packit |
df99a1 |
//C- |
|
|
Packit |
df99a1 |
//C- | The computer code originally released by LizardTech under this
|
|
Packit |
df99a1 |
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
|
|
Packit |
df99a1 |
//C- | ORIGINAL CODE." Subject to any third party intellectual property
|
|
Packit |
df99a1 |
//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
|
|
Packit |
df99a1 |
//C- | non-exclusive license to make, use, sell, or otherwise dispose of
|
|
Packit |
df99a1 |
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
|
|
Packit |
df99a1 |
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
|
|
Packit |
df99a1 |
//C- | General Public License. This grant only confers the right to
|
|
Packit |
df99a1 |
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
|
|
Packit |
df99a1 |
//C- | the extent such infringement is reasonably necessary to enable
|
|
Packit |
df99a1 |
//C- | recipient to make, have made, practice, sell, or otherwise dispose
|
|
Packit |
df99a1 |
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
|
|
Packit |
df99a1 |
//C- | any greater extent that may be necessary to utilize further
|
|
Packit |
df99a1 |
//C- | modifications or combinations.
|
|
Packit |
df99a1 |
//C- |
|
|
Packit |
df99a1 |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
|
|
Packit |
df99a1 |
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
Packit |
df99a1 |
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
|
|
Packit |
df99a1 |
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
df99a1 |
//C- +------------------------------------------------------------------
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
df99a1 |
# include "config.h"
|
|
Packit |
df99a1 |
#endif
|
|
Packit |
df99a1 |
#if NEED_GNUG_PRAGMAS
|
|
Packit |
df99a1 |
# pragma implementation
|
|
Packit |
df99a1 |
#endif
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
#include "DjVuFileCache.h"
|
|
Packit |
df99a1 |
#include "debug.h"
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
#include <stddef.h>
|
|
Packit |
df99a1 |
#include <stdlib.h>
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
#ifdef HAVE_NAMESPACES
|
|
Packit |
df99a1 |
namespace DJVU {
|
|
Packit |
df99a1 |
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
#endif
|
|
Packit |
df99a1 |
#endif
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
DjVuFileCache::~DjVuFileCache(void) {}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
int
|
|
Packit |
df99a1 |
DjVuFileCache::Item::qsort_func(const void *el1, const void *el2)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
const Item *item1 = *(Item**)el1;
|
|
Packit |
df99a1 |
const Item *item2 = *(Item**)el2;
|
|
Packit |
df99a1 |
time_t time1=item1->get_time();
|
|
Packit |
df99a1 |
time_t time2=item2->get_time();
|
|
Packit |
df99a1 |
return time1<time2 ? -1 : time1>time2 ? 1 : 0;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::set_max_size(int xmax_size)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
DEBUG_MSG("DjVuFileCache::set_max_size(): resizing to " << xmax_size << "\n");
|
|
Packit |
df99a1 |
DEBUG_MAKE_INDENT(3);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
max_size=xmax_size;
|
|
Packit |
df99a1 |
cur_size=calculate_size();
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
if (max_size>=0) clear_to_size(enabled ? max_size : 0);
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::enable(bool en)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
enabled=en;
|
|
Packit |
df99a1 |
set_max_size(max_size);
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::add_file(const GP<DjVuFile> & file)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
DEBUG_MSG("DjVuFileCache::add_file(): trying to add a new item\n");
|
|
Packit |
df99a1 |
DEBUG_MAKE_INDENT(3);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
// See if the file is already cached
|
|
Packit |
df99a1 |
GPosition pos;
|
|
Packit |
df99a1 |
for(pos=list;pos;++pos)
|
|
Packit |
df99a1 |
if (list[pos]->get_file()==file) break;
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
if (pos) list[pos]->refresh(); // Refresh the timestamp
|
|
Packit |
df99a1 |
else
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
// Doesn't exist in the list yet
|
|
Packit |
df99a1 |
int _max_size=enabled ? max_size : 0;
|
|
Packit |
df99a1 |
if (max_size<0) _max_size=max_size;
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
int add_size=file->get_memory_usage();
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
if (_max_size>=0 && add_size>_max_size)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
DEBUG_MSG("but this item is way too large => doing nothing\n");
|
|
Packit |
df99a1 |
return;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
if (_max_size>=0) clear_to_size(_max_size-add_size);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
list.append(new Item(file));
|
|
Packit |
df99a1 |
cur_size+=add_size;
|
|
Packit |
df99a1 |
file_added(file);
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::clear_to_size(int size)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
DEBUG_MSG("DjVuFileCache::clear_to_size(): dropping cache size to " << size << "\n");
|
|
Packit |
df99a1 |
DEBUG_MAKE_INDENT(3);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
if (size==0)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
list.empty();
|
|
Packit |
df99a1 |
cur_size=0;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
if (list.size() > 20)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
// More than 20 elements in the cache: use qsort to
|
|
Packit |
df99a1 |
// sort them before picking up the oldest
|
|
Packit |
df99a1 |
GPArray<Item> item_arr(list.size()-1);
|
|
Packit |
df99a1 |
GPosition pos;
|
|
Packit |
df99a1 |
int i;
|
|
Packit |
df99a1 |
for(pos=list, i=0;pos;++pos, i++)
|
|
Packit |
df99a1 |
item_arr[i] = list[pos];
|
|
Packit |
df99a1 |
list.empty();
|
|
Packit |
df99a1 |
qsort(&item_arr[0], item_arr.size(), sizeof(item_arr[0]), Item::qsort_func);
|
|
Packit |
df99a1 |
for(i=0;i<item_arr.size() && cur_size > (int)size;i++)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
Item *item = item_arr[i];
|
|
Packit |
df99a1 |
cur_size -= item->get_size();
|
|
Packit |
df99a1 |
file_cleared(item->file);
|
|
Packit |
df99a1 |
item_arr[i] = 0;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
for (; i
|
|
Packit |
df99a1 |
list.append(item_arr[i]);
|
|
Packit |
df99a1 |
if (cur_size <= 0)
|
|
Packit |
df99a1 |
cur_size = calculate_size();
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
// Less than 20 elements: no reason to presort
|
|
Packit |
df99a1 |
while(cur_size > (int)size && list.size() > 0)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
// Remove the oldest cache item
|
|
Packit |
df99a1 |
GPosition oldest_pos=list;
|
|
Packit |
df99a1 |
GPosition pos=list;
|
|
Packit |
df99a1 |
for(++pos;pos;++pos)
|
|
Packit |
df99a1 |
if (list[pos]->get_time()<list[oldest_pos]->get_time())
|
|
Packit |
df99a1 |
oldest_pos=pos;
|
|
Packit |
df99a1 |
cur_size -= list[oldest_pos]->get_size();
|
|
Packit |
df99a1 |
GP<DjVuFile> file=list[oldest_pos]->file;
|
|
Packit |
df99a1 |
list.del(oldest_pos);
|
|
Packit |
df99a1 |
file_cleared(file);
|
|
Packit |
df99a1 |
// cur_size *may* become negative because items may change their
|
|
Packit |
df99a1 |
// size after they've been added to the cache
|
|
Packit |
df99a1 |
if (cur_size <= 0)
|
|
Packit |
df99a1 |
cur_size = calculate_size();
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
if (cur_size <= 0)
|
|
Packit |
df99a1 |
cur_size = calculate_size();
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
DEBUG_MSG("done: current cache size=" << cur_size << "\n");
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
int
|
|
Packit |
df99a1 |
DjVuFileCache::calculate_size(void)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
int size=0;
|
|
Packit |
df99a1 |
for(GPosition pos=list;pos;++pos)
|
|
Packit |
df99a1 |
size+=list[pos]->get_size();
|
|
Packit |
df99a1 |
return size;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::del_file(const DjVuFile * file)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
DEBUG_MSG("DjVuFileCache::del_file(): Removing an item from cache\n");
|
|
Packit |
df99a1 |
DEBUG_MAKE_INDENT(3);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
for(GPosition pos=list;pos;++pos)
|
|
Packit |
df99a1 |
if (list[pos]->get_file()==file)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
GP<DjVuFile> file=list[pos]->get_file();
|
|
Packit |
df99a1 |
cur_size-=list[pos]->get_size();
|
|
Packit |
df99a1 |
list.del(pos);
|
|
Packit |
df99a1 |
file_deleted(file);
|
|
Packit |
df99a1 |
break;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
if (cur_size<0) cur_size=calculate_size();
|
|
Packit |
df99a1 |
DEBUG_MSG("current cache size=" << cur_size << "\n");
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
GPList<DjVuFileCache::Item>
|
|
Packit |
df99a1 |
DjVuFileCache::get_items(void)
|
|
Packit |
df99a1 |
{
|
|
Packit |
df99a1 |
GCriticalSectionLock lock(&class_lock);
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
return list;
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::file_added(const GP<DjVuFile> &) {}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::file_deleted(const GP<DjVuFile> &) {}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
void
|
|
Packit |
df99a1 |
DjVuFileCache::file_cleared(const GP<DjVuFile> &) {}
|
|
Packit |
df99a1 |
|
|
Packit |
df99a1 |
#ifdef HAVE_NAMESPACES
|
|
Packit |
df99a1 |
}
|
|
Packit |
df99a1 |
# ifndef NOT_USING_DJVU_NAMESPACE
|
|
Packit |
df99a1 |
using namespace DJVU;
|
|
Packit |
df99a1 |
# endif
|
|
Packit |
df99a1 |
#endif
|
|
Packit |
df99a1 |
|