|
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 "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 // |
|
15 |
|
16 #if !defined(__U32PERM_H__) |
|
17 #define __U32PERM_H__ |
|
18 #if !defined(__S32STD_H__) |
|
19 #include <s32std.h> |
|
20 #endif |
|
21 #if !defined(__U32FRAME_H__) |
|
22 #include "U32FRAME.H" |
|
23 #endif |
|
24 |
|
25 #if defined(_DEBUG)&&!defined(__SMALL_BUNDLE) |
|
26 //#define __SMALL_BUNDLE |
|
27 #endif |
|
28 |
|
29 //Forward declaratons |
|
30 class RPermanentFileStoreIter; |
|
31 class TDriveInfo; |
|
32 |
|
33 //The offset of the header of a permanent file store. |
|
34 //Since all permanent file store operations work in their own coordinate system, where physical file offset 32 is |
|
35 //logical offset 0, KPermanentStoreHeaderOffset is set with -16, which means that the physical file offset is 32 - 16 = 16. |
|
36 const TInt KPermanentStoreHeaderOffset=-16; |
|
37 |
|
38 //Permanent file store header length: sizeof(backup TOC ref) + sizeof(handle) + sizeof(TOC ref) + sizeof(crc) = 4 + 4 + 4 + 2 = 14 |
|
39 const TInt KPermanentStoreHeaderLength=14; |
|
40 |
|
41 //Backup TOC ref length - 4 bytes |
|
42 const TInt KPermanentStoreBackupLength=4; |
|
43 |
|
44 // |
|
45 const TInt KMaskHandleHash=0xff000000; |
|
46 const TInt KHandleInvalid=0x80000000; |
|
47 const TInt KHandleTocBase=0x40000000; |
|
48 const TInt KMaskHandleClear=0x30000000; |
|
49 const TInt KMaskHandleGen=0x0f000000; |
|
50 const TInt KIncHandleGen=0x01000000; |
|
51 // |
|
52 const TInt KMaxHandleIndex=0x00ffffff; |
|
53 const TInt KMaskHandleIndex=0x00ffffff; |
|
54 const TInt KSizeHandleIndex=3; |
|
55 // |
|
56 const TInt KTocDeltaCap = 64; //up to 64 entries in a delta TOC |
|
57 const TInt KTocDeltaMagic = 2; |
|
58 const TInt KMaxTocDeltaMagic = KMaxTUint16; |
|
59 const TInt KTocDelta=KHandleInvalid; |
|
60 // |
|
61 const TInt KOffsetTocHeader=-12; |
|
62 const TInt KSizeTocEntry=5; //base toc entry size is 5 bytes (when stored in the file, 8 bytes when presented in memory) |
|
63 const TInt KSizeTocDeltaEntry=8;//delta toc entry size is 8 bytes |
|
64 const TInt KSizeTocDeltaExtra=7; |
|
65 const TInt KElementsTocBuf=48; |
|
66 const TInt KBackTocBuf=20*KSizeTocEntry; |
|
67 const TInt KSizeTocBuf=KElementsTocBuf*KSizeTocEntry; |
|
68 |
|
69 //TPermanentStoreHeader class. |
|
70 // |
|
71 //Represents the data kept in the permanent store file header. |
|
72 //Data members: |
|
73 // - iBackup - "backup TOC reference", 32-bits integer, which keeps the 31-bit file offset of the backup TOC. |
|
74 // Plays important role in the "store commit" procedure. |
|
75 // The LSB is a "dirty" bit. If during the store opening phase the dirty bit is found to be set, |
|
76 // then it means - the previous "store commit" operation has not been completed successfully and |
|
77 // the backup TOC shall be used instead of the TOC; |
|
78 // - iHandle - 32-bit stream handle (MSB - invalid/deleted, 3 bits - unused, 4 bits - generation counter, 24 bits - stream handle). |
|
79 // Plays important role in the "stream relocation" procedure during store compaction. |
|
80 // iHandle keeps the handle of the stream being relocated, so if the commit phase fails, the original stream entry |
|
81 // can be restored at the moment when the store is reopened; |
|
82 // - iRef - Current "TOC reference". Represents a file offset, where the current TOC is; |
|
83 // - iCrc - 16-bit CRC, protecting iBackup, iHandle, iRef; |
|
84 class TPermanentStoreHeader |
|
85 { |
|
86 public: |
|
87 TPermanentStoreHeader() {} |
|
88 inline TUint8* Ptr(); |
|
89 inline const TUint8* Ptr() const; |
|
90 TBool IsValid() const; |
|
91 // |
|
92 inline TPermanentStoreHeader(TInt aToc); |
|
93 inline TPermanentStoreHeader(TInt aBackupToc,TInt aHandle,TInt aReference); |
|
94 // |
|
95 inline TBool IsDirty() const; |
|
96 inline void MarkDirty(); |
|
97 inline void SetBackupToc(TInt aBackupToc); |
|
98 inline TInt BackupToc() const; |
|
99 // |
|
100 inline TInt Handle() const; |
|
101 inline TInt Reference() const; |
|
102 private: |
|
103 void Set(TInt aBackupToc,TInt aHandle,TInt aReference); |
|
104 private: |
|
105 TUint32 iBackup; |
|
106 TInt32 iHandle; |
|
107 TInt32 iRef; |
|
108 TUint16 iCrc; |
|
109 }; |
|
110 |
|
111 //CPermanentStoreToc class. |
|
112 // |
|
113 //Represents the data kept in the permanent file store TOC (Table Of Content). |
|
114 //Each TOC consists of: |
|
115 // - TOC header - CPermanentStoreToc::STocHead structure; |
|
116 // - set of TOC entries - CPermanentStoreToc::TEntry structure; |
|
117 // |
|
118 //Each TOC entry consists of: |
|
119 // - A stream handle (32 bits: MSB - invalid/deleted, 3 bits - unused, 4 bits - generation counter, 24 bits - stream handle); |
|
120 // - A stream ref - the offset of the stream data in the permannet file store; |
|
121 NONSHARABLE_CLASS(CPermanentStoreToc) : public CBase |
|
122 { |
|
123 public: |
|
124 struct TEntry |
|
125 { |
|
126 TInt handle; |
|
127 TInt ref; |
|
128 static TInt Compare(const TEntry&, const TEntry&); |
|
129 }; |
|
130 struct STocHead |
|
131 { |
|
132 TInt32 primary; |
|
133 TInt32 avail; |
|
134 TUint32 count; |
|
135 }; |
|
136 enum TPut {EWrite,ETestBeforeWrite}; |
|
137 public: |
|
138 static CPermanentStoreToc* NewL(TStreamPos aBase,TStreamExchange& aHost,TInt aToc,TInt aBaseReloc); |
|
139 ~CPermanentStoreToc(); |
|
140 // |
|
141 inline TInt Extent() const; |
|
142 void Move(TInt aToc,TInt anExtent); |
|
143 // |
|
144 inline TBool IsVirtual() const; |
|
145 TInt RealizeL(TInt aPrimary,TInt anExtent) const; |
|
146 void Adopt(TInt aToc,TInt aPrimary); |
|
147 // |
|
148 inline TInt Primary() const; |
|
149 inline void Changed(); |
|
150 // |
|
151 TInt AllocL(TInt anOffset); |
|
152 TInt AllocL(); |
|
153 void Cancel(TInt aHandle); |
|
154 void FreeL(TInt aHandle); |
|
155 TInt AtL(TInt aHandle) const; |
|
156 void PutL(TInt aHandle,TInt anOffset,TPut aCheck); |
|
157 TInt GetL(TInt aHandle); |
|
158 TInt Set(TInt aHandle,TInt anOffset); |
|
159 // |
|
160 CPermanentStoreToc(TStreamPos aBase,TStreamExchange& aHost); |
|
161 void ConstructL(TInt aToc,TInt aBaseReloc); |
|
162 |
|
163 TInt RefSpan(TInt aHandle,TInt& aLength); |
|
164 |
|
165 private: |
|
166 inline TStreamPos Base() const; |
|
167 inline TStreamExchange& Host() const; |
|
168 inline const TEntry* Entry(TInt aHandle) const; |
|
169 TEntry* Entry(TInt aHandle); |
|
170 TEntry& FetchL(TInt aHandle); |
|
171 TEntry& DoAllocL(); |
|
172 TInt DoAtL(TInt aHandle) const; |
|
173 void PutBaseL(TInt aHandle, TInt aReference); |
|
174 void PutDeltaL(TInt aPos, TInt aHandle, TInt aReference); |
|
175 void PutTocL(TInt aTocBase, TPut aCheck); |
|
176 inline TBool HasDelta() const; |
|
177 TBool IsDelta() const; |
|
178 TInt InternalizeL(RReadStream& aIn, TInt aBaseReloc); |
|
179 TInt DeltaL(RFrame16Buf& aBuf,TInt aExtent,const STocHead& aHead) const; |
|
180 TInt RewriteL(RFrame16Buf& aBuf,TInt aExtent,const STocHead& aHead) const; |
|
181 TInt SmallTocL(RFrame16Buf& aBuf,TInt aExtent,const STocHead& aHead) const; |
|
182 private: |
|
183 TInt iPrimary; |
|
184 TInt iAvail; |
|
185 TInt iCount; |
|
186 RArray<TEntry> iEntries; |
|
187 // |
|
188 TStreamPos iBase; |
|
189 TStreamExchange* iHost; |
|
190 TInt iMagic; |
|
191 TInt iOff; |
|
192 TInt iExt; |
|
193 TInt iTocOff; |
|
194 TInt iTocExt; |
|
195 __MUTABLE TInt iWindow; |
|
196 __MUTABLE TUint8 iBuf[KSizeTocBuf]; |
|
197 private: |
|
198 friend class RPermanentStoreTocIter; |
|
199 }; |
|
200 |
|
201 // |
|
202 class RPermanentStoreTocIter |
|
203 { |
|
204 public: |
|
205 typedef CPermanentStoreToc::TEntry TEntry; |
|
206 typedef CPermanentStoreToc::STocHead STocHead; |
|
207 public: |
|
208 RPermanentStoreTocIter(const CPermanentStoreToc& aTable); |
|
209 inline void Close(); |
|
210 void Release(); |
|
211 // |
|
212 void ResetL(); |
|
213 TBool NextL(TEntry& anEntry); |
|
214 private: |
|
215 const CPermanentStoreToc& iTable; |
|
216 RFrame16Buf iBuf; |
|
217 TInt iIndex; |
|
218 TInt iCount; |
|
219 TInt iNext; |
|
220 const TEntry* iDelta; |
|
221 const TEntry* iDeltaEnd; |
|
222 }; |
|
223 |
|
224 // |
|
225 class TPermanentStoreCache |
|
226 { |
|
227 public: |
|
228 struct TItem {TInt handle;TInt offset;TInt extent;}; |
|
229 public: |
|
230 inline TPermanentStoreCache(); |
|
231 const TItem* At(TInt aHandle) const; |
|
232 void Relocated(TInt aHandle,TInt anOffset); |
|
233 void Put(const TItem* anItem,TInt anOffset,TInt anExtent); |
|
234 void Add(TInt aHandle,TInt anOffset,TInt anExtent); |
|
235 void Remove(TInt aHandle); |
|
236 void Invalidate(); |
|
237 private: |
|
238 enum {EItems=2*16-1}; |
|
239 private: |
|
240 TItem iItems[EItems]; |
|
241 }; |
|
242 |
|
243 // |
|
244 NONSHARABLE_CLASS(CPermanentStoreCoord) : public CBase |
|
245 { |
|
246 private: |
|
247 enum {EReady,EBackup=0x1,EClip=0x2}; |
|
248 enum TFileQoS |
|
249 { |
|
250 EUnknown, // |
|
251 ESimple, //File, "write byte" is an atomic operation |
|
252 EBlockAtomic, //File, "block write" is an atomic operation |
|
253 ETransactional //Transactional file system. |
|
254 }; |
|
255 typedef TPermanentStoreCache::TItem TItem; |
|
256 public: |
|
257 inline TBool IsTrim() const; |
|
258 TStreamPos LimitL(); |
|
259 inline void Clipped(); |
|
260 // |
|
261 TStreamId PrimaryL(); |
|
262 void ChangedL(); |
|
263 TBool CommitL(TStreamId aPrimary); |
|
264 TBool RevertL(TStreamId& aPrimary); |
|
265 // |
|
266 TStreamId ExtendL(); |
|
267 void DeleteL(TStreamId anId); |
|
268 // |
|
269 CPermanentStoreCoord(TStreamPos aBase,TStreamExchange& aHost); |
|
270 void InternalizeL(RReadStream& aStream); |
|
271 ~CPermanentStoreCoord(); |
|
272 private: |
|
273 void CanExtendL(); |
|
274 TInt DoCreateL(); |
|
275 void DoReplaceL(TInt aHandle); |
|
276 TInt DoOpenL(TInt& anOffset,TInt aHandle); |
|
277 void DoRelease(TInt aHandle,TInt anOffset,TInt anExtent); |
|
278 TInt DoCommit(TInt aHandle,TInt anOffset,TInt anExtent); |
|
279 // |
|
280 inline TStreamPos Base() const; |
|
281 inline TStreamExchange& Host() const; |
|
282 inline TInt Toc() const; |
|
283 inline CPermanentStoreToc& Table() const; |
|
284 CPermanentStoreToc& TableL(); |
|
285 CPermanentStoreToc& ConsolidateL(); |
|
286 void RelocateL(TInt aHandle,TInt anOffset); |
|
287 void MoveL(TInt aToc,TInt anExtent); |
|
288 inline TUint Generation() const; |
|
289 inline void Inc(); |
|
290 inline void Dec(); |
|
291 inline TBool Accessed() const; |
|
292 TFileQoS FileQoSL(); |
|
293 TBool IsBlockAtomicL(TInt aDriveNo) const; |
|
294 // |
|
295 MStreamBuf* BeginL(TPermanentStoreHeader& aHeader); |
|
296 private: |
|
297 TStreamPos iBase; |
|
298 TStreamExchange *iHost; |
|
299 TInt iToc; |
|
300 CPermanentStoreToc* iTable; |
|
301 TInt iReloc; |
|
302 TInt iTarget; |
|
303 TPermanentStoreCache iCache; |
|
304 // |
|
305 TUint iGen; |
|
306 TInt iRefs; |
|
307 TInt iAccess; |
|
308 TInt iExtend; |
|
309 TInt iState; |
|
310 TInt iExt; |
|
311 TFileQoS iFileQos; |
|
312 private: |
|
313 friend class HPermanentStoreBuf; |
|
314 friend class CPermanentStoreCollector; |
|
315 friend class RPermanentFileStoreIter; |
|
316 }; |
|
317 // |
|
318 NONSHARABLE_CLASS(HPermanentStoreBuf) : public RFrame16Buf |
|
319 { |
|
320 public: |
|
321 static HPermanentStoreBuf* CreateL(CPermanentStoreCoord& aCoord,TStreamId& anId,TInt aMode=ERead|EWrite); |
|
322 static HPermanentStoreBuf* ReplaceL(CPermanentStoreCoord& aCoord,TStreamId anId,TInt aMode=ERead|EWrite); |
|
323 static HPermanentStoreBuf* OpenL(CPermanentStoreCoord& aCoord,TStreamId anId,TInt aMode=ERead|EWrite); |
|
324 // |
|
325 virtual ~HPermanentStoreBuf(); |
|
326 private: |
|
327 static HPermanentStoreBuf* NewLC(CPermanentStoreCoord& aCoord); |
|
328 static HPermanentStoreBuf* ExtendLC(CPermanentStoreCoord& aCoord,TInt aMode); |
|
329 inline HPermanentStoreBuf(CPermanentStoreCoord& aCoord); |
|
330 inline CPermanentStoreCoord& Coord() const; |
|
331 // |
|
332 void DoRelease(); |
|
333 void DoSynchL(); |
|
334 private: |
|
335 CPermanentStoreCoord* iCoord; |
|
336 TInt iHandle; |
|
337 }; |
|
338 // |
|
339 class TPermanentStoreStreamIter |
|
340 { |
|
341 #if defined(__SMALL_BUNDLE) |
|
342 enum {EBundleSize=8-1}; |
|
343 #else |
|
344 enum {EBundleSize=64-1}; |
|
345 #endif |
|
346 public: |
|
347 void Reset(); |
|
348 TInt FillL(CPermanentStoreToc& aToc); |
|
349 TInt Next(); |
|
350 // |
|
351 void Relocated(TInt aStream); |
|
352 private: |
|
353 static void Push(TInt* aHeap,TInt* aHole,TInt aValue); |
|
354 static TInt PopPush(TInt* aHeap,TInt* anEnd,TInt aValue); |
|
355 private: |
|
356 TInt* iNext; |
|
357 const TInt* iFinish; |
|
358 TInt iMore; |
|
359 TInt iPos; |
|
360 TInt iTable[EBundleSize]; |
|
361 }; |
|
362 |
|
363 class TPermanentStoreRelocator; |
|
364 NONSHARABLE_CLASS(CPermanentStoreCollector) : public CBase,public MIncrementalCollector |
|
365 { |
|
366 enum TState |
|
367 { |
|
368 EGetFree,ESkip,EInitRelocator,EFillRelocator,EEvalRelocator,EScanRelocator,ERelocateStream,ERelocateToc, |
|
369 EFastSort,EFastExtent,EFastRelocate |
|
370 }; |
|
371 enum {EGranularity = 64}; |
|
372 enum {EExtentStep = 64}; |
|
373 public: |
|
374 struct TEntry |
|
375 { |
|
376 TInt len; |
|
377 RPermanentStoreTocIter::TEntry entry; |
|
378 }; |
|
379 public: |
|
380 static CPermanentStoreCollector* CompactorL(CPermanentStoreCoord& aCoord); |
|
381 static CPermanentStoreCollector* ReclaimerL(CPermanentStoreCoord& aCoord); |
|
382 protected: |
|
383 CPermanentStoreCollector(CPermanentStoreCoord& aCoord); |
|
384 ~CPermanentStoreCollector(); |
|
385 private: |
|
386 void DoRelease(); |
|
387 void DoResetL(TInt& aCount); |
|
388 void DoNextL(TInt& aStep,TInt& aTotal); |
|
389 // |
|
390 TInt GetFreeL(); |
|
391 TInt SkipL(TInt& aTotal); |
|
392 TInt InitRelocator(); |
|
393 TInt FillRelocatorL(); |
|
394 TInt EvalRelocatorL(); |
|
395 TInt ScanRelocator(); |
|
396 TInt RelocateStreamL(); |
|
397 TBool HaveEnoughSpace() const; |
|
398 TInt ExtentL(TInt aStream); |
|
399 // |
|
400 // fast compaction |
|
401 TInt FastResetL(); |
|
402 void FastSort(); |
|
403 void FastExtentL(TInt& aTotal); |
|
404 void FastRelocateL(TInt& aTotal); |
|
405 TEntry* BestFit(TInt aPos, TInt aExt, TEntry* aFirst, TEntry* aLast); |
|
406 // |
|
407 // common utilities |
|
408 void RelocateTocL(TInt& aTotal); |
|
409 void RelocateStreamL(const TEntry& aReloc, TInt aExtent); |
|
410 TInt RelocateL(TInt aStream, TInt aLength, TFrameType16 aType, TInt aExtent); |
|
411 // |
|
412 inline TBool Compacting() const; |
|
413 inline CPermanentStoreCoord& Coord() const; |
|
414 inline TStreamExchange& Host() const; |
|
415 private: |
|
416 CPermanentStoreCoord* iCoord; |
|
417 TUint iCoordGen; |
|
418 TStreamExchange* iHost; |
|
419 TStreamMark iMark; |
|
420 TState iState; |
|
421 TInt iFree; |
|
422 TInt iEnd; |
|
423 TEntry* iNext; |
|
424 TEntry* iLast; |
|
425 RArray<TEntry> iStreams; |
|
426 TPermanentStoreRelocator* iReloc; |
|
427 TPermanentStoreStreamIter iIter; |
|
428 }; |
|
429 |
|
430 #include "U32PERM.INL" |
|
431 #endif |