Blame libdjvu/DjVuFileCache.h

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
#ifndef _DJVUFILECACHE_H
Packit df99a1
#define _DJVUFILECACHE_H
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
#include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma interface
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
#include "DjVuFile.h"
Packit df99a1
Packit df99a1
#ifndef macintosh //MCW can't compile
Packit df99a1
# ifndef UNDER_CE
Packit df99a1
#  include <sys/types.h>
Packit df99a1
#  include <time.h>
Packit df99a1
# endif 
Packit df99a1
#else
Packit df99a1
# include <time.h>
Packit df99a1
#endif
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
/** @name DjVuFileCache.h
Packit df99a1
    Files #"DjVuFileCache.h"# and #"DjVuFileCache.cpp"# implement a simple
Packit df99a1
    caching mechanism for keeping a given number of \Ref{DjVuFile} instances
Packit df99a1
    alive. The cache estimates the size of its elements and gets rid of
Packit df99a1
    the oldest ones when necessary.
Packit df99a1
Packit df99a1
    See \Ref{DjVuFileCache} for details.
Packit df99a1
    
Packit df99a1
    @memo Simple DjVuFile caching class.
Packit df99a1
    @author Andrei Erofeev <eaf@geocities.com>
Packit df99a1
*/
Packit df99a1
Packit df99a1
//@{
Packit df99a1
Packit df99a1
/** #DjVuFileCache# is a simple list of \Ref{DjVuFile} instances. It keeps
Packit df99a1
    track of the total size of all elements and can get rid of the oldest
Packit df99a1
    one once the total size becomes over some threshold. Its main purpose
Packit df99a1
    is to keep the added \Ref{DjVuFile} instances alive until their size
Packit df99a1
    exceeds some given threshold (set by \Ref{set_maximum_size}() function).
Packit df99a1
    The user is supposed to use \Ref{DjVuPortcaster::name_to_port}() to
Packit df99a1
    find a file corresponding to a given name. The cache provides no
Packit df99a1
    naming services */
Packit df99a1
#ifdef UNDER_CE
Packit df99a1
class DjVuFileCache : public GPEnabled
Packit df99a1
{
Packit df99a1
protected:
Packit df99a1
   DjVuFileCache(const int) {}
Packit df99a1
public:
Packit df99a1
   static GP<DjVuFileCache> create(const int);
Packit df99a1
   virtual ~DjVuFileCache(void);
Packit df99a1
   void del_file(const DjVuFile *) {}
Packit df99a1
   void add_file(const GP<DjVuFile> &) {}
Packit df99a1
   void clear(void) {}
Packit df99a1
   void set_max_size(int) {}
Packit df99a1
   int get_max_size(void) const {return 0;}
Packit df99a1
   void enable(bool en) {}
Packit df99a1
   bool is_enabled(void) const {return false;}
Packit df99a1
} ;
Packit df99a1
#else
Packit df99a1
class DjVuFileCache : public GPEnabled
Packit df99a1
{
Packit df99a1
protected:
Packit df99a1
   DjVuFileCache(const int max_size=5*2*1024*1024);
Packit df99a1
public:
Packit df99a1
      /** Constructs the #DjVuFileCache#
Packit df99a1
	  @param max_size Maximum allowed size of the cache in bytes. */
Packit df99a1
   static GP<DjVuFileCache> create(const int max_size=5*2*1024*1024);
Packit df99a1
Packit df99a1
   virtual ~DjVuFileCache(void);
Packit df99a1
Packit df99a1
      /** Removes file #file# from the cache */
Packit df99a1
   void		del_file(const DjVuFile * file);
Packit df99a1
Packit df99a1
      /** Adds the given file to the cache. It it's already there, its
Packit df99a1
	  timestamp will be refreshed. */
Packit df99a1
   void		add_file(const GP<DjVuFile> & file);
Packit df99a1
Packit df99a1
      /** Clears the cache. All items will be deleted. */
Packit df99a1
   void		clear(void);
Packit df99a1
      /** Sets new maximum size. If the total size of all items in the cache
Packit df99a1
	  is greater than #max_size#, the cache will be deleting the oldest
Packit df99a1
	  items until the size is OK. */
Packit df99a1
   void		set_max_size(int max_size);
Packit df99a1
Packit df99a1
      /** Returns the maximum allowed size of the cache. */
Packit df99a1
   int		get_max_size(void) const;
Packit df99a1
Packit df99a1
      /** Enables or disables the cache. See \Ref{is_enabled}() for details
Packit df99a1
	  @param en - If {\em en} is TRUE, the cache will be enabled.
Packit df99a1
	         Otherwise it will be disabled.
Packit df99a1
	*/
Packit df99a1
   void		enable(bool en);
Packit df99a1
Packit df99a1
      /** Returns #TRUE# if the cache is enabled, #FALSE# otherwise.
Packit df99a1
	  When a cache is disabled, \Ref{add_file}(), and
Packit df99a1
	  \Ref{del_file}() do nothing. But the {\em maximum size} is preserved
Packit df99a1
	  inside the class so that next time the cache is enabled, it will
Packit df99a1
	  be configured the same way. Clearly this "enable/disable" thing is
Packit df99a1
	  for convenience only. One could easily simulate this behavior by
Packit df99a1
	  setting the {\em maximum size} of the cache to #ZERO#. */
Packit df99a1
   bool		is_enabled(void) const;
Packit df99a1
Packit df99a1
public:
Packit df99a1
   class Item;
Packit df99a1
   
Packit df99a1
   class Item : public GPEnabled
Packit df99a1
	{
Packit df99a1
	public:
Packit df99a1
	   virtual ~Item(void);
Packit df99a1
	   time_t	get_time(void) const;
Packit df99a1
	   GP<DjVuFile>	get_file(void) const;
Packit df99a1
	   unsigned int	get_size(void) const;
Packit df99a1
	   void		refresh(void);
Packit df99a1
Packit df99a1
	public:
Packit df99a1
	   GP<DjVuFile>	file;
Packit df99a1
	   time_t	time;
Packit df99a1
	   static int	qsort_func(const void * el1, const void * el2);
Packit df99a1
	   Item(void);
Packit df99a1
	   Item(const GP<DjVuFile> & xfile);
Packit df99a1
	};
Packit df99a1
Packit df99a1
protected:
Packit df99a1
   GCriticalSection	class_lock;
Packit df99a1
   
Packit df99a1
      /** This function is called right after the given file has been added
Packit df99a1
	  to the cache for management. */
Packit df99a1
   virtual void	file_added(const GP<DjVuFile> & file);
Packit df99a1
      /** This function is called when the given file is no longer
Packit df99a1
	  managed by the cache. */
Packit df99a1
   virtual void	file_deleted(const GP<DjVuFile> & file);
Packit df99a1
      /** This function is called when after the cache decides to get rid
Packit df99a1
	  of the file. */
Packit df99a1
   virtual void	file_cleared(const GP<DjVuFile> & file);
Packit df99a1
Packit df99a1
   GPList<Item>	get_items(void);
Packit df99a1
private:
Packit df99a1
   GPList<Item>	list;
Packit df99a1
   bool		enabled;
Packit df99a1
   int		max_size;
Packit df99a1
   int		cur_size;
Packit df99a1
Packit df99a1
   int		calculate_size(void);
Packit df99a1
   void		clear_to_size(int size);
Packit df99a1
};
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
//@}
Packit df99a1
   
