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 "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 // This file holds the class methods for the Rom Address Lookup Cache feature
15 // of FBServ: CFbsRalCacheEl, CFbsRalCache.
23 // ============================================================================
26 Destructor of CFbsRalCacheEl objects releasing memory used by attributes.
28 @post Object state undefined.
30 CFbsRalCacheEl::~CFbsRalCacheEl()
32 User::Free(iFilename);
37 Default constructor for CFbsRalCacheEl objects.
39 @post CFbsRalCacheEl initialised with a null mapping information.
41 CFbsRalCacheEl::CFbsRalCacheEl()
42 : CBase(), iFilename(0), iAddress(0)
47 Constructor for constructing TFbsRalCacheEl objects with valid
50 @param aFilename full name of the file to store
51 @param aMemAddr ROM memory address file starts at
52 @post TFbsRalCacheEl initialised with the filename and memory address
54 @panic EEmptyFilename panic generated when no chars in name
55 @panic EInvalidRomAddress panic generated when address out of range
59 const TDesC& aFilename,
63 // Allocate memory for new cache element object and construct.
64 CFbsRalCacheEl* ptr = new CFbsRalCacheEl;
68 // Initialise attributes of cache element.
69 ptr->iFilename = HBufC::New(aFilename.Length());
75 *(ptr->iFilename) = aFilename;
76 ptr->iAddress = aMemAddr;
81 // Done, return address to caller.
86 This routine is used by callers to release the memory directly used for the
87 CFbsRalCacheEl object specified WITHOUT destroying the object first. Users
88 wishing to destroy the object can do so by calling the destructory directly.
89 This is necessary since new/delete are not used for objects of this class as
90 out of memory panics must be avoided.
91 Therefore this routine must be used with caution to avoid memory leaks!
93 @param aThisRef Ptr to object to free. Ptr set to 0 on exit
96 CFbsRalCacheEl::FreeOnly(
97 CFbsRalCacheEl*& aThisRef
100 User::Free (aThisRef);
105 An optimised routine for checking the filename in the entry matches that
106 supplied to the routine. It compares filenames in a right to left fashion
107 since most differences in filenames are in the last 16 characters or so.
108 ASCII characters are compared in a case insensitive fashion.
110 @param aSearchKey filename to compare entry with
111 @return ETrue if entry matches search key, EFalse otherwise.
114 CFbsRalCacheEl::MatchKey(
115 const TDesC& aSearchKey
121 // Check both filename descriptors are the same length
122 TInt charIndex = iFilename->Length();
123 if (charIndex != aSearchKey.Length())
126 // Then check every character is the same by comparing right to left.
130 if (ToLower((*iFilename)[charIndex]) != ToLower(aSearchKey[charIndex]))
134 // If we reach here we know the entry matches what we are looking for.
138 TInt CFbsRalCacheEl::ToLower(TInt aInt)
140 return (0x40 < aInt && 0x5B > aInt) ? (aInt + 0x20) : aInt;
143 // ============================================================================
146 Destructor of CFbsRalCache objects by resetting the circular buffer.
148 @post Object state undefined.
150 CFbsRalCache::~CFbsRalCache()
153 RDebug::Print(_L("FBS RALCache destructed for process:%x\n"),(TUint)iProcId);
164 CFbsRalCache private constructor.
166 @param aFs valid file serve session handle used in lookups.
167 @post TFbsRalCache initialised with empty cache buffer.
168 @see Public construction achieved using static New() class method.
170 CFbsRalCache::CFbsRalCache(
173 : CCirBuf<CFbsRalCacheEl>(), iFs(aFs)
177 iProcId = current.Id();
178 RDebug::Print(_L("FBS RALCache constructed for process:%x\n"),
184 CFbsRalCache public constructor. This method allocates memory for a new
185 object on the default heap and initialises it with the supplied data.
187 @param aCacheSize number (fixed) of entries in the cache.
188 @param aFs valid file serve session handle used in lookups.
189 @return Pointer to new object, otherwise 0 when error detected.
190 @post TFbsRalCache initialised with empty cache buffer.
201 // Allocate memory for new cache object
202 // and call its constructor to initialise it.
203 CFbsRalCache* ptr = new CFbsRalCache(aFs);
207 // Reserve capacity in circular buffer for cache entries.
208 TRAPD(lc, ptr->SetLengthL(aCacheSize));
215 // Successful, new cache created.
220 This is the main cache lookup method for getting a file's start address
221 should it be found in ROM. It first scans the cache for a match and on a hit
222 it returns the stored address. On a miss it uses the file server to get
223 the file's ROM address. This result if +ve is stored and the Most-recently-used
224 cache, otherwise 0 is returned.
226 @param aFileKey Full file name of file to get address for.
227 @return Start address in ROM for the file, 0 if not in ROM.
228 @post Cache updated with new entry if ROM file and not in cache.
231 CFbsRalCache::Lookup(
232 const TDesC& aFileKey
236 // Search the cache from the head to the tail should it have any entries
237 // based on a MRU policy.
241 for (TInt num = Count(); num; num--)
243 // Calculate the address of the entry.
247 CFbsRalCacheEl* entry = (CFbsRalCacheEl*)ptr;
249 // Compare the entry key with that suppled for a match and return.
250 if (entry->MatchKey(aFileKey))
253 RDebug::Print(_L("FBS RALCache lookup HIT: %S\n"), &aFileKey);
256 return entry->iAddress;
262 RDebug::Print(_L("FBS RALCache lookup MISS: %S\n"), &aFileKey);
265 // Filename not in cache, ask file server for it's ROM address.
266 TAny *romAddr = iFs.IsFileInRom (aFileKey);
269 // Store new filename/address mapping in cache and return.
270 (void)AddItem (aFileKey, romAddr);
274 // It's not a file in ROM
279 This method will create a cache entry from the supplied data and add it to the
280 head of the circular cache buffer. If the cache is full this will result in the
281 entry at the tail being overwritten with the new entry inserted at the head.
283 @param aFilename full name of the file to store
284 @param aMemAddr ROM memory address file starts at
285 @return ETrue when successfully added, EFalse otherwise.
286 @post Cache updated as described above.
289 CFbsRalCache::AddItem(
290 const TDesC& aFilename,
294 // Construct the new cache entry from the supplied data.
295 CFbsRalCacheEl* entry = CFbsRalCacheEl::New(aFilename, aMemAddr);
299 // Make room in the cache if we need to based on MRU policy.
300 if (Count() == Length()) // Is it full?
303 // Add a copy of the cache entry to the circular buffer.
306 // Failed, can't cache it!
307 entry->~CFbsRalCacheEl();
308 CFbsRalCacheEl::FreeOnly(entry);
312 // Item now cached so clean up local entry memory only
313 CFbsRalCacheEl::FreeOnly(entry);
318 This method will create a cache entry from the supplied data and add it to the
319 head of the circular cache buffer. If tha cache is full this will result in the
320 entry at the tail being overwritten with the new entry inserted at the head.
322 @post Cache updated as described above.
325 CFbsRalCache::DropItem()
327 // No items to drop from cache!?
331 // Remove cache entry at tail copying into temporary variable.
332 CFbsRalCacheEl entry;
336 RDebug::Print(_L("FBS RALCache droped element: %S, %x, %d\n"),
337 entry.iFilename, entry.iAddress, entry.iHitCount);
340 // 'entry' destroyed on exit and cleaned up.
346 CFbsRalCache::PrintCache()
352 RDebug::Print(_L("FBS RALCache contents (%d):\n"), num);
358 CFbsRalCacheEl* entry = (CFbsRalCacheEl*)ptr;
359 RDebug::Print(_L("FBS RALCache El %d: %S, %x, %d\n"), num,
360 entry->iFilename, entry->iAddress, entry->iHitCount);
365 RDebug::Print(_L("FBS RALCache emtpy.\n"));
370 // ==========================================================================