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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\sfat32\inc\sl_dir_cache.h
23 #ifndef SL_DIR_CACHE_H
24 #define SL_DIR_CACHE_H
26 #include "sf_memory_man.h"
27 #include "sf_memory_client.h"
29 #include <e32hashtab.h>
31 //---------------------------------------------------------------------------------------------------------------------------------
32 class CDynamicDirCache;
36 The dynamic directory cache page class
38 class TDynamicDirCachePage
49 ~TDynamicDirCachePage();
50 static TDynamicDirCachePage* NewL(CDynamicDirCache* aOwnerCache, TInt64 aStartMedPos, TUint8* aStartRamAddr);
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();
61 inline TUint32 PageSizeInBytes() const;
62 inline TUint32 PageSizeInSegs() const;
64 inline void SetPos(TInt64 aPos);
65 inline void ResetPos();
66 inline TInt64 StartPos() const;
68 inline TUint8* PtrInPage(TInt64 aPos) const;
69 inline TBool PosCachedInPage(TInt64 aPos) const;
74 // declared to disable copying and assignment
75 TDynamicDirCachePage& operator=(const TDynamicDirCachePage&);
76 TDynamicDirCachePage(const TDynamicDirCachePage&);
78 // private constructor, as this class is not supposed to be created on stack
79 TDynamicDirCachePage(CDynamicDirCache* aOwnerCache, TInt64 aStartMedPos, TUint8* aStartRamAddr);
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
92 The lookup table entry class
98 TLookupEntry(): iPos(0), iRange(0), iPage(NULL) {};
99 TLookupEntry(TInt64 aPos, TUint32 aRange, TDynamicDirCachePage* aPage): iPos(aPos), iRange(aRange), iPage(aPage) {};
103 TDynamicDirCachePage* iPage;
106 //---------------------------------------------------------------------------------------------------------------------------------
107 typedef TDblQue<TDynamicDirCachePage> TCachePageList;
109 Dynamic directory cache.
110 For now it is directly derived from MWTCacheInterface.
111 Provides caching FAT directory data.
113 class CDynamicDirCache : public CBase, public MWTCacheInterface
117 static CDynamicDirCache* NewL(TDriveInterface& aDrive, TUint32 aMinPageNum, TUint32 aMaxPageNum, TUint32 aPageSizeLog2, const TDesC& aClientName);
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);
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;
132 TUint32 PageSizeInSegs() const;
134 // Debugging functions
139 CDynamicDirCache(TDriveInterface& aDrive, TUint32 aMinSizeInBytes, TUint32 aMaxSizeInBytes, TUint32 aPageSizeInBytesLog2);
140 void ConstructL(const TDesC& aClientName);
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);
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
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
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;
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
188 CCacheMemoryClient* iCacheMemoryClient; ///< interface to cache memory manager
189 TUint32 iPermanentlyAllocatedPageCount; ///< count of pages in locked queue that are never unlocked
193 #include"sl_dir_cache.inl"
195 #endif //SL_DIR_CACHE_H