Packit df99a1
inline
Packit df99a1
DjVuFileCache::Item::Item(void) : time(::time(0)) {};
Packit df99a1
Packit df99a1
inline
Packit df99a1
DjVuFileCache::Item::Item(const GP<DjVuFile> & xfile) :
Packit df99a1
      file(xfile), time(::time(0)) {}
Packit df99a1
Packit df99a1
inline
Packit df99a1
DjVuFileCache::Item::~Item(void) {}
Packit df99a1
Packit df99a1
inline GP<DjVuFile>
Packit df99a1
DjVuFileCache::Item::get_file(void) const
Packit df99a1
{
Packit df99a1
   return file;
Packit df99a1
}
Packit df99a1
Packit df99a1
inline unsigned int
Packit df99a1
DjVuFileCache::Item::get_size(void) const
Packit df99a1
{
Packit df99a1
   return file->get_memory_usage();
Packit df99a1
}
Packit df99a1
Packit df99a1
inline time_t
Packit df99a1
DjVuFileCache::Item::get_time(void) const
Packit df99a1
{
Packit df99a1
   return time;
Packit df99a1
}
Packit df99a1
Packit df99a1
inline void
Packit df99a1
DjVuFileCache::Item::refresh(void)
Packit df99a1
{
Packit df99a1
   time=::time(0);
Packit df99a1
}
Packit df99a1
Packit df99a1
inline
Packit df99a1
DjVuFileCache::DjVuFileCache(const int xmax_size) :
Packit df99a1
      enabled(true), max_size(xmax_size), cur_size(0) {}
Packit df99a1
Packit df99a1
inline void
Packit df99a1
DjVuFileCache::clear(void)
Packit df99a1
{
Packit df99a1
   clear_to_size(0);
Packit df99a1
}
Packit df99a1
Packit df99a1
inline bool
Packit df99a1
DjVuFileCache::is_enabled(void) const
Packit df99a1
{
Packit df99a1
   return enabled;
Packit df99a1
}
Packit df99a1
Packit df99a1
inline int
Packit df99a1
DjVuFileCache::get_max_size(void) const
Packit df99a1
{
Packit df99a1
   return max_size;
Packit df99a1
}
Packit df99a1
Packit df99a1
#endif
Packit df99a1
Packit df99a1
inline GP<DjVuFileCache>
Packit df99a1
DjVuFileCache::create(const int max_size)
Packit df99a1
{
Packit df99a1
  return new DjVuFileCache(max_size);
Packit df99a1
}
Packit df99a1
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
#endif