userlibandfileserver/fileserver/sfile/sf_memory_man.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32\sfile\sf_memory_man.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 
       
    24 #include <e32std.h>
       
    25 #include <e32std_private.h>
       
    26 #include "sf_std.h"
       
    27 #include <e32uid.h>
       
    28 #include <e32wins.h>
       
    29 #include <f32file.h>
       
    30 #include <hal.h>
       
    31 #include "sf_memory_man.h"
       
    32 #include "sf_memory_client.h"
       
    33 
       
    34 /**
       
    35 Destructor of the CCacheMemoryManager, need to destroy all registered clients.
       
    36 */
       
    37 CCacheMemoryManager::~CCacheMemoryManager()
       
    38 	{
       
    39 	for (TInt i = 0; i < iRegisteredClients.Count(); i++)
       
    40 		{
       
    41 		iRegisteredClients[i]->Reset();
       
    42 		delete iRegisteredClients[i];
       
    43 		}
       
    44 	iRegisteredClients.Close();
       
    45 	}
       
    46 
       
    47 /**
       
    48 Static factory function of CCacheMemoryManager
       
    49 @param	aCacheSize	the total size of the virtual address space
       
    50 */
       
    51 CCacheMemoryManager* CCacheMemoryManager::NewL(TInt aCacheSize)
       
    52 	{
       
    53 	CCacheMemoryManager* cacheMemoryManager = new (ELeave) CCacheMemoryManager(aCacheSize);
       
    54 
       
    55 	CleanupStack::PushL(cacheMemoryManager);
       
    56 	cacheMemoryManager->ConstructL();
       
    57 	CleanupStack::Pop(1, cacheMemoryManager);
       
    58 
       
    59 	return cacheMemoryManager;
       
    60 	}
       
    61 
       
    62 /**
       
    63 Constructor of CCacheMemoryManager
       
    64 @param	aMaxSize	the total size of the virtual address space
       
    65 */
       
    66 CCacheMemoryManager::CCacheMemoryManager(TUint32 aMaxSize)
       
    67 :iBase(NULL),
       
    68 iSizeInBytes(aMaxSize),
       
    69 iCurrentOffsetMark(0)
       
    70 	{
       
    71 	}
       
    72 
       
    73 /**
       
    74 Second phase constructor of CCacheMemoryManager.
       
    75 Creates RChunk object and sets low memory threshold.
       
    76 */
       
    77 void CCacheMemoryManager::ConstructL()
       
    78 	{
       
    79 	// calculate the low-memory threshold below which we fail any attempt to allocate memory
       
    80 	TMemoryInfoV1Buf meminfo;
       
    81 	TInt r = UserHal::MemoryInfo(meminfo);
       
    82 	ASSERT(r==KErrNone);
       
    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);
       
    89 	ASSERT(r==KErrNone);
       
    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);
       
    95 	}
       
    96 
       
    97 /**
       
    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
       
   101 
       
   102 @internalTechnology
       
   103 @released
       
   104 
       
   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.
       
   109 @leave	if no memory
       
   110 */
       
   111 EXPORT_C CCacheMemoryClient* CCacheMemoryManager::ConnectClientL(const TDesC& aClientName, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs)
       
   112 	{
       
   113 	__PRINT3(_L("CCacheMemoryManager::ConnectClientL([%S], minSeg=%d, maxSeg=%d)"), &aClientName, aMinSizeInSegs, aMaxSizeInSegs);
       
   114 	
       
   115 	// search for existing clients by name
       
   116 	for (TInt i = 0; i < iRegisteredClients.Count(); i++)
       
   117 		{
       
   118 		if (aClientName.Compare(iRegisteredClients[i]->Name()) == 0)
       
   119 			{
       
   120 			ASSERT(iRegisteredClients[i]->iTouchedRegionFlag == 0);
       
   121 			__PRINT1(_L("CCacheMemoryManager::ConnectClientL: [%S] found!"), &aClientName);
       
   122 			return iRegisteredClients[i];
       
   123 			}
       
   124 		}
       
   125 
       
   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()))
       
   130 		{
       
   131 		ASSERT(0);
       
   132 		User::Leave(KErrArgument);
       
   133 		}
       
   134 	
       
   135 	// note: client creation may leave under OOM conditions
       
   136 	CCacheMemoryClient* client = CCacheMemoryClient::NewL(*this, aClientName, iCurrentOffsetMark, aMinSizeInSegs, aMaxSizeInSegs);
       
   137 
       
   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);
       
   141 	if (err != KErrNone)
       
   142 		{
       
   143 		ASSERT(0);
       
   144 		delete client;
       
   145 		client = NULL;
       
   146 		User::Leave(err);
       
   147 		}
       
   148 
       
   149 	// record current offset mark for next client
       
   150 	iCurrentOffsetMark += (aMaxSizeInSegs << SegmentSizeInBytesLog2());
       
   151 	return client;
       
   152 	}
       
   153 
       
   154 /**
       
   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.
       
   159 */
       
   160 TInt CCacheMemoryManager::AllocateAndLockSegments(TUint8* aStartRamAddr, TInt aSegmentCount)
       
   161 	{
       
   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));
       
   166 	if (r != KErrNone)
       
   167 		{
       
   168 		return r;
       
   169 		}
       
   170 
       
   171 	if (isMemoryLow || (meminfo().iFreeRamInBytes < iLowMemoryThreshold))
       
   172 		{
       
   173 		__PRINT(_L("CCacheMemoryManager: free RAM below threshold !!!"));
       
   174 		return KErrNoMemory;
       
   175 		}
       
   176 	return Commit(aStartRamAddr, aSegmentCount);
       
   177 	}
       
   178 
       
   179 /**
       
   180 Notify the change of memory status
       
   181 @param	aIsMemoryLow	the flag that sets current memory status
       
   182 */
       
   183 void CCacheMemoryManager::FreeMemoryChanged(TBool aIsMemoryLow)
       
   184 	{
       
   185 	isMemoryLow = aIsMemoryLow;
       
   186 	}
       
   187 
       
   188 /**
       
   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.
       
   193 */
       
   194 TInt CCacheMemoryManager::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
       
   195 	{
       
   196 	return Decommit(aStartRamAddr, aSegmentCount);
       
   197 	}
       
   198 
       
   199 /**
       
   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.
       
   204 */
       
   205 TInt CCacheMemoryManager::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
       
   206 	{
       
   207 	return Lock(aStartRamAddr, aSegmentCount);
       
   208 	}
       
   209 
       
   210 /**
       
   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.
       
   215 */
       
   216 TInt CCacheMemoryManager::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
       
   217 	{
       
   218 	return Unlock(aStartRamAddr, aSegmentCount);
       
   219 	}
       
   220 
       
   221 /**
       
   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.
       
   226 */
       
   227 TInt CCacheMemoryManager::Commit(TUint8* aStartRamAddr, TInt aSegmentCount)
       
   228 	{
       
   229 	TInt offset = aStartRamAddr - iBase;
       
   230 	TInt r = iChunk.Commit(offset, aSegmentCount << KSegmentSizeLog2);
       
   231 	return r;
       
   232 	}
       
   233 
       
   234 /**
       
   235 Actual implementation of DecommitSegments().
       
   236 @see CCacheMemoryManager::DecommitSegments().
       
   237 */
       
   238 TInt CCacheMemoryManager::Decommit(TUint8* aStartRamAddr, TInt aSegmentCount)
       
   239 	{
       
   240 	return iChunk.Decommit(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
       
   241 	}
       
   242 
       
   243 /**
       
   244 Actual implementation of UnlockSegments().
       
   245 @see CCacheMemoryManager::UnlockSegments().
       
   246 */
       
   247 TInt CCacheMemoryManager::Unlock(TUint8* aStartRamAddr, TInt aSegmentCount)
       
   248 	{
       
   249 	TInt r = iChunk.Unlock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
       
   250 	return r;
       
   251 	}
       
   252 
       
   253 /**
       
   254 Actual implementation of LockSegments().
       
   255 @see CCacheMemoryManager::LockSegments().
       
   256 */
       
   257 TInt CCacheMemoryManager::Lock(TUint8* aStartRamAddr, TInt aSegmentCount)
       
   258 	{
       
   259 	return iChunk.Lock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
       
   260 	}
       
   261 
       
   262 
       
   263 //=====================================================================
       
   264 TUint8* CCacheMemoryManager::Base()
       
   265 	{
       
   266 	return iChunk.Base();
       
   267 	}
       
   268 
       
   269 /**
       
   270 Get function, returns log2 value of a segment size in bytes.
       
   271 
       
   272 @internalTechnology
       
   273 @released
       
   274 */
       
   275 EXPORT_C TUint CCacheMemoryManager::SegmentSizeInBytesLog2() const
       
   276 	{
       
   277 	return KSegmentSizeLog2;
       
   278 	}
       
   279 
       
   280 
       
   281 //=============================================================================
       
   282 /**
       
   283 The singleton of global cache memory manager
       
   284 */
       
   285 CCacheMemoryManager* CCacheMemoryManagerFactory::iCacheMemoryManager = NULL;
       
   286 
       
   287 /**
       
   288 Global factory function of CCacheMemoryManager. 
       
   289 */
       
   290 void CCacheMemoryManagerFactory::CreateL()
       
   291 	{
       
   292 	iCacheMemoryManager = CCacheMemoryManager::NewL(TGlobalCacheMemorySettings::CacheSize());
       
   293 	}
       
   294 
       
   295 /**
       
   296 Global get function of CCacheMemoryManager. 
       
   297 
       
   298 @internalTechnology
       
   299 @released
       
   300 */
       
   301 EXPORT_C CCacheMemoryManager* CCacheMemoryManagerFactory::CacheMemoryManager()
       
   302 	{
       
   303 	return iCacheMemoryManager;
       
   304 	}
       
   305 
       
   306 /**
       
   307 Global destroy function of CCacheMemoryManager. 
       
   308 */
       
   309 void CCacheMemoryManagerFactory::Destroy()
       
   310 	{
       
   311 	delete iCacheMemoryManager;
       
   312 	iCacheMemoryManager = NULL;
       
   313 	}
       
   314 
       
   315 //=============================================================================
       
   316 const TInt KByteToByteShift = 10;
       
   317 TInt32 TGlobalCacheMemorySettings::iCacheSizeInBytes	= KDefaultGlobalCacheMemorySize << KByteToByteShift;
       
   318 TInt32 TGlobalCacheMemorySettings::iLowMemoryThreshold  = KDefaultLowMemoryThreshold;
       
   319 _LIT8(KLitSectionNameCacheMemory,"CacheMemory");
       
   320 
       
   321 /**
       
   322 Read ESTART.TXT file for global cache memory settings 
       
   323 */
       
   324 void TGlobalCacheMemorySettings::ReadPropertiesFile()
       
   325 	{
       
   326 	// Get size of cache in kilobytes
       
   327 	TInt32 cacheSizeInKBytes;
       
   328 	if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("GlobalCacheMemorySize"), cacheSizeInKBytes))
       
   329 		{
       
   330 		iCacheSizeInBytes = cacheSizeInKBytes << KByteToByteShift;
       
   331 		}
       
   332 
       
   333 	// Get low memory threshold
       
   334 	TInt32 lowMemoryThreshold;
       
   335 	if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("LowMemoryThreshold"), lowMemoryThreshold))
       
   336 		iLowMemoryThreshold = lowMemoryThreshold;
       
   337 	}
       
   338 
       
   339 TInt TGlobalCacheMemorySettings::CacheSize()
       
   340 	{
       
   341 	return iCacheSizeInBytes;
       
   342 	}
       
   343 
       
   344 TInt TGlobalCacheMemorySettings::LowMemoryThreshold()
       
   345 	{
       
   346 	return iLowMemoryThreshold;
       
   347 	}
       
   348 
       
   349