os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_memory_man.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// f32\sfile\sf_memory_man.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
/**
sl@0
    19
 @file
sl@0
    20
 @internalTechnology
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <e32std_private.h>
sl@0
    26
#include "sf_std.h"
sl@0
    27
#include <e32uid.h>
sl@0
    28
#include <e32wins.h>
sl@0
    29
#include <f32file.h>
sl@0
    30
#include <hal.h>
sl@0
    31
#include "sf_memory_man.h"
sl@0
    32
#include "sf_memory_client.h"
sl@0
    33
sl@0
    34
/**
sl@0
    35
Destructor of the CCacheMemoryManager, need to destroy all registered clients.
sl@0
    36
*/
sl@0
    37
CCacheMemoryManager::~CCacheMemoryManager()
sl@0
    38
	{
sl@0
    39
	for (TInt i = 0; i < iRegisteredClients.Count(); i++)
sl@0
    40
		{
sl@0
    41
		iRegisteredClients[i]->Reset();
sl@0
    42
		delete iRegisteredClients[i];
sl@0
    43
		}
sl@0
    44
	iRegisteredClients.Close();
sl@0
    45
	}
sl@0
    46
sl@0
    47
/**
sl@0
    48
Static factory function of CCacheMemoryManager
sl@0
    49
@param	aCacheSize	the total size of the virtual address space
sl@0
    50
*/
sl@0
    51
CCacheMemoryManager* CCacheMemoryManager::NewL(TInt aCacheSize)
sl@0
    52
	{
sl@0
    53
	CCacheMemoryManager* cacheMemoryManager = new (ELeave) CCacheMemoryManager(aCacheSize);
sl@0
    54
sl@0
    55
	CleanupStack::PushL(cacheMemoryManager);
sl@0
    56
	cacheMemoryManager->ConstructL();
sl@0
    57
	CleanupStack::Pop(1, cacheMemoryManager);
sl@0
    58
sl@0
    59
	return cacheMemoryManager;
sl@0
    60
	}
sl@0
    61
sl@0
    62
/**
sl@0
    63
Constructor of CCacheMemoryManager
sl@0
    64
@param	aMaxSize	the total size of the virtual address space
sl@0
    65
*/
sl@0
    66
CCacheMemoryManager::CCacheMemoryManager(TUint32 aMaxSize)
sl@0
    67
:iBase(NULL),
sl@0
    68
iSizeInBytes(aMaxSize),
sl@0
    69
iCurrentOffsetMark(0)
sl@0
    70
	{
sl@0
    71
	}
sl@0
    72
sl@0
    73
/**
sl@0
    74
Second phase constructor of CCacheMemoryManager.
sl@0
    75
Creates RChunk object and sets low memory threshold.
sl@0
    76
*/
sl@0
    77
void CCacheMemoryManager::ConstructL()
sl@0
    78
	{
sl@0
    79
	// calculate the low-memory threshold below which we fail any attempt to allocate memory
sl@0
    80
	TMemoryInfoV1Buf meminfo;
sl@0
    81
	TInt r = UserHal::MemoryInfo(meminfo);
sl@0
    82
	ASSERT(r==KErrNone);
sl@0
    83
	User::LeaveIfError(r);
sl@0
    84
	iLowMemoryThreshold = (meminfo().iTotalRamInBytes * TGlobalCacheMemorySettings::LowMemoryThreshold()) / 100;
sl@0
    85
	TChunkCreateInfo createInfo;
sl@0
    86
	createInfo.SetCache(iSizeInBytes);
sl@0
    87
	createInfo.SetOwner(EOwnerProcess);
sl@0
    88
	r = iChunk.Create(createInfo);
sl@0
    89
	ASSERT(r==KErrNone);
sl@0
    90
	User::LeaveIfError(r);
sl@0
    91
	UserSvr::RegisterTrustedChunk(iChunk.Handle());
sl@0
    92
	iBase = iChunk.Base();
sl@0
    93
	iRegisteredClients.ReserveL(10);
sl@0
    94
	__PRINT3(_L("CCacheMemoryManager::ConstructL(lowMem=%d, iSize=%d, base=0x%lx)"), iLowMemoryThreshold, iSizeInBytes, iBase);
sl@0
    95
	}
sl@0
    96
sl@0
    97
