Update contrib.
1 // Copyright (c) 2006-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\sfile\sf_cache_man.h
23 #if !defined(__SF_CACHE_MAN_H__)
24 #define __SF_CACHE_MAN_H__
27 #define __SIMULATE_LOCK_FAILURES__
34 const TInt KByteToByteShift = 10;
36 enum TCacheManagerFault
39 ECacheAlreadyCreated = 1,
40 EIllegalDriveNumber = 2,
41 EIllegalCacheSize = 3,
43 EInvalidAllocCount = 5,
44 EInvalidLockCount = 6,
45 EInvalidFillCount = 7,
46 EAppendToFreeQueueFailed = 8,
49 EInvalidSegmentCount = 11,
51 EInvalidDirtyCount = 13,
53 EUnexpectedCommitFailure = 15,
54 EUnexpectedLockFailure = 16,
55 EInvalidCacheLine = 17,
57 ERemovingEmptyUnlocked = 19,
58 EFreeingLockedCacheLine = 20,
59 EFreeingDirtyCacheLine = 21,
60 ESetDirtyNotLocked = 22,
61 EClearDirtyNotLocked = 23,
62 ESetFilledNotLocked = 24,
63 EManagerNotLocked = 25,
64 EExtendingUnownedCacheline = 26,
65 EUnlockingUnownedCacheline = 27,
66 EInvalidLockedPageStart = 28,
67 EInvalidLockedPageCount = 29,
68 EInvalidLockRange = 30,
69 ESetDirtyInvalidLockRange = 31,
70 ELockingAndAlreadyDirty = 32,
75 class CCacheManagerFactory
78 static void CreateL();
79 static TInt Destroy();
80 static CCacheManager* CacheManager();
82 static CCacheManager* iCacheManager;
86 NONSHARABLE_CLASS(CCacheManager) : public CBase
94 TUint8 iAllocatedSegments; // number of allocated pages
95 TUint8 iFilledSegments; // number of full pages (i.e. pages with data in)
96 TUint8 iDirtySegments; // number of dirty pages
97 TUint8 iLockCount; // number of times cacheline has been locked
98 TUint8 iLockedSegmentStart; // zero based index of first locked paged
99 TUint8 iLockedSegmentCount; // number of locked pages
100 TUint8 iSpare[2]; // padding
101 CCacheClient* iClient; // owner of this cacheline
102 TInt64 iPos; // arbitrary data owned by client
106 CCacheClient* CreateClientL();
107 void RegisterClient(CCacheClient& aClient);
108 void DeregisterClient(CCacheClient& aClient);
112 TInt SegmentSizeLog2();
113 TInt64 SegmentSizeMask();
114 TInt CacheLineSize();
115 TInt CacheLineSizeLog2();
116 TInt CacheLineSizeInSegments();
118 // called from CCacheClient
119 TInt AllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine*& aCacheLine, TInt aSegmentCount);
120 TInt ReAllocateAndLockCacheLine(CCacheClient* aClient, TInt64 aPos, const TCacheLine& aCacheLine, TInt aSegmentCount);
122 TInt ExtendCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount);
123 void RemoveEmptySegments(CCacheClient* aClient, const TCacheLine& aCacheLine);
125 TInt LockCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aLockedPageStart, TInt aLockedPageCount);
126 TBool UnlockCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine);
128 TBool FreeCacheLine(CCacheClient* aClient, const TCacheLine& aCacheLine);
131 TInt LockCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
132 TInt FillCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
133 TInt DirtyCount(CCacheClient* aClient, const TCacheLine& aCacheLine);
134 TBool TooManyLockedSegments();
136 void SetFilled(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount);
137 void SetDirty(CCacheClient* aClient, const TCacheLine& aCacheLine, TInt aSegmentCount);
138 void ClearDirty(CCacheClient* aClient, const TCacheLine& aCacheLine);
139 TBool InUse(const TCacheLine& aCacheLine);
141 // called from CKernEventNotifier::FreeMemoryChangeCallback()
142 void FreeMemoryChanged(TBool aMemoryLow);
144 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
146 void DumpCacheLine(TCacheLine& aCacheLine);
147 void SimulateLockFailureMode(TBool aEnable);
148 void AllocateMaxSegments(TBool aEnable);
149 TBool AllocateMaxSegments();
151 TFileCacheStats& Stats();
153 void SimulateWriteFailure();
154 TBool SimulateWriteFailureEnabled();
155 TBool SimulatedFailure(TInt& aFailureCount);
161 CCacheManager(TUint aCacheSize);
164 inline TUint8* Base();
166 inline void CacheLock();
167 inline void CacheUnlock();
169 TBool StealCacheLine(CCacheClient* aClient);
171 TInt Lock(TUint8* aAddr, TInt aSegmentCount);
172 TInt Unlock(TUint8* aAddr, TInt aSegmentCount);
173 TInt Commit(TUint8* aAddr, TInt aSegmentCount);
174 TInt Decommit(TUint8* aAddr, TInt aSegmentCount);
176 static CCacheManager* NewCacheL(TInt aUncommitedCacheSize);
178 void FreeCacheLine(TCacheLine& aCacheLine);
184 TCacheLine* iCacheLines;
185 RPointerArray<TCacheLine> iFreeQueue;
186 RPointerArray<TCacheLine> iUsedQueue;
190 TInt iNumOfCacheLines;
193 TInt iCacheLineSizeLog2;
196 TInt iMaxLockedSegments;
198 TInt iSegmentsPerCacheLine;
200 TInt iLockedSegmentCount;
203 TBool iSimulateLockFailureMode;
205 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
206 TBool iManagerLocked;
207 TInt iLockFailureCount;
208 TInt iCommitFailureCount;
209 TInt iAllocFailureCount;
210 TBool iAllocateMaxSegments;
212 TFileCacheStats iStats;
213 TBool iSimulateWriteFailure;
216 // low memory notification stuff
217 TBool iMemoryLow; // ETrue if kernel has notified us that memory has fallen below a specified threshold
218 TInt iLowMemoryThreshold;
220 // index of the next cacheline in iUsedQueue to steal if
221 // all cachelines are in use
222 TInt iNextCacheLineToSteal;
224 friend class CCacheManagerFactory;
225 friend class CCacheClient;
229 NONSHARABLE_CLASS(TGlobalFileCacheSettings)
232 static void ReadPropertiesFile();
234 static TBool Enabled();
235 static TInt CacheSize();
236 static TInt MaxLockedSize();
237 static TInt LowMemoryThreshold();
239 static TBool iEnabled;
240 static TInt32 iCacheSize;
241 static TInt32 iMaxLockedSize;
242 static TInt32 iLowMemoryThreshold;
245 #endif // !defined(__SF_CACHE_MAN_H__)