userlibandfileserver/fileserver/sfat32/sl_cache.h
changeset 2 4122176ea935
child 6 0173bcd7697c
equal deleted inserted replaced
0:a41df078684a 2:4122176ea935
       
     1 // Copyright (c) 1996-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_cache.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #ifndef SL_CACHE_H
       
    24 #define SL_CACHE_H
       
    25 
       
    26 
       
    27 //---------------------------------------------------------------------------------------------------------------------------------
       
    28 //-- dedicated FAT directory cache related stuff
       
    29 
       
    30 //-- if defined, a dedicated cache will be used for FAT directories
       
    31 #define ENABLE_DEDICATED_DIR_CACHE
       
    32 
       
    33 //---------------------------------------------------------------------------------------------------------------------------------
       
    34 
       
    35 
       
    36 /** 
       
    37     An abstract interface to the media Write-Through cache
       
    38 */
       
    39 class MWTCacheInterface
       
    40     {
       
    41 public:
       
    42         
       
    43 	/** Enums for control functions. See Control() */
       
    44 	enum TControl
       
    45 	    {
       
    46 	    EDisableCache = 0, 	///< disable/enable cache, can be used for debug purposes
       
    47 	    EDumpCache = 1, 	///< print full cache content, can be used for debug purposes
       
    48 	    ECacheInfo = 2, 	///< print cache info, can be used for debug purposes
       
    49 	    };
       
    50 
       
    51         virtual ~MWTCacheInterface() {}
       
    52         
       
    53         /** the same meaning and parameters as in CRawDisk::ReadL */
       
    54         virtual void    ReadL(TInt64 aPos, TInt aLength, TDes8& aDes)=0;
       
    55         
       
    56         /** the same meaning and parameters as in CRawDisk::WriteL */
       
    57         virtual void    WriteL(TInt64 aPos,const TDesC8& aDes)=0;
       
    58         
       
    59         /** Invalidates whole directory cache*/
       
    60         virtual void    InvalidateCache(void)=0;
       
    61 
       
    62         /** invalidate a single cache page if the aPos is cached*/
       
    63         virtual void    InvalidateCachePage(TUint64 aPos)=0;
       
    64         
       
    65         /**
       
    66         Finds out if the media position "aPosToSearch" is in the cache and returns cache page information in this case.
       
    67         
       
    68         @param  aPosToSearch    linear media position to lookup in the cache
       
    69         @param  aCachedPosStart if "aPosToSearch" is cached, here will be media position of this page start
       
    70           
       
    71         @return 0 if aPosToSearch isn't cached, otherwise  cache page size in bytes (see also aCachedPosStart).
       
    72         */
       
    73         virtual TUint32  PosCached(const TInt64& aPosToSearch, TInt64& aCachedPosStart) = 0;
       
    74         
       
    75         /**
       
    76         @return size of the cache in bytes. Can be 0.
       
    77         */
       
    78         virtual TUint32 CacheSizeInBytes() const = 0;
       
    79         
       
    80         /**
       
    81         Make the page indexed by aPos the MRU page in the cache.
       
    82         Assumes cache evicts pages according to LRU algorithm.
       
    83         */
       
    84         virtual void MakePageMRU(TInt64 aPos) = 0;
       
    85 
       
    86         /**
       
    87         @return log2 number of the size of the cache in bytes.
       
    88         */
       
    89         virtual TUint32 PageSizeInBytesLog2() const = 0;
       
    90 
       
    91         /**
       
    92         Control method.
       
    93         
       
    94           @param  aFunction   control function
       
    95           @param  aParam1     just arbitrary parameter 
       
    96           @param  aParam2     just arbitrary parameter 
       
    97           @return Standard error code.
       
    98         */
       
    99         virtual TInt Control(TUint32 aFunction, TUint32 aParam1, TAny* aParam2)=0;
       
   100         
       
   101         /**
       
   102         Set cache base position at aBasePos
       
   103         @param  aBasePos  base position of the cache pages. Affects pages alignment.
       
   104         */
       
   105         virtual void SetCacheBasePos(TInt64 aBasePos)=0;
       
   106         
       
   107     };
       
   108 
       
   109 //---------------------------------------------------------------------------------------------------------------------------------
       
   110 
       
   111 /**
       
   112 This class represents the media Write-Through cache page
       
   113 */
       
   114 class CWTCachePage
       
   115     {
       
   116 public:   
       
   117         
       
   118         static CWTCachePage* NewL(TUint32 aPageSizeLog2);
       
   119         void ConstructL(TUint32 aPageSizeLog2);
       
   120         
       
   121         ~CWTCachePage();
       
   122         
       
   123         inline TBool   PosCached(TInt64 aPos) const;
       
   124         inline TUint32 PosInCachePage(TInt64 aPos) const; 
       
   125         inline TUint8* PtrInCachePage(TInt64 aPos) const; 
       
   126         inline TUint32 PageSize() const;
       
   127         
       
   128 protected:
       
   129         
       
   130         CWTCachePage();
       
   131         CWTCachePage(const CWTCachePage&);
       
   132         CWTCachePage& operator=(const CWTCachePage&);
       
   133         
       
   134 public:
       
   135         
       
   136         TInt32  iValid;     ///< 0 if the page doesn't contain valid data
       
   137         TInt64  iStartPos;  ///< cache page base media position
       
   138         RBuf8   iData;      ///< page Data
       
   139     };
       
   140 
       
   141 //---------------------------------------------------------------------------------------------------------------------------------
       
   142 
       
   143 /**
       
   144     Media Write-through cache.
       
   145 */
       
   146 class CMediaWTCache : public CBase, public MWTCacheInterface
       
   147     {
       
   148 public:
       
   149         ~CMediaWTCache();
       
   150         
       
   151         static CMediaWTCache* NewL(TFatDriveInterface& aDrive, TUint32 aNumPages, TUint32 aPageSizeLog2);
       
   152 
       
   153         void ConstructL(TUint32 aNumPages, TUint32 aPageSizeLog2);
       
   154         
       
   155         //-- overloads from the base class
       
   156         void    ReadL (TInt64 aPos,TInt aLength,TDes8& aDes);
       
   157         void    WriteL(TInt64 aPos,const TDesC8& aDes);
       
   158         void    InvalidateCache(void);
       
   159         void    InvalidateCachePage(TUint64 aPos);
       
   160 
       
   161 
       
   162         TUint32 PosCached(const TInt64& aPosToSearch, TInt64& aCachedPosStart);
       
   163         TUint32 CacheSizeInBytes()  const;
       
   164         void 	MakePageMRU(TInt64 aPos);
       
   165         TUint32 PageSizeInBytesLog2()	const;
       
   166         TInt    Control(TUint32 aFunction, TUint32 aParam1, TAny* aParam2);
       
   167         inline void SetCacheBasePos(TInt64 aBasePos);
       
   168         //--
       
   169         
       
   170 protected:
       
   171         CMediaWTCache();
       
   172         CMediaWTCache(TFatDriveInterface& aDrive);
       
   173         
       
   174         inline TInt64  CalcPageStartPos(TInt64 aPos) const;
       
   175         inline TUint32 PageSize() const;
       
   176         
       
   177         void MakePageLRU(TInt aPageNo);
       
   178         
       
   179         TInt    FindPageByPos(TInt64 aPos) const;
       
   180         TUint32 GrabPage() const;
       
   181         TUint32 GrabReadPageL(TInt64 aPos);
       
   182         TUint32 FindOrGrabReadPageL(TInt64 aPos);
       
   183         
       
   184 protected:
       
   185         TFatDriveInterface& iDrive;        ///< reference to the driver for media access
       
   186         TUint32             iPageSizeLog2; ///< Log2 (cache page size)
       
   187         mutable TBool       iAllPagesValid;///< ETrue if all cache pages have valid data
       
   188         TInt64              iCacheBasePos; ///< Cache pages base position, used to align them at cluster size
       
   189         RPointerArray<CWTCachePage> iPages; ///< array of pointers to the cache pages. Used for organising LRU list
       
   190         TUint32             iCacheDisabled :1; ///< if not 0 the cache is disabled totally and all reads and writes go via TFatDriveInterface directly
       
   191     };
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 #include"sl_cache.inl"
       
   197 
       
   198 #endif //SL_CACHE_H
       
   199 
       
   200 
       
   201 
       
   202