/**
sl@0
    98
Connect or register a client.
sl@0
    99
Note: callers of this function should be constructor of various caches, it is their resposibility
sl@0
   100
	to make sure that parameters are sensible when calling this function
sl@0
   101
sl@0
   102
@internalTechnology
sl@0
   103
@released
sl@0
   104
sl@0
   105
@param	aClientName	an identifier of the client to be connected
sl@0
   106
@param	aMinSizeInSegs	minimum client size in segments
sl@0
   107
@param	aMaxSizeInSegs	maximum client size in segments
sl@0
   108
@return	CCacheMemoryClient*	pointer to the client that connected, or NULL if parameters are not valid.
sl@0
   109
@leave	if no memory
sl@0
   110
*/
sl@0
   111
EXPORT_C CCacheMemoryClient* CCacheMemoryManager::ConnectClientL(const TDesC& aClientName, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs)
sl@0
   112
	{
sl@0
   113
	__PRINT3(_L("CCacheMemoryManager::ConnectClientL([%S], minSeg=%d, maxSeg=%d)"), &aClientName, aMinSizeInSegs, aMaxSizeInSegs);
sl@0
   114
	
sl@0
   115
	// search for existing clients by name
sl@0
   116
	for (TInt i = 0; i < iRegisteredClients.Count(); i++)
sl@0
   117
		{
sl@0
   118
		if (aClientName.Compare(iRegisteredClients[i]->Name()) == 0)
sl@0
   119
			{
sl@0
   120
			ASSERT(iRegisteredClients[i]->iTouchedRegionFlag == 0);
sl@0
   121
			__PRINT1(_L("CCacheMemoryManager::ConnectClientL: [%S] found!"), &aClientName);
sl@0
   122
			return iRegisteredClients[i];
sl@0
   123
			}
sl@0
   124
		}
sl@0
   125
sl@0
   126
	// if it is a new drive/file system who wants to connect, create a new client for it
sl@0
   127
	// parameter validation
sl@0
   128
	ASSERT(iSizeInBytes > iCurrentOffsetMark + (aMaxSizeInSegs << SegmentSizeInBytesLog2()));
sl@0
   129
	if (iSizeInBytes < iCurrentOffsetMark + (aMaxSizeInSegs << SegmentSizeInBytesLog2()))
sl@0
   130
		{
sl@0
   131
		ASSERT(0);
sl@0
   132
		User::Leave(KErrArgument);
sl@0
   133
		}
sl@0
   134
	
sl@0
   135
	// note: client creation may leave under OOM conditions
sl@0
   136
	CCacheMemoryClient* client = CCacheMemoryClient::NewL(*this, aClientName, iCurrentOffsetMark, aMinSizeInSegs, aMaxSizeInSegs);
sl@0
   137
sl@0
   138
	// if error happens during client registration, the client will be deleted
sl@0
   139
	// this may leave under OOM conditions
sl@0
   140
	TInt err = iRegisteredClients.Append(client);
sl@0
   141
	if (err != KErrNone)
sl@0
   142
		{
sl@0
   143
		ASSERT(0);
sl@0
   144
		delete client;
sl@0
   145
		client = NULL;
sl@0
   146
		User::Leave(err);
sl@0
   147
		}
sl@0
   148
sl@0
   149
	// record current offset mark for next client
sl@0
   150
	iCurrentOffsetMark += (aMaxSizeInSegs << SegmentSizeInBytesLog2());
sl@0
   151
	return client;
sl@0
   152
	}
sl@0
   153
sl@0
   154
/**
sl@0
   155
Commit a contiguous set of segments.
sl@0
   156
@param	aStartRamAddr	the start ram address of the region to be committed.
sl@0
   157
@param	aSegmentCount	the segment number of the contiguous region to be committed.
sl@0
   158
@return	TInt	KErrNone if succeeded, KErrNoMemory if passed low memory threshold, otherwise a system-wide error code.
sl@0
   159
*/
sl@0
   160
TInt CCacheMemoryManager::AllocateAndLockSegments(TUint8* aStartRamAddr, TInt aSegmentCount)
sl@0
   161
	{
sl@0
   162
	__PRINT2(_L("CCacheMemoryManager::AllocateAndLockSegments(base=0x%x, seg=%d)"), aStartRamAddr, aSegmentCount);
sl@0
   163
	TMemoryInfoV1Buf meminfo;
sl@0
   164
	TInt r = UserHal::MemoryInfo(meminfo);
sl@0
   165
	__ASSERT_DEBUG(r==KErrNone,Fault(EMemoryInfoFailed));
sl@0
   166
	if (r != KErrNone)
sl@0
   167
		{
sl@0
   168
		return r;
sl@0
   169
		}
sl@0
   170
sl@0
   171
	if (isMemoryLow || (meminfo().iFreeRamInBytes < iLowMemoryThreshold))
sl@0
   172
		{
sl@0
   173
		__PRINT(_L("CCacheMemoryManager: free RAM below threshold !!!"));
sl@0
   174
		return KErrNoMemory;
sl@0
   175
		}
sl@0
   176
	return Commit(aStartRamAddr, aSegmentCount);
sl@0
   177
	}
