Update contrib.
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\sfile\sf_memory_man.cpp
25 #include <e32std_private.h>
31 #include "sf_memory_man.h"
32 #include "sf_memory_client.h"
35 Destructor of the CCacheMemoryManager, need to destroy all registered clients.
37 CCacheMemoryManager::~CCacheMemoryManager()
39 for (TInt i = 0; i < iRegisteredClients.Count(); i++)
41 iRegisteredClients[i]->Reset();
42 delete iRegisteredClients[i];
44 iRegisteredClients.Close();
48 Static factory function of CCacheMemoryManager
49 @param aCacheSize the total size of the virtual address space
51 CCacheMemoryManager* CCacheMemoryManager::NewL(TInt aCacheSize)
53 CCacheMemoryManager* cacheMemoryManager = new (ELeave) CCacheMemoryManager(aCacheSize);
55 CleanupStack::PushL(cacheMemoryManager);
56 cacheMemoryManager->ConstructL();
57 CleanupStack::Pop(1, cacheMemoryManager);
59 return cacheMemoryManager;
63 Constructor of CCacheMemoryManager
64 @param aMaxSize the total size of the virtual address space
66 CCacheMemoryManager::CCacheMemoryManager(TUint32 aMaxSize)
68 iSizeInBytes(aMaxSize),
74 Second phase constructor of CCacheMemoryManager.
75 Creates RChunk object and sets low memory threshold.
77 void CCacheMemoryManager::ConstructL()
79 // calculate the low-memory threshold below which we fail any attempt to allocate memory
80 TMemoryInfoV1Buf meminfo;
81 TInt r = UserHal::MemoryInfo(meminfo);
83 User::LeaveIfError(r);
84 iLowMemoryThreshold = (meminfo().iTotalRamInBytes * TGlobalCacheMemorySettings::LowMemoryThreshold()) / 100;
85 TChunkCreateInfo createInfo;
86 createInfo.SetCache(iSizeInBytes);
87 createInfo.SetOwner(EOwnerProcess);
88 r = iChunk.Create(createInfo);
90 User::LeaveIfError(r);
91 UserSvr::RegisterTrustedChunk(iChunk.Handle());
92 iBase = iChunk.Base();
93 iRegisteredClients.ReserveL(10);
94 __PRINT3(_L("CCacheMemoryManager::ConstructL(lowMem=%d, iSize=%d, base=0x%lx)"), iLowMemoryThreshold, iSizeInBytes, iBase);
98 Connect or register a client.
99 Note: callers of this function should be constructor of various caches, it is their resposibility
100 to make sure that parameters are sensible when calling this function
105 @param aClientName an identifier of the client to be connected
106 @param aMinSizeInSegs minimum client size in segments
107 @param aMaxSizeInSegs maximum client size in segments
108 @return CCacheMemoryClient* pointer to the client that connected, or NULL if parameters are not valid.
111 EXPORT_C CCacheMemoryClient* CCacheMemoryManager::ConnectClientL(const TDesC& aClientName, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs)
113 __PRINT3(_L("CCacheMemoryManager::ConnectClientL([%S], minSeg=%d, maxSeg=%d)"), &aClientName, aMinSizeInSegs, aMaxSizeInSegs);
115 // search for existing clients by name
116 for (TInt i = 0; i < iRegisteredClients.Count(); i++)
118 if (aClientName.Compare(iRegisteredClients[i]->Name()) == 0)
120 ASSERT(iRegisteredClients[i]->iTouchedRegionFlag == 0);
121 __PRINT1(_L("CCacheMemoryManager::ConnectClientL: [%S] found!"), &aClientName);
122 return iRegisteredClients[i];
126 // if it is a new drive/file system who wants to connect, create a new client for it
127 // parameter validation
128 ASSERT(iSizeInBytes > iCurrentOffsetMark + (aMaxSizeInSegs << SegmentSizeInBytesLog2()));
129 if (iSizeInBytes < iCurrentOffsetMark + (aMaxSizeInSegs << SegmentSizeInBytesLog2()))
132 User::Leave(KErrArgument);
135 // note: client creation may leave under OOM conditions
136 CCacheMemoryClient* client = CCacheMemoryClient::NewL(*this, aClientName, iCurrentOffsetMark, aMinSizeInSegs, aMaxSizeInSegs);
138 // if error happens during client registration, the client will be deleted
139 // this may leave under OOM conditions
140 TInt err = iRegisteredClients.Append(client);
149 // record current offset mark for next client
150 iCurrentOffsetMark += (aMaxSizeInSegs << SegmentSizeInBytesLog2());
155 Commit a contiguous set of segments.
156 @param aStartRamAddr the start ram address of the region to be committed.
157 @param aSegmentCount the segment number of the contiguous region to be committed.
158 @return TInt KErrNone if succeeded, KErrNoMemory if passed low memory threshold, otherwise a system-wide error code.
160 TInt CCacheMemoryManager::AllocateAndLockSegments(TUint8* aStartRamAddr, TInt aSegmentCount)
162 __PRINT2(_L("CCacheMemoryManager::AllocateAndLockSegments(base=0x%x, seg=%d)"), aStartRamAddr, aSegmentCount);
163 TMemoryInfoV1Buf meminfo;
164 TInt r = UserHal::MemoryInfo(meminfo);
165 __ASSERT_DEBUG(r==KErrNone,Fault(EMemoryInfoFailed));
171 if (isMemoryLow || (meminfo().iFreeRamInBytes < iLowMemoryThreshold))
173 __PRINT(_L("CCacheMemoryManager: free RAM below threshold !!!"));
176 return Commit(aStartRamAddr, aSegmentCount);
180 Notify the change of memory status
181 @param aIsMemoryLow the flag that sets current memory status
183 void CCacheMemoryManager::FreeMemoryChanged(TBool aIsMemoryLow)
185 isMemoryLow = aIsMemoryLow;
189 Decommit a contiguous set of segments.
190 @param aStartRamAddr the start ram address of the region to be decommitted.
191 @param aSegmentCount the segment number of the contiguous region to be decommitted.
192 @return TInt KErrNone if succeeded, otherwise a system-wide error code.
194 TInt CCacheMemoryManager::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
196 return Decommit(aStartRamAddr, aSegmentCount);
200 Lock a contiguous set of segments.
201 @param aStartRamAddr the start ram address of the region to be locked.
202 @param aSegmentCount the segment number of the contiguous region to be locked.
203 @return TInt KErrNone if succeeded, otherwise a system-wide error code.
205 TInt CCacheMemoryManager::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
207 return Lock(aStartRamAddr, aSegmentCount);
211 Unlock a contiguous set of segments.
212 @param aStartRamAddr the start ram address of the region to be unlocked.
213 @param aSegmentCount the segment number of the contiguous region to be unlocked.
214 @return TInt KErrNone if succeeded, otherwise a system-wide error code.
216 TInt CCacheMemoryManager::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
218 return Unlock(aStartRamAddr, aSegmentCount);
222 Commit a contiguous set of segments.
223 @param aStartRamAddr the start ram address of the region to be committed.
224 @param aSegmentCount the segment number of the contiguous region to be committed.
225 @return TInt KErrNone if succeeded, otherwise a system-wide error code.
227 TInt CCacheMemoryManager::Commit(TUint8* aStartRamAddr, TInt aSegmentCount)
229 TInt offset = aStartRamAddr - iBase;
230 TInt r = iChunk.Commit(offset, aSegmentCount << KSegmentSizeLog2);
235 Actual implementation of DecommitSegments().
236 @see CCacheMemoryManager::DecommitSegments().
238 TInt CCacheMemoryManager::Decommit(TUint8* aStartRamAddr, TInt aSegmentCount)
240 return iChunk.Decommit(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
244 Actual implementation of UnlockSegments().
245 @see CCacheMemoryManager::UnlockSegments().
247 TInt CCacheMemoryManager::Unlock(TUint8* aStartRamAddr, TInt aSegmentCount)
249 TInt r = iChunk.Unlock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
254 Actual implementation of LockSegments().
255 @see CCacheMemoryManager::LockSegments().
257 TInt CCacheMemoryManager::Lock(TUint8* aStartRamAddr, TInt aSegmentCount)
259 return iChunk.Lock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
263 //=====================================================================
264 TUint8* CCacheMemoryManager::Base()
266 return iChunk.Base();
270 Get function, returns log2 value of a segment size in bytes.
275 EXPORT_C TUint CCacheMemoryManager::SegmentSizeInBytesLog2() const
277 return KSegmentSizeLog2;
281 //=============================================================================
283 The singleton of global cache memory manager
285 CCacheMemoryManager* CCacheMemoryManagerFactory::iCacheMemoryManager = NULL;
288 Global factory function of CCacheMemoryManager.
290 void CCacheMemoryManagerFactory::CreateL()
292 iCacheMemoryManager = CCacheMemoryManager::NewL(TGlobalCacheMemorySettings::CacheSize());
296 Global get function of CCacheMemoryManager.
301 EXPORT_C CCacheMemoryManager* CCacheMemoryManagerFactory::CacheMemoryManager()
303 return iCacheMemoryManager;
307 Global destroy function of CCacheMemoryManager.
309 void CCacheMemoryManagerFactory::Destroy()
311 delete iCacheMemoryManager;
312 iCacheMemoryManager = NULL;
315 //=============================================================================
316 const TInt KByteToByteShift = 10;
317 TInt32 TGlobalCacheMemorySettings::iCacheSizeInBytes = KDefaultGlobalCacheMemorySize << KByteToByteShift;
318 TInt32 TGlobalCacheMemorySettings::iLowMemoryThreshold = KDefaultLowMemoryThreshold;
319 _LIT8(KLitSectionNameCacheMemory,"CacheMemory");
322 Read ESTART.TXT file for global cache memory settings
324 void TGlobalCacheMemorySettings::ReadPropertiesFile()
326 // Get size of cache in kilobytes
327 TInt32 cacheSizeInKBytes;
328 if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("GlobalCacheMemorySize"), cacheSizeInKBytes))
330 iCacheSizeInBytes = cacheSizeInKBytes << KByteToByteShift;
333 // Get low memory threshold
334 TInt32 lowMemoryThreshold;
335 if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("LowMemoryThreshold"), lowMemoryThreshold))
336 iLowMemoryThreshold = lowMemoryThreshold;
339 TInt TGlobalCacheMemorySettings::CacheSize()
341 return iCacheSizeInBytes;
344 TInt TGlobalCacheMemorySettings::LowMemoryThreshold()
346 return iLowMemoryThreshold;