userlibandfileserver/fileserver/sfat32/sl_fatcache.h
changeset 2 4122176ea935
child 6 0173bcd7697c
equal deleted inserted replaced
0:a41df078684a 2:4122176ea935
       
     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\sfat\sl_facache.h
       
    15 // FAT cache base classes definition
       
    16 // FAT12 and FAT16 cache classes definition
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23 */
       
    24 
       
    25 #ifndef SL_FAT_CACHE_H
       
    26 #define SL_FAT_CACHE_H
       
    27 
       
    28 
       
    29 //-----------------------------------------------------------------------------
       
    30 
       
    31 /**
       
    32     A simple abstraction of the 32 bit flags
       
    33 */
       
    34 class T32Bits
       
    35 {
       
    36  public:
       
    37     T32Bits() : iData(0) {}
       
    38 
       
    39     inline void  Clear();
       
    40     inline TBool HasBitsSet() const;
       
    41     inline void SetBit(TUint32 aIndex);
       
    42     inline TBool operator[](TUint32 aIndex) const;
       
    43 
       
    44  private:
       
    45     TUint32 iData; ///< 32 bits data
       
    46 };
       
    47 
       
    48 //-----------------------------------------------------------------------------
       
    49 
       
    50 class CFatBitCache;
       
    51 
       
    52 /**
       
    53     An abstract base class for all types of FAT caches.
       
    54     Provides user interface and some common for all types of FAT caches functionality.
       
    55 */
       
    56 class CFatCacheBase : public CBase
       
    57 {
       
    58  public:
       
    59 
       
    60     virtual ~CFatCacheBase();
       
    61 
       
    62     //-- public interface
       
    63     virtual void Close(TBool /*aDiscardDirtyData*/) {};
       
    64     virtual void FlushL() = 0;
       
    65 
       
    66     virtual TUint32 ReadEntryL(TUint32 aIndex) = 0;
       
    67     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry) = 0;
       
    68     
       
    69     virtual TInt Invalidate() = 0;
       
    70     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries) = 0;
       
    71 
       
    72     TInt ReadFatData(TUint32 aPos, TUint32 aLen, TDes8& aData) const;
       
    73     TInt WriteFatData(TUint32 aPos, const TDesC8& aData) const;
       
    74 
       
    75     inline TUint32  FatStartPos() const;
       
    76     inline TUint32  FatSize() const;
       
    77     inline TFatType FatType() const;
       
    78 
       
    79  public:
       
    80     
       
    81     //-- auxilary interface to additional bit supercache (it may exist only in FAT32 cache implementation)
       
    82     virtual CFatBitCache* BitCacheInterface();
       
    83 
       
    84 
       
    85  protected:
       
    86     CFatCacheBase();
       
    87 
       
    88     virtual void InitialiseL(CFatMountCB* aOwner);
       
    89 
       
    90     inline TBool IsDirty() const;
       
    91     inline void SetDirty(TBool aDirty);
       
    92     inline TUint NumFATs() const;
       
    93 
       
    94     TBool CheckInvalidatingDirtyCache() const;
       
    95 
       
    96     inline TUint FAT_SectorSzLog2() const;
       
    97     inline TUint FAT_SectorSz() const; 
       
    98     inline TUint FAT_ClusterSzLog2() const;
       
    99 
       
   100  protected:
       
   101     
       
   102     enum {KInvalidFatNo = 0xFF}; ///< used to invalidate current FAT no.
       
   103     TUint   iCurrentFatNo;       ///< current FAT number WriteFatData will write to.
       
   104 
       
   105  private:    
       
   106     //-- values cached from owning mount.
       
   107     TUint32     iFatStartPos;   ///< media position of FAT1 start 
       
   108     TUint32     iFatSize;       ///< size of FAT in bytes
       
   109     TUint16     iNumFATs;       ///< number of FATs on the volume
       
   110     TUint16     iFatSecSzLog2;  ///< Log2(FAT Sector size)
       
   111     TUint16     iFatClustSzLog2;///< Log2(FAT cluster size)
       
   112     TFatType    iFatType;       ///< FAT type
       
   113     TFatDriveInterface* ipDrive;///< interface to the media driver
       
   114     //---
       
   115 
       
   116     TBool       iDirty;         ///< ETrue if the cache is dirty
       
   117 };
       
   118 
       
   119 
       
   120 //-----------------------------------------------------------------------------
       
   121 
       
   122 /**
       
   123     Fixed FAT12 cache. This is a contiguous cache that caches whole FAT12.
       
   124     This cache is logically divided to sectors, maximal number of sectors in this cache is KMaxSectorsInCache (32).
       
   125     
       
   126     Read granularity: whole cache; anyway it can't be larger than 6126 bytes.
       
   127     Write granularity: cache sector size, which is always "FAT Sector Size" and non-configurable.
       
   128 */
       
   129 class CFat12Cache : public CFatCacheBase
       
   130 {
       
   131  public:
       
   132     static CFat12Cache* NewL(CFatMountCB* aOwner, TUint32 aFatSize);
       
   133 
       
   134     //-- overrides from base class
       
   135     virtual void Close(TBool aDiscardDirtyData);
       
   136     virtual void FlushL();
       
   137 
       
   138     virtual TUint32 ReadEntryL(TUint32 aIndex);
       
   139     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
       
   140 
       
   141     virtual TInt Invalidate();
       
   142     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
       
   143     //------------------------------------
       
   144 
       
   145  private:
       
   146     
       
   147     void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize); 
       
   148 
       
   149     CFat12Cache();
       
   150     CFat12Cache(const CFat12Cache&);
       
   151     CFat12Cache& operator=(const CFat12Cache&);
       
   152 
       
   153 
       
   154     inline TUint32 NumSectors() const;
       
   155     void AssertCacheReallyClean() const;
       
   156 
       
   157  private:
       
   158 
       
   159     enum {KMaxSectorsInCache = 32};  ///< maximal number sectors in FAT12 cache
       
   160     enum {KFat12EntryMask = 0x0FFF}; ///< FAT12 entry mask
       
   161 
       
   162     TUint32 iSectorsInCache;    ///< total number sectors in the cache, KMaxSectorsInCache max.
       
   163     T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
       
   164     RBuf8   iData;              ///< Whole FAT12 cache data.
       
   165 };
       
   166 
       
   167 
       
   168 //-----------------------------------------------------------------------------
       
   169 
       
   170 /**
       
   171     Abstract base class for paged caches, i.e. those that consist of some number of cache pages.
       
   172     In this case the most of the functionality is implemented in page classes and this is just a page container.
       
   173     Each cache page in turn is logically divided to sectors. The sector is a logical unit of write granularity
       
   174     See also CFatCachePageBase et al.
       
   175 */
       
   176 class CFatPagedCacheBase : public CFatCacheBase
       
   177 {
       
   178  public:
       
   179 
       
   180     inline TUint PageSizeLog2()  const;
       
   181     inline TUint PageSize()      const;
       
   182     
       
   183     inline TUint SectorSizeLog2() const;
       
   184     inline TUint SectorsInPage()  const;
       
   185 
       
   186  protected:
       
   187     CFatPagedCacheBase();
       
   188 
       
   189  protected:
       
   190     
       
   191     enum {KMaxSectorsInPage = 32}; ///< maximal number sectors in FAT cache page
       
   192 
       
   193     TUint iPageSizeLog2;    ///< Log2(page size)
       
   194     TUint iSectorSizeLog2;  ///< Log2(page sector size)
       
   195  
       
   196 };
       
   197 
       
   198 //-----------------------------------------------------------------------------
       
   199 
       
   200 class CFat16FixedCachePage;
       
   201 
       
   202 /**
       
   203     FAT16 fixed paged cache. Used for FAT16 only and caches whole FAT16 (its max size is 131048 bytes).
       
   204     Consists of the fixed array of cache pages; Pages are allocated on demand and never get evicted.
       
   205     Each page is logically divided to page sectors. The number of pages depends on the FAT16 size.
       
   206 
       
   207     Read granularity: One page, which size is 2^aRdGranularityLog2
       
   208     Write granularity: cache's page sector; its size is 2^aWrGranularityLog2
       
   209 */
       
   210 class CFat16FixedCache : public CFatPagedCacheBase
       
   211 {
       
   212  public:
       
   213 
       
   214     static CFat16FixedCache* NewL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2);
       
   215 
       
   216     //-- overrides from base class
       
   217     virtual void Close(TBool aDiscardDirtyData);
       
   218     virtual void FlushL();
       
   219 
       
   220     virtual TUint32 ReadEntryL(TUint32 aIndex);
       
   221     virtual void WriteEntryL(TUint32 aIndex, TUint32 aEntry);
       
   222     
       
   223 
       
   224     virtual TInt Invalidate();
       
   225     virtual TInt InvalidateRegion(TUint32 aStartEntry, TUint32 aNumEntries);
       
   226     //------------------------------------
       
   227 
       
   228  private:
       
   229 
       
   230     void InitialiseL(CFatMountCB* aOwner, TUint32 aFatSize, TUint32 aRdGranularityLog2, TUint32 aWrGranularityLog2); 
       
   231     
       
   232     CFat16FixedCache();
       
   233     CFat16FixedCache(const CFat16FixedCache&);
       
   234     CFat16FixedCache& operator=(const CFat16FixedCache&);
       
   235 
       
   236     inline TUint NumPages() const;
       
   237     void AssertCacheReallyClean() const;
       
   238 
       
   239  private:    
       
   240     RPointerArray<CFat16FixedCachePage> iPages;  ///< array of pointer to the cahe pages; if the entry is NULL, it means that the page isn't allocated yet.
       
   241 
       
   242 };
       
   243 
       
   244 
       
   245 //-----------------------------------------------------------------------------
       
   246 
       
   247 
       
   248 /**
       
   249     An abstract base class for the cache page. Paged caches, i.e derived form CFatPagedCacheBase uses this functionality.
       
   250     Provides an interface and common functionality for all types of cache pages.
       
   251 
       
   252     The FAT cache page contains a number of FAT16 or FAT32 entries, their number is always the power of 2.
       
   253     The page is logically divided into sectors, the maximal number of sectors in the page is KMaxSectorsInPage (32).
       
   254     The page read granularity is whole page and the write granularity is the sector  (see aRdGranularityLog2, aWrGranularityLog2 from the cache)
       
   255 
       
   256     The caching is write-back, i.e WriteCachedEntryL() modifies data in the cache and marks corresponding page sector as dirty.
       
   257     FlushL() shall be called to flust all dirty sectors in page to the media
       
   258 
       
   259 */
       
   260 class CFatCachePageBase : public CBase
       
   261 {
       
   262 public:
       
   263     
       
   264     ~CFatCachePageBase();
       
   265 
       
   266     //----------------
       
   267     virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult) = 0;
       
   268     virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry) = 0; 
       
   269     virtual TUint32 ReadFromMediaL(TUint32 aFatIndex) = 0;
       
   270     virtual void FlushL(TBool aKeepDirty);
       
   271     
       
   272     //----------------
       
   273     inline TBool IsEntryCached(TUint32 aFatIndex) const ;
       
   274     void Invalidate(TBool aIgnoreDirtyData = EFalse);
       
   275     
       
   276     inline TBool IsDirty() const;
       
   277     inline TBool IsValid() const;
       
   278     
       
   279     inline TUint32 StartFatIndex() const;
       
   280 
       
   281 protected:
       
   282     CFatCachePageBase(CFatPagedCacheBase& aCache);
       
   283 
       
   284     /** possible states of the page */
       
   285     enum TState
       
   286         {
       
   287         EInvalid, ///< the page's data are invalid
       
   288         EClean,   ///< the page is clean, data valid and the same as on the media  
       
   289         EDirty    ///< the page is dirty, there are data eventually to be flushed to the media, iDirtySectors contains dirty sectors bitmap.
       
   290         };
       
   291 
       
   292     inline void SetState(TState aState);
       
   293     inline TState State() const;
       
   294     inline void SetClean();
       
   295     inline TUint32 PageSize() const; 
       
   296     inline TUint32 NumSectors() const; 
       
   297     
       
   298     virtual void DoWriteSectorL(TUint32 aSector)=0;
       
   299     inline TUint32 EntriesInPage() const;
       
   300 
       
   301 protected:
       
   302     TUint32 iStartIndexInFAT;   ///< FAT index this page starts from
       
   303     T32Bits iDirtySectors;      ///< dirty sectors bitmap. '1' bit corresponds to the dirty sector;
       
   304     CFatPagedCacheBase& iCache; ///< reference to the owher cache
       
   305     RBuf8   iData;              ///< page Data
       
   306 
       
   307 private:
       
   308     TState  iState;             ///< page state
       
   309     TUint32 iFatEntriesInPage;  ///< number of FAT entries in the page. 
       
   310 
       
   311 };
       
   312 
       
   313 
       
   314 //---------------------------------------------------------------------------------------------------------------------------------
       
   315 
       
   316 /**
       
   317     FAT16 cache page. Used only by CFat16FixedCache.
       
   318 */
       
   319 class CFat16FixedCachePage : public CFatCachePageBase
       
   320 {
       
   321  public:
       
   322     ~CFat16FixedCachePage() {}
       
   323     
       
   324     static CFat16FixedCachePage* NewL(CFatPagedCacheBase& aCache);
       
   325 
       
   326     //-- overrides
       
   327     virtual TBool ReadCachedEntryL (TUint32 aFatIndex, TUint32& aResult);
       
   328     virtual TBool WriteCachedEntryL(TUint32 aFatIndex, TUint32 aFatEntry); 
       
   329     virtual TUint32 ReadFromMediaL(TUint32 aFatIndex);
       
   330     //----
       
   331 
       
   332  private:
       
   333     CFat16FixedCachePage(CFatPagedCacheBase& aCache);
       
   334 
       
   335     //-- outlaws here
       
   336     CFat16FixedCachePage();
       
   337     CFat16FixedCachePage(const CFat16FixedCachePage&);
       
   338     CFat16FixedCachePage& operator=(const CFat16FixedCachePage&);
       
   339 
       
   340     virtual void DoWriteSectorL(TUint32 aSector);
       
   341 
       
   342     inline TFat16Entry* GetEntryPtr(TUint32 aFatIndex) const;
       
   343 
       
   344  private:
       
   345     enum {KFat16EntryMask = 0xFFFF}; ///< FAT16 entry mask
       
   346 };
       
   347 
       
   348 
       
   349 
       
   350 //---------------------------------------------------------------------------------------------------------------------------------
       
   351 
       
   352 
       
   353 
       
   354 #include "sl_fatcache.inl"
       
   355 
       
   356 
       
   357 #endif //SL_FAT_CACHE_H
       
   358 
       
   359 
       
   360 
       
   361 
       
   362 
       
   363 
       
   364 
       
   365 
       
   366 
       
   367 
       
   368 
       
   369 
       
   370 
       
   371 
       
   372 
       
   373 
       
   374 
       
   375 
       
   376 
       
   377 
       
   378 
       
   379 
       
   380