sl@0
   178
sl@0
   179
/**
sl@0
   180
Notify the change of memory status
sl@0
   181
@param	aIsMemoryLow	the flag that sets current memory status
sl@0
   182
*/
sl@0
   183
void CCacheMemoryManager::FreeMemoryChanged(TBool aIsMemoryLow)
sl@0
   184
	{
sl@0
   185
	isMemoryLow = aIsMemoryLow;
sl@0
   186
	}
sl@0
   187
sl@0
   188
/**
sl@0
   189
Decommit a contiguous set of segments.
sl@0
   190
@param	aStartRamAddr	the start ram address of the region to be decommitted.
sl@0
   191
@param	aSegmentCount	the segment number of the contiguous region to be decommitted.
sl@0
   192
@return	TInt	KErrNone if succeeded, otherwise a system-wide error code.
sl@0
   193
*/
sl@0
   194
TInt CCacheMemoryManager::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
sl@0
   195
	{
sl@0
   196
	return Decommit(aStartRamAddr, aSegmentCount);
sl@0
   197
	}
sl@0
   198
sl@0
   199
/**
sl@0
   200
Lock a contiguous set of segments.
sl@0
   201
@param	aStartRamAddr	the start ram address of the region to be locked.
sl@0
   202
@param	aSegmentCount	the segment number of the contiguous region to be locked.
sl@0
   203
@return	TInt	KErrNone if succeeded, otherwise a system-wide error code.
sl@0
   204
*/
sl@0
   205
TInt CCacheMemoryManager::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
sl@0
   206
	{
sl@0
   207
	return Lock(aStartRamAddr, aSegmentCount);
sl@0
   208
	}
sl@0
   209
sl@0
   210
/**
sl@0
   211
Unlock a contiguous set of segments.
sl@0
   212
@param	aStartRamAddr	the start ram address of the region to be unlocked.
sl@0
   213
@param	aSegmentCount	the segment number of the contiguous region to be unlocked.
sl@0
   214
@return	TInt	KErrNone if succeeded, otherwise a system-wide error code.
sl@0
   215
*/
sl@0
   216
TInt CCacheMemoryManager::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount)
sl@0
   217
	{
sl@0
   218
	return Unlock(aStartRamAddr, aSegmentCount);
sl@0
   219
	}
sl@0
   220
sl@0
   221
/**
sl@0
   222
Commit a contiguous set of segments.
sl@0
   223
@param	aStartRamAddr	the start ram address of the region to be committed.
sl@0
   224
@param	aSegmentCount	the segment number of the contiguous region to be committed.
sl@0
   225
@return	TInt	KErrNone if succeeded, otherwise a system-wide error code.
sl@0
   226
*/
sl@0
   227
TInt CCacheMemoryManager::Commit(TUint8* aStartRamAddr, TInt aSegmentCount)
sl@0
   228
	{
sl@0
   229
	TInt offset = aStartRamAddr - iBase;
sl@0
   230
	TInt r = iChunk.Commit(offset, aSegmentCount << KSegmentSizeLog2);
sl@0
   231
	return r;
sl@0
   232
	}
sl@0
   233
sl@0
   234
/**
sl@0
   235
Actual implementation of DecommitSegments().
sl@0
   236
@see CCacheMemoryManager::DecommitSegments().
sl@0
   237
*/
sl@0
   238
