QGpCoreTools/Cache.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2006-07-06
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef CACHE_H
00029 #define CACHE_H
00030 
00031 #include "QGpCoreToolsDLLExport.h"
00032 #include "Translations.h"
00033 
00034 namespace QGpCoreTools {
00035 
00036 #define INV_MEGABYTES 9.5367431640625e-07
00037 #define MEGABYTES 1048576.0
00038 
00039 class CacheItem;
00040 
00041 class AllocatedCacheItem : private QList<const CacheItem *>
00042 {
00043   TRANSLATIONS("AllocatedCacheItem")
00044 public:
00045   AllocatedCacheItem() {}
00046   ~AllocatedCacheItem() {}
00047 
00048   const CacheItem * bestToFree();
00049 
00050   void add(const CacheItem * sig) {append(sig);}
00051   void remove(const CacheItem * sig) {removeAt(indexOf(sig));}
00052   void clear() {QList<const CacheItem *>::clear();}
00053   int count() const {return QList<const CacheItem *>::count();}
00054 
00055   QString humanInfo() const;
00056 };
00057 
00058 class QGPCORETOOLS_EXPORT Cache
00059 {
00060   TRANSLATIONS("Cache")
00061 public:
00062   Cache();
00063   ~Cache() {}
00064 
00065   // Parameters, not changed while cache is running
00066   void setSize(double nMegaBytes) {_maxLoadedBytes=nMegaBytes * MEGABYTES;}
00067   void setSwapDir(const QDir& d);
00068 
00069   // Memory allocation and desallocation
00070   bool makeAvailable(const CacheItem * item);
00071   void free(CacheItem * item, bool saveTemp=true);
00072   bool enlarge(CacheItem * item, double finalSize);
00073 
00074   double freeBytes() const {return _maxLoadedBytes -_loadedBytes;}
00075   double freeMegaBytes() const {return freeBytes() * INV_MEGABYTES;}
00076 private:
00077   friend class CacheItem;
00078   bool free(double byteSize);
00079 
00080   AllocatedCacheItem _allocatedList;
00081   double _maxLoadedBytes;
00082   double _loadedBytes;
00083   QDir _swapDir;
00084   mutable QMutex _mutex;
00085 };
00086 
00087 } // namespace QGpCoreTools
00088 
00089 #endif // CACHE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines