os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/sl_dir_cache.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-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_dir_cache.h
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @internalTechnology
    21 */
    22 
    23 #ifndef SL_DIR_CACHE_H
    24 #define SL_DIR_CACHE_H
    25 
    26 #include "sf_memory_man.h"
    27 #include "sf_memory_client.h"
    28 #include "sl_cache.h"
    29 #include <e32hashtab.h>
    30 
    31 //---------------------------------------------------------------------------------------------------------------------------------
    32 class 	CDynamicDirCache;
    33 
    34 
    35 /**
    36 The dynamic directory cache page class
    37 */
    38 class TDynamicDirCachePage
    39     {
    40 public:
    41 	enum TPageType
    42 		{
    43 		EUnknown,
    44 		ELocked,
    45 		EUnlocked,
    46 		};
    47 
    48 public:
    49 	~TDynamicDirCachePage();
    50 	static TDynamicDirCachePage* NewL(CDynamicDirCache* aOwnerCache, TInt64 aStartMedPos, TUint8* aStartRamAddr);
    51 	
    52 	inline void	SetLocked(TBool);
    53 	inline TBool	IsLocked() const;
    54 	inline TUint8*	StartPtr()	const;
    55 	inline void 	SetStartPtr(TUint8* aPtr);
    56 	inline void	SetValid(TBool aIsValid);
    57 	inline TBool	IsValid()	const;
    58 	inline void	SetPageType(TPageType aType);
    59 	inline TPageType PageType();
    60 
    61 	inline TUint32	PageSizeInBytes() const;
    62     inline TUint32	PageSizeInSegs() const;
    63 
    64     inline void	SetPos(TInt64 aPos);
    65     inline void	ResetPos();
    66 	inline TInt64	StartPos()	const;
    67 
    68 	inline TUint8*	PtrInPage(TInt64 aPos) const;
    69 	inline TBool	PosCachedInPage(TInt64 aPos) const;
    70 
    71 	inline void	Deque();
    72 
    73 private:
    74 	// declared to disable copying and assignment
    75 	TDynamicDirCachePage& operator=(const TDynamicDirCachePage&);
    76 	TDynamicDirCachePage(const TDynamicDirCachePage&);
    77 
    78 	// private constructor, as this class is not supposed to be created on stack
    79 	TDynamicDirCachePage(CDynamicDirCache* aOwnerCache, TInt64 aStartMedPos, TUint8* aStartRamAddr);
    80 
    81 public:
    82 	TDblQueLink			iLink;			///< the embedded link object, see TCachePageList
    83     TInt64  			iStartMedPos;	///< the starting media address that this page caches
    84     TUint8*				iStartRamAddr;	///< the starting ram address that thsi page lives
    85     CDynamicDirCache* 	iOwnerCache;	///< pointer to the cache that owns this page
    86     TBool				iValid 	:1;		///< flag to indicate the validity of the page content
    87 	TBool				iLocked :1;		///< flag to indicate if the page is locked or not
    88 	TPageType			iType;			///< page type, see TPageType
    89     };
    90 
    91 /**
    92 The lookup table entry class
    93 @see	CDynamicDirCache
    94 */
    95 class TLookupEntry
    96 	{
    97 	public:
    98 		TLookupEntry(): iPos(0), iRange(0), iPage(NULL) {};
    99 		TLookupEntry(TInt64 aPos, TUint32 aRange, TDynamicDirCachePage* aPage): iPos(aPos), iRange(aRange), iPage(aPage) {};
   100 	public:
   101 		TInt64 					iPos;
   102 		TUint32					iRange;
   103 		TDynamicDirCachePage* 	iPage;
   104 	};
   105 
   106 //---------------------------------------------------------------------------------------------------------------------------------
   107 typedef TDblQue<TDynamicDirCachePage> TCachePageList;
   108 /**
   109 Dynamic directory cache.
   110 For now it is directly derived from MWTCacheInterface.
   111 Provides caching FAT directory data.
   112 */
   113 class CDynamicDirCache : public CBase, public MWTCacheInterface
   114     {
   115 public:
   116 	~CDynamicDirCache();
   117 	static CDynamicDirCache* NewL(TDriveInterface& aDrive, TUint32 aMinPageNum, TUint32 aMaxPageNum, TUint32 aPageSizeLog2, const TDesC& aClientName);
   118 
   119 	//-- overloads from the base class
   120 	void    ReadL (TInt64 aPos, TInt aLength, TDes8& aDes);
   121 	void    WriteL(TInt64 aPos, const TDesC8& aDes);
   122 	void    InvalidateCache(void);
   123     void    InvalidateCachePage(TUint64 aPos);
   124 
   125 	TUint32 PosCached(const TInt64& aPosToSearch, TInt64& aCachedPosStart);
   126 	TUint32 CacheSizeInBytes()  const;
   127 	TInt    Control(TUint32 aFunction, TUint32 aParam1, TAny* aParam2);
   128 	void 	SetCacheBasePos(TInt64 aBasePos);
   129 	void 	MakePageMRU(TInt64 aPos);
   130 	TUint32	PageSizeInBytesLog2() const;
   131 	
   132 	TUint32 PageSizeInSegs() const;
   133 
   134     // Debugging functions
   135 	void Dump();
   136 	void Info() const;
   137 
   138 protected:
   139 	CDynamicDirCache(TDriveInterface& aDrive, TUint32 aMinSizeInBytes, TUint32 aMaxSizeInBytes, TUint32 aPageSizeInBytesLog2);
   140 	void ConstructL(const TDesC& aClientName);
   141 
   142 	void ReadDataFromSinglePageL(TInt64 aPos, TInt aLength, TDes8& aDes);
   143 	void WriteDataOntoSinglePageL(TInt64 aPos, const TUint8* aData, TUint32 aDataLen);
   144 	TDynamicDirCachePage* FindPageByPos(TInt64 aPos);
   145 	TDynamicDirCachePage* UpdateActivePageL(TInt64 aPos);
   146 	TDynamicDirCachePage* AllocateAndLockNewPageL(TInt64 aStartMedPos);
   147 	TUint8* LockPage(TDynamicDirCachePage* aPage);
   148 	TInt 	UnlockPage(TDynamicDirCachePage* aPage);
   149 	TInt 	DecommitPage(TDynamicDirCachePage* aPage);
   150 	inline TInt64  CalcPageStartPos(TInt64 aPos) const;
   151 	void CheckThresholds();
   152 	inline TBool CacheIsFull() const;
   153 	inline TUint32 MaxCacheSizeInPages() const;
   154 	TInt DeQueue(TDynamicDirCachePage* aPage);
   155 	TInt AddFirstOntoQueue(TDynamicDirCachePage* aPage, TDynamicDirCachePage::TPageType aType);
   156 	TInt LookupTblRemove(TInt64 aPagePos);
   157 	TInt LookupTblAdd(TDynamicDirCachePage* aPage);
   158 	TDynamicDirCachePage* LookupTblFind(TInt64 aPos);
   159 	TInt ResetPagePos(TDynamicDirCachePage* aPage);
   160 	void MakePageLastLocked(TDynamicDirCachePage* aPage);
   161 	void DoMakePageMRU(TInt64 aPos);
   162 	void DoInvalidateCache(void);
   163 	
   164 private:
   165 	TUint32				iPageSizeLog2;		///< log2 value of cache pages size in bytes
   166 	TUint32				iMinCacheSizeInBytes;	///< minimum cache data size
   167 	TUint32				iMaxCacheSizeInBytes;	///< maximum cache data size
   168 	TUint32				iMinSizeInPages;	///< minimum cache page number
   169 	TUint32				iMaxSizeInPages;	///< maximum cache page number
   170 	TUint32             iPageSizeInBytes;	///< cache page size in bytes
   171 	TInt64              iCacheBasePos; 		///< cache pages base position, used to align them at cluster size
   172 
   173 	TDriveInterface&    iDrive;        		///< reference to the driver for media access
   174 	TUint32             iCacheDisabled : 1; ///< if not 0 the cache is disabled totally and all reads and writes go via TDriveInterface directly
   175 
   176 	
   177 	// data structures for LRU page list	
   178 	TCachePageList 	iLockedQ;				///< the locked queue that manages all locked pages, limited by minimum page number
   179 	TCachePageList 	iUnlockedQ;				///< the unlocked queue that manages all locked pages, limited by maximum page number - minimum page number
   180 	TUint32			iLockedQCount;
   181 	TUint32			iUnlockedQCount;
   182 
   183 	// data structures for look up table
   184 	THashFunction32<TLookupEntry> 	iHashFunction;
   185 	TIdentityRelation<TLookupEntry>	iIdentityFunction;
   186 	RHashSet<TLookupEntry> 			iLookupTable;	///< a lookup table that used to speed up page look up
   187 
   188 	CCacheMemoryClient*	iCacheMemoryClient;	///< interface to cache memory manager
   189 	TUint32 iPermanentlyAllocatedPageCount;	///< count of pages in locked queue that are never unlocked
   190 
   191     };
   192 
   193 #include"sl_dir_cache.inl"
   194 
   195 #endif //SL_DIR_CACHE_H
   196 
   197 
   198 
   199