TInt CCacheMemoryManager::Decommit(TUint8* aStartRamAddr, TInt aSegmentCount)
sl@0
   239
	{
sl@0
   240
	return iChunk.Decommit(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
Actual implementation of UnlockSegments().
sl@0
   245
@see CCacheMemoryManager::UnlockSegments().
sl@0
   246
*/
sl@0
   247
TInt CCacheMemoryManager::Unlock(TUint8* aStartRamAddr, TInt aSegmentCount)
sl@0
   248
	{
sl@0
   249
	TInt r = iChunk.Unlock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
sl@0
   250
	return r;
sl@0
   251
	}
sl@0
   252
sl@0
   253
/**
sl@0
   254
Actual implementation of LockSegments().
sl@0
   255
@see CCacheMemoryManager::LockSegments().
sl@0
   256
*/
sl@0
   257
TInt CCacheMemoryManager::Lock(TUint8* aStartRamAddr, TInt aSegmentCount)
sl@0
   258
	{
sl@0
   259
	return iChunk.Lock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2);
sl@0
   260
	}
sl@0
   261
sl@0
   262
sl@0
   263
//=====================================================================
sl@0
   264
TUint8* CCacheMemoryManager::Base()
sl@0
   265
	{
sl@0
   266
	return iChunk.Base();
sl@0
   267
	}
sl@0
   268
sl@0
   269
/**
sl@0
   270
Get function, returns log2 value of a segment size in bytes.
sl@0
   271
sl@0
   272
@internalTechnology
sl@0
   273
@released
sl@0
   274
*/
sl@0
   275
EXPORT_C TUint CCacheMemoryManager::SegmentSizeInBytesLog2() const
sl@0
   276
	{
sl@0
   277
	return KSegmentSizeLog2;
sl@0
   278
	}
sl@0
   279
sl@0
   280
sl@0
   281
//=============================================================================
sl@0
   282
/**
sl@0
   283
The singleton of global cache memory manager
sl@0
   284
*/
sl@0
   285
CCacheMemoryManager* CCacheMemoryManagerFactory::iCacheMemoryManager = NULL;
sl@0
   286
sl@0
   287
/**
sl@0
   288
Global factory function of CCacheMemoryManager. 
sl@0
   289
*/
sl@0
   290
void CCacheMemoryManagerFactory::CreateL()
sl@0
   291
	{
sl@0
   292
	iCacheMemoryManager = CCacheMemoryManager::NewL(TGlobalCacheMemorySettings::CacheSize());
sl@0
   293
	}
sl@0
   294
sl@0
   295
/**
sl@0
   296
Global get function of CCacheMemoryManager. 
sl@0
   297
sl@0
   298
@internalTechnology
sl@0
   299
@released
sl@0
   300
*/
sl@0
   301
EXPORT_C CCacheMemoryManager* CCacheMemoryManagerFactory::CacheMemoryManager()
sl@0
   302
	{
sl@0
   303
	return iCacheMemoryManager;
sl@0
   304
	}
sl@0
   305
sl@0
   306
/**
sl@0
   307
Global destroy function of CCacheMemoryManager. 
sl@0
   308
*/
sl@0
   309
void CCacheMemoryManagerFactory::Destroy()
sl@0
   310
	{
sl@0
   311
	delete iCacheMemoryManager;
sl@0
   312
	iCacheMemoryManager = NULL;
sl@0
   313
	}
sl@0
   314
sl@0
   315
//=============================================================================
sl@0
   316
const TInt KByteToByteShift = 10;
sl@0
   317
TInt32 TGlobalCacheMemorySettings::iCacheSizeInBytes	= KDefaultGlobalCacheMemorySize << KByteToByteShift;
sl@0
   318
TInt32 TGlobalCacheMemorySettings::iLowMemoryThreshold  = KDefaultLowMemoryThreshold;
sl@0
   319
_LIT8(KLitSectionNameCacheMemory,"CacheMemory");
sl@0
   320
sl@0
   321
/**
sl@0
   322
Read ESTART.TXT file for global cache memory settings 
sl@0
   323
*/
sl@0
   324
void TGlobalCacheMemorySettings::ReadPropertiesFile()
sl@0
   325
	{
sl@0
   326
	// Get size of cache in kilobytes
sl@0
   327
	TInt32 cacheSizeInKBytes;
sl@0
   328
	if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("GlobalCacheMemorySize"), cacheSizeInKBytes))
sl@0
   329
		{
sl@0
   330
		iCacheSizeInBytes = cacheSizeInKBytes << KByteToByteShift;
sl@0
   331
		}
sl@0
   332
sl@0
   333
	// Get low memory threshold
sl@0
   334
	TInt32 lowMemoryThreshold;
sl@0
   335
	if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("LowMemoryThreshold"), lowMemoryThreshold))
sl@0
   336
		iLowMemoryThreshold = lowMemoryThreshold;
sl@0
   337
	}
sl@0
   338
sl@0
   339
TInt TGlobalCacheMemorySettings::CacheSize()
sl@0
   340
	{
sl@0
   341
	return iCacheSizeInBytes;
sl@0
   342
	}
sl@0
   343
sl@0
   344
TInt TGlobalCacheMemorySettings::LowMemoryThreshold()
sl@0
   345
	{
sl@0
   346
	return iLowMemoryThreshold;
sl@0
   347
	}
sl@0
   348
sl@0
   349