userlibandfileserver/fileserver/sfat32/inc/sl_fatcache32.h
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32\sfat32\inc\sl_facache32.h
       
    15 // FAT32 various cache classes definition
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23 
       
    24 #ifndef SL_FAT_CACHE_32_H
       
    25 #define SL_FAT_CACHE_32_H
       
    26 
       
    27 #include "sl_fatcache.h"
       
    28 
       
    29 class CFat32LruCachePage;
       
    30 
       
    31 
       
    32 //---------------------------------------------------------------------------------------------------------------------------------
       
    33 
       
    34 /**
       
    35     FAT32 LRU paged cache. Used for FAT32 only.
       
    36 
       
    37     Consists of LRU list of cache pages; Each page is logically divided to page sectors. The number of pages depends on the
       
    38     maximal amount of memory this cache allowed to use.  Usually, whole FAT32 can not be cached fully because of its large size.
       
    39     So, this type caches the most receinlly used areas of the FAT32. This is the wriite-back cache.
       
    40  
       
    41     Read granularity: One page, which size is 2^aRdGranularityLog2
       
    42     Write granularity: cache's page sector; its size is 2^aWrGranularityLog2
       
    43 */
       
    44 class CFat32LruCache : public CFatPagedCacheBase
       
    45 {
       
    46  public:
       
    47 
       
    48     static CFat32LruCache* NewL(CFatMountCB* aOwner, TUint32 aMaxMemSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2);
       
    49 
       
    50     //-- overrides from base class
       
    51     virtual void Close(TBool aDiscardDirtyData);
       
    52     virtual void FlushL();
       
    53 
       
    54     virtual TUint32 ReadEntryL(TUint32 aIndex);
       
    55     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
       
    56 
       
    57     virtual TInt Invalidate();
       
    58     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
       
    59     
       
    60 
       
    61     virtual CFatBitCache* BitCacheInterface();
       
    62 
       
    63  public:     
       
    64      TBool FindFreeEntryInCacheSectorL(TUint32& aFatEntryIndex);
       
    65 
       
    66  private:
       
    67     
       
    68     void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2);
       
    69     
       
    70     CFat32LruCache();
       
    71     CFat32LruCache(const CFat32LruCache&);
       
    72     CFat32LruCache& operator=(const CFat32LruCache&);
       
    73     
       
    74     TBool ReadCachedEntryL(TUint32 aFatIndex, TFat32Entry& aResult);
       
    75     TBool WriteCachedEntryL(TUint32 aFatIndex, TFat32Entry aFatEntry);
       
    76 
       
    77     CFat32LruCachePage* DoGetSpareCachePageL();
       
    78 
       
    79     void AssertCacheReallyClean() ;
       
    80 
       
    81  private:
       
    82 
       
    83     typedef TDblQue<CFat32LruCachePage> TPageList;
       
    84     typedef TDblQueIter<CFat32LruCachePage> TPageIterator;
       
    85 
       
    86     TUint32   iMaxFatEntries;       ///< maximal number of FAT entries in FAT table
       
    87     TUint     iNumPagesAllocated;   ///< number of pages currently allocated
       
    88     TUint     iMaxPages;            ///< maximal pages allowed to allocate
       
    89     TPageList iPageList;            ///< LRU list of cache pages.
       
    90 
       
    91     
       
    92     CFatBitCache *iBitCache; ///< pointer to the FAT bit supercache
       
    93 
       
    94 };
       
    95 
       
    96 
       
    97 //---------------------------------------------------------------------------------------------------------------------------------
       
    98 
       
    99 /**
       
   100     FAT32 LRU cache page. Used only by CFat32LruCache. 
       
   101 */
       
   102 class CFat32LruCachePage : public CFatCachePageBase
       
   103 {
       
   104  public:
       
   105 
       
   106     static CFat32LruCachePage* NewL(CFatPagedCacheBase& aCache);
       
   107 
       
   108     //-- overrides
       
   109     virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult);
       
   110     virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry); 
       
   111     virtual TUint32 ReadFromMediaL(TUint32 aFatIndex);
       
   112     //----
       
   113 
       
   114  private:
       
   115     CFat32LruCachePage(CFatPagedCacheBase& aCache);
       
   116 
       
   117     //-- outlaws here
       
   118     CFat32LruCachePage();
       
   119     CFat32LruCachePage(const CFat32LruCachePage&);
       
   120     CFat32LruCachePage& operator=(const CFat32LruCachePage&);
       
   121 
       
   122     inline TFat32Entry* GetEntryPtr(TUint32 aFatIndex) const;
       
   123     virtual void DoWriteSectorL(TUint32 aSector);
       
   124 
       
   125  private: 
       
   126     enum {KFat32EntryMask = 0x0FFFFFFF}; ///< FAT32 entry mask 
       
   127 
       
   128  public:
       
   129     TDblQueLink iLink; ///< list link object. See TPageList
       
   130 };
       
   131 
       
   132 //---------------------------------------------------------------------------------------------------------------------------------
       
   133 
       
   134 /**
       
   135     FAT32 bit supercache. This is a special cache above the CFat32LruCache.
       
   136     Used for quick lookup for the free entries in FAT32 table without accessing media and thrashing FAT32 LRU cache.
       
   137     
       
   138     Logically consists of a bit vector, where each bit represents one FAT sector (one unit of read granularity within CFat32LruCachePage)
       
   139     for the _whole_ FAT table.
       
   140     
       
   141     If some bit in the vector is set to '1' it means that the corresponding FAT cache sector (not necessarily currently cached) _may_ have
       
   142     a at least one free FAT entry. If the bit is '0' it means that there is no free fat entries in this part of the FAT.
       
   143 
       
   144     The situation when bit set to '1' corresponds to the FAT cache sector without free entries is quite possible, but it is resolved by this cache.
       
   145     The situation when '0' bit corresponds to the fat sector that _does_ contain free entries is extremely unlikely and can be
       
   146     caused by direct raw writes to the FAT, for example. Nothing terribly wrong with this situation, the search for free entry will fall back
       
   147     to the old algorithm CFatTable::FindClosestFreeClusterL() .
       
   148     
       
   149     The information in this cache is also updated on flushing dirty sectros by  CFat32LruCachePage.
       
   150 
       
   151 
       
   152 */
       
   153 class CFatBitCache : public CBase
       
   154 {
       
   155  public:
       
   156 
       
   157     ~CFatBitCache();
       
   158  
       
   159     static CFatBitCache* New(CFat32LruCache& aOnwerFatCache);
       
   160 
       
   161     void Close();
       
   162    
       
   163     TBool StartPopulating();
       
   164     TBool FinishPopulating(TBool aSuccess);
       
   165 
       
   166     
       
   167     /** possible states of this cache */
       
   168     enum TState
       
   169         {
       
   170         EInvalid,       ///< initial, invalid
       
   171         ENotPopulated,  ///< initialised, but not populated yet
       
   172         EPopulating,    ///< in the process of populating
       
   173         EPopulated,     ///< successfully populated; the only consistent state.
       
   174         };
       
   175     
       
   176     inline TState State() const;
       
   177     inline TBool  UsableState() const;
       
   178     inline TBool  FatSectorHasFreeEntry(TUint32 aFatSectorNum) const;
       
   179     inline void   SetFreeEntryInFatSector(TUint32 aFatSectorNum, TBool aHasFreeEntry);
       
   180 
       
   181     TBool SetFreeFatEntry(TUint32 aFatIndex);
       
   182     TInt  FindClosestFreeFatEntry(TUint32& aFatIndex);
       
   183     void  MarkFatRange(TUint32 aStartFatIndex, TUint32 aEndFatIndex, TBool aAsFree);
       
   184 
       
   185 
       
   186     void Dump() const;
       
   187 
       
   188  private:
       
   189 
       
   190     //-- outlaws
       
   191     CFatBitCache();
       
   192     CFatBitCache(const CFatBitCache&);
       
   193     CFatBitCache& operator=(const CFatBitCache&);
       
   194 
       
   195     CFatBitCache(CFat32LruCache& aOnwerFatCache);
       
   196 
       
   197     TInt Initialise();
       
   198 
       
   199     inline void SetState(TState aState);
       
   200     inline TUint32 FatIndexToCacheSectorNumber(TUint32 aFatIndex) const;
       
   201     inline TUint32 CacheSectorNumberToFatIndex(TUint32 aCacheSecNum) const;
       
   202 
       
   203  private:
       
   204 
       
   205     TState          iState;             ///< internal state of the cache
       
   206     RBitVector      iBitCache;          ///< bit vector itself
       
   207     TUint32         iFatIdxToSecCoeff;  ///< Log2(FatCacheSectorSize/Sizeof(FAT32 entry)). Used to convert FAT32 index to FAT32 cache sector number and back.
       
   208     CFat32LruCache& iOwnerFatCache;     ///< reference to the owner FAT32 LRU cache
       
   209 
       
   210     DBG_STATEMENT(TUint  iPopulatingThreadId;)  ///< used in debug mode for checking multithreading issues
       
   211 };
       
   212 
       
   213 //---------------------------------------------------------------------------------------------------------------------------------
       
   214 
       
   215 #include "sl_fatcache32.inl"
       
   216 
       
   217 #endif //SL_FAT_CACHE_32_H
       
   218 
       
   219 
       
   220 
       
   221 
       
   222 
       
   223 
       
   224 
       
   225 
       
   226 
       
   227 
       
   228 
       
   229 
       
   230 
       
   231 
       
   232 
       
   233 
       
   234 
       
   235 
       
   236 
       
   237 
       
   238 
       
   239 
       
   240