src/hbservers/hbthemeserver/hbcache_p.cpp
changeset 34 ed14f46c0e55
parent 31 7516d6d86cf5
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbServers module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbcache_p.h"
       
    27 #include "hbmemoryutils_p.h"
       
    28 
       
    29 /*!
       
    30     \fn HbCache::HbCache()
       
    31     Ctor
       
    32  */
       
    33 HbCache::HbCache()
       
    34 {
       
    35 
       
    36 }
       
    37 
       
    38 /*!
       
    39     \fn HbCache::HbCache()
       
    40     Dtor
       
    41  */
       
    42 HbCache::~HbCache()
       
    43 {
       
    44     clear();
       
    45 }
       
    46 
       
    47 /*!
       
    48     \fn HbCache::value()
       
    49     Value provides a mechanism for returning the value of the cache item associated with the key
       
    50     \a key denotes the unique identifier for the cache item whose value is to be returned
       
    51 
       
    52  */
       
    53 HbCacheItem* HbCache::value(const QString &key) const
       
    54 {
       
    55     return cache.value(key, 0);
       
    56 }
       
    57 
       
    58 /*!
       
    59     \fn HbCache::cacheItem()
       
    60     Provides a mechanism for finidng whether a cache item is present in the cache.
       
    61     If found, returns the cache item and increments the reference count else returns NULL.
       
    62     \a key denotes the unique identifier for the cache item that is to be searched in the cache.
       
    63 
       
    64  */
       
    65 HbCacheItem* HbCache::cacheItem(const QString &key)
       
    66 {
       
    67     HbCacheItem *item = 0;
       
    68     if (!cache.contains(key)) {
       
    69         return 0;
       
    70     }
       
    71     // Get the cache item associated with the key
       
    72     item = cache[key];
       
    73     item->refCount++;
       
    74     //if item is also present in LRU list, remove it from there to avoid
       
    75     // deletion following LRU policy
       
    76     if (unusedResources.contains(item)) {
       
    77         unusedResources.removeAll(item);
       
    78     }
       
    79     return item;
       
    80 }
       
    81 
       
    82 /*!
       
    83     \fn HbCache::insert()
       
    84     Provides a mechanism for inserting a cache item into cache.
       
    85     If successful, this function increases item's reference count by one and returns true.
       
    86     \a key denotes the unique identifier for the cache item that is to be searched in the cache.
       
    87     \a item represents the cache-item to be inserted.
       
    88  */
       
    89 bool HbCache::insert(const QString &key, HbCacheItem *item)
       
    90 {
       
    91     if (!item) {
       
    92         return false;
       
    93     }
       
    94     cache.insert(key, const_cast<HbCacheItem *>(item));
       
    95     item->refCount++;
       
    96     //if item is also present in LRU list, remove it from there to avoid
       
    97     // deletion following LRU policy
       
    98     if (unusedResources.contains(item)) {
       
    99         unusedResources.removeAll(item);
       
   100     }
       
   101     return true;
       
   102 }
       
   103 
       
   104 /*!
       
   105     \fn HbCache::remove()
       
   106     Provides a mechanism for removing a cache item from cache.
       
   107     If successful, this function decreases item's reference count by one and if
       
   108     the reference count reaches zero for an item, it's pushed at the back of
       
   109     unused-resources list for removal later in case of OOM scenario.
       
   110     \a key denotes the unique identifier for the cache item that is to be searched in the cache.
       
   111  */
       
   112 bool HbCache::remove(const QString &key)
       
   113 {
       
   114     if (key.isEmpty() || !cache.contains(key)) {
       
   115         return false;
       
   116     }
       
   117     HbCacheItem *item = cache[key];
       
   118     //reference count can obviously be never less than zero, meaning that for all the
       
   119     //css files stored with server-css-cache, there would be minimum zero client (app)
       
   120     //associated
       
   121     if (item->refCount > 0) {
       
   122         item->refCount--;
       
   123     }
       
   124     if (item->refCount == 0) {
       
   125         //Inserting item with ref-count 0 into unused-resources list for removal policy later
       
   126         // in case of OOM condition
       
   127         unusedResources.append(item);
       
   128     }
       
   129     return true;
       
   130 }
       
   131 
       
   132 /*!
       
   133     \fn HbCache::contains()
       
   134     Returns true if an item is found in the cache corresponding to given key.
       
   135     \a key denotes the unique identifier for the cache item that is to be searched in the cache.
       
   136  */
       
   137 bool HbCache::contains(const QString &key) const
       
   138 {
       
   139     return (cache.contains(key));
       
   140 }
       
   141 
       
   142 /*!
       
   143     \fn HbCache::lruList()
       
   144     Returns a handle to the list of cache which holds cache-items with zero reference count.
       
   145  */
       
   146 QList<HbCacheItem*> &HbCache::lruList()
       
   147 {
       
   148     return unusedResources;
       
   149 }
       
   150 
       
   151 /*!
       
   152     \fn HbCache::cacheHandle()
       
   153     Returns a handle to the cache which holds (css-file-name, cacheItem) key-value pair.
       
   154  */
       
   155 QHash<QString, HbCacheItem *> &HbCache::cacheHandle()
       
   156 {
       
   157     return cache;
       
   158 }
       
   159 /*!
       
   160     \fn HbCache::clear()
       
   161     Clears the shared resources allocated by cache-items and deletes all cache-items.
       
   162  */
       
   163 void HbCache::clear()
       
   164 {
       
   165     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   166     QHash<QString, HbCacheItem*>::const_iterator itEnd(cache.constEnd());
       
   167     for (QHash<QString, HbCacheItem*>::const_iterator iter = cache.constBegin();
       
   168             iter != itEnd;
       
   169             ++iter) {
       
   170         HbCacheItem *temp = iter.value();
       
   171         manager->free(temp->offset);
       
   172         delete temp;
       
   173     }
       
   174     cache.clear();
       
   175 }