os/kernelhwsrv/kerneltest/e32test/defrag/t_ramdefrag.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\defrag\t_ramdefrag.cpp
    15 // RAM Defragmentation Functional Tests
    16 // 
    17 //
    18 
    19 //#define RUN_ALL_TESTS			// Uncomment to ensure that all tests are run regardless of test failures
    20 //#define DEBUG_VER				// Uncomment for information output from tests
    21 
    22 
    23 
    24 #define __E32TEST_EXTENSION__
    25 #include <e32test.h>
    26 RTest test(_L("T_RAMDEFRAG"));
    27 #include <e32rom.h>
    28 #include <u32hal.h>
    29 #include <f32file.h>
    30 #include <f32dbg.h>
    31 #include <e32svr.h>
    32 #include <e32msgqueue.h>
    33 #include <e32math.h>
    34 #include <hal.h>
    35 #include "testdefs.h"
    36 
    37 
    38 #include <dptest.h>
    39 
    40 #include "t_ramdefrag.h"
    41 
    42 #define READ(a) ReadByte((volatile TUint8*)(a))
    43 
    44 
    45 #ifdef RUN_ALL_TESTS
    46 #define TEST_FAIL {gTestStepFailed++;}
    47 #define CLEANUP(a) {}
    48 #else
    49 #define TEST_FAIL {TestCleanup(); test(EFalse);}
    50 #define CLEANUP(a) {if (!gFailPrintPageInfo) \
    51 						{ \
    52 						PrintPageInfo(); \
    53 						gFailPrintPageInfo = ETrue; \
    54 						} \
    55 					a;}
    56 #endif
    57 
    58 
    59 
    60 #define TEST_DRIVER_OPEN		1
    61 #define TEST_DRIVER_CLOSE		0
    62 
    63 #define BEST_MOVABLE			1
    64 #define BEST_DISCARDABLE		2
    65 #define BEST_FIXED				3
    66 
    67 #define Z_ALLOC_CONTIG			1
    68 #define Z_ALLOC_DISC			2
    69 
    70 LOCAL_D TUint gTestStarted = EFalse;								// Used to ensure matching TestStart() and TestEnd().
    71 LOCAL_D TBool gPagedRom = ETrue;									// Stores whether or not is a paged ROM
    72 LOCAL_D TInt gTestStepFailed = 0;									// Stores the number of test steps failed
    73 LOCAL_D TBool gFailPrintPageInfo = EFalse;							// Set to ETrue once CLEANUP has been invoked once.
    74 LOCAL_D TBool gFileCacheRun = EFalse;								// Set to ETrue whe FSCaching tests have been run
    75 
    76 LOCAL_D TInt gRamSize;												// The total RAM size in bytes
    77 LOCAL_D TInt gFreeRam;												// The amount of free RAM available in bytes
    78 LOCAL_D TInt gPageSize;												// The number of bytes per page
    79 LOCAL_D TUint gPageShift;
    80 #ifdef DEBUG_VER
    81 LOCAL_D TInt gRamUsed; 												// The amount of RAM used in bytes
    82 #endif
    83 LOCAL_D TInt gInitialRam;											// The initial free RAM before a test starts
    84 LOCAL_D TInt gEndRam;												// The end free RAM when a test finishes
    85 LOCAL_D TUint gOriginalMinCacheSize;									// The original DP minSize
    86 LOCAL_D TUint gOriginalMaxCacheSize;									// The original DP maxSize
    87 LOCAL_D TInt gTotalRamLost;											// The total amount of RAM lost during the test
    88 
    89 LOCAL_D TUint gZoneCount = 0;										// Number of zones
    90 LOCAL_D const TUint KInvalidZoneID = 0xffffffff;					// Invalid value for a zone ID
    91 LOCAL_D STestPageCount gTotalPageCount;
    92 
    93 
    94 LOCAL_D struct SRamZoneConfig*	gZoneConfigArray;					// Contains the configurations of all the zones
    95 LOCAL_D struct SRamZoneUtilisation*	gZoneUtilArray;					// Contains the utilisations of all the zones
    96 LOCAL_D struct SRamZoneUtilisation* gOriginalPageCountArray;		// Contains the original utilisations of the zones
    97 LOCAL_D TInt* gPrefArray;											// Contains the preference order of the zones 
    98 LOCAL_D TUint8* gOrigFlagArray;										// Contains the orignal values for the zone flags
    99 
   100 LOCAL_D TInt gDefragMaxPages = 0;
   101 
   102 const TInt KFillAllMovable = -1;
   103 
   104 LOCAL_D RChunk* gChunkArray1 = NULL;								// Stores reference to all the chunks that have been created
   105 LOCAL_D RChunk* gChunkArray2 = NULL;								// Stores reference to all the chunks that have been created
   106 LOCAL_D TUint gChunkArraySize1 = 0;									// The size of the array gChunkArray
   107 LOCAL_D TUint gChunkArraySize2 = 0;									// The size of the array gChunkArray
   108 const TUint KChunkDefaultSize = 0x300000;	
   109 const TUint KMaxChunks = 14;
   110 LOCAL_D const TUint KNumAllocChunks = 10;							// The number of chunks to be allocd for some tests.
   111 	
   112 LOCAL_D RRamDefragFuncTestLdd Ldd;									// Main Ldd used to call into device driver
   113 
   114 LOCAL_D TBuf<20> gTestThreadName =_L("TestThread");	
   115 LOCAL_D RThread gTestThread;
   116 LOCAL_D TRequestStatus status;
   117 
   118 LOCAL_D TInt gDrive;												// The removable media drive
   119 LOCAL_D RFs gTheFs;									
   120 LOCAL_D TFileName gSessionPath;
   121 
   122 const TInt KNoRemovableDrive = -1;									// gDrive is set to this when no suitable drive can be found.
   123 const TInt KDefaultCacheSize = (128 + 12) * 1024;					// The default file system cache size 
   124 const TUint KNumFilesOrig = (32 * 1024 * 1024) / KDefaultCacheSize;	// The number of files that are needed to fill the file system cache
   125 LOCAL_D TInt gFilesNeededToFillCache = KNumFilesOrig ;				// Not constant as can change depending on the size of the disk
   126 LOCAL_D RFile gFile[KNumFilesOrig];
   127 
   128 LOCAL_D TInt* gCandList1;											// Array of zones that have the same preference and the same
   129 LOCAL_D TInt* gCandList2;											// amount of free pages
   130 const TInt KInvalidCandIndex = -1;
   131 
   132 //
   133 // GetDrive
   134 //
   135 // Gets the removable drive number
   136 //
   137 TInt GetDrive()
   138 	{
   139 	RFs theFs;
   140 	TInt r = theFs.Connect();
   141 	test_KErrNone(r);
   142 
   143 	TInt driveLet = KNoRemovableDrive;
   144 
   145 	TInt i = EDriveA;
   146 	for (; i <= EDriveZ; i++)
   147 		{
   148 		TVolumeInfo volInfo;
   149 		r = theFs.Volume(volInfo, i);
   150 		if (r == KErrNone)
   151 			{// This drive no. exists so determine if it is removable and 
   152 			//  formattable media.
   153 			if ((volInfo.iDrive.iDriveAtt & KDriveAttRemovable) &&
   154 				(volInfo.iDrive.iMediaAtt & KMediaAttFormattable))	
   155 				{
   156 				driveLet = i;
   157 				break;
   158 				}
   159 			}
   160 		}
   161 	theFs.Close();
   162 	return driveLet;
   163 	}
   164 
   165 
   166 //
   167 // DeviceDriver
   168 //
   169 // Opens or closes the device driver used
   170 //
   171 TInt DeviceDriver(TInt aFunctionNum)
   172 	{
   173 	TInt r = 0;
   174 	switch (aFunctionNum)
   175 		{
   176 		case TEST_DRIVER_OPEN:
   177 			{
   178 			r = User::LoadLogicalDevice(KRamDefragFuncTestLddName);
   179 			test(r==KErrNone || r==KErrAlreadyExists);
   180 			r = Ldd.Open();
   181 			test_KErrNone(r);
   182 			}
   183 		break;
   184 		
   185 		case TEST_DRIVER_CLOSE:
   186 			{
   187 			Ldd.Close();
   188 			r = User::FreeLogicalDevice(KRamDefragFuncTestLddName);
   189 			test_KErrNone(r);
   190 			}
   191 		break;
   192 		
   193 		default:
   194 		break;
   195 		
   196 		}
   197 	return r;
   198 	}
   199 
   200 
   201 //
   202 // GetOriginalPageCount
   203 //
   204 // Obtains the orginal types of pages in each of the zones
   205 //
   206 void GetOriginalPageCount()
   207 	{
   208 	TUint index;
   209 	TInt ret = 0;
   210 	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
   211 	for (index = 0; index < gZoneCount; index ++)
   212 		{
   213 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gOriginalPageCountArray[index]);
   214 		test(ret == KErrNone);
   215 		}
   216 	}
   217 
   218 
   219 //
   220 // PrintPageInfo
   221 //
   222 // Prints various page information to the screen
   223 //
   224 void PrintPageInfo()
   225 	{
   226 	test.Printf(_L("\nZONE CONFIGURATIONS:\n"));
   227 	for (TUint index = 0; index < gZoneCount; index ++)
   228 		{
   229 		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
   230 		test(ret == KErrNone);
   231 		test.Printf(_L("config : id=0x%08x  index=%-2d  base=0x%08x  end=0x%08x  pages=0x%08x  pref=%-2d  flags=0x%x\n"),
   232 					gZoneConfigArray[index].iZoneId,gZoneConfigArray[index].iZoneIndex,
   233 					gZoneConfigArray[index].iPhysBase,gZoneConfigArray[index].iPhysEnd,
   234 					gZoneConfigArray[index].iPhysPages,gZoneConfigArray[index].iPref,gZoneConfigArray[index].iFlags);
   235 		}
   236 	test.Printf(_L("\nZONE UTILISATIONS:\n"));
   237 	for (TUint index = 0; index < gZoneCount; index ++)
   238 		{
   239 		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
   240 		test(ret == KErrNone);
   241 		test.Printf(_L("usage  : id=0x%08x  index=%-2d  pref=%d  pages=0x%08x free=0x%08x  unknown=0x%08x  fixed=0x%08x  move=0x%08x  discard=0x%08x  other=0x%08x\n"),
   242 					gZoneUtilArray[index].iZoneId,gZoneUtilArray[index].iZoneIndex,gZoneConfigArray[index].iPref, 
   243 					gZoneUtilArray[index].iPhysPages,gZoneUtilArray[index].iFreePages,
   244 					gZoneUtilArray[index].iAllocUnknown,gZoneUtilArray[index].iAllocFixed,gZoneUtilArray[index].iAllocMovable,
   245 					gZoneUtilArray[index].iAllocDiscardable,gZoneUtilArray[index].iAllocOther);
   246 		}
   247 	}
   248 
   249 
   250 //
   251 // GetAllPageInfo
   252 //
   253 // Get various different page information for all zones
   254 // Also updates the total page count
   255 //
   256 void GetAllPageInfo()
   257 	{
   258 	TInt ret = 0;
   259 	gTotalPageCount.iFreePages = 0;
   260 	gTotalPageCount.iUnknownPages = 0;
   261 	gTotalPageCount.iFixedPages = 0;
   262 	gTotalPageCount.iMovablePages = 0;
   263 	gTotalPageCount.iDiscardablePages = 0;
   264 	gTotalPageCount.iOtherPages = 0;
   265 
   266 	// now get the config of each of the zones.
   267 	TUint	index;
   268 	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
   269 	for (index = 0; index < gZoneCount; index ++)
   270 		{
   271 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
   272 		test(ret == KErrNone);
   273 
   274 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
   275 		test(ret == KErrNone);
   276 		
   277 		gTotalPageCount.iFreePages += gZoneUtilArray[index].iFreePages;
   278 		gTotalPageCount.iUnknownPages += gZoneUtilArray[index].iAllocUnknown;
   279 		gTotalPageCount.iFixedPages += gZoneUtilArray[index].iAllocFixed;
   280 		gTotalPageCount.iMovablePages += gZoneUtilArray[index].iAllocMovable;
   281 		gTotalPageCount.iDiscardablePages += gZoneUtilArray[index].iAllocDiscardable;
   282 		gTotalPageCount.iOtherPages += gZoneUtilArray[index].iAllocOther;
   283 		}
   284 	TESTDEBUG(test.Printf(_L("free=0x%x unknown=0x%x fixed=0x%x move=0x%x discard=0x%x other=0x%x\n"),
   285 					gTotalPageCount.iFreePages, gTotalPageCount.iUnknownPages, gTotalPageCount.iFixedPages, 
   286 					gTotalPageCount.iMovablePages, gTotalPageCount.iDiscardablePages,gTotalPageCount.iOtherPages));
   287 
   288 	TESTDEBUG(PrintPageInfo());
   289 	}
   290 
   291 void RestoreRamZoneFlags()
   292 	{
   293 	GetAllPageInfo(); // Update the current set of RAM zone flag data.
   294 	for (TUint index=0; index < gZoneCount; index++)
   295 		{
   296 		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
   297 		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ORIG_FLAG, gOrigFlagArray[index]);
   298 		}
   299 	}
   300 
   301 void ResetRamZoneFlags()
   302 	{
   303 	GetAllPageInfo(); // Update the current set of RAM zone flag data.
   304 	for (TUint index=0; index < gZoneCount; index++)
   305 		{
   306 		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
   307 		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, RESET_FLAG);
   308 		}
   309 	}
   310 
   311 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
   312 void ResetDPCache();
   313 
   314 void FSCachCleanUp()
   315 	{
   316 	// If the File System Caching tests have been run, 
   317 	// ensure that they are cleaned up
   318 	if (gFileCacheRun)
   319 		{
   320 		TUint i = 0;
   321 		// First close all the open handles to the RFile objects open
   322 		for (i = 0; i < KNumFilesOrig; i++) 
   323 			{
   324 			gFile[i].Close();
   325 			}
   326 
   327 		// Now call EmptyRamZone on every zone to ensure that 
   328 		// discardable pages are cleaned up
   329 		
   330 		GetAllPageInfo();
   331 		for (i = 0; i < gZoneCount; i++)
   332 			{
   333 			TUint zoneID = gZoneConfigArray[i].iZoneId;
   334 			Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
   335 			}
   336 		gFileCacheRun = EFalse;
   337 		}
   338 	}
   339 
   340 //
   341 // TestCleanup
   342 //
   343 // Cleans up all the allocations made at the beginning of the test
   344 //
   345 void TestCleanup()
   346 	{
   347 
   348 	Ldd.ResetDriver();
   349 
   350 	// Revert the cleared flags to their original values before the tests were carried out
   351 	RestoreRamZoneFlags();
   352 
   353 	// Reset the DP cache and remove any allocated chunks and fixed pages.
   354 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
   355 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
   356 	ResetDPCache();
   357 	Ldd.FreeAllFixedPages();
   358 	Ldd.FreeFromAllZones();
   359 	FSCachCleanUp();
   360 
   361 	User::Free(gPrefArray);
   362 	gPrefArray = NULL;
   363 
   364 	User::Free(gOrigFlagArray);
   365 	gOrigFlagArray = NULL;
   366 
   367 	User::Free(gCandList1);
   368 	gCandList1 = NULL;
   369 
   370 	User::Free(gCandList2);
   371 	gCandList2 = NULL;
   372 
   373 	User::Free(gOriginalPageCountArray);
   374 	gOriginalPageCountArray = NULL;
   375 
   376 	// Output the last possible state of memory
   377 	if (!gFailPrintPageInfo)
   378 		PrintPageInfo();
   379 
   380 	User::Free(gZoneConfigArray);
   381 	gZoneConfigArray = NULL;
   382 
   383 	User::Free(gZoneUtilArray);
   384 	gZoneUtilArray = NULL;
   385 
   386 	}
   387 
   388 
   389 // TestSetup
   390 //
   391 // Get the necessary information needed to carry out the tests
   392 //
   393 TInt TestSetup()
   394 	{
   395 	// Get the MMC drive
   396 	gDrive = GetDrive();
   397 
   398 	// first get the number of zones
   399 	TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneCount,&gZoneCount,0);
   400 	if (ret != KErrNone)
   401 		{
   402 		test.Printf(_L("Cannot obtain the number of zones\n"));
   403 		return ret;
   404 		}
   405 	test.Printf(_L("RAM Zones (count=%u)\n"),gZoneCount);
   406 
   407 	// Obtain the size of the RAM and the size of a page	
   408 	ret = HAL::Get(HAL::EMemoryRAM, gRamSize);
   409 	if (ret != KErrNone)
   410 		{
   411 		test.Printf(_L("Cannot obtain the size of RAM\n"));
   412 		return ret;
   413 		}
   414 
   415 	// Retrieve the page size and use it to detemine the page shift (assumes 32-bit system).
   416 	ret = HAL::Get(HAL::EMemoryPageSize, gPageSize);
   417 	if (ret != KErrNone)
   418 		{
   419 		test.Printf(_L("Cannot obtain the page size\n"));
   420 		return ret;
   421 		}
   422 
   423 	TUint32 pageMask = gPageSize;
   424 	TUint i = 0;
   425 	for (; i < 32; i++)
   426 		{
   427 		if (pageMask & 1)
   428 			{
   429 			if (pageMask & ~1u)
   430 				{
   431 				test.Printf(_L("ERROR - page size not a power of 2"));
   432 				return KErrNotSupported;
   433 				}
   434 			gPageShift = i;
   435 			break;
   436 			}
   437 		pageMask >>= 1;
   438 		}
   439 
   440 	gZoneConfigArray = (SRamZoneConfig *)User::AllocZ(sizeof(SRamZoneConfig) * gZoneCount);
   441 	if (gZoneConfigArray == NULL)
   442 		return KErrNoMemory;
   443 	gZoneUtilArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
   444 	if (gZoneUtilArray == NULL)
   445 		return KErrNoMemory;
   446 	gPrefArray = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
   447 	if (gPrefArray == NULL)
   448 		return KErrNoMemory;
   449 	gOrigFlagArray = (TUint8 *)User::AllocZ(sizeof(TUint8) * gZoneCount);
   450 	if (gOrigFlagArray == NULL)
   451 		return KErrNoMemory;
   452 	gOriginalPageCountArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
   453 	if (gOriginalPageCountArray == NULL)
   454 		return KErrNoMemory;
   455 
   456 	gCandList1 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
   457 	if (gCandList1 == NULL)
   458 		return KErrNoMemory;
   459 	gCandList2 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
   460 	if (gCandList2 == NULL)
   461 		return KErrNoMemory;
   462 
   463 	GetAllPageInfo();
   464 	PrintPageInfo();
   465 	
   466 	// Store the original flags
   467 	for (i=0; i < gZoneCount; i++)
   468 		gOrigFlagArray[i] = gZoneConfigArray[i].iFlags;
   469 	
   470 	// Now clear the flags for carrying out tests
   471 	Ldd.ResetDriver();
   472 	ResetRamZoneFlags();
   473 	
   474 	
   475 	// Check whether the ROM is paged or not
   476 	TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
   477 	if(!romHeader->iPageableRomStart)
   478 		{
   479 		test.Printf(_L("Not a Paged ROM - Skipping all discardable page tests.\n"));
   480 		gPagedRom = EFalse;
   481 		}
   482 	else
   483 		{// Save the current state of the DP cache so it can be restored when required and
   484 		// after the test has finished.
   485 		TUint currentCacheSize;
   486 		DPTest::CacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize, currentCacheSize);
   487 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
   488 									gOriginalMinCacheSize >> gPageShift, gOriginalMaxCacheSize >> gPageShift, 
   489 									currentCacheSize >> gPageShift));
   490 		}
   491 
   492 	return KErrNone;
   493 	}
   494 
   495 
   496 // 
   497 // UpdateRamInfo
   498 //
   499 // Updating the various RAM information
   500 //
   501 void UpdateRamInfo()
   502 	{
   503 	HAL::Get(HAL::EMemoryRAMFree, gFreeRam);
   504 	TESTDEBUG(gRamUsed = gRamSize - gFreeRam);
   505 	}
   506 	
   507 
   508 // 
   509 // CheckRamDifference
   510 //
   511 // Checks the difference between the initial free RAM and the end free RAM
   512 //
   513 void CheckRamDifference()
   514 	{
   515 	if (gInitialRam == gEndRam)
   516 		{
   517 		TESTDEBUG(test.Printf(_L("No RAM was lost during this test\n")));
   518 		}
   519 	else
   520 		{
   521 		TInt diff = gInitialRam - gEndRam;
   522 
   523 
   524 		gTotalRamLost = gTotalRamLost + diff;
   525 		}
   526 	}
   527 
   528 TInt VerifyMovDisAlloc();
   529 //
   530 // TestStart
   531 //
   532 // Updates the RAM information at the beginning of a test step	
   533 //
   534 void TestStart()
   535 	{
   536 	test(!gTestStarted);
   537 	gTestStarted = ETrue;
   538 
   539 	Ldd.ResetDriver();
   540 	
   541 	Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
   542 	if (VerifyMovDisAlloc() != KErrNone)
   543 		{
   544 		CLEANUP(;);
   545 		TEST_FAIL;
   546 		}
   547 	
   548 	UpdateRamInfo();
   549 	gInitialRam = gFreeRam;
   550 	TESTDEBUG(test.Printf(_L("Initial Free RAM = 0x%x, Initial RAM Used = 0x%x\n"), gFreeRam, gRamUsed));
   551 	}
   552 
   553 
   554 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
   555 //
   556 // TestEnd
   557 //
   558 // Updates RAM information at end of test step and checks the RAM delta
   559 //
   560 void TestEnd()
   561 	{
   562 	test(gTestStarted);
   563 	gTestStarted = EFalse;
   564 
   565 	gDefragMaxPages = 0;
   566 
   567 	// Clean up anything that may need to be cleaned.
   568 	ResetRamZoneFlags();
   569 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
   570 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
   571 	ResetDPCache();
   572 	Ldd.FreeAllFixedPages();
   573 	FSCachCleanUp();
   574 	
   575 	UpdateRamInfo();
   576 	gEndRam = gFreeRam;
   577 	TESTDEBUG(test.Printf(_L("End RAM Free = 0x%x, End RAM Used = 0x%x\n"), gEndRam, gRamUsed));
   578 	CheckRamDifference();
   579 	test.Printf(_L(" \n"));
   580 	}
   581 
   582 //
   583 // CheckZonesSwitchedOff
   584 //
   585 // Checks that zones have been switched off
   586 //
   587 TBool CheckZonesSwitchedOff()
   588 	{
   589 	GetAllPageInfo();
   590 	for (TUint i = 0; i < gZoneCount; i++)
   591 		{
   592 		if (gOriginalPageCountArray[i].iFreePages != gOriginalPageCountArray[i].iPhysPages &&
   593 			gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
   594 			{
   595 			return ETrue;
   596 			}
   597 		}
   598 	return EFalse;
   599 	}
   600 	
   601 
   602 //
   603 // CheckZoneIsOff
   604 //
   605 // Checks if a particular zone is empty 
   606 //
   607 TBool CheckZoneIsOff(TUint aZoneIndex)
   608 	{
   609 	GetAllPageInfo();
   610 	if (gZoneUtilArray[aZoneIndex].iFreePages == gZoneUtilArray[aZoneIndex].iPhysPages)
   611 		{
   612 		TESTDEBUG(test.Printf(_L("Zone index %d is Empty\n"), aZoneIndex));
   613 		return ETrue;
   614 		}
   615 	else
   616 		{
   617 		TESTDEBUG(test.Printf(_L("Zone index %d is Not empty\n"), aZoneIndex));
   618 		return EFalse;
   619 		}
   620 	}
   621 
   622 
   623 //
   624 // GetPrefOrder
   625 //
   626 // Go through each zone ordering them in preference order
   627 //
   628 void GetPrefOrder()
   629 	{
   630 	GetAllPageInfo();
   631 	TESTDEBUG(PrintPageInfo());
   632 
   633 	for (TUint i=0; i < (TUint)gZoneCount; i++)
   634 		gPrefArray[i] = KErrNotFound;
   635 
   636 	for (TUint curIndex = 0; curIndex < gZoneCount; curIndex++)
   637 		{
   638 		TBool currentEmpty = gZoneUtilArray[curIndex].iPhysPages == gZoneUtilArray[curIndex].iFreePages;
   639 		TUint currentPref = gZoneConfigArray[curIndex].iPref;
   640 		TUint currentImmovPages = 	gZoneUtilArray[curIndex].iAllocFixed + 
   641 									gZoneUtilArray[curIndex].iAllocUnknown;
   642 		TUint morePrefCnt = 0;
   643 		for (TUint index = 0; index < gZoneCount; index++)
   644 			{// A RAM zone with the same iPref is more preferable if it has 
   645 			// more immovable pages.
   646 			if (gZoneConfigArray[index].iPref < currentPref || 
   647 				(gZoneConfigArray[index].iPref == currentPref && 
   648 				(currentImmovPages < gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ||
   649 				(currentEmpty &&
   650 				gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages))))
   651 				{
   652 				morePrefCnt++;
   653 				}
   654 			}
   655 
   656 		while (gPrefArray[morePrefCnt] != KErrNotFound)
   657 			{// Zone(s) of this preference and size already exist so 
   658 			 // place this one after it/them
   659 			morePrefCnt++;
   660 			}
   661 		gPrefArray[morePrefCnt] = curIndex;
   662 		}
   663 	}
   664 
   665 //
   666 // ZonesSamePref
   667 //
   668 // Return ETrue if the RAM zones are of the same preference.
   669 //
   670 // NOTE - This requires GetAllPageInfo() to have already been called.
   671 //
   672 TBool ZonesSamePref(TUint aZoneIndex1, TUint aZoneIndex2)
   673 	{
   674 	TBool zoneEmpty1 = gZoneUtilArray[aZoneIndex1].iFreePages == gZoneUtilArray[aZoneIndex1].iPhysPages;
   675 	TBool zoneEmpty2 = gZoneUtilArray[aZoneIndex2].iFreePages == gZoneUtilArray[aZoneIndex2].iPhysPages;
   676 	if (gZoneConfigArray[aZoneIndex1].iPref == gZoneConfigArray[aZoneIndex2].iPref &&
   677 		(gZoneUtilArray[aZoneIndex1].iAllocFixed + gZoneUtilArray[aZoneIndex1].iAllocUnknown == 
   678 		gZoneUtilArray[aZoneIndex2].iAllocFixed + gZoneUtilArray[aZoneIndex2].iAllocUnknown &&
   679 		(zoneEmpty1 == zoneEmpty2)))
   680 		{
   681 		return ETrue;
   682 		}
   683 
   684 	return EFalse;	
   685 	}
   686 
   687 //
   688 // FindMostPrefEmpty
   689 //
   690 // Checks all zones and returns the most preferable RAM zone which 
   691 // is completely emtpy
   692 //
   693 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable empty RAM zone.
   694 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable empty RAM zone.
   695 //
   696 // @return KErrNotFound if a zone cannot be found, else KErrNone
   697 //
   698 TInt FindMostPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
   699 	{
   700 	// Get the most pref zone which is completely free to use as a test zone
   701 	GetPrefOrder();
   702 	TUint prefIndex = 0;
   703 	for (; prefIndex < gZoneCount; prefIndex++)
   704 		{
   705 		TUint zoneIndex = gPrefArray[prefIndex];
   706 		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
   707 			{
   708 			aZoneIndex = zoneIndex;
   709 			if (aPrefIndex)
   710 				{
   711 				*aPrefIndex = prefIndex;
   712 				}
   713 			return KErrNone;
   714 			}
   715 		}
   716 	return KErrNotFound;
   717 	}
   718 
   719 //
   720 // FindLeastPrefEmpty
   721 //
   722 // Checks all zones and returns the least preferable RAM zone which 
   723 // is completely emtpy
   724 //
   725 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the least preferable empty RAM zone.
   726 // @param aPrefIndex On return this will contain the index into gPrefArray of the least preferable empty RAM zone.
   727 //
   728 // @return KErrNotFound if a zone cannot be found, else KErrNone
   729 //
   730 TInt FindLeastPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
   731 	{
   732 	// Get the most pref zone which is completely free to use as a test zone
   733 	GetPrefOrder();
   734 	TInt prefIndex = gZoneCount - 1;
   735 	for (; prefIndex >= 0; prefIndex--)
   736 		{
   737 		TUint zoneIndex = gPrefArray[prefIndex];
   738 		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
   739 			{
   740 			aZoneIndex = zoneIndex;
   741 			if (aPrefIndex)
   742 				{
   743 				*aPrefIndex = (TUint)prefIndex;
   744 				}
   745 			return KErrNone;
   746 			}
   747 		}
   748 	return KErrNotFound;
   749 	}
   750 
   751 //
   752 // FindMostPrefWithFree
   753 //
   754 // Checks all zones and returns the most preferable RAM zone which 
   755 // has at least 1 free page 
   756 //
   757 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable RAM zone with free pages.
   758 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable RAM zone with free pages.
   759 //
   760 // @return KErrNotFound if a zone cannot be found, else KErrNone
   761 //
   762 TInt FindMostPrefWithFree(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
   763 	{
   764 	// Get the most pref zone which has free pages
   765 	GetPrefOrder();
   766 	TUint prefIndex = 0;
   767 	for (; prefIndex < gZoneCount; prefIndex++)
   768 		{
   769 		TUint zoneIndex = gPrefArray[prefIndex];
   770 		if (gZoneUtilArray[zoneIndex].iFreePages)
   771 			{
   772 			aZoneIndex = zoneIndex;
   773 			if (aPrefIndex)
   774 				{
   775 				*aPrefIndex = prefIndex;
   776 				}
   777 			return KErrNone;
   778 			}
   779 		}
   780 	return KErrNotFound;
   781 	}
   782 //
   783 // CanGenSucceed
   784 //
   785 // Check whether a call to TRamDefragRequest::DefragRam() would be able to 
   786 // succeed or not. 
   787 //
   788 TBool CanGenSucceed()
   789 	{
   790 	GetPrefOrder();
   791 	TBool genSucceed = EFalse;
   792 	// Work out if general has anything to do
   793 	for(TInt prefIndex = (TInt)gZoneCount-1; prefIndex >= 0; prefIndex--)
   794 		{
   795 		TUint zoneIndex = gPrefArray[prefIndex];
   796 		TESTDEBUG(test.Printf(_L("prefIndex = %d zoneIndex = 0x%x\n"), prefIndex, zoneIndex));
   797 
   798 		TUint samePrefCount = 1;
   799 		TUint samePrefSucceed = 0;
   800 		// The number of zones of this preference that will be skipped by the general defrag
   801 		TUint samePrefEmptyImmovable = 0;
   802 
   803 		// Determine how many zones have the same preference as this one
   804 		TInt prevPrefIndex = (prefIndex != 0)? (prefIndex - 1) : -1;
   805 		for (; prevPrefIndex >= 0; prevPrefIndex--)
   806 			{
   807 			TUint prevIndex = gPrefArray[prevPrefIndex];
   808 			if (ZonesSamePref(zoneIndex, prevIndex))
   809 				{
   810 				samePrefCount++;
   811 				}
   812 			else // no more zones with this preference
   813 				break;
   814 			}
   815 		TESTDEBUG(test.Printf(_L("samePrefCount = %d\n"), samePrefCount));
   816 
   817 		for (TInt l = prefIndex - (samePrefCount-1); l <= prefIndex; l++)
   818 			{
   819 			TUint curPrefIndex = gPrefArray[l];
   820 			TESTDEBUG(test.Printf(_L("curPrefIndex = %d\n"), curPrefIndex));
   821 			if (gZoneUtilArray[curPrefIndex].iFreePages != gZoneConfigArray[curPrefIndex].iPhysPages)
   822 				{
   823 				TBool clearMovable = EFalse;
   824 				TBool clearDiscardable = EFalse;
   825 
   826 				if (gZoneUtilArray[curPrefIndex].iAllocUnknown || gZoneUtilArray[curPrefIndex].iAllocFixed)
   827 					{
   828 					TESTDEBUG(test.Printf(_L("unknown or fixed\n")));
   829 					samePrefEmptyImmovable++;
   830 					continue;
   831 					}
   832 				if (gZoneUtilArray[curPrefIndex].iAllocMovable)
   833 					{// determine if movable can potentially be cleared from this zone
   834 					TUint freeInLower = 0;
   835 					for (TInt j=0; j <= prefIndex; j++)
   836 						{
   837 						TUint idx = gPrefArray[j];
   838 						if (idx == curPrefIndex)
   839 							continue;
   840 						freeInLower += gZoneUtilArray[idx].iFreePages;
   841 						}
   842 					if (gZoneUtilArray[curPrefIndex].iAllocMovable <= freeInLower)
   843 						{
   844 						clearMovable = ETrue;
   845 						TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
   846 						}
   847 					}
   848 				else
   849 					{
   850 					TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
   851 					clearMovable = ETrue;
   852 					}
   853 				if (gZoneUtilArray[curPrefIndex].iAllocDiscardable)
   854 					{
   855 					if (gPagedRom)
   856 						{
   857 						TUint minCacheSize = 0;
   858 						TUint maxCacheSize = 0;
   859 						TUint currentCacheSize = 0;
   860 
   861 						DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
   862 						TUint spareCache = currentCacheSize - minCacheSize;
   863 						if (spareCache >= gZoneUtilArray[curPrefIndex].iAllocDiscardable )
   864 							{
   865 							clearDiscardable = ETrue;
   866 							TESTDEBUG(test.Printf(_L("Paged: Can clear discardable\n")));
   867 							}
   868 						else
   869 							{// determine space for discardable in more preferable zones
   870 							TUint freeInLower = 0;
   871 							for (TInt j=0; j <= prefIndex; j++)
   872 								{
   873 								TUint idx = gPrefArray[j];
   874 								if (idx == curPrefIndex)
   875 									continue;
   876 								freeInLower += gZoneUtilArray[idx].iFreePages;
   877 								}
   878 							if (gZoneUtilArray[curPrefIndex].iAllocDiscardable - spareCache <= freeInLower)
   879 								{
   880 								clearDiscardable = ETrue;	
   881 								TESTDEBUG(test.Printf(_L("Paged: Can clear discardable curPrefIndex = %d\n"), curPrefIndex));
   882 								}
   883 							}
   884 						}
   885 					else
   886 						{//Should always be OK to discard as no min cache size on non-paged ROMS
   887 						clearDiscardable = ETrue;
   888 						test.Printf(_L("Can clear discardable curPrefIndex = %d\n"), curPrefIndex);
   889 						}
   890 					}
   891 				else
   892 					{
   893 					clearDiscardable = ETrue;
   894 					}
   895 
   896 				if (clearDiscardable && clearMovable)
   897 					{
   898 					samePrefSucceed++;
   899 					TESTDEBUG(test.Printf(_L("General should succeed ID=%x\n"), gZoneConfigArray[curPrefIndex].iZoneId));
   900 					}
   901 				}
   902 			else
   903 				{//zone already empty
   904 				samePrefEmptyImmovable++;
   905 				}
   906 			}
   907 		if (samePrefSucceed == 0 && samePrefEmptyImmovable == 0)
   908 			{// no zones can be defragged and none are already empty/have immovable.
   909 			break;
   910 			}
   911 		if (samePrefEmptyImmovable != samePrefCount)
   912 			{// Have reached some zones with allocated pages in them.
   913 			if (samePrefSucceed + samePrefEmptyImmovable == samePrefCount)
   914 				{// general should definitely succeed as each of the zones of this preference 
   915 				// can be emptied or are already empty/have immovable pages allocated.
   916 				TESTDEBUG(test.Printf(_L("General should succeed \n")));
   917 				genSucceed = ETrue;
   918 				}
   919 			break;
   920 			}
   921 		prefIndex -= samePrefCount - 1;
   922 		}
   923 	return genSucceed;
   924 	}
   925 
   926 
   927 //
   928 // ReadByte
   929 //
   930 // Read a particular byte 
   931 //
   932 TUint8 ReadByte(volatile TUint8* aPtr)
   933 	{
   934 	return *aPtr;
   935 	}
   936 
   937 
   938 //
   939 // AllocDiscardable
   940 //
   941 // Allocate Discardable pages in the form of demand paged pages
   942 //
   943 // @param aNumDiscardableBytes On return this will contain the number of discardable bytes above the min cache size.
   944 // @param aMaxBytes The new limit for maximum number of bytes in the DP cache, set to KMaxTUInt64 to fill RAM.
   945 // @param aMinOffsetBytes When not set to KMaxTUint64, this sets the min cache size to be the max cache size - aMinOffsetBytes.
   946 //
   947 TInt AllocDiscardable(TInt& aNumDiscardableBytes, TUint64 aMaxBytes = KMaxTUint64, TUint64 aMinOffsetBytes = KMaxTUint64)
   948 	{
   949 	TUint minCacheSize = 0;
   950 	TUint maxCacheSize = 0;
   951 	TUint currentCacheSize = 0;
   952 	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
   953 	TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
   954 								minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
   955 
   956 	TESTDEBUG(test.Printf(_L("SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), minCacheSize >> gPageShift, aMaxBytes >> gPageShift));
   957 
   958 	if (aMaxBytes == KMaxTUint64)
   959 		{// Need to fill all of free memory with discardable pages
   960 		UpdateRamInfo();
   961 		maxCacheSize = minCacheSize + gFreeRam;
   962 		if (aMinOffsetBytes != KMaxTUint64)
   963 			{// Set the min cache size relative to the max cache size.
   964 			minCacheSize = maxCacheSize - aMinOffsetBytes;
   965 			}
   966 		TESTDEBUG(test.Printf(_L("free 0x%x max 0x%x min 0x%x\n"), gFreeRam, maxCacheSize, minCacheSize));
   967 		}
   968 	else
   969 		{
   970 		maxCacheSize = aMaxBytes;
   971 		}
   972 
   973 	TInt r = DPTest::SetCacheSize(maxCacheSize, maxCacheSize);
   974 	if (r != KErrNone)
   975 		return r;
   976 	r = DPTest::SetCacheSize(minCacheSize, maxCacheSize);
   977 	if (r != KErrNone)
   978 		return r;
   979 	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
   980 	TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
   981 					minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
   982 
   983 	aNumDiscardableBytes = currentCacheSize - minCacheSize;
   984 	TESTDEBUG(test.Printf(_L("Number of discardable bytes 0x%x\n"), aNumDiscardableBytes));
   985 
   986 	if (aMaxBytes == KMaxTUint64)
   987 		{
   988 		UpdateRamInfo();
   989 		if (gFreeRam != aNumDiscardableBytes)
   990 			{// The only free RAM should be that of the DP cache.
   991 			test.Printf(_L("gFreeRam 0x%x aNumDiscardableBytes 0x%x\n"), gFreeRam, aNumDiscardableBytes);
   992 			return KErrGeneral;
   993 			}
   994 		}
   995 	return KErrNone;
   996 	}	
   997 	
   998 
   999 //
  1000 // ResetDPCache
  1001 //
  1002 // Flush the cache and set the boundaries back to their original values
  1003 //
  1004 void ResetDPCache()
  1005 	{
  1006 	if (gPagedRom)
  1007 		{
  1008 		TUint minCacheSize = 0;
  1009 		TUint maxCacheSize = 0;
  1010 		TUint currentCacheSize = 0;
  1011 		
  1012 		TESTDEBUG(test.Printf(_L("FlushCache\n")));
  1013 		TInt r = DPTest::FlushCache();
  1014 
  1015 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  1016 		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  1017 						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  1018 		
  1019 		
  1020 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  1021 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize);
  1022 		TESTDEBUG(test.Printf(_L("SetCacheSize returns r = %d\n"), r));
  1023 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  1024 		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  1025 						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  1026 		}
  1027 	}
  1028 	
  1029 //
  1030 // WriteToChunk
  1031 //
  1032 // Write to a number of chunks
  1033 //
  1034 TInt WriteToChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk, TUint8 aStart=0)
  1035 	{
  1036 	for (TUint i=0; i<10; i++) // Write to all chunks 10 times
  1037 		{
  1038 		for (TUint j=0; j < aChunkArraySize; j++) //Write to all open chunks except aChunk.
  1039 			{
  1040 			if (aChunkArray[j].Handle() != NULL)
  1041 				{
  1042 				if ((TInt)j == aChunk) // Don't write to specified chunk
  1043 					{	
  1044 					continue;	
  1045 					}
  1046 				TUint8* base = aChunkArray[j].Base();
  1047 				for (TUint8 k = aStart; k < aStart + 10; k++)
  1048 					{
  1049 					*base++ = k; // write 0 - 9 to the chunk
  1050 					}
  1051 				}		
  1052 			}
  1053 		}
  1054 	return KErrNone;
  1055 	}
  1056 
  1057 
  1058 //
  1059 // ReadChunk
  1060 //
  1061 // Read chunks - If a chunk is specified, that particular chunk is not read
  1062 //
  1063 TInt ReadChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk=-1)
  1064 	{
  1065 	for (TUint j=0; j < aChunkArraySize; j++) //Read all open chunks
  1066 		{
  1067 		if (aChunkArray[j].Handle() != NULL)
  1068 			{
  1069 			if ((TInt)j == aChunk) // Don't read specified chunk
  1070 				{	
  1071 				continue;	
  1072 				}
  1073 			TUint8* base = aChunkArray[j].Base();
  1074 			while ((aChunkArray[j].Base() + aChunkArray[j].Size()) != base)
  1075 				{
  1076 				READ(base++);				
  1077 				}
  1078 			}		
  1079 		}
  1080 	return KErrNone;
  1081 	}
  1082 	
  1083 
  1084 //
  1085 // RemoveChunkAlloc
  1086 //
  1087 // Remove ALL chunks allocated
  1088 //
  1089 // @param aChunkArray The array that stores a reference to the chunks created.
  1090 // @param aChunkArraySize The size of aChunkArray.
  1091 //
  1092 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize)
  1093 	{
  1094 	TInt closedChunks = 0;
  1095 
  1096 	if (aChunkArray == NULL)
  1097 		{// The chunk array has already been deleted.
  1098 		return;
  1099 		}
  1100 
  1101 	for (TUint i = 0; i < aChunkArraySize; i++)
  1102 		{
  1103 		if (aChunkArray[i].Handle() != NULL)
  1104 			{
  1105 			aChunkArray[i].Close();
  1106 			closedChunks ++;
  1107 			}
  1108 		}
  1109 	delete[] aChunkArray;
  1110 	aChunkArray = NULL;
  1111 	aChunkArraySize = 0;
  1112 	UpdateRamInfo();
  1113 	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
  1114 	}	
  1115 
  1116 
  1117 TBool SpaceAvailForPageTables(TUint aZoneIndex, TInt aNumPages)
  1118 	{
  1119 	// Every 1MB allocated needs a new page table
  1120 	const TUint KDataBytesPerPageTable = 1024 * 1024; 
  1121 	
  1122 	// 1 Page can fit 4 page tables
  1123 	const TUint KPageTablesPerPage = 4;
  1124 	
  1125 	
  1126 	GetAllPageInfo();
  1127 	if (aNumPages == KFillAllMovable)
  1128 		{
  1129 		aNumPages = gTotalPageCount.iFreePages;
  1130 		}
  1131 
  1132 	TUint allocBytes = aNumPages << gPageShift;
  1133 
  1134 	// Add 1 as you always require at least 1 page table
  1135 	TUint pageTablesRequired = (allocBytes / KDataBytesPerPageTable) + 1;
  1136 	// Add 1 as the first 1-3 page tables may require a new page.
  1137 	TUint pageTablePagesRequired = (pageTablesRequired / KPageTablesPerPage) + 1;
  1138 	
  1139 	// Determine the number of free pages in the other zones
  1140 	TUint freeInOther = 0;
  1141 	for (TUint index = 0; index < gZoneCount; index++)
  1142 		{
  1143 		if (index != aZoneIndex)
  1144 			{
  1145 			freeInOther += gZoneUtilArray[index].iFreePages;
  1146 			}
  1147 		}
  1148 
  1149 	// Need an overhead for the heap to grow (5 pages)
  1150 	const TUint KOverhead = 5;
  1151 	if (freeInOther < pageTablePagesRequired + KOverhead)
  1152 		{// Not enough space in other zones to fit all page tables
  1153 		test.Printf(_L("No space in other zones for page table pages\n"));
  1154 		return EFalse;
  1155 		}
  1156 	// There is space available in the other zones to fit all the page tables
  1157 	return ETrue;
  1158 	}
  1159 //
  1160 // AllocMovable
  1161 //
  1162 // Allocate movable memory in the form of chunks
  1163 //
  1164 // @param aChunkArray The array to store a reference to the chunks created.
  1165 // @param aChunkArraySize The size of aChunkArray.
  1166 // @param aNumChunks The number of chunks to create.
  1167 // @param aNumPages The size of each chunk.
  1168 //
  1169 TInt AllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TInt aNumChunks, TUint aChunkSize=KChunkDefaultSize, TBool aForceFill = ETrue)
  1170 	{
  1171 	TUint i = 0;
  1172 	TInt r = 0;
  1173 	TUint chunksAllocd = 0;
  1174 	TBool fillAll = EFalse;
  1175 
  1176 	TInt numChunks = aNumChunks;
  1177 	UpdateRamInfo();
  1178 	// Allocate chunks to take up all of memory with the maximum number of chunks
  1179 	if (aNumChunks == KFillAllMovable)
  1180 		{
  1181 		fillAll = ETrue;
  1182 		if (aChunkSize == KChunkDefaultSize)
  1183 			{
  1184 			numChunks = KMaxChunks;
  1185 			aChunkSize = gFreeRam / numChunks; 
  1186 			}
  1187 		else
  1188 			{
  1189 			numChunks = gFreeRam  / aChunkSize;
  1190 			}
  1191 		}
  1192 
  1193 	test.Printf(_L("gFreeRam = 0x%x, aChunkSize = 0x%x, numChunks = %d\n"), gFreeRam, aChunkSize, numChunks);
  1194 	
  1195 	// Allocate as many chunks as is specified, either with the default chunk size or a specified chunk size
  1196 	if (aChunkArray == NULL)
  1197 		{
  1198 		aChunkArraySize = numChunks;
  1199 		aChunkArray = new RChunk[aChunkArraySize];
  1200 		if (aChunkArray == NULL)
  1201 			return KErrNoMemory;
  1202 		}
  1203 
  1204 	// Create chunks for each RChunk with a NULL handle.
  1205 	for (i = 0; i < aChunkArraySize; i++)
  1206 		{
  1207 		if (aChunkArray[i].Handle() == NULL)
  1208 			{
  1209 			// Keep going even if a chunk creation fails as the flag tests rely 
  1210 			// on this.
  1211 			r = aChunkArray[i].CreateLocal(aChunkSize, aChunkSize);
  1212 			if (r != KErrNone && fillAll && aForceFill)
  1213 				{
  1214 				while (aChunkArray[i].CreateLocal(aChunkSize, aChunkSize) != KErrNone)
  1215 					{
  1216 					aChunkSize -= gPageSize;
  1217 					}
  1218 				}
  1219 			if (r == KErrNone)	
  1220 				{
  1221 				chunksAllocd++;
  1222 				}
  1223 			}
  1224 		User::After(10); // Wait so that the next chunk gets allocated in the next time slice
  1225 		}
  1226 	test.Printf(_L("Number of chunks allocd = %d\n"),chunksAllocd);
  1227 	return r;
  1228 	}
  1229 
  1230 //
  1231 // ZoneAllocMovable
  1232 //
  1233 // Allocate the specified number of movable pages to a specific zone
  1234 // If the number of pages is not specified, then fill the specified zone with
  1235 // movable pages
  1236 //
  1237 // @param aChunkArray The array to store a reference to the chunks created.
  1238 // @param aChunkArraySize The size of aChunkArray.
  1239 // @param aZoneIndex The zone index to allocate movable pages to.
  1240 // @param aNumPages The number of movable pages to allocate.
  1241 //
  1242 TInt ZoneAllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TUint aZoneIndex, TUint aNumPages = KMaxTUint)
  1243 	{
  1244 	ResetRamZoneFlags();
  1245 	TInt r = KErrNone;
  1246 	TUint allocBytes = 0;
  1247 
  1248 	if (aNumPages == KMaxTUint)
  1249 		{
  1250 		aNumPages = gZoneUtilArray[aZoneIndex].iFreePages;
  1251 		}
  1252 
  1253 	allocBytes = aNumPages << gPageShift;
  1254 
  1255 	if (!SpaceAvailForPageTables(aZoneIndex, aNumPages))
  1256 		{
  1257 		return KErrGeneral;
  1258 		}
  1259 
  1260 	// Block all other zones from allocation
  1261 	for(TUint index = 0; index < gZoneCount; index++)
  1262 		{
  1263 		if (index == aZoneIndex)
  1264 			{
  1265 			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  1266 			}
  1267 		else
  1268 			{
  1269 			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
  1270 			}
  1271 		if (r != KErrNone)
  1272 			{
  1273 			test.Printf(_L("Failed to set RAM zone flag on zone %d r = %d\n"), index, r);
  1274 			return r;
  1275 			}
  1276 		}
  1277 
  1278 	// Allocate the movable pages
  1279 	r = AllocMovable(aChunkArray, aChunkArraySize, 1, allocBytes);
  1280 
  1281 	ResetRamZoneFlags();
  1282 	return r;
  1283 	}
  1284 
  1285 //
  1286 // ZoneAllocDiscard
  1287 //
  1288 // Allocate the specified number of discardable pages to a specific zone
  1289 //
  1290 // @param aZoneIndex The zone index to allocate discardable pages to.
  1291 // @param aNumPages The number of discardable pages to allocate.
  1292 // @param aDisPages On return this will contain the number of discardable pages allocated
  1293 //
  1294 TInt ZoneAllocDiscard(TUint aZoneIndex, TUint aNumPages, TInt& aDisPages)
  1295 	{
  1296 	TInt r = KErrNone;
  1297 	ResetRamZoneFlags();
  1298 	
  1299 	for (TUint index = 0; index < gZoneCount; index++)
  1300 		{
  1301 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  1302 		if (index == aZoneIndex)
  1303 			{
  1304 			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  1305 			}
  1306 		else
  1307 			{
  1308 			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
  1309 			}
  1310 
  1311 		if (r != KErrNone)
  1312 			{			
  1313 			test.Printf(_L("Failed to set flag r = %d\n"), r);
  1314 			return r;
  1315 			}
  1316 		}
  1317 	
  1318 	TUint disBytes = gTotalPageCount.iDiscardablePages + aNumPages << gPageShift;
  1319 	r = AllocDiscardable(aDisPages, disBytes);
  1320 	aDisPages = aDisPages >> gPageShift;
  1321 	if (r != KErrNone)
  1322 		{
  1323 		test.Printf(_L("Discardable pages not allocated r = %d\n"), r);
  1324 		}
  1325 	ResetRamZoneFlags();
  1326 	return r;
  1327 	}
  1328 
  1329 //
  1330 // FreeMovable
  1331 //
  1332 // Free movable pages by closing chunks. 
  1333 // The function will close every other chunk so that the movable pages are scattered in every zone
  1334 //
  1335 // @param aChunkArray The array that stores reference to the chunks created.
  1336 // @param aChunkArraySize The size of aChunkArray.
  1337 //
  1338 TInt FreeMovable(RChunk* aChunkArray, TUint aChunkArraySize)
  1339 	{
  1340 	TUint i;
  1341 	TInt closedChunks = 0;
  1342 		
  1343 	for (i=0; i < aChunkArraySize; i+=2) // Close every other chunk
  1344 		{
  1345 		if (aChunkArray[i].Handle() != NULL)
  1346 			{
  1347 			aChunkArray[i].Close();
  1348 			closedChunks ++;
  1349 			}
  1350 		}
  1351 	UpdateRamInfo();
  1352 	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
  1353 	return KErrNone;
  1354 	}
  1355 
  1356 //
  1357 // GetBestZone
  1358 //
  1359 // Obtains the most preferable zone for allocating a specific type of page
  1360 //
  1361 // @param aPageType The page type that we are interested in
  1362 // @param aBestPrefIndex The index into the preference array for the zone.
  1363 //
  1364 // @return KErrNotFound if a zone cannot be found, else the zone index into gZoneUtilArray
  1365 //
  1366 TInt GetBestZone(TInt aPageType, TUint* aBestPrefIndex = NULL)
  1367 	{
  1368 	GetPrefOrder();
  1369 
  1370 
  1371 	switch (aPageType)
  1372 		{
  1373 		case BEST_MOVABLE:			
  1374 		case BEST_DISCARDABLE:
  1375 			{
  1376 			TInt bestIndex = KErrNotFound;
  1377 			TUint bestPrefIndex = 0;
  1378 			TUint curIndex = 0;
  1379 			TInt startIndex = KErrNotFound;
  1380 			TBool zoneFound = EFalse;
  1381 			// Find the least preferable zone that has movable or discardable pages
  1382 			for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
  1383 				{
  1384 				curIndex = gPrefArray[prefIndex];
  1385 				if (gZoneUtilArray[curIndex].iAllocMovable || gZoneUtilArray[curIndex].iAllocDiscardable)
  1386 					{
  1387 					startIndex = prefIndex;
  1388 					break;
  1389 					}
  1390 				}
  1391 			if (startIndex == KErrNotFound)
  1392 				return startIndex;
  1393 				
  1394 			// Work up the preference list to look for the best zone
  1395 			for (TInt prefIndex = startIndex; prefIndex >= 0; prefIndex--)
  1396 				{
  1397 				curIndex = gPrefArray[prefIndex];
  1398 				if (gZoneUtilArray[curIndex].iFreePages)
  1399 					{
  1400 					bestIndex = curIndex;
  1401 					bestPrefIndex = prefIndex;
  1402 					zoneFound = ETrue;
  1403 					break;
  1404 					}
  1405 				}
  1406 			
  1407 			// If the zone still isn't found, look down the preference list
  1408 			if (!zoneFound)
  1409 				{
  1410 				for (TUint prefIndex = startIndex; prefIndex < gZoneCount; prefIndex++)
  1411 					{
  1412 					curIndex = gPrefArray[prefIndex];
  1413 					if (gZoneUtilArray[curIndex].iFreePages)
  1414 						{
  1415 						bestIndex = curIndex;
  1416 						bestPrefIndex = prefIndex;
  1417 						zoneFound = ETrue;
  1418 						break;
  1419 						}
  1420 					}
  1421 				}
  1422 				
  1423 			test.Printf(_L("leastPref = %d\n"), bestIndex);
  1424 			if (aBestPrefIndex)
  1425 				*aBestPrefIndex = bestPrefIndex;
  1426 			return bestIndex;
  1427 			}
  1428 
  1429 		case BEST_FIXED:
  1430 			for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
  1431 				{
  1432 				TUint mostPref = gPrefArray[prefIndex];
  1433 				if (gZoneUtilArray[mostPref].iFreePages != 0 || 
  1434 					gZoneUtilArray[mostPref].iAllocMovable != 0 ||
  1435 					gZoneUtilArray[mostPref].iAllocDiscardable != 0)
  1436 					{
  1437 					test.Printf(_L("mostPref = %d\n"), mostPref);
  1438 					if (aBestPrefIndex)
  1439 						*aBestPrefIndex = prefIndex;
  1440 					return mostPref;
  1441 					}
  1442 				}
  1443 			break;
  1444 		}
  1445 	
  1446 	test.Printf(_L("Cannot find zone\n"));
  1447 	return KErrNotFound;
  1448 	}
  1449 
  1450 void GetCandList1(TInt aIndex);
  1451 //
  1452 // VerifyMovDisAlloc
  1453 //
  1454 // Checks that all movable and discardable pages are in the correct RAM zones.
  1455 // Should only be invoked after a general defragmentation or a general 
  1456 // defragmentation followed by an allocation.
  1457 //
  1458 //	NOTE - This shouldn't be used to verify RAM if a RAM specific allocation or
  1459 //	zone claim operation has been performed.
  1460 //
  1461 // @return KErrNone if RAM layout is good, KErrGeneral if not, KErrNotFound if no
  1462 // movable or discardable pages are allocated.
  1463 //
  1464 TInt VerifyMovDisAlloc()
  1465 	{
  1466 	GetPrefOrder();
  1467 	TInt leastInUse = KErrNotFound;
  1468 	TUint requiredMovDis = 0;
  1469 	TUint totalMorePrefInUse = 0;
  1470 	TBool verifySpread = ETrue;
  1471 	TBool prevZoneNotFull = EFalse;
  1472 
  1473 	// Determine which is the least prefable RAM zone in use and how many pages
  1474 	// are allocated of each type.
  1475 	for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
  1476 		{
  1477 		TUint index = gPrefArray[prefIndex];
  1478 		TUint allocMov = gZoneUtilArray[index].iAllocMovable;
  1479 		TUint allocDis = gZoneUtilArray[index].iAllocDiscardable;
  1480 		TUint allocFixed = gZoneUtilArray[index].iAllocFixed;
  1481 		TESTDEBUG(test.Printf(_L("pref ID 0x%x\n"), gZoneConfigArray[index].iZoneId));
  1482 		if (allocMov || allocDis || allocFixed)
  1483 			{
  1484 			TBool zoneNotFull = EFalse;
  1485 			GetCandList1(index);
  1486 			TUint candIndex = 0;
  1487 			for (; candIndex < gZoneCount; candIndex++)
  1488 				{
  1489 				TInt zoneIndexCand = gCandList1[candIndex];
  1490 				if (zoneIndexCand == KInvalidCandIndex)
  1491 					{
  1492 					break;
  1493 					}
  1494 				allocMov  += gZoneUtilArray[zoneIndexCand].iAllocMovable;
  1495 				allocDis  += gZoneUtilArray[zoneIndexCand].iAllocDiscardable;
  1496 				if (gZoneUtilArray[zoneIndexCand].iFreePages)
  1497 					{
  1498 					zoneNotFull = ETrue;
  1499 					}
  1500 				}
  1501 			prefIndex -= candIndex - 1;
  1502 			if (leastInUse == KErrNotFound)
  1503 				{// Have found least preferable RAM zone that is in use.
  1504 				leastInUse = index;
  1505 				if (allocFixed)
  1506 					{// The least preferable RAM zone has fixed pages in it so
  1507 					// RAM may be more spread out than is necessary.
  1508 					verifySpread = EFalse;
  1509 					}
  1510 				}
  1511 			else
  1512 				{
  1513 				if ((allocMov || allocDis) && prevZoneNotFull)
  1514 					{// The previous least preferable RAM zones were not full so shouldn't
  1515 					// be any movable or discardable pages in the RAM zones of this preference.
  1516 					test.Printf(_L("Movable or discardable pages in more preferable RAM zones unnecessarily\n"));
  1517 					return KErrGeneral;
  1518 					}
  1519 				prevZoneNotFull = zoneNotFull;
  1520 
  1521 				// Not the least preferable RAM zone so add to total allocatable.
  1522 				totalMorePrefInUse += allocMov + allocDis + gZoneUtilArray[index].iFreePages;
  1523 				}
  1524 			requiredMovDis += allocMov + allocDis;
  1525 			}
  1526 		}
  1527 
  1528 	if (leastInUse == KErrNotFound)
  1529 		{// No movable or discardable pages are allocated.
  1530 		test.Printf(_L("No in use RAM zones found????\n"));
  1531 		return KErrNotFound;
  1532 		}
  1533 
  1534 	if (totalMorePrefInUse > requiredMovDis)
  1535 		{// There enough allocatable pages in the RAM zones below the currently 
  1536 		// least preferable RAM in use.
  1537 		test.Printf(_L("Memory is spread out totalMorePref 0x%x required 0x%x\n"), totalMorePrefInUse, requiredMovDis);
  1538 		if (verifySpread)
  1539 			return KErrGeneral;
  1540 		}
  1541 	return KErrNone;
  1542 	}
  1543 
  1544 
  1545 //
  1546 // GetCandList1
  1547 //
  1548 // Populates a list of all zones that have the same preference and the same amount 
  1549 // of immovable pages
  1550 //
  1551 void GetCandList1(TInt aIndex)
  1552 	{
  1553 	for (TUint i=0; i<gZoneCount; i++)
  1554 		{
  1555 		gCandList1[i] = KInvalidCandIndex;
  1556 		}
  1557 
  1558 	TInt candListIndex = 0;
  1559 	GetAllPageInfo();
  1560 	for (TUint i=0; i<gZoneCount; i++)
  1561 		{
  1562 		if (ZonesSamePref(aIndex, i))
  1563 			{
  1564 			gCandList1[candListIndex] = i;
  1565 			candListIndex ++;
  1566 			}
  1567 		}
  1568 	}
  1569 
  1570 //
  1571 // GetCandList2
  1572 //
  1573 // Populates a list of all zones that have the same preference and the same amount 
  1574 // of immovable pages
  1575 //
  1576 void GetCandList2(TInt aIndex)
  1577 	{
  1578 	for (TUint i=0; i<gZoneCount; i++)
  1579 		{
  1580 		gCandList2[i] = KInvalidCandIndex;
  1581 		}
  1582 
  1583 	TInt candListIndex = 0;
  1584 	GetAllPageInfo();
  1585 	for (TUint i=0; i<gZoneCount; i++)
  1586 		{
  1587 		if (ZonesSamePref(aIndex, i))
  1588 			{
  1589 			gCandList2[candListIndex] = i;
  1590 			candListIndex ++;
  1591 			}
  1592 		}
  1593 	}
  1594 
  1595 //
  1596 // GetCandListFixed1
  1597 //
  1598 // Populates a list of all zones that have the same preference and the same 
  1599 // amount of immovable pages, it will ignore RAM zones that are full of 
  1600 // immovable pages as these are the only RAM zones that fixed pages can't be 
  1601 // allocated into.
  1602 //
  1603 void GetCandListFixed1(TInt aIndex)
  1604 	{
  1605 	for (TUint i=0; i<gZoneCount; i++)
  1606 		{
  1607 		gCandList1[i] = KInvalidCandIndex;
  1608 		}
  1609 
  1610 	TInt candListIndex = 0;
  1611 	GetAllPageInfo();
  1612 	for (TUint i=0; i<gZoneCount; i++)
  1613 		{
  1614 		if (ZonesSamePref(aIndex, i) &&
  1615 			gZoneUtilArray[i].iAllocFixed + gZoneUtilArray[i].iAllocUnknown !=
  1616 			gZoneConfigArray[i].iPhysPages) 
  1617 			{
  1618 			gCandList1[candListIndex] = i;
  1619 			candListIndex ++;
  1620 			}
  1621 		}
  1622 	}
  1623 
  1624 //
  1625 // MultiGenDefragThreadFunc
  1626 //
  1627 // Called when a general defrag is called to run at the same time as another operation
  1628 //
  1629 TInt MultiGenDefragThreadFunc(TAny* /*aPtr*/)
  1630 	{
  1631 	RRamDefragFuncTestLdd Ldd2;
  1632 	TInt r = Ldd2.Open();
  1633 	if (r != KErrNone)
  1634 		{
  1635 		RDebug::Printf("Unable to open Ldd2 in MultiGenDefragThreadFunc, r = %d\n", r);
  1636 		return r;
  1637 		}
  1638 
  1639 	RThread thisThread = RThread();
  1640 	thisThread.SetPriority(EPriorityLess);
  1641 	r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  1642 	Ldd2.Close();
  1643 	thisThread.Close();
  1644 	return r;
  1645 	}
  1646 
  1647 TBool gTestLoop = EFalse;
  1648 //
  1649 // MultiLoopGenDefragThreadFunc
  1650 //
  1651 // Called when a general defrag is called to run at the same time as another operation
  1652 //
  1653 TInt MultiLoopGenDefragThreadFunc(TAny* /*aPtr*/)
  1654 	{
  1655 	RRamDefragFuncTestLdd Ldd2;
  1656 	TInt r = Ldd2.Open();
  1657 	if (r != KErrNone)
  1658 		{
  1659 		RDebug::Printf("Unable to open Ldd2 in MultiLoopGenDefragThreadFunc, r = %d\n", r);
  1660 		return r;
  1661 		}
  1662 
  1663 	RThread thisThread = RThread();
  1664 	thisThread.SetPriority(EPriorityLess);
  1665 	while (gTestLoop)
  1666 		{
  1667 		r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  1668 		if (r != KErrNone)
  1669 			goto threadExit;
  1670 		}
  1671 threadExit:
  1672 	Ldd2.Close();
  1673 	thisThread.Close();
  1674 	return r;
  1675 	}
  1676 
  1677 //
  1678 // MultiEmptyZoneThreadFunc
  1679 //
  1680 // Called when a zone defrag is called to run at the same time as another operation
  1681 //
  1682 TInt MultiEmptyZoneThreadFunc(TAny* zoneID)
  1683 	{
  1684 	RRamDefragFuncTestLdd Ldd2;
  1685 	TInt r = Ldd2.Open();
  1686 	if (r != KErrNone)
  1687 		{
  1688 		RDebug::Printf("Unable to open Ldd2 in MultiEmptyZoneThreadFunc, r = %d\n", r);
  1689 		return r;
  1690 		}
  1691 
  1692 	RThread thisThread = RThread();
  1693 	thisThread.SetPriority(EPriorityLess);
  1694 	r = Ldd2.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, (TUint)zoneID);
  1695 	Ldd2.Close();
  1696 	thisThread.Close();
  1697 	return r;
  1698 	}
  1699 
  1700 
  1701 //
  1702 // MultiClaimZoneThreadFunc
  1703 //
  1704 // Called when a ClaimRamZone is called to run at the same time as another operation
  1705 //
  1706 TInt MultiClaimZoneThreadFunc(TAny* zoneID)
  1707 	{
  1708 	RRamDefragFuncTestLdd Ldd2;
  1709 	TInt r = Ldd2.Open();
  1710 	if (r != KErrNone)
  1711 		{
  1712 		RDebug::Printf("Unable to open Ldd2 in MultiClaimZoneThreadFunc, r = %d\n", r);
  1713 		return r;
  1714 		}
  1715 
  1716 	RThread thisThread = RThread();
  1717 	thisThread.SetPriority(EPriorityLess);
  1718 	r = Ldd2.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, (TUint)zoneID);
  1719 	Ldd2.Close();
  1720 	thisThread.Close();
  1721 	return r;
  1722 	}
  1723 
  1724 
  1725 //
  1726 // TestAllocStrategies
  1727 //
  1728 // Verifying that pages are allocated correctly.  All tests rely on a general defragmentation occuring
  1729 // in TestStart() so that memory is in a defragmented state before the allocations.
  1730 //
  1731 //---------------------------------------------------------------------------------------------------------------------
  1732 //! @SYMTestCaseID				KBASE-t_ramdefrag-0525
  1733 //! @SYMTestCaseDesc			Verifying that pages are allocated correctly
  1734 //! @SYMTestType				CIT
  1735 //! @SYMPREQ					PREQ308
  1736 //! @SYMTestPriority			High
  1737 //! @SYMTestActions				
  1738 //! 	1.	Allocate one chunk to memory. 
  1739 //! 		Check which zone the chunk has been allocated to by checking the 
  1740 //! 		number of movable pages before and after the allocation has taken place. 
  1741 //!		2.	Allocate one chunk to memory that is large enough to cause an empty RAM 
  1742 //!			zone to be used.
  1743 //! 	3.	Allocate a certain number of fixed pages to memory. 
  1744 //! 		Check which zone the chunk has been allocated to by checking the 
  1745 //! 		number of fixed pages before and after the allocation has taken place. 
  1746 //!		4.	Allocate fixed pages when it is known that discardable pages will need 
  1747 //!			to be discarded for the allocation to succeed.
  1748 //!		5.	Allocate fixed pages when it is known that movable pages will need 
  1749 //!			to be moved for the allocation to succeed.
  1750 //! 	6.	Allocate fixed pages when it is known that movable pages will need 
  1751 //!			to be moved for the allocation to succeed, Determine the "best" zone for fixed pages and allocate 1 more
  1752 //!			than that. 
  1753 //!		7.	Allocate discardable pages by loading pages that are demand paged. 
  1754 //! 		Check which zone the memory has been allocated to by checking the number of 
  1755 //! 		discardable pages before and after the allocation has taken place. 
  1756 //! 
  1757 //! @SYMTestExpectedResults
  1758 //! 	1.	The memory has been allocated to the most preferred zone with the least amount of 
  1759 //! 		space accounting for the zone threshold for movable pages. 
  1760 //!		2.	The new RAM zone is used and the movable page are allocated into it first then the other
  1761 //!			more preferable RAM zones.
  1762 //! 	3.	The fixed pages are allocated to the most preferred zone
  1763 //!		4.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
  1764 //!		5.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
  1765 //! 	6.	Extra page is placed in the next preferable to the "best"zone 
  1766 //!		7.	Memory is allocated to the most preferred zone with the least amount of 
  1767 //! 		space accounting for the zone threshold for discardable pages. 
  1768 //---------------------------------------------------------------------------------------------------------------------
  1769 TInt TestAllocStrategies()
  1770 	{
  1771 	
  1772 	test.Start(_L("Test1: Check which zone a movable page has been allocated to "));	
  1773 	TestStart(); //This will perform a general defrag which should tidy up RAM for us
  1774 	gChunkArray1 = new RChunk;
  1775 	gChunkArraySize1 = 1;
  1776 	TBool zoneFound = EFalse;
  1777 
  1778 	// Determine if suitable RAM zone exists for testing
  1779 	TInt best = GetBestZone(BEST_MOVABLE);
  1780 	if (best == KErrNotFound)
  1781 		{
  1782 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  1783 		}
  1784 	else
  1785 		{
  1786 		test.Printf(_L("best = %d\n"), best);
  1787 		// Populate the candidate list
  1788 		GetCandList2(best);
  1789 		// Allocate 1 movable page
  1790 		GetOriginalPageCount();
  1791 		if (AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize) != KErrNone)
  1792 			{
  1793 			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
  1794 			}
  1795 		else
  1796 			{
  1797 			GetAllPageInfo();
  1798 			TInt r = VerifyMovDisAlloc();
  1799 			// Need to check all candidates to see if page has gone into any one of them
  1800 			for (TUint i=0; i < gZoneCount; i++)
  1801 				{
  1802 				if (gCandList2[i] == KInvalidCandIndex)
  1803 					{
  1804 					break;
  1805 					}
  1806 				TUint zoneIndex = gCandList2[i];
  1807 				if (gOriginalPageCountArray[zoneIndex].iAllocMovable < gZoneUtilArray[zoneIndex].iAllocMovable)
  1808 					{
  1809 					zoneFound = ETrue;
  1810 					break;
  1811 					}
  1812 				}
  1813 
  1814 			if (r == KErrNone && zoneFound)
  1815 				{
  1816 				test.Printf(_L("Pass: Movable allocated to the zone expected\n"));
  1817 				}
  1818 			else
  1819 				{
  1820 				test.Printf(_L("Fail: Movable allocated to a zone that was not expected, r %d\n"), r);
  1821 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  1822 				TEST_FAIL;
  1823 				}	
  1824 			}
  1825 		}
  1826 	TestEnd();
  1827 
  1828 	test.Next(_L("Test2: Check that movable pages allocated from least preferable RAM zone to be used\n"));
  1829 	TestStart();
  1830 	gChunkArray1 = new RChunk;
  1831 	gChunkArraySize1 = 1;
  1832 	// Determine if suitable RAM zone exists for testing.
  1833 	TUint bestPrefIndex;
  1834 	best = GetBestZone(BEST_MOVABLE, &bestPrefIndex); 
  1835 	TUint morePrefIndex = bestPrefIndex - 1;
  1836 	TUint nextLeastPrefIndex = bestPrefIndex + 1;
  1837 	
  1838 
  1839 	// Check number of free pages in the more preferable zones
  1840 	TUint freeInMorePref = 0;
  1841 	for (TUint i = 0; i < bestPrefIndex; i++)
  1842 		{
  1843 		freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
  1844 		}
  1845 	
  1846 	test.Printf(_L("freeInMorePref = 0x%x\n"), freeInMorePref);
  1847 	const TUint KHeapOverhead = 5; // Room for kernel heap allocations so they don't affect the page count
  1848 	if (best == KErrNotFound || nextLeastPrefIndex >= gZoneCount ||
  1849 		(gZoneUtilArray[gPrefArray[nextLeastPrefIndex]].iFreePages < KHeapOverhead && 
  1850 		freeInMorePref < KHeapOverhead)||
  1851 		gZoneConfigArray[gPrefArray[nextLeastPrefIndex]].iPref == gZoneConfigArray[best].iPref ||
  1852 		gZoneConfigArray[gPrefArray[morePrefIndex]].iPref == gZoneConfigArray[best].iPref)
  1853 		{// No less preferable RAM zone or there are more or less preferable of
  1854 		// same preference so re-ordering potential makes verification too complex.
  1855 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  1856 		}
  1857 	else
  1858 		{
  1859 		// Ensure the zone is almost full as chunks will get allocated in blocks
  1860 		// by almost filling the best zone with fixed pages, leaving a space (e.g. 20 pages)
  1861 		const TUint KSpaceNeeded = 20;
  1862 		if (gZoneUtilArray[best].iFreePages > KSpaceNeeded)
  1863 			{
  1864 			TUint zoneID = gZoneUtilArray[best].iZoneId;
  1865 			TUint diffAlloc = gZoneUtilArray[best].iFreePages - KSpaceNeeded;
  1866 			TInt r = Ldd.ZoneAllocDiscontiguous(zoneID, diffAlloc);
  1867 			if (r != KErrNone)
  1868 				{
  1869 				CLEANUP(Ldd.FreeAllFixedPages());
  1870 				TEST_FAIL;
  1871 				}
  1872 			}
  1873 		
  1874 
  1875 		
  1876 		GetAllPageInfo();
  1877 		// Update the number of free pages in the more preferable zones
  1878 		freeInMorePref = 0;
  1879 		for (TUint i = 0; i < bestPrefIndex; i++)
  1880 			{
  1881 			freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
  1882 			}
  1883 		TUint origFixed = gTotalPageCount.iFixedPages;
  1884 
  1885 		GetOriginalPageCount();
  1886 
  1887 		// Allocate enough movable pages that the next least preferable RAM zone 
  1888 		// will need to be used.
  1889 		TUint movPages = gZoneUtilArray[best].iFreePages + 1;
  1890 		TUint movBytes = movPages << gPageShift;
  1891 		TInt r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes);
  1892 		if (r != KErrNone)
  1893 			{
  1894 			CLEANUP(Ldd.FreeAllFixedPages());
  1895 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  1896 			TEST_FAIL;
  1897 			}
  1898 
  1899 		GetAllPageInfo();
  1900 		TUint curFixed = gTotalPageCount.iFixedPages;
  1901 		TInt difFixed = curFixed - origFixed;	
  1902 		
  1903 		// If there is space in the more preferable zones then they should be allocated starting from
  1904 		// the "best" zone to the more preferable zones, else from the next least preferable zone to the more preferable zones
  1905 		TUint prefIndex;		
  1906 		if ((TInt)freeInMorePref > difFixed)
  1907 			{//No new zones should be turned on, allocation should have gone into bestPrefIndex then down towards most preferred zones
  1908 			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
  1909 			if (gOriginalPageCountArray[best].iAllocMovable >= gZoneUtilArray[best].iAllocMovable ||
  1910 				gOriginalPageCountArray[nextIndex].iAllocMovable < gZoneUtilArray[nextIndex].iAllocMovable)
  1911 				{
  1912 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
  1913 								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
  1914 								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
  1915 				CLEANUP(Ldd.FreeAllFixedPages());
  1916 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  1917 				TEST_FAIL;
  1918 				}
  1919 			prefIndex = morePrefIndex;
  1920 			}
  1921 		else
  1922 			{// If there are enough free pages in "nextIndex" to fit all movable pages allocated then the movable 
  1923 			// page count in "best" should stay the same, else they should be allocated between "nextIndex" and "best".
  1924 			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
  1925 			test.Printf(_L("nextIndex = %d\n"), nextIndex);
  1926 			if (gOriginalPageCountArray[nextIndex].iAllocMovable >= gZoneUtilArray[nextIndex].iAllocMovable ||
  1927 				(gOriginalPageCountArray[nextIndex].iFreePages >= movPages && 
  1928 				gOriginalPageCountArray[best].iAllocMovable != gZoneUtilArray[best].iAllocMovable) ||
  1929 				(gOriginalPageCountArray[nextIndex].iFreePages < movPages && 
  1930 				gOriginalPageCountArray[best].iAllocMovable == gZoneUtilArray[best].iAllocMovable))
  1931 				{
  1932 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
  1933 								gOriginalPageCountArray[nextIndex].iFreePages, gZoneUtilArray[nextIndex].iFreePages, 
  1934 								gZoneUtilArray[nextIndex].iFreePages);
  1935 				CLEANUP(Ldd.FreeAllFixedPages());
  1936 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  1937 				TEST_FAIL;
  1938 				}
  1939 			prefIndex = bestPrefIndex;
  1940 			}
  1941 		
  1942 		// Check that movable pages have only been allocated into the more 
  1943 		// preferable RAM zones if the less preferable ram zones in use are full.
  1944 		prefIndex++;
  1945 		do
  1946 			{
  1947 			prefIndex--;
  1948 			TUint indexCurrent = gPrefArray[prefIndex];			
  1949 			TUint indexLessPref = gPrefArray[prefIndex+1];
  1950 
  1951 			if (gOriginalPageCountArray[indexCurrent].iAllocMovable < gZoneUtilArray[indexCurrent].iAllocMovable &&
  1952 				gZoneUtilArray[indexLessPref].iFreePages)
  1953 				{// Current in use zone or less preferable than current has free pages so fail
  1954 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
  1955 								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
  1956 								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
  1957 				CLEANUP(Ldd.FreeAllFixedPages());
  1958 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  1959 				TEST_FAIL;
  1960 				}
  1961 			}
  1962 		while(prefIndex);
  1963 		test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
  1964 		}
  1965 	TestEnd();
  1966 
  1967 	test.Next(_L("Test3: Check which zone a fixed page has been allocated to "));	
  1968 	for (TUint testStep = 0; testStep < 2; testStep++)
  1969 		{
  1970 		switch (testStep)
  1971 			{
  1972 			case 0:
  1973 				test.Printf(_L("Testing discontiguous allocations\n"));
  1974 				break;
  1975 				
  1976 			case 1:
  1977 				test.Printf(_L("Testing contiguous allocations\n"));
  1978 				break;
  1979 			}
  1980 		TestStart();
  1981 		zoneFound = EFalse;
  1982 		GetOriginalPageCount();
  1983 		
  1984 		best = GetBestZone(BEST_FIXED);
  1985 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
  1986 		if (best == KErrNotFound)
  1987 			{
  1988 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  1989 			}
  1990 		else
  1991 			{
  1992 			GetCandList1(best);
  1993 			TInt allocFixedPages = 1; // Try and allocate just 1 fixed page
  1994 			switch (testStep)
  1995 				{
  1996 				case 0:
  1997 					Ldd.AllocateFixed(allocFixedPages); 
  1998 					break;
  1999 					
  2000 				case 1:
  2001 					TUint allocFixedBytes = allocFixedPages << gPageShift;
  2002 					Ldd.AllocContiguous(allocFixedBytes); 
  2003 					break;
  2004 				}
  2005 			
  2006 			
  2007 			GetAllPageInfo();
  2008 			for (TUint i=0; i<gZoneCount; i++)
  2009 				{
  2010 				if (gCandList1[i] == KInvalidCandIndex)
  2011 					{
  2012 					break;
  2013 					}
  2014 				TUint zoneIndex = gCandList1[i];
  2015 				if (gOriginalPageCountArray[zoneIndex].iAllocFixed + allocFixedPages <= gZoneUtilArray[zoneIndex].iAllocFixed)
  2016 					{
  2017 					zoneFound = ETrue;
  2018 					break;
  2019 					}
  2020 				}
  2021 
  2022 			if (zoneFound)
  2023 				{
  2024 				test.Printf(_L("Pass: Chunk has been allocated to the zone expected\n"));
  2025 				}
  2026 			else
  2027 				{
  2028 				test.Printf(_L("Fail: Fixed been allocated to a zone that was not expected\n"));
  2029 				CLEANUP(Ldd.FreeAllFixedPages());
  2030 				TEST_FAIL;
  2031 				}	
  2032 			
  2033 			Ldd.FreeAllFixedPages();
  2034 			}
  2035 		TestEnd();
  2036 		}
  2037 
  2038 	test.Next(_L("Test4: Check fixed page allocations will discard pages"));
  2039 	for (TUint testStep = 0; testStep < 2; testStep++)
  2040 		{
  2041 		switch (testStep)
  2042 			{
  2043 			case 0:
  2044 				test.Printf(_L("Testing discontiguous allocations\n"));
  2045 				break;
  2046 				
  2047 			case 1:
  2048 				test.Printf(_L("Testing contiguous allocations\n"));
  2049 				break;
  2050 			}
  2051 		TestStart();
  2052 		TInt discardBytes;
  2053 		TInt r;
  2054 
  2055 		if (!gPagedRom)
  2056 			{
  2057 			test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  2058 			goto SkipTest4;
  2059 			}
  2060 
  2061 		best = GetBestZone(BEST_FIXED);
  2062 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
  2063 		if (best == KErrNotFound)
  2064 			{
  2065 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  2066 			goto SkipTest4;
  2067 			}
  2068 		// Ensure discardable pages in the preferred RAM zone.
  2069 		r = AllocDiscardable(discardBytes);
  2070 		if (r != KErrNone)
  2071 			{
  2072 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2073 			TEST_FAIL;
  2074 			}
  2075 		if (discardBytes < gPageSize)
  2076 			{// Can't discard any pages so test can't run.
  2077 			test.Printf(_L("Memory too full to perform test - Skipping...\n"));
  2078 			goto SkipTest4;
  2079 			}
  2080 
  2081 		// Make sure all RAM zones that the fixed page allocation could potentially 
  2082 		// go to have discardable pages allocated in it.
  2083 		GetOriginalPageCount();
  2084 		GetCandListFixed1(best);
  2085 		for (TUint i = 0; i < gZoneCount; i++)
  2086 			{
  2087 			if (gCandList1[i] == KInvalidCandIndex)
  2088 				{
  2089 				break;
  2090 				}
  2091 			TUint zoneIndex = gCandList1[i];
  2092 			if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable == 0)
  2093 				{
  2094 				test.Printf(_L("No dicardable pages in one of the candidates RAM zones - Skipping...\n"));
  2095 				goto SkipTest4;
  2096 				}
  2097 			if (gOriginalPageCountArray[zoneIndex].iFreePages != 0)
  2098 				{
  2099 				test.Printf(_L("Some free pages in candidate RAM zone ID%x\n"), 
  2100 							gZoneConfigArray[zoneIndex].iZoneId);
  2101 				CLEANUP(ResetDPCache());
  2102 				TEST_FAIL;
  2103 				}
  2104 			}
  2105 
  2106 		// Allocate 1 fixed page and ensure that it discards a page.
  2107 		switch (testStep)
  2108 			{
  2109 			case 0:
  2110 				r = Ldd.AllocateFixed(1); 
  2111 				break;
  2112 				
  2113 			case 1:
  2114 				r = Ldd.AllocContiguous(gPageSize); 
  2115 				break;
  2116 			}
  2117 
  2118 		// Find RAM zone the fixed page was allocated into.
  2119 		GetAllPageInfo();
  2120 		zoneFound = EFalse;
  2121 		for (TUint i = 0; i < gZoneCount; i++)
  2122 			{
  2123 			if (gCandList1[i] == KInvalidCandIndex)
  2124 				{
  2125 				break;
  2126 				}
  2127 			TUint zoneIndex = gCandList1[i];
  2128 			if (gOriginalPageCountArray[zoneIndex].iAllocFixed < gZoneUtilArray[zoneIndex].iAllocFixed)
  2129 				{
  2130 				zoneFound = ETrue;
  2131 				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable <= gZoneUtilArray[zoneIndex].iAllocDiscardable &&
  2132 					gOriginalPageCountArray[zoneIndex].iAllocMovable <= gZoneUtilArray[zoneIndex].iAllocMovable)
  2133 					{
  2134 					test.Printf(_L("Fixed pages allocated but no pages discarded in RAM zone ID 0x%x\n"), 
  2135 						gZoneConfigArray[zoneIndex].iZoneId);
  2136 					CLEANUP(ResetDPCache());
  2137 					TEST_FAIL;
  2138 					}
  2139 				}
  2140 			}
  2141 
  2142 		if (!zoneFound || r != KErrNone)
  2143 			{
  2144 			test.Printf(_L("No fixed pages were allocated r = %d\n"), r);
  2145 			CLEANUP(ResetDPCache());
  2146 			TEST_FAIL;
  2147 			}
  2148 		else
  2149 			{
  2150 			test.Printf(_L("Pass: Pages been allocated to the zone expected\n"));
  2151 			}
  2152 	SkipTest4 :
  2153 		// This will free any DP cache pages and fixed pages allocated.
  2154 		TestEnd();
  2155 		}
  2156 
  2157 
  2158 	for (TUint testIndex = 0; testIndex < 2; testIndex++)
  2159 		{
  2160 		switch (testIndex)
  2161 			{
  2162 			case 0:
  2163 				test.Next(_L("Test5: Check fixed page allocations (discontiguous) will move pages"));
  2164 				break;
  2165 
  2166 			case 1:
  2167 				test.Next(_L("Test6: Check fixed page will only go into a new zone if all other fix page zones are full of fix"));
  2168 				break;
  2169 			}
  2170 
  2171 		TestStart();
  2172 		gChunkArray1 = new RChunk;
  2173 		gChunkArraySize1 = 1;
  2174 		TInt r = KErrNone;
  2175 		TUint freeInOtherZones = 0;
  2176 		TUint allocatablePages;
  2177 		TUint allocImmovPages;
  2178 		TUint bestPrefIndex;
  2179 		TUint nextBestIndex = 0;
  2180 		const TUint KMovAllocOverhead = 5;	// need pages for page tables and kernel heap expansion.
  2181 		
  2182 		best = GetBestZone(BEST_FIXED, &bestPrefIndex);
  2183 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
  2184 		GetCandListFixed1(best);
  2185 		TUint candidates = 0;
  2186 		for (TUint i = 0; i < gZoneCount; i++)
  2187 			{
  2188 			if (gCandList1[i] == KInvalidCandIndex)
  2189 				{
  2190 				break;
  2191 				}
  2192 			candidates++;
  2193 			}
  2194 		if (best == KErrNotFound || 
  2195 			(gZoneUtilArray[best].iAllocMovable == 0 && gZoneUtilArray[best].iFreePages < KMovAllocOverhead) ||
  2196 			candidates != 1)
  2197 			{
  2198 			test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
  2199 			goto SkipTest5;
  2200 			}
  2201 
  2202 		if (testIndex == 1)
  2203 			{// need to work out what the next best zone would be
  2204 			GetPrefOrder();
  2205 			if (bestPrefIndex + 1 >= gZoneCount)
  2206 				{
  2207 				test.Printf(_L("Cannot find next best zone - Skipping...\n"));
  2208 				goto SkipTest5;
  2209 				}
  2210 			nextBestIndex = gPrefArray[bestPrefIndex + 1];
  2211 			test.Printf(_L("nextBestIndex= %d\n"), nextBestIndex);
  2212 			GetCandListFixed1(nextBestIndex);
  2213 			candidates = 0;
  2214 			for (TUint i = 0; i < gZoneCount; i++)
  2215 				{
  2216 				if (gCandList1[i] == KInvalidCandIndex)
  2217 					{
  2218 					break;
  2219 					}
  2220 				candidates++;
  2221 				}
  2222 			if (gZoneUtilArray[nextBestIndex].iPhysPages == gZoneUtilArray[nextBestIndex].iAllocFixed + 
  2223 															gZoneUtilArray[nextBestIndex].iAllocUnknown ||
  2224 				candidates != 1)
  2225 				{
  2226 				test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
  2227 				goto SkipTest5;
  2228 				}
  2229 			}
  2230 
  2231 
  2232 
  2233 		for (TUint i = 0; i < gZoneCount; i++)
  2234 			{
  2235 			if (i != (TUint)best)
  2236 				{
  2237 				freeInOtherZones += gZoneUtilArray[i].iFreePages;
  2238 				}
  2239 			}
  2240 		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
  2241 							gZoneUtilArray[best].iAllocMovable + 
  2242 							gZoneUtilArray[best].iAllocDiscardable;
  2243 		
  2244 		if (allocatablePages > freeInOtherZones)
  2245 			{
  2246 			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
  2247 			goto SkipTest5;
  2248 			}
  2249 		
  2250 		// Allocate the fixed array before getting any page counts
  2251 		r = Ldd.AllocFixedArray(allocatablePages + 1);
  2252 		if (r != KErrNone)
  2253 			{
  2254 			test.Printf(_L("Failed to allocate fixed array r = %d - Skipping...\n"), r);
  2255 			goto SkipTest5;
  2256 			}
  2257 		
  2258 		// Fill the RAM zone with movable pages if none already in it.
  2259 		GetAllPageInfo();
  2260 		if (gZoneUtilArray[best].iAllocMovable == 0)
  2261 			{
  2262 			if (!gZoneUtilArray[best].iFreePages)
  2263 				{
  2264 				test.Printf(_L("RAM zone ID %x too full for test - Skipping...\n"), gZoneConfigArray[best].iZoneId);
  2265 				goto SkipTest5;
  2266 				}
  2267 			// Fill the zone with movable pages
  2268 			r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, best);
  2269 			if (r != KErrNone)
  2270 				{
  2271 				test.Printf(_L("Failed to fill zone index %d with movable r = %d\n"), best, r);
  2272 				CLEANUP(ResetRamZoneFlags());
  2273 				TEST_FAIL;
  2274 				}
  2275 			
  2276 			if (GetBestZone(BEST_FIXED) != best)
  2277 				{
  2278 				test.Printf(_L("Unable to complete test; RAM zone reordering - Skipping...\n"));
  2279 				goto SkipTest5;
  2280 				}
  2281 			}	
  2282 		// Allocate fixed pages after reseting the allocation flags.
  2283 		GetAllPageInfo();
  2284 		ResetRamZoneFlags();
  2285 		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
  2286 							gZoneUtilArray[best].iAllocMovable + 
  2287 							gZoneUtilArray[best].iAllocDiscardable;
  2288 		switch (testIndex)
  2289 			{
  2290 			case 0:
  2291 				r = Ldd.AllocateFixed2(allocatablePages);
  2292 				GetAllPageInfo();
  2293 				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
  2294 				if (r != KErrNone || gZoneConfigArray[best].iPhysPages != allocImmovPages)
  2295 					{
  2296 					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages\n"), gZoneConfigArray[best].iZoneId);
  2297 					CLEANUP(ResetRamZoneFlags());
  2298 					TEST_FAIL;
  2299 					}
  2300 				else
  2301 					{
  2302 					test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
  2303 					}
  2304 				break;
  2305 
  2306 			case 1:
  2307 				GetOriginalPageCount();
  2308 				r = Ldd.AllocateFixed2(allocatablePages + 1);
  2309 				GetAllPageInfo();
  2310 				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
  2311 				if (r != KErrNone || gZoneUtilArray[best].iPhysPages != allocImmovPages ||
  2312 					gZoneUtilArray[nextBestIndex].iAllocFixed <= gOriginalPageCountArray[nextBestIndex].iAllocFixed)
  2313 					{
  2314 					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages or next best ID 0x%x no fix pages\n"), 
  2315 									gZoneConfigArray[best].iZoneId, gZoneConfigArray[nextBestIndex].iZoneId);
  2316 					test.Printf(_L("nextBest %d origFix 0x%x curFix 0x%x\n"), 
  2317 									nextBestIndex, gOriginalPageCountArray[nextBestIndex].iAllocFixed, 
  2318 									gZoneUtilArray[nextBestIndex].iAllocFixed);
  2319 					CLEANUP(ResetRamZoneFlags());
  2320 					TEST_FAIL;
  2321 					}
  2322 				// Go through every other zone and check that fixed pages haven't increased
  2323 				for (TUint i = 0; i < gZoneCount; i++)
  2324 					{
  2325 					if (i != (TUint)best && i != (TUint)nextBestIndex &&
  2326 						gZoneUtilArray[i].iAllocFixed > gOriginalPageCountArray[i].iAllocFixed)
  2327 						{
  2328 						test.Printf(_L("FAIL: Fix page count increased zoneIndex %d orig 0x%x current 0x%x\n"), 
  2329 										i, gOriginalPageCountArray[i].iAllocFixed, gZoneUtilArray[i].iAllocFixed);
  2330 						CLEANUP(ResetRamZoneFlags());
  2331 						TEST_FAIL;
  2332 						}
  2333 					}
  2334 
  2335 				test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
  2336 				break;
  2337 			}
  2338 	SkipTest5 :
  2339 		// This will perform any required clean up.
  2340 		ResetRamZoneFlags();
  2341 		TestEnd();
  2342 	}
  2343 
  2344 	test.Next(_L("Test7: Check which zone a discardable page has been allocated to"));	
  2345 	TestStart();
  2346 	if (gPagedRom)
  2347 		{
  2348 		GetAllPageInfo();
  2349 
  2350 		// Try to allocate just one more discardable page
  2351 		TUint allocBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
  2352 		TInt discardablePages;
  2353 
  2354 		best = GetBestZone(BEST_DISCARDABLE);
  2355 		if (best == KErrNotFound)
  2356 			{
  2357 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  2358 			}
  2359 		else
  2360 			{
  2361 			// Populate the candidate list
  2362 			GetCandList2(best);
  2363 			zoneFound = EFalse;
  2364 			GetOriginalPageCount();
  2365 			TInt r = AllocDiscardable(discardablePages, allocBytes);
  2366 			if (r != KErrNone)
  2367 				{
  2368 				test.Printf(_L("Fail: r %d\n"), r);
  2369 				CLEANUP(ResetDPCache());
  2370 				TEST_FAIL;
  2371 				}
  2372 
  2373 			r = VerifyMovDisAlloc();
  2374 			// Need to check all candidates to see if page has gone into any one of them
  2375 			for (TUint i=0; i < gZoneCount; i++)
  2376 				{
  2377 				if (gCandList2[i] == KInvalidCandIndex)
  2378 					{
  2379 					break;
  2380 					}
  2381 				TUint zoneIndex = gCandList2[i];
  2382 				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable < gZoneUtilArray[zoneIndex].iAllocDiscardable)
  2383 					{
  2384 					zoneFound = ETrue;
  2385 					break;
  2386 					}
  2387 				}
  2388 
  2389 			if (r == KErrNone && zoneFound)
  2390 				{
  2391 				test.Printf(_L("Pass: Discardable allocated to the zone expected\n"));
  2392 				}
  2393 			else
  2394 				{
  2395 				test.Printf(_L("Fail: Discardable been allocated to a zone that was not expected r %d\n"), r);
  2396 				CLEANUP(ResetDPCache());
  2397 				TEST_FAIL;
  2398 				}	
  2399 			}
  2400 		}
  2401 	else
  2402 		{
  2403 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  2404 		}
  2405 	TestEnd();
  2406 
  2407 	test.Next(_L("Test8: Check fixed page allocations (contiguous) will move pages"));
  2408 	TestStart();
  2409 	gChunkArray1 = new RChunk;
  2410 	gChunkArraySize1 = 1;
  2411 	
  2412 	TInt r = KErrNone;
  2413 	TUint testZoneIndex = 0;
  2414 
  2415 	// Get the most pref zone which is completely free to use as a test zone
  2416 	r = FindMostPrefEmpty(testZoneIndex);
  2417 	if (r != KErrNone)
  2418 		{
  2419 		test.Printf(_L("Cannot find empty zone - Skipping...\n"));
  2420 		goto skipTest8;
  2421 		}
  2422 
  2423 		
  2424 	// fill the test zone with movable pages
  2425 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex);
  2426 	GetAllPageInfo();
  2427 	if (r != KErrNone ||
  2428 		gZoneUtilArray[testZoneIndex].iAllocMovable != gZoneUtilArray[testZoneIndex].iPhysPages)
  2429 		{
  2430 		test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
  2431 		goto skipTest8;
  2432 		}
  2433 
  2434 	if (gTotalPageCount.iFreePages < 1)
  2435 		{
  2436 		test.Printf(_L("Insufficient memory - totalFreePages = 0x%x - Skipping...\n"), gTotalPageCount.iFreePages);
  2437 		goto skipTest8;
  2438 		}
  2439 
  2440 	for (TUint zoneIndex = 0; zoneIndex < gZoneCount; zoneIndex++)
  2441 		{
  2442 		TUint zoneId = gZoneConfigArray[zoneIndex].iZoneId;
  2443 		if (zoneIndex != testZoneIndex)
  2444 			{
  2445 			r = Ldd.SetZoneFlag(zoneId, gZoneConfigArray[zoneIndex].iFlags, NO_FIXED_FLAG);
  2446 			if (r != KErrNone)
  2447 				{
  2448 				test.Printf(_L("Failed to set flag on zone index %d r = %d- Skipping...\n"), zoneIndex, r);
  2449 				goto skipTest8;
  2450 				}
  2451 			}
  2452 		}
  2453 	
  2454 	//attempt to alloc 1 contiguous fixed page
  2455 	r = Ldd.AllocContiguous(gPageSize);
  2456 	GetAllPageInfo();
  2457 	if (r != KErrNone ||
  2458 		!gZoneUtilArray[testZoneIndex].iAllocFixed)
  2459 		{
  2460 		test.Printf(_L("FAIL: no fixed pages in testZoneIndex(%d) r = %d\n"), testZoneIndex, r);
  2461 		CLEANUP(Ldd.FreeAllFixedPages());
  2462 		TEST_FAIL;
  2463 		}
  2464 	else
  2465 		{
  2466 		test.Printf(_L("Passed...\n"));
  2467 		}
  2468 	
  2469 skipTest8:
  2470 	TestEnd();
  2471 
  2472 	test.End();
  2473 	return KErrNone;
  2474 	}
  2475 
  2476 	
  2477 //
  2478 // TestMovingPages
  2479 //
  2480 //---------------------------------------------------------------------------------------------------------------------
  2481 //! @SYMTestCaseID				KBASE-t_ramdefrag-0526
  2482 //! @SYMTestType				CIT
  2483 //! @SYMTestCaseDesc			Verifying that pages are moved correctly
  2484 //! @SYMPREQ					PREQ308
  2485 //! @SYMTestPriority			High
  2486 //! @SYMTestActions				
  2487 //! 	1.	Fragment the memory. Write to all chunks apart from chunk 9. 
  2488 //! 		Following this start a RAM defrag and whilst defrag is running write to chunk 9. 
  2489 //! 	2.	Fragment the memory. Start a RAM defrag and whilst defrag is running 
  2490 //! 		continuously write different values to the chunks. 
  2491 //! 	3.	Fragment the memory. Following this start a RAM defrag and whilst this 
  2492 //! 		is happening, continuously read from all chunks. 
  2493 //! 	4.	Allocate some movable pages. Call a device driver that will allocate fixed 
  2494 //! 		pages and write values to these fixed pages. 
  2495 //! 		Close every other chunk so that only 5 chunks are now still open. 
  2496 //! 		Following this perform a RAM defrag. 
  2497 //! 		Read from the fixed pages that were originally written to, ensuring 
  2498 //! 		that they have not changed.   
  2499 //! 	5.	Without starting any processes, allocate discardable pages by loading 
  2500 //! 		pages that are demand paged. 
  2501 //! 		Read each of the constants from beginning to end. 
  2502 //! 		Following this perform a RAM defrag. 
  2503 //! 
  2504 //! @SYMTestExpectedResults
  2505 //! 	1.	Zones are emptied
  2506 //! 	2.	Zones are emptied	
  2507 //! 	3.	Zones are emptied
  2508 //! 	4.	The values written to the fixed pages have not changed. 
  2509 //! 	5.	Zones are emptied
  2510 //---------------------------------------------------------------------------------------------------------------------
  2511 TInt TestMovingPages()
  2512 	{
  2513 	const TInt KAllChunks = -1;  // Specifies that all chunks should be written to
  2514 	
  2515 	test.Start(_L("Test1: Whilst moving page, change the usage "));	
  2516 	TestStart();	
  2517 	
  2518 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
  2519 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2520 
  2521 	// Find a chunk that exists
  2522 	TInt chunkIndex = gChunkArraySize1 - 1;
  2523 	for (; chunkIndex >= 0 && gChunkArray1[chunkIndex].Handle() == NULL; chunkIndex--);
  2524 	if (chunkIndex < 0)
  2525 		{
  2526 		test.Printf(_L("No chunks were allocated\n"));
  2527 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2528 		TEST_FAIL;
  2529 		}
  2530 
  2531 	WriteToChunk(gChunkArray1, gChunkArraySize1, chunkIndex);
  2532 
  2533 	TInt r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  2534 	if (r != KErrNone)
  2535 		{
  2536 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2537 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2538 		TEST_FAIL;
  2539 		}	
  2540 	gTestThread.Logon(status);
  2541 	gTestThread.Resume();	
  2542 
  2543 	TUint8* base = gChunkArray1[chunkIndex].Base();
  2544 	while (status.Int() == KRequestPending)
  2545 		{
  2546 		User::After(10000);
  2547 		for (TInt8 k = 0; k < 10; k ++)
  2548 			{
  2549 			if (base == gChunkArray1[chunkIndex].Base() + gChunkArray1[chunkIndex].Size())
  2550 				{
  2551 				base = gChunkArray1[chunkIndex].Base();
  2552 				}
  2553 			*base++ = k; // write 0 - 9 to the chunk
  2554 			}
  2555 		}
  2556 	
  2557 	User::WaitForRequest(status);
  2558 	r = status.Int();
  2559 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
  2560 
  2561 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2562 
  2563 	if (r == KErrNone)
  2564 		{
  2565 		test.Printf(_L("Correct return value\n"));
  2566 		test.Printf(_L("Passed...\n"));
  2567 		}
  2568 	else
  2569 		{
  2570 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  2571 		TEST_FAIL;	
  2572 		}
  2573 		
  2574 	gTestThread.Close();
  2575 	TestEnd();
  2576 	
  2577 	
  2578 	test.Next(_L("Test2: Whilst moving page, change the contents of the page"));	
  2579 	TestStart();	
  2580 		
  2581 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
  2582 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2583 	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
  2584 	
  2585 	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  2586 	if (r != KErrNone)
  2587 		{
  2588 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2589 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2590 		TEST_FAIL;
  2591 		}	
  2592 	gTestThread.Logon(status);
  2593 	gTestThread.Resume();
  2594 
  2595 	TUint8 startValue = 0;
  2596 	while (status.Int() == KRequestPending)
  2597 		{
  2598 		User::After(10000);
  2599 		WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks, startValue);
  2600 		if (++startValue > 245)
  2601 			{
  2602 			startValue = 0;
  2603 			}
  2604 		}
  2605 		
  2606 	User::WaitForRequest(status);
  2607 	r = status.Int();
  2608 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
  2609 	
  2610 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2611 
  2612 	if (r == KErrNone)
  2613 		{
  2614 		test.Printf(_L("Correct return value\n"));
  2615 		test.Printf(_L("Passed...\n"));	
  2616 		}
  2617 	else
  2618 		{
  2619 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  2620 		TEST_FAIL;	
  2621 		}
  2622 
  2623 	gTestThread.Close();
  2624 	TestEnd();
  2625 	
  2626 
  2627 	test.Next(_L("Test3: Whilst moving page, read pages"));	
  2628 	TestStart();	
  2629 	
  2630 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
  2631 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2632 	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
  2633 	
  2634 	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  2635 	if (r != KErrNone)
  2636 		{
  2637 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2638 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2639 		TEST_FAIL;
  2640 		}	
  2641 	gTestThread.Logon(status);
  2642 	gTestThread.Resume();
  2643 
  2644 	while (status.Int() == KRequestPending)
  2645 		{
  2646 		User::After(100000000);
  2647 		ReadChunk(gChunkArray1, gChunkArraySize1);
  2648 		}		
  2649 	
  2650 	User::WaitForRequest(status);
  2651 	r = status.Int();
  2652 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
  2653 
  2654 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2655 
  2656 	if (r == KErrNone)
  2657 		{
  2658 		test.Printf(_L("Correct return value\n"));
  2659 		test.Printf(_L("Passed...\n"));	
  2660 		}
  2661 	else
  2662 		{
  2663 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2664 		TEST_FAIL;	
  2665 		}
  2666 		
  2667 	gTestThread.Close();
  2668 	TestEnd();
  2669 
  2670 	test.Next(_L("Test4: Allocate fixed pages and then perform a defrag"));	
  2671 	TestStart();	
  2672 	
  2673 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  2674 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2675 	
  2676 	Ldd.AllocateFixedWrite(FILL_ALL_FIXED);
  2677 
  2678 	GetOriginalPageCount();
  2679 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2680 	test.Printf(_L("defrag returns %d\n"), r);
  2681 
  2682 	if (CheckZonesSwitchedOff() != EFalse)
  2683 		{
  2684 		test.Printf(_L("Fail: Zones were switched off when they shouldn't have been\n"));
  2685 		CLEANUP(r = Ldd.FreeAllFixedPagesRead());
  2686 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2687 		TEST_FAIL;
  2688 		}
  2689 	else
  2690 		{
  2691 		test.Printf(_L("Passed...\n"));	
  2692 		}
  2693 	r = Ldd.FreeAllFixedPagesRead();
  2694 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
  2695 	TestEnd();
  2696 	
  2697 	test.Next(_L("Test5: Allocate discardable pages and then perform a defrag"));	
  2698 	TestStart();	
  2699 	if (gPagedRom)
  2700 		{
  2701 		TInt discardablePages;
  2702 				
  2703 		UpdateRamInfo();
  2704 		r = AllocDiscardable(discardablePages);
  2705 		if (r != KErrNone)
  2706 			{
  2707 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2708 			TEST_FAIL;
  2709 			}
  2710 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
  2711 
  2712 		GetOriginalPageCount();
  2713 		TBool genSucceed = CanGenSucceed();
  2714 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2715 
  2716 		TESTDEBUG(test.Printf(_L("defrag returns %d\n"), r));
  2717 		
  2718 		if (genSucceed && CheckZonesSwitchedOff() == EFalse)
  2719 			{
  2720 			test.Printf(_L("Fail: Zones were not switched off when they should have been\n"));
  2721 			CLEANUP(ResetDPCache());
  2722 			TEST_FAIL;
  2723 			}
  2724 		else
  2725 			{
  2726 			test.Printf(_L("Passed...\n"));	
  2727 			}
  2728 		ResetDPCache();
  2729 		
  2730 		}
  2731 	else
  2732 		{
  2733 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  2734 		}
  2735 	TestEnd();
  2736 		
  2737 	test.End();
  2738 	return 0;
  2739 	}
  2740 		
  2741 
  2742 //
  2743 // TestMovPgsDefrag
  2744 //
  2745 //---------------------------------------------------------------------------------------------------------------------
  2746 //! @SYMTestCaseID				KBASE-t_ramdefrag-0527
  2747 //! @SYMTestType				CIT
  2748 //! @SYMTestCaseDesc			Verifying the moving of pages in the defrag implmentation
  2749 //! @SYMPREQ					PREQ308
  2750 //! @SYMTestPriority			High
  2751 //! @SYMTestActions				
  2752 //! 	1.	Fragment the memory. Following this perform a RAM defrag. 
  2753 //! 		Now that all movable memory has now been moved and the relevant source
  2754 //! 		zones have been switched off, again attempt to perform a RAM defrag. 
  2755 //! 	2.	Fragment the memory. Following this perform a RAM defrag and whilst 
  2756 //! 		this is happening, continuously create more chunks. 
  2757 //!		3.	Set up memory so that there are a mix of movable and free pages in a low preference zone, with there
  2758 //!			being movable pages in the higher preference zones. Following this call a general defrag
  2759 //!		4.	Set up memory so that there are a mix of movable and free pages and a couple of free pages 
  2760 //!			in a low preference zone, with there being enough free pages in the higher preference zones for the 
  2761 //!			pages to be moved to. Following this call a general defrag
  2762 //!		5.	Set up memory so that there is a fixed page and a movable page in an empty zone (testZone). Also ensure that 
  2763 //!			there are movable and free pages in more preferable zones than the test zone. Following this call a general
  2764 //!			defrag
  2765 //!		6.	Set up memory so that the mostPrefZone contains movable and free pages. Allocate 1 movable and 1 fixed
  2766 //!			page into an empty zone (testZone). Following this call a general defrag. 
  2767 //! 
  2768 //! @SYMTestExpectedResults
  2769 //! 	1.	Second defrag does not empty any zones
  2770 //! 	2.	No zones have been emptied
  2771 //!		3.	Zones are emptied if the general defrag can succeed
  2772 //! 	4.	Defrag fills up the less preferable zones first
  2773 //!		5.	movable pages are moved from the testZone
  2774 //!		6.	testZone is not emptied
  2775 //---------------------------------------------------------------------------------------------------------------------
  2776 TInt TestMovPgsDefrag()
  2777 	{
  2778 	TInt r = KErrNone;
  2779 
  2780 	test.Start(_L("Test1: Performing a defrag twice"));
  2781 	TestStart();
  2782 	
  2783 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  2784 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2785 	
  2786 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2787 	TESTDEBUG(test.Printf(_L("After calling general defrag r = %d\n"), r));
  2788 
  2789 	r = VerifyMovDisAlloc();	
  2790 	// The first defrag should empty zones if the general can succeed 
  2791 	if (r != KErrNone)
  2792 		{
  2793 		test.Printf(_L("Fail: r = %d, memory is not laid out as expected\n"), r);
  2794 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2795 		TEST_FAIL;
  2796 		}
  2797 
  2798 	GetOriginalPageCount();
  2799 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2800 	
  2801 	TESTDEBUG(test.Printf(_L("After calling general defrag again r = %d\n"), r));
  2802 	
  2803 	// The second call to general defrag should have nothing further to do
  2804 	if (r != KErrNone || CheckZonesSwitchedOff() != EFalse)
  2805 		{
  2806 		test.Printf(_L("Fail: r = %d, expected = %d, or zones have been switched off \n"), 
  2807 						r, KErrNone);
  2808 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2809 		TEST_FAIL;
  2810 		}
  2811 	else
  2812 		{
  2813 		test.Printf(_L("Passed...\n"));	
  2814 		}
  2815 	
  2816 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
  2817 	TestEnd();	
  2818 	
  2819 	
  2820 	
  2821 	test.Next(_L("Test2: Ensure new memory allocations can occur successfully during a general defrag"));
  2822 	TestStart();
  2823 	
  2824 	// Create a thread to call Defrag continuously and resume it
  2825 	r = gTestThread.Create(gTestThreadName,MultiLoopGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  2826 	if (r != KErrNone)
  2827 		{
  2828 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  2829 		TEST_FAIL;
  2830 		}		
  2831 	gTestThread.Logon(status);
  2832 	gTestLoop = ETrue;
  2833 	gTestThread.Resume();
  2834 	
  2835 	// Whilst the defrag loop is taking place, continuously allocate and free memory
  2836 	for (TInt i = 0; i < 100; i++)
  2837 		{
  2838 		r = AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  2839 		if (r != KErrNone)
  2840 			break;
  2841 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2842 		}
  2843 	gTestLoop = EFalse;
  2844 	User::WaitForRequest(status);
  2845 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
  2846 		
  2847 	if (r != KErrNone || status.Int() != KErrNone)
  2848 		{
  2849 		test.Printf(_L("Fail: r = %d, status.Int() = %d\n"), r, status.Int());	
  2850 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2851 		TEST_FAIL;
  2852 		}
  2853 	else
  2854 		{
  2855 		test.Printf(_L("Passed...\n"));	
  2856 		}
  2857 
  2858 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2859 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
  2860 	gTestThread.Close();	
  2861 	TestEnd();
  2862 	
  2863 	
  2864 	test.Next(_L("Test3: Check whether RAM defrag switches off any zones"));
  2865 	TestStart();
  2866 	
  2867 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  2868 	FreeMovable(gChunkArray1, gChunkArraySize1);
  2869 
  2870 	GetOriginalPageCount();
  2871 	TBool genSucceed = CanGenSucceed();
  2872 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2873 	
  2874 	TESTDEBUG(test.Printf(_L("After calling zone defrag r = %d\n"), r));
  2875 	
  2876 	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
  2877 		{
  2878 		test.Printf(_L("Fail: No zones were switched off or r = %d, expected = %d\n"), r, KErrNone);
  2879 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  2880 		TEST_FAIL;
  2881 		}
  2882 	else
  2883 		{
  2884 		test.Printf(_L("Passed...\n"));	
  2885 		}
  2886 
  2887 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  2888 	TestEnd();
  2889 
  2890 
  2891 	test.Next(_L("Test4: Verify that a general defrag moves pages to the least preferable RAM zone in use"));
  2892 	TestStart();
  2893 	gChunkArray1 = new RChunk;
  2894 	gChunkArraySize1 = 1;
  2895 	// Get the preference order
  2896 	GetPrefOrder();
  2897 
  2898 	TUint mostPrefIndex = 0;
  2899 	TUint mostPrefArrayIndex = 0;
  2900 	TUint totMov = 0;
  2901 	TUint lastPrefIndex = 0;
  2902 	TUint lastPrefArrayIndex = 0;
  2903 	TUint movPages = 0;
  2904 	TUint movBytes = 0;
  2905 	TUint totFreeInTestZones = 0;
  2906 	TBool zoneNotEmptyOrFull = EFalse;
  2907 
  2908 
  2909 	// Find the first most pref zone that has free pages in it
  2910 	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
  2911 	if (r != KErrNone)
  2912 		{
  2913 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  2914 		goto skipTest4;
  2915 		}
  2916 
  2917 	// Find the next most preferable free zone
  2918 	lastPrefArrayIndex = mostPrefArrayIndex + 1;
  2919 
  2920 	while (lastPrefArrayIndex < gZoneCount &&
  2921 		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
  2922 		{
  2923 		lastPrefArrayIndex++;
  2924 		}
  2925 
  2926 	if (lastPrefArrayIndex >= gZoneCount)
  2927 		{
  2928 		test.Printf(_L("Skipping...\n"));
  2929 		goto skipTest4;
  2930 		}
  2931 
  2932 	// Block all other zones
  2933 	for (TUint prefIndex = lastPrefArrayIndex + 1; prefIndex < gZoneCount; prefIndex++)
  2934 		{
  2935 		TUint zoneIndex = gPrefArray[prefIndex];
  2936 		Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
  2937 		}
  2938 
  2939 
  2940 	// Zone alloc to fill half of the last zone under test
  2941 	GetAllPageInfo();
  2942 	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
  2943 	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, gZoneUtilArray[lastPrefIndex].iFreePages >> 1);
  2944 	if (r != KErrNone)
  2945 		{
  2946 		test.Printf(_L("Failed to allocate fixed pages r = %d - Skipping...\n"), r);
  2947 		goto skipTest4;
  2948 		}
  2949 
  2950 
  2951 	// Go through the zones under test and determine the number of free pages
  2952 	GetAllPageInfo();
  2953 	for(TUint prefIndex = mostPrefArrayIndex; prefIndex <= lastPrefArrayIndex; prefIndex++)
  2954 		{
  2955 		TUint zoneIndex = gPrefArray[prefIndex];
  2956 		totFreeInTestZones += gZoneUtilArray[zoneIndex].iFreePages;
  2957 		}
  2958 
  2959 	// Allocate movable pages to almost fill zones under test
  2960 	movPages = totFreeInTestZones;
  2961 	movBytes = movPages << gPageShift;
  2962 	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
  2963 		{
  2964 		movBytes -= gPageSize;
  2965 		movPages--;
  2966 		}
  2967 	if (!movBytes)
  2968 		{
  2969 		test.Printf(_L("Failed to allocate 0x%x movable pages - Skipping...\n"), movPages);
  2970 		goto skipTest4;
  2971 		}
  2972 
  2973 
  2974 	// Free the allocated fixed pages
  2975 	Ldd.FreeAllFixedPages();
  2976 
  2977 	// Reset all the flags
  2978 	ResetRamZoneFlags();
  2979 	
  2980 	// Check that the most preferable zone still has movable pages in it
  2981 	// and also check that the last zone under test has free pages in it
  2982 	GetAllPageInfo();
  2983 	if (gTotalPageCount.iMovablePages < movPages ||
  2984 		gZoneUtilArray[mostPrefIndex].iAllocMovable == 0 ||
  2985 		gZoneUtilArray[lastPrefIndex].iFreePages == 0)
  2986 		{
  2987 		test.Printf(_L("Setup failed - Skipping...\n"));
  2988 		goto skipTest4;
  2989 		}
  2990 
  2991 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  2992 	// Check that defrag allocated the movable from the less preferable to the more preferable
  2993 	GetPrefOrder();
  2994 	totMov = gTotalPageCount.iMovablePages;
  2995 	for(TInt prefIndex = gZoneCount - 1; prefIndex >= 0 ; prefIndex--)
  2996 		{
  2997 		TUint zoneIndex = gPrefArray[prefIndex];
  2998 		GetCandList1(zoneIndex);
  2999 		TUint candIndex = 0;
  3000 		for (; candIndex < gZoneCount; candIndex++)
  3001 			{// Check all the zones of this preference
  3002 			TInt zoneIndexCand = gCandList1[candIndex];
  3003 			if (zoneIndexCand == KInvalidCandIndex)
  3004 				{
  3005 				break;
  3006 				}
  3007 			totMov -= gZoneUtilArray[zoneIndexCand].iAllocMovable;
  3008 			if (gZoneUtilArray[zoneIndexCand].iFreePages != gZoneUtilArray[zoneIndexCand].iPhysPages &&
  3009 				gZoneUtilArray[zoneIndexCand].iFreePages != 0)
  3010 				{
  3011 				zoneNotEmptyOrFull = ETrue;
  3012 				}
  3013 			}
  3014 		prefIndex -= candIndex - 1 ;
  3015 
  3016 		if (zoneNotEmptyOrFull && totMov != 0)
  3017 			{
  3018 			test.Printf(_L("FAIL: index = %d free = 0x%x totMov = 0x%x\n"), 
  3019 							zoneIndex, gZoneUtilArray[zoneIndex].iFreePages, totMov);
  3020 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3021 			CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  3022 			TEST_FAIL;
  3023 			}
  3024 		}
  3025 
  3026 skipTest4:
  3027 	TestEnd();
  3028 
  3029 
  3030 	test.Next(_L("Test5: Verify that a general defrag tidies up if RAM zone to be emptied contains fixed pages"));
  3031 	TestStart();
  3032 	gChunkArray1 = new RChunk;
  3033 	gChunkArraySize1 = 1;
  3034 	gChunkArray2 = new RChunk;
  3035 	gChunkArraySize2 = 1;
  3036 
  3037 	// find 2 empty zones - put 1 movable in 1 of them and 1 movable and 1 fixed in the other
  3038 	TUint testZoneIndex1;
  3039 	TUint testZoneIndex2;
  3040 	TUint testZoneID2;
  3041 	TUint numFreeInUse = 0;
  3042 
  3043 	r = FindMostPrefEmpty(testZoneIndex1);
  3044 	if (r != KErrNone)
  3045 		{
  3046 		test.Printf(_L("Skipping...\n"));
  3047 		goto skipTest5;
  3048 		}
  3049 	// Allocate 1 movable page to the test zone
  3050 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex1, 1);
  3051 	if (r != KErrNone)
  3052 		{
  3053 		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex1, r);
  3054 		goto skipTest5;
  3055 		}
  3056 	
  3057 	r = FindMostPrefEmpty(testZoneIndex2);
  3058 	if (r != KErrNone)
  3059 		{
  3060 		test.Printf(_L("Skipping...\n"));
  3061 		goto skipTest5;
  3062 		}
  3063 	// Allocate 1 movable page to the test zone
  3064 	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, testZoneIndex2, 1);
  3065 	if (r != KErrNone)
  3066 		{
  3067 		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex2, r);
  3068 		goto skipTest5;
  3069 		}
  3070 
  3071 	// Zone alloc to put 1 fixed page last zone under test
  3072 	testZoneID2 = gZoneConfigArray[testZoneIndex2].iZoneId;
  3073 	r = Ldd.ZoneAllocContiguous(testZoneID2, gPageSize);
  3074 	if (r != KErrNone)
  3075 		{
  3076 		test.Printf(_L("Failed to allocate 1 fixed page to zone %d index, r = %d - Skipping...\n"), testZoneIndex2, r);
  3077 		goto skipTest5;
  3078 		}
  3079 	
  3080 	// Allocate 2 fixed (as there is 1 in our test zone) to prevent reordering
  3081 	GetAllPageInfo();
  3082 	for (TUint index = 0; index < gZoneCount; index++)
  3083 		{
  3084 		TUint totImmovable = gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ;
  3085 		if (index != testZoneIndex1 &&
  3086 			index != testZoneIndex2 &&
  3087 			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
  3088 			totImmovable < 2)
  3089 			{
  3090 			r = Ldd.ZoneAllocToMany(index, 2 - totImmovable);
  3091 			if (r != KErrNone)
  3092 				{
  3093 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Skipping...\n"), 
  3094 								2, index, r);
  3095 				goto skipTest5; 
  3096 				}
  3097 			}
  3098 		}
  3099 
  3100 	// Check that the number of free pages in the other in-use zones >= movable pages in test zone
  3101 	GetAllPageInfo();
  3102 	for (TUint index = 0; index < gZoneCount; index++)
  3103 		{
  3104 		if (index != testZoneIndex1 &&
  3105 			index != testZoneIndex2 &&
  3106 			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
  3107 			{
  3108 			numFreeInUse += gZoneUtilArray[index].iFreePages;
  3109 			}
  3110 		}
  3111 	if (gZoneUtilArray[testZoneIndex1].iAllocMovable + gZoneUtilArray[testZoneIndex1].iAllocDiscardable + 
  3112 		gZoneUtilArray[testZoneIndex2].iAllocMovable + gZoneUtilArray[testZoneIndex2].iAllocDiscardable > numFreeInUse ||
  3113 		gZoneUtilArray[testZoneIndex2].iAllocFixed == 0)
  3114 		{
  3115 		test.Printf(_L("Setup failed - Skipping...\n"));
  3116 		goto skipTest5;
  3117 		}
  3118 
  3119 	// Added extra tracing to debug random failures
  3120 	PrintPageInfo();
  3121 
  3122 	// Call a general defrag
  3123 	GetOriginalPageCount();
  3124 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3125 
  3126 	// Check that the defrag tidies up
  3127 	GetAllPageInfo();
  3128 	if(gZoneUtilArray[testZoneIndex1].iAllocMovable ||
  3129 		gZoneUtilArray[testZoneIndex2].iAllocMovable)
  3130 		{
  3131 		test.Printf(_L("FAIL: testZoneIndex1(%d): mov orig 0x%x cur 0x%x, testZoneIndex2(%d) mov orig 0x%x cur 0x%x \n"), 
  3132 						testZoneIndex1, gOriginalPageCountArray[testZoneIndex1].iAllocMovable, gZoneUtilArray[testZoneIndex1].iAllocMovable, 
  3133 						testZoneIndex2, gOriginalPageCountArray[testZoneIndex2].iAllocMovable, gZoneUtilArray[testZoneIndex2].iAllocMovable);
  3134 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3135 		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  3136 		CLEANUP(Ldd.FreeAllFixedPages());
  3137 		TEST_FAIL;
  3138 		}
  3139 
  3140 skipTest5:
  3141 	TestEnd();
  3142 
  3143 
  3144 	test.Next(_L("Test6: Verify that a general defrag will not move pages if RAM zone to be emptied contains fixed pages"));
  3145 	TestStart();
  3146 	gChunkArray1 = new RChunk;
  3147 	gChunkArraySize1 = 1;
  3148 	// Find the first most pref zone that has free pages in it
  3149 	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
  3150 	if (r != KErrNone)
  3151 		{
  3152 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
  3153 		goto skipTest6;
  3154 		}
  3155 
  3156 	// Find the next most preferable free zone
  3157 	lastPrefArrayIndex = mostPrefArrayIndex + 1;
  3158 	while (lastPrefArrayIndex < gZoneCount &&
  3159 		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
  3160 		{
  3161 		lastPrefArrayIndex++;
  3162 		}
  3163 	if (lastPrefArrayIndex >= gZoneCount)
  3164 		{
  3165 		test.Printf(_L("Skipping...\n"));
  3166 		goto skipTest6;
  3167 		}
  3168 
  3169 
  3170 	// Zone alloc to put 1 fixed page last zone under test
  3171 	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
  3172 	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, 1);
  3173 	if (r != KErrNone)
  3174 		{
  3175 		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d - Skipping...\n"), lastPrefIndex, r);
  3176 		goto skipTest6;
  3177 		}
  3178 	
  3179 	GetAllPageInfo();
  3180 	if (!gZoneUtilArray[mostPrefIndex].iFreePages)
  3181 		{
  3182 		test.Printf(_L("Not enough space in zone under test  mostPrefIndex = %d - Skipping...\n"), mostPrefIndex);
  3183 		goto skipTest6;
  3184 		}
  3185 
  3186 	// Block all zones apart from the last zone under test from allocation
  3187 	for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
  3188 		{
  3189 		TUint zoneIndex = gPrefArray[prefIndex];
  3190 		if (zoneIndex != lastPrefIndex)
  3191 			{
  3192 			Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
  3193 			}
  3194 		}
  3195 	
  3196 	TESTDEBUG(test.Printf(_L("mostPrefIndex = %d lastPrefIndex = %d\n"), mostPrefIndex, lastPrefIndex));
  3197 	
  3198 	// Allocate movable pages to the lastPrefZone that will fit into the most pref zone
  3199 	movPages = gZoneUtilArray[mostPrefIndex].iFreePages;
  3200 	movBytes = movPages << gPageShift;
  3201 	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
  3202 		{
  3203 		movBytes -= gPageSize;
  3204 		}
  3205 	if (!movBytes)
  3206 		{
  3207 		test.Printf(_L("Failed to allocate 0x%x movable pages r = %d - Skipping...\n"), movPages, r);
  3208 		goto skipTest6;
  3209 		}
  3210 
  3211 	// Reset all the flags
  3212 	ResetRamZoneFlags();
  3213 	
  3214 
  3215 	// Check that the number of movable pages in the least pref will fit
  3216 	// into the most preferable zone
  3217 	GetAllPageInfo();
  3218 	if (gZoneUtilArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[mostPrefIndex].iFreePages)
  3219 		{
  3220 		test.Printf(_L("Setup failed - Skipping...\n"));
  3221 		PrintPageInfo();
  3222 		goto skipTest6;
  3223 		}
  3224 	
  3225 	// Added extra tracing to debug random failures
  3226 	PrintPageInfo();	
  3227 
  3228 	// Call a general defrag
  3229 	GetOriginalPageCount();
  3230 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3231 
  3232 	// Check that the defrag didn't move any pages from the least pref zone
  3233 	GetAllPageInfo();
  3234 	if(gOriginalPageCountArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[lastPrefIndex].iAllocMovable)
  3235 		{
  3236 		test.Printf(_L("FAIL: mostPref(index %d): origMov = 0x%x curMov = 0x%x, lastPref(index %d): origMov = 0x%x curMov = 0x%x \n"), 
  3237 						mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
  3238 						lastPrefIndex, 	gOriginalPageCountArray[lastPrefIndex].iAllocMovable, gZoneUtilArray[lastPrefIndex].iAllocMovable);
  3239 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3240 		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  3241 		CLEANUP(Ldd.FreeAllFixedPages());
  3242 		TEST_FAIL;
  3243 		}
  3244 
  3245 skipTest6:
  3246 	TestEnd();
  3247 
  3248 	test.End();
  3249 	return KErrNone;
  3250 	}
  3251 
  3252 //
  3253 // GenDefragTest123Setup
  3254 //
  3255 // Used to set up the memory ready for testing in TestGenDefrag, Test Steps 1, 2, 3 and 4
  3256 //
  3257 // @param aMovAlloc The number of movable pages to allocate.
  3258 // @param aFixAlloc The number of fixed pages to allocate.
  3259 // @param aDisAlloc The number of discardable pages to allocate.
  3260 // @param aMovIndex On return contains the test zone index.
  3261 // @param aNumFreeInUse On return contains the number of free pages in the inuse zones.
  3262 // @param aTestNo The test number that is using this setup.
  3263 //
  3264 TInt GenDefragTest123Setup(TUint aMovAlloc, TUint aFixAlloc, TUint aDisAlloc, TUint& aMovIndex, TUint& aNumFreeInUse, TUint aTestNo)
  3265 	{
  3266 	TInt r = KErrNone;
  3267 	aNumFreeInUse = 0;
  3268 	const TUint KFillUpTo = 5;	// Rough estimate of how full the in-use zones should be
  3269 	const TUint KSpaceNeeded = 2;
  3270 	TUint movPrefIndex = 0;
  3271 	if (aTestNo == 4)
  3272 		{// Test 4 we need to look for the LEAST preferable RAM zone which is empty
  3273 		TUint mostPrefIndex;
  3274 		r = FindLeastPrefEmpty(aMovIndex, &movPrefIndex);
  3275 		TInt r2 = FindMostPrefEmpty(mostPrefIndex);
  3276 		if (r != KErrNone || r2 != KErrNone || !gPagedRom ||
  3277 			gZoneConfigArray[aMovIndex].iPref == gZoneConfigArray[mostPrefIndex].iPref)
  3278 			{
  3279 			test.Printf(_L("r %d r2 %d or not a paged ROM or Equal zone preferences- Skipping test step...\n"), 
  3280 							r, r2);
  3281 			return KErrGeneral;
  3282 			}
  3283 		}
  3284 	else
  3285 		{// All other tests look the MOST preferable RAM zone which is empty
  3286 		r = FindMostPrefEmpty(aMovIndex, &movPrefIndex);
  3287 		if (r != KErrNone || !gPagedRom)
  3288 			{
  3289 			test.Printf(_L("No suitable RAM zone found or not a paged ROM - Skipping test step...\n"));
  3290 			return KErrGeneral;
  3291 			}
  3292 		}
  3293 
  3294 	// Go through all the in-use zones and check how many free pages there are
  3295 	// Allocate fixed so there is only KFillUpTo pages in each in-use zone to
  3296 	// ensure that the total number of free pages in the in-use zones can fit into
  3297 	// the zone under test
  3298 	GetAllPageInfo();
  3299 	for (TUint index = 0; index < gZoneCount; index++)
  3300 		{
  3301 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
  3302 			gZoneUtilArray[index].iFreePages > KFillUpTo)
  3303 			{
  3304 			r = Ldd.ZoneAllocToMany(index, gZoneUtilArray[index].iFreePages - KFillUpTo);
  3305 			if (r != KErrNone)
  3306 				{
  3307 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
  3308 								gZoneUtilArray[index].iFreePages - KFillUpTo, index, r);
  3309 				return KErrGeneral; 
  3310 				}
  3311 			}
  3312 		}
  3313 
  3314 	GetAllPageInfo();
  3315 	// If any of the in-use zones doesn't contain any fixed and unknown pages allocate one fixed
  3316 	for (TUint index = 0; index < gZoneCount; index++)
  3317 		{
  3318 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
  3319 			!gZoneUtilArray[index].iAllocFixed && !gZoneUtilArray[index].iAllocUnknown)
  3320 			{
  3321 			r = Ldd.ZoneAllocToMany(index, 1);
  3322 			if (r != KErrNone)
  3323 				{
  3324 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
  3325 								1, index, r);
  3326 				return KErrGeneral;
  3327 				}
  3328 			}
  3329 		}
  3330 
  3331 	GetAllPageInfo();
  3332 	// Check the total number of free pages in the in-use zones
  3333 	for (TUint index = 0; index < gZoneCount; index++)
  3334 		{
  3335 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
  3336 			{
  3337 			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
  3338 			}
  3339 		}
  3340 	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
  3341 
  3342 	test.Printf(_L("aMovIndex = %d\n"), aMovIndex);
  3343 	if (aNumFreeInUse < KSpaceNeeded)
  3344 		{
  3345 		test.Printf(_L("RAM zone to be tested is too full - Skipping test step...\n"));
  3346 		return KErrGeneral;
  3347 		}
  3348 
  3349 
  3350 	// Ensure that there are movable pages in the zone under test
  3351 	// ensuring that there is also room for discardable pages
  3352 	GetAllPageInfo();
  3353 	if (aMovAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocMovable == 0)
  3354 		{
  3355 		// Allocate just aMovAlloc movable pages
  3356 		TUint movPagesAlloc = aMovAlloc;
  3357 		if (aTestNo == 2 || aTestNo == 4)
  3358 			{// Tests 2 and 4 require that movable pages can't be moved from zone
  3359 			movPagesAlloc = aNumFreeInUse + 1;
  3360 			if (movPagesAlloc > gZoneUtilArray[aMovIndex].iFreePages)
  3361 				{
  3362 				test.Printf(_L("Insufficiant space in zone %d to allocate 0x%x pages - Ending setup...\n"), 
  3363 								aMovIndex, movPagesAlloc);
  3364 				return KErrGeneral;
  3365 				}
  3366 			}
  3367 
  3368 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aMovIndex, movPagesAlloc);
  3369 		if (gZoneUtilArray[aMovIndex].iAllocMovable != movPagesAlloc ||
  3370 			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
  3371 			r != KErrNone)
  3372 			{// Movable page didn't go into RAM, or RAM zone is full.
  3373 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
  3374 			test.Printf(_L("zone mov 0x%x free 0x%x r=%d\n"), 
  3375 						gZoneUtilArray[aMovIndex].iAllocMovable,	
  3376 						gZoneUtilArray[aMovIndex].iFreePages, r);
  3377 			return KErrGeneral;
  3378 			}
  3379 		}
  3380 
  3381 	// Block all other zones from allocation
  3382 	for(TUint i = 0; i < gZoneCount; i++)
  3383 		{
  3384 		if (i != aMovIndex)
  3385 			{
  3386 			r = Ldd.SetZoneFlag(gZoneConfigArray[i].iZoneId, gZoneConfigArray[i].iFlags, NO_ALLOC_FLAG);
  3387 			if (r != KErrNone)
  3388 				{
  3389 				test.Printf(_L("Failed to set flag on zone index %d r = %d- Ending setup...\n"), i, r);
  3390 				return KErrGeneral;
  3391 				}
  3392 			}
  3393 		}
  3394 
  3395 	// Now ensure that there are fixed pages in the zone under test
  3396 	// ensuring that there is also room for discardable pages
  3397 	GetAllPageInfo();
  3398 	if (aFixAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocFixed == 0)
  3399 		{//Allocate just aFixAlloc fixed pages 
  3400 		r = Ldd.ZoneAllocToMany(aMovIndex, aFixAlloc);
  3401 		GetAllPageInfo();
  3402 		if (gZoneUtilArray[aMovIndex].iAllocMovable == 0 ||
  3403 			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
  3404 			gZoneUtilArray[aMovIndex].iAllocFixed == 0 ||
  3405 			r != KErrNone)
  3406 			{// Fixed page didn't go into RAM or RAM zone is full.
  3407 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
  3408 			test.Printf(_L("zone mov 0x%x fixed 0x%x free 0x%x r=%d\n"), 
  3409 						gZoneUtilArray[aMovIndex].iAllocMovable,	
  3410 						gZoneUtilArray[aMovIndex].iAllocFixed,	
  3411 						gZoneUtilArray[aMovIndex].iFreePages, r);
  3412 			return KErrGeneral;
  3413 			}
  3414 		}
  3415 
  3416 
  3417 	
  3418 	// Allocate aDisAlloc number of discardable pages
  3419 	if (aDisAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
  3420 		{//Allocate just aDisAlloc discardable pages 
  3421 		TInt discard;
  3422 		r = ZoneAllocDiscard(aMovIndex, aDisAlloc, discard);
  3423 		GetAllPageInfo();
  3424 		if (r != KErrNone || gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
  3425 			{// Discardable page didn't go into RAM or RAM zone is full.
  3426 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
  3427 			test.Printf(_L("zone mov 0x%x discardable 0x%x free 0x%x r=%d\n"), 
  3428 						gZoneUtilArray[aMovIndex].iAllocDiscardable,	
  3429 						gZoneUtilArray[aMovIndex].iAllocFixed,	
  3430 						gZoneUtilArray[aMovIndex].iFreePages, r);
  3431 			return KErrGeneral;
  3432 			}
  3433 		}
  3434 	
  3435 	// Update the total number of free pages in the in-use zones
  3436 	aNumFreeInUse = 0;
  3437 	for (TUint index = 0; index < gZoneCount; index++)
  3438 		{
  3439 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
  3440 			index != aMovIndex)
  3441 			{
  3442 			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
  3443 			}
  3444 		}
  3445 	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
  3446 
  3447 	// now reset all the flags 
  3448 	ResetRamZoneFlags();
  3449 	
  3450 	return KErrNone;
  3451 	}
  3452 //
  3453 // TestGenDefrag
  3454 //
  3455 //---------------------------------------------------------------------------------------------------------------------
  3456 //! @SYMTestCaseID				KBASE-t_ramdefrag-0528
  3457 //! @SYMTestType				CIT
  3458 //! @SYMTestCaseDesc			Verifying general scenarios when RAM defrag would take place
  3459 //! @SYMPREQ					PREQ308
  3460 //! @SYMTestPriority			High
  3461 //! @SYMTestActions				
  3462 //! 	1.	Allocate 1 movable, 1 fixed and 1 discardable pages into the most preferable empty RAM zone
  3463 //!			ensuring that there is enough free space in the more preferable RAM zones for the
  3464 //!			movable pages in the RAM zone under test. Following this call a general defrag.
  3465 //!		2.	Allocate 1 movable and 1 discardable page into into the most preferable empty RAM zone
  3466 //!			ensuring that there is not enough free space in the more preferable RAM zones for the
  3467 //!			movable pages in the RAM zone under test. Following this call a general defrag. 
  3468 //!		3.	Allocate 1 movable and 1 discardable and 1 fixed page into the most preferable empty RAM zone(a). 
  3469 //!			Following this, place 1 movable page in an empty zone(b) ensuring that there is enough free space in 
  3470 //!			the more preferable RAM zones for the movable pages in both zones (a) and (b). 
  3471 //!			Following this call a general defrag. 
  3472 //!		4.	Allocate 1 movable and 1 discardable page into the least preferable empty RAM zone
  3473 //!			ensuring that there is not enough free space in the more preferable RAM zones for the
  3474 //!			movable pages in the RAM zone under test. Following this call a general defrag. 
  3475 //! 	5.	Fragment the memory. 
  3476 //! 		Following this allocate discardable pages by loading pages that are demand paged. 
  3477 //! 		Read each of the constants from beginning to end. 
  3478 //! 		Following this perform a RAM defrag. 
  3479 //! 	6.	Call a device driver that will continuously allocate fixed pages to the
  3480 //! 		memory until it reports out of memory. Following this perform a RAM defrag. 
  3481 //!		7.	Defrag memory filled with discardable pages when the min cache size is reached
  3482 //!		8.	Fragment the memory. Following this, continuously call a general defrag, each time 
  3483 //!			reducing the size of the chunks that are allocated  
  3484 //! 
  3485 //! @SYMTestExpectedResults
  3486 //! 	1.	The RAM zone under test is not emptied and all the discardable pages contained in it
  3487 //!			have not been discarded.
  3488 //! 	2.	The RAM zone under test is not emptied and all the discardable pages contained in it
  3489 //!			have not been discarded.
  3490 //! 	3.	RAM zones (a) and (b) have been emptied and all the discardable pages contained in them
  3491 //!			have not been discarded.
  3492 //!		4.	The RAM zone under test is emptied
  3493 //! 	5.	Pages are discarded
  3494 //! 	6.	No zones are emptied
  3495 //!		7.	The least preferable zone is skipped but the other zones are defragmented as required.
  3496 //! 	8.	Zones are emptied if the general defrag can succeed
  3497 //---------------------------------------------------------------------------------------------------------------------
  3498 TInt TestGenDefrag()
  3499 	{
  3500 	TInt r = KErrNone;
  3501 	
  3502 	test.Start(_L("Test1: Test General Defrag doesn't discard pages when fixed page in zone to be emptied"));	
  3503 	TestStart();
  3504 	gChunkArray1 = new RChunk;
  3505 	gChunkArraySize1 = 1;
  3506 	TUint numFreeInUse = 0;
  3507 	TUint testNo = 1;
  3508 
  3509 	// We just need 1 movable, 1 discardable and 1 fixed in the zone to ensure it cannot be emptied
  3510 	// as the fixed should block the defrag
  3511 	TInt movAllocPages = 1;
  3512 	TInt fixAllocPages = 1;
  3513 	TInt disAllocPages = 1;
  3514 	TUint zoneIndex;
  3515 
  3516 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
  3517 		{
  3518 		test.Printf(_L("Setup failed - Skipping..\n"));
  3519 		goto skipTest1;
  3520 		}
  3521 
  3522 	// Call a general defrag and check that no discardable pages were freed.
  3523 	GetOriginalPageCount();
  3524 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3525 	GetAllPageInfo();
  3526 	if (r != KErrNone || 
  3527 		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
  3528 		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
  3529 		{
  3530 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
  3531 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
  3532 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
  3533 		CLEANUP(Ldd.FreeAllFixedPages());
  3534 		TEST_FAIL;
  3535 		}
  3536 	else
  3537 		{
  3538 		test.Printf(_L("Passed...\n"));	
  3539 		}
  3540 
  3541 skipTest1:
  3542 	// TestEnd() will perform any required clean up.
  3543 	TestEnd();
  3544 
  3545 	test.Next(_L("Test2: Test General Defrag doesn't discard pages when zone can't be emptied"));	
  3546 	TestStart();	
  3547 	gChunkArray1 = new RChunk;
  3548 	gChunkArraySize1 = 1;
  3549 	testNo++;
  3550 	// No fixed, but there should be no space for the movable to go to
  3551 	// GenDefragTest123Setup(), will ensure that this is the case
  3552 	// by allocating 1 more movable page than there is free in the in-use zones
  3553 	movAllocPages = 1;
  3554 	fixAllocPages = 0;
  3555 	disAllocPages = 1;
  3556 
  3557 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
  3558 		{
  3559 		test.Printf(_L("Setup failed - Skipping..\n"));
  3560 		goto skipTest2;
  3561 		}
  3562 
  3563 	// Call a general defrag and check that no discardable pages were freed.
  3564 	GetOriginalPageCount();
  3565 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3566 	
  3567 	GetAllPageInfo();
  3568 	if (r != KErrNone || 
  3569 		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
  3570 		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
  3571 		{
  3572 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
  3573 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
  3574 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
  3575 		CLEANUP(Ldd.FreeAllFixedPages());
  3576 		TEST_FAIL;
  3577 		}
  3578 	else
  3579 		{
  3580 		test.Printf(_L("Passed...\n"));	
  3581 		}
  3582 
  3583 skipTest2:
  3584 	// TestEnd() will perform any required clean up.
  3585 	TestEnd();
  3586 
  3587 	test.Next(_L("Test3: Test General Defrag tidies when fixed page in zone to be emptied but other can be emptied"));	
  3588 	TestStart();
  3589 	gChunkArray1 = new RChunk;
  3590 	gChunkArraySize1 = 1;
  3591 	gChunkArray2 = new RChunk;
  3592 	gChunkArraySize2 = 1;
  3593 	TUint totalToMove;
  3594 	testNo++;
  3595 
  3596 	// First use GenDefragTest123Setup() to ensure we place 1 fixed, 1 movable and 1 discardable
  3597 	// in the least preferable zone
  3598 	movAllocPages = 1;
  3599 	fixAllocPages = 1;
  3600 	disAllocPages = 1;
  3601 	TBool zoneFound = EFalse;	
  3602 	TUint emptyZoneIndex = 0;
  3603 
  3604 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
  3605 		{
  3606 		test.Printf(_L("Setup failed - Skipping..\n"));
  3607 		goto skipTest3;
  3608 		}
  3609 
  3610 	// Find an empty zone and place a movable page in it
  3611 	GetAllPageInfo();
  3612 	for (TUint i = 0; i < gZoneCount; i++)
  3613 		{
  3614 		if (gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
  3615 			{
  3616 			zoneFound = ETrue;
  3617 			emptyZoneIndex = i;
  3618 			break;
  3619 			}
  3620 		}
  3621 	if (!zoneFound)
  3622 		{
  3623 		test.Printf(_L("Can't find empty zone - Skipping..\n"));
  3624 		goto skipTest3;
  3625 		}
  3626 
  3627 	// Allocate 1 movable page into the empty zone
  3628 	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, emptyZoneIndex, 1);
  3629 	GetAllPageInfo();
  3630 	if (r != KErrNone ||
  3631 		gZoneUtilArray[emptyZoneIndex].iAllocMovable == 0 ||
  3632 		gZoneUtilArray[emptyZoneIndex].iAllocFixed != 0)
  3633 		{
  3634 		test.Printf(_L("Movable pages not allocated or fixed pages allocated. - Skipping...\n"));
  3635 		test.Printf(_L("zone mov 0x%x free 0x%x r =% d\n"), 
  3636 					gZoneUtilArray[emptyZoneIndex].iAllocMovable,	
  3637 					gZoneUtilArray[emptyZoneIndex].iFreePages, r);
  3638 		goto skipTest3;
  3639 		}
  3640 
  3641 	// Check that the amount we are allocating can actually be moved into the in-use zones
  3642 	totalToMove = gZoneUtilArray[zoneIndex].iAllocMovable + gZoneUtilArray[zoneIndex].iAllocDiscardable + 
  3643 					gZoneUtilArray[emptyZoneIndex].iAllocMovable + gZoneUtilArray[emptyZoneIndex].iAllocDiscardable;
  3644 	
  3645 	// Check the total number of free pages in the in-use zones
  3646 	numFreeInUse = 0;
  3647 	for (TUint index = 0; index < gZoneCount; index++)
  3648 		{
  3649 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
  3650 			index != zoneIndex && index != emptyZoneIndex)
  3651 			{
  3652 			numFreeInUse += gZoneUtilArray[index].iFreePages;
  3653 			}
  3654 		}
  3655 	if(numFreeInUse < totalToMove)
  3656 		{
  3657 		test.Printf(_L("No space to move pages numFreeInUse = 0x%x totalToMove = 0x%x - Skipping..\n"), 
  3658 						numFreeInUse, totalToMove);
  3659 		goto skipTest3;
  3660 		}
  3661 
  3662 	// Call a general defrag and check that zone is emptied.
  3663 	GetOriginalPageCount();
  3664 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3665 	
  3666 	GetAllPageInfo();
  3667 	if (r != KErrNone || 
  3668 		gZoneUtilArray[zoneIndex].iAllocMovable ||
  3669 		gZoneUtilArray[zoneIndex].iAllocDiscardable ||
  3670 		gZoneUtilArray[emptyZoneIndex].iAllocMovable)
  3671 		{
  3672 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
  3673 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
  3674 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
  3675 		CLEANUP(Ldd.FreeAllFixedPages());
  3676 		TEST_FAIL;
  3677 		}
  3678 	else
  3679 		{
  3680 		test.Printf(_L("Passed...\n"));	
  3681 		}
  3682 
  3683 skipTest3:
  3684 	// TestEnd() will perform any required clean up.
  3685 	TestEnd();
  3686 
  3687 	test.Next(_L("Test4: Test General Defrag moves pages into the next most pref if they don't fit into the most pref"));	
  3688 	TestStart();
  3689 	gChunkArray1 = new RChunk;	
  3690 	gChunkArraySize1 = 1;
  3691 	testNo++;
  3692 	
  3693 	// No fixed, but there should be no space for the movable to go to
  3694 	// GenDefragTest123Setup(), will ensure that this is the case
  3695 	// by allocating 1 more movable page than there is free in the in-use zones
  3696 	movAllocPages = 1;
  3697 	fixAllocPages = 0;
  3698 	disAllocPages = 1;
  3699 
  3700 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
  3701 		{
  3702 		test.Printf(_L("Setup failed - Skipping..\n"));
  3703 		goto skipTest4;
  3704 		}
  3705 
  3706 	// Call a general defrag and check that the test zone is emptied.
  3707 	GetOriginalPageCount();
  3708 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3709 	
  3710 	GetAllPageInfo();
  3711 	if (r != KErrNone || 
  3712 		gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
  3713 		{
  3714 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
  3715 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
  3716 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
  3717 		CLEANUP(Ldd.FreeAllFixedPages());
  3718 		TEST_FAIL;
  3719 		}
  3720 	else
  3721 		{
  3722 		test.Printf(_L("Passed...\n"));	
  3723 		}
  3724 
  3725 skipTest4:
  3726 	// TestEnd() will perform any required clean up.
  3727 	TestEnd();
  3728 
  3729 	test.Next(_L("Test5: Defrag memory filled with discardable pages\n"));
  3730 	TestStart();
  3731 	if (gPagedRom)
  3732 		{
  3733 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  3734 		FreeMovable(gChunkArray1, gChunkArraySize1);
  3735 		
  3736 		GetAllPageInfo();	
  3737 		TInt discardablePages;
  3738 		UpdateRamInfo();
  3739 		
  3740 		r = AllocDiscardable(discardablePages);
  3741 		if (r != KErrNone)
  3742 			{
  3743 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  3744 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3745 			TEST_FAIL;
  3746 			}
  3747 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
  3748 		
  3749 		GetOriginalPageCount();
  3750 		TBool genSucceed = CanGenSucceed();
  3751 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3752 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  3753 		
  3754 		if (r != KErrNone)
  3755 			{
  3756 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
  3757 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3758 			TEST_FAIL;
  3759 			}
  3760 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
  3761 			{
  3762 			test.Printf(_L("No Zones Switched off and should have been\n"));
  3763 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3764 			TEST_FAIL;
  3765 			}
  3766 		else
  3767 			{
  3768 			test.Printf(_L("Passed...\n"));	
  3769 			}
  3770 
  3771 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  3772 		ResetDPCache();	
  3773 		}
  3774 	else
  3775 		{
  3776 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  3777 		}
  3778 	TestEnd();
  3779 		
  3780 	
  3781 	test.Next(_L("Test6: Defrag memory filled with fixed pages"));	
  3782 	TestStart();
  3783 	
  3784 	TESTDEBUG(test.Printf(_L("Filling memory with fixed pages, r = %d\n")));
  3785 	r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  3786 
  3787 	GetOriginalPageCount();
  3788 	TBool genSucceed = CanGenSucceed();
  3789 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3790 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  3791 
  3792 	// The zones should be full of fixed pages so the general should do nothing.
  3793 	if (r != KErrNone || 
  3794 		genSucceed || 
  3795 		CheckZonesSwitchedOff())
  3796 		{
  3797 		test.Printf(_L("Fail: r = %d, expected = %d, or zone have been emptied\n"), r, KErrNone);
  3798 		CLEANUP(Ldd.FreeAllFixedPages());
  3799 		TEST_FAIL;
  3800 		}
  3801 	else
  3802 		{
  3803 		test.Printf(_L("Passed...\n"));	
  3804 		}
  3805 	Ldd.FreeAllFixedPages();
  3806 	TestEnd();
  3807 
  3808 	test.Next(_L("Test7: Defrag memory filled with discardable pages when the min cache size is reached\n"));
  3809 	TestStart();
  3810 	if (gPagedRom)
  3811 		{
  3812 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  3813 		FreeMovable(gChunkArray1, gChunkArraySize1);
  3814 		
  3815 		GetAllPageInfo();	
  3816 		TInt discardablePages;
  3817 		UpdateRamInfo();
  3818 		
  3819 		r = AllocDiscardable(discardablePages);
  3820 		if (r != KErrNone)
  3821 			{
  3822 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  3823 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3824 			TEST_FAIL;
  3825 			}
  3826 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
  3827 
  3828 		TUint minCacheSize = 0;
  3829 		TUint maxCacheSize = 0;
  3830 		TUint currentCacheSize = 0;
  3831 
  3832 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  3833 
  3834 		TUint setMin = maxCacheSize;
  3835 		TUint64 setMax = maxCacheSize;
  3836 		TInt r = DPTest::SetCacheSize(setMin, setMax);
  3837 		if (r != KErrNone)
  3838 			{
  3839 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  3840 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3841 			CLEANUP(ResetDPCache());
  3842 			TEST_FAIL;
  3843 			}			
  3844 		TESTDEBUG(test.Printf(_L("After SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), 
  3845 															setMin >> gPageShift, setMax >> gPageShift));
  3846 		
  3847 		GetOriginalPageCount();
  3848 		genSucceed = CanGenSucceed();
  3849 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3850 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  3851 
  3852 		if (r != KErrNone)
  3853 			{
  3854 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
  3855 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3856 			CLEANUP(ResetDPCache());
  3857 			TEST_FAIL;
  3858 			}
  3859 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
  3860 			{
  3861 			test.Printf(_L("No Zones Switched off and should have been\n"));
  3862 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3863 			CLEANUP(ResetDPCache());
  3864 			TEST_FAIL;
  3865 			}
  3866 		else
  3867 			{
  3868 			test.Printf(_L("Passed...\n"));	
  3869 			}
  3870 
  3871 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  3872 		ResetDPCache();
  3873 		
  3874 		
  3875 		}
  3876 	else
  3877 		{
  3878 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  3879 		}
  3880 	TestEnd();
  3881 
  3882 	test.Next(_L("Test8: Defrag fragmented memory for various smaller chunk sizes"));	
  3883 	TInt chunkSize = 0x80000;
  3884 	while (chunkSize >= 0x4000)
  3885 		{
  3886 		test.Printf(_L("chunkSize = %dKB\n"), chunkSize/1024);
  3887 		TestStart();
  3888 		r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, chunkSize, EFalse);
  3889 		r = FreeMovable(gChunkArray1, gChunkArraySize1);
  3890 		
  3891 		GetOriginalPageCount();
  3892 		TBool genSucceed = CanGenSucceed();
  3893 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3894 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  3895 		
  3896 		if (r != KErrNone)
  3897 			{
  3898 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
  3899 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3900 			TEST_FAIL;
  3901 			}
  3902 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
  3903 			{
  3904 			test.Printf(_L("No Zones Switched off and should have been\n"));
  3905 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3906 			TEST_FAIL;
  3907 			}
  3908 		else
  3909 			{
  3910 			test.Printf(_L("Passed...\n"));	
  3911 			}
  3912 
  3913 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  3914 		TestEnd();
  3915 		chunkSize = chunkSize>>1; 
  3916 		}
  3917 
  3918 	test.Next(_L("Test9: Defrag fragmented memory "));	
  3919 	TestStart();
  3920 	
  3921 	r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
  3922 	r = FreeMovable(gChunkArray1, gChunkArraySize1);
  3923 	
  3924 	GetOriginalPageCount();
  3925 	genSucceed = CanGenSucceed();
  3926 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  3927 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  3928 
  3929 	if (r != KErrNone)
  3930 		{
  3931 		test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
  3932 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3933 		TEST_FAIL;
  3934 		}
  3935 	else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
  3936 		{
  3937 		test.Printf(_L("No Zones Switched off and should have been\n"));
  3938 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3939 		TEST_FAIL;
  3940 		}
  3941 	else
  3942 		{
  3943 		test.Printf(_L("Passed...\n"));	
  3944 		}
  3945 
  3946 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  3947 	TestEnd();
  3948 
  3949 	test.End();
  3950 	return KErrNone;
  3951 	}
  3952 
  3953 
  3954 //
  3955 // TestGetRamZonePageCount
  3956 //
  3957 //---------------------------------------------------------------------------------------------------------------------
  3958 //! @SYMTestCaseID				KBASE-t_ramdefrag-0529
  3959 //! @SYMTestType				CIT
  3960 //! @SYMTestCaseDesc			Verifying the implementation of the function GetRamZonePageCount()
  3961 //! @SYMPREQ					PREQ308
  3962 //! @SYMTestPriority			High
  3963 //! @SYMTestActions				
  3964 //! 	1.	Fragment the memory. Following this, call GetRamZonePageCount() on every zone and verify
  3965 //!			the values with the test HAL functions. 
  3966 //! 	2.	Fragment the memory. Following this, call function with an valid aID
  3967 //! 
  3968 //! @SYMTestExpectedResults
  3969 //! 	1.	KErrNone
  3970 //! 	2.	KErrArgument
  3971 //---------------------------------------------------------------------------------------------------------------------
  3972 TInt TestGetRamZonePageCount()
  3973 	{
  3974 	test.Start(_L("Test1: Call GetRamZonePageCount() on every zone one after the other"));	
  3975 	TestStart();	
  3976 		
  3977 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  3978 	FreeMovable(gChunkArray1, gChunkArraySize1);
  3979 	
  3980 	for (TUint index = 0; index < gZoneCount; index++)
  3981 		{
  3982 		GetAllPageInfo();
  3983 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  3984 
  3985 		STestUserSidePageCount pageData;
  3986 		TInt r = Ldd.PageCount(zoneID, &pageData);
  3987 		TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
  3988 		if (r != KErrNone)
  3989 			{
  3990 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  3991 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  3992 			TEST_FAIL;
  3993 			}
  3994 		
  3995 		if (pageData.iFreePages != gZoneUtilArray[index].iFreePages ||
  3996 			pageData.iFixedPages != gZoneUtilArray[index].iAllocFixed ||
  3997 			pageData.iMovablePages != gZoneUtilArray[index].iAllocMovable ||
  3998 			pageData.iDiscardablePages != gZoneUtilArray[index].iAllocDiscardable)
  3999 			{
  4000 			test.Printf(_L("RAM zone page count does not match test HAL page count, Zone %d\n"), zoneID);
  4001 			test.Printf(_L("PgCnt: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
  4002 									pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages);
  4003 			test.Printf(_L("HalFunc: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
  4004 									gZoneUtilArray[index].iFreePages, gZoneUtilArray[index].iAllocFixed, 
  4005 									gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);
  4006 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4007 			TEST_FAIL;
  4008 			}
  4009 		else
  4010 			{
  4011 			test.Printf(_L("Zone %d Passed...\n"), zoneID);	
  4012 			}
  4013 		TESTDEBUG(test.Printf(_L("iFreePages = 0x%x, iFixedPages = 0x%x, iMovablePages = 0x%x, iDiscardablePages = 0x%x\n"),
  4014 						pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages));
  4015 		}	
  4016 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4017 	TestEnd();
  4018 	
  4019 	test.Next(_L("Test2: Call GetRamZonePageCount() with an invalid aID "));	
  4020 	TestStart();	
  4021 		
  4022 	TUint zoneID = KInvalidZoneID;
  4023 	STestUserSidePageCount pageData;
  4024 	TInt r = Ldd.PageCount(zoneID, &pageData);
  4025 	TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
  4026 	if (r != KErrArgument)
  4027 		{
  4028 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  4029 		TEST_FAIL;
  4030 		}
  4031 	else
  4032 		{
  4033 		test.Printf(_L("Passed...\n"));	
  4034 		}
  4035 	TestEnd();
  4036 	
  4037 	test.End();
  4038 	return 0;
  4039 	}
  4040 
  4041 const TUint KIndex2Alloc = 10;
  4042 const TUint KTestMaxPages = 6;
  4043 
  4044 //
  4045 // DefragMaxPagesSetup
  4046 //
  4047 // Arranges the memory layout in preparation for TestDefragRamMaxPages()
  4048 //
  4049 // WARNING THIS WILL BE UNRELIABLE IF aMaxPages > KIndex2Alloc
  4050 // 
  4051 TInt DefragMaxPagesSetup(TInt aPageType, TUint aMaxPages, TUint& aIndex, TUint& aIndex2)
  4052 	{
  4053 	TInt r = KErrNoMemory;
  4054 	
  4055 	// Get the preference order and determine the 2 most preferable zones
  4056 	// that are currently not in use.
  4057 	GetPrefOrder();
  4058 	TUint freeInUsePages = 0;
  4059 	TBool zonesFound = EFalse;
  4060 	for (TUint i = 1; i < gZoneCount; i++)
  4061 		{
  4062 		aIndex = gPrefArray[i];
  4063 		aIndex2 = gPrefArray[i-1];
  4064 		TUint indexFree = gZoneUtilArray[aIndex].iFreePages;
  4065 		TUint index2Free = gZoneUtilArray[aIndex2].iFreePages;
  4066 		if (indexFree == gZoneUtilArray[aIndex].iPhysPages &&
  4067 			index2Free == gZoneUtilArray[aIndex2].iPhysPages &&
  4068 			indexFree >= aMaxPages && index2Free >= KIndex2Alloc &&
  4069 			freeInUsePages >= KIndex2Alloc + aMaxPages)
  4070 			{
  4071 			zonesFound = ETrue;
  4072 			break;
  4073 			}
  4074 		freeInUsePages += index2Free;
  4075 		}	
  4076 	
  4077 	// Could suitable RAM zones be found.
  4078 	if (!zonesFound)
  4079 		{
  4080 		test.Printf(_L("Insufficient memory - Skipping test...\n"));
  4081 		return KErrNoMemory;
  4082 		}
  4083 
  4084 	if (aPageType == BEST_MOVABLE)
  4085 		{		
  4086 		// Allocate KIndex2Alloc movable pages to aIndex2
  4087 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aIndex2, KIndex2Alloc);
  4088 		if (r != KErrNone)
  4089 			{
  4090 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
  4091 			r = KErrNoMemory;
  4092 			goto error;
  4093 			}
  4094 
  4095 			
  4096 
  4097 		// Now allow movable pages to be allocated into the least preferable RAM zone under test only.
  4098 		GetAllPageInfo();
  4099 		if (aMaxPages >= gZoneUtilArray[aIndex].iFreePages)
  4100 			{
  4101 			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
  4102 			r = KErrNoMemory;
  4103 			goto error;
  4104 			}
  4105 		// Allocate aMaxPages movable pages to aIndex
  4106 		r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, aIndex, aMaxPages);
  4107 		if (r != KErrNone)
  4108 			{
  4109 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
  4110 			r = KErrNoMemory;
  4111 			goto error;
  4112 			}
  4113 		
  4114 		// Determine how many free pages there are in the RAM zones more preferable
  4115 		// than the RAM zones under test.
  4116 		GetPrefOrder();
  4117 		freeInUsePages = 0;
  4118 		for (TUint i = 0; i < gZoneCount; i++)
  4119 			{
  4120 			TUint tmpIndex = gPrefArray[i];
  4121 			if (tmpIndex == aIndex2)
  4122 				break;
  4123 			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
  4124 			}
  4125 		// Verify that the RAM layout is still suitable for the test.
  4126 		if (gZoneUtilArray[aIndex].iAllocMovable != aMaxPages ||
  4127 			gZoneUtilArray[aIndex2].iAllocMovable != KIndex2Alloc ||
  4128 			gZoneUtilArray[aIndex].iAllocDiscardable || gZoneUtilArray[aIndex].iAllocFixed || 
  4129 			gZoneUtilArray[aIndex2].iAllocDiscardable || gZoneUtilArray[aIndex2].iAllocFixed ||
  4130 			freeInUsePages < KIndex2Alloc + aMaxPages)
  4131 			{
  4132 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
  4133 			r = KErrNoMemory;
  4134 			goto error;
  4135 			}
  4136 
  4137 		// now reset all the flags
  4138 		ResetRamZoneFlags();
  4139 
  4140 		// Perform a general defrag
  4141 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
  4142 		goto exit;
  4143 		}
  4144 		
  4145 	if (aPageType == BEST_DISCARDABLE)
  4146 		{
  4147 
  4148 		TUint minCacheSize = 0;
  4149 		TUint maxCacheSize = 0;
  4150 		TUint currentCacheSize = 0;
  4151 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  4152 		test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  4153 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift);
  4154 					
  4155 
  4156 	
  4157 		// Allocate a number of discardable pages to the 2nd least preferable zone under test
  4158 		TInt disPages;
  4159 		r = ZoneAllocDiscard(aIndex2, KIndex2Alloc, disPages);
  4160 		if (r != KErrNone)
  4161 			{
  4162 			test.Printf(_L("ZoneAllocDiscard() r = %d KIndex2Alloc = 0x%x disPages = 0x%x aIndex2 = %d - Skipping test...\n"),
  4163 							r, KIndex2Alloc, disPages, aIndex2);
  4164 			GetAllPageInfo();
  4165 			PrintPageInfo();
  4166 			r = KErrNoMemory;
  4167 			goto error;
  4168 			}
  4169 		TUint discFillBytes = KIndex2Alloc << gPageShift;
  4170 		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
  4171 		if (r != KErrNone)
  4172 			{
  4173 			test.Printf(_L("SetCacheSize r = 0x%x currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
  4174 							r, currentCacheSize + discFillBytes);
  4175 			GetAllPageInfo();
  4176 			PrintPageInfo();
  4177 			r = KErrNoMemory;
  4178 			goto error;
  4179 			}
  4180 
  4181 	
  4182 		// Allocate a discardable pages equal to aMaxPages to the least preferable zone under test
  4183 		GetAllPageInfo();
  4184 		if(aMaxPages >= gTotalPageCount.iFreePages)
  4185 			{
  4186 			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
  4187 			r = KErrNoMemory;
  4188 			goto error;
  4189 			}
  4190 		TUint allocPages = aMaxPages + KIndex2Alloc;
  4191 		r = ZoneAllocDiscard(aIndex, aMaxPages, disPages);
  4192 		if (r != KErrNone)
  4193 			{
  4194 			test.Printf(_L("ZoneAllocDiscard() r = %d aMaxPages = 0x%x disPages = 0x%x aIndex = %d - Skipping test...\n"),
  4195 							r, aMaxPages, disPages, aIndex);
  4196 			GetAllPageInfo();
  4197 			PrintPageInfo();
  4198 			r = KErrNoMemory;
  4199 			goto error;
  4200 			}
  4201 		discFillBytes = allocPages << gPageShift;
  4202 		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
  4203 		if (r != KErrNone)
  4204 			{
  4205 			test.Printf(_L("SetCacheSize r = %d currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
  4206 							r, currentCacheSize + discFillBytes);
  4207 			GetAllPageInfo();
  4208 			PrintPageInfo();
  4209 			r = KErrNoMemory;
  4210 			goto error;
  4211 			}
  4212 
  4213 
  4214 		// Determine how many free pages there are in the RAM zones more preferable
  4215 		// than the RAM zones under test.
  4216 		GetPrefOrder();
  4217 		freeInUsePages = 0;
  4218 		for (TUint i = 0; i < gZoneCount; i++)
  4219 			{
  4220 			TUint tmpIndex = gPrefArray[i];
  4221 			if (tmpIndex == aIndex2)
  4222 				break;
  4223 			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
  4224 			}
  4225 		// Verify that the RAM layout is still suitable for the test.
  4226 		if (r != KErrNone || gZoneUtilArray[aIndex].iAllocDiscardable != aMaxPages ||
  4227 			gZoneUtilArray[aIndex2].iAllocDiscardable != KIndex2Alloc ||
  4228 			gZoneUtilArray[aIndex].iAllocMovable || gZoneUtilArray[aIndex].iAllocFixed || 
  4229 			gZoneUtilArray[aIndex2].iAllocMovable || gZoneUtilArray[aIndex2].iAllocFixed ||
  4230 			freeInUsePages < KIndex2Alloc + aMaxPages)
  4231 			{
  4232 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
  4233 			r = KErrNoMemory;
  4234 			goto error;
  4235 			}
  4236 
  4237 		// now reset all the flags
  4238 		ResetRamZoneFlags();
  4239 
  4240 		// Perform a general defrag with maxPages = KTestMaxPages
  4241 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
  4242 		goto exit;
  4243 
  4244 		}
  4245 error:
  4246 	// Reset all the flags
  4247 	ResetRamZoneFlags();
  4248 exit:
  4249 	return r;
  4250 	}
  4251 
  4252 //
  4253 // TestDefragRamMaxPages
  4254 //
  4255 //---------------------------------------------------------------------------------------------------------------------
  4256 //! @SYMTestCaseID				KBASE-t_ramdefrag-0530
  4257 //! @SYMTestType				CIT
  4258 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::DefragRam()
  4259 //! @SYMPREQ					PREQ308
  4260 //! @SYMTestPriority			High
  4261 //! @SYMTestActions				
  4262 //! 	1.	Call function with invalid aMaxPages 
  4263 //! 	2.	Call DefragRam when aMaxPages < number of movable pages in least preferable zone 
  4264 //! 	3.	Call DefragRam when aMaxPages > number of movable pages in least preferable zone
  4265 //! 	4.	Call DefragRam when aMaxPages = number of movable pages in least preferable zone
  4266 //! 	5.	Call DefragRam when aMaxPages < number of discardable pages in least preferable zone 
  4267 //! 	6.	Call DefragRam when aMaxPages > number of discardable pages in least preferable zone
  4268 //! 	7.	Call DefragRam when aMaxPages = number of discardable pages in least preferable zone
  4269 //! 
  4270 //! @SYMTestExpectedResults
  4271 //! 	1.	KErrArgument
  4272 //! 	2.	Least preferable zone and 2nd least preferable zone have not been emptied
  4273 //! 	3.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
  4274 //! 	4.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
  4275 //! 	5.	Least preferable zone and 2nd least preferable zone have not been emptied
  4276 //! 	6.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
  4277 //! 	7.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
  4278 //---------------------------------------------------------------------------------------------------------------------
  4279 TInt TestDefragRamMaxPages()
  4280 	{
  4281 	test.Start(_L("Test1: Call DefragRam with invalid aMaxPages "));	
  4282 	TestStart();	
  4283 	
  4284 	gDefragMaxPages = -1;
  4285 	
  4286 	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4287 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
  4288 	if (r != KErrArgument)
  4289 		{
  4290 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  4291 		TEST_FAIL;
  4292 		}
  4293 	else
  4294 		{
  4295 		test.Printf(_L("Passed...\n"));	
  4296 		}	
  4297 	TestEnd();
  4298 	
  4299 	test.Next(_L("Test2: Call DefragRam aMaxPages < number of movable pages in least preferable zone "));	
  4300 	TestStart();	
  4301 	gChunkArray1 = new RChunk;
  4302 	gChunkArraySize1 = 1;
  4303 	gChunkArray2 = new RChunk;
  4304 	gChunkArraySize2 = 1;
  4305 	TUint index = 0;
  4306 	TUint index2 = 0;
  4307 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages + 1, index, index2);	
  4308 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4309 		{// Cannot perform test with fixed pages in least preferable zones
  4310 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4311 		}
  4312 	else
  4313 		{
  4314 		if (r == KErrNone)
  4315 			{
  4316 			GetAllPageInfo();
  4317 			if (gZoneUtilArray[index].iAllocMovable < KTestMaxPages + 1 ||
  4318 				gZoneUtilArray[index2].iAllocMovable < KIndex2Alloc)
  4319 				{
  4320 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4321 										index, KTestMaxPages + 1, index2, KIndex2Alloc);
  4322 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4323 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4324 				CLEANUP(Ldd.FreeFromAllZones());
  4325 				TEST_FAIL;
  4326 				}
  4327 
  4328 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4329 			// if there is free space elsewhere
  4330 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4331 			GetAllPageInfo();
  4332 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages + 1 &&
  4333 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
  4334 				{
  4335 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4336 										index, 0, index2, 0);
  4337 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4338 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4339 				CLEANUP(Ldd.FreeFromAllZones());
  4340 				TEST_FAIL;
  4341 				}
  4342 			else
  4343 				{
  4344 				test.Printf(_L("Passed...\n"));
  4345 				}
  4346 			}
  4347 		else 
  4348 			{
  4349 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4350 			}
  4351 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4352 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
  4353 		}
  4354 	Ldd.FreeFromAllZones();
  4355 	TestEnd();
  4356 
  4357 
  4358 	test.Next(_L("Test3: Call DefragRam aMaxPages > number of movable pages in least preferable zone "));	
  4359 	TestStart();	
  4360 	gChunkArray1 = new RChunk;
  4361 	gChunkArraySize1 = 1;
  4362 	gChunkArray2 = new RChunk;
  4363 	gChunkArraySize2 = 1;
  4364 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages - 1, index, index2);
  4365 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4366 		{// Cannot perform test with fixed pages in least preferable zones
  4367 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4368 		}
  4369 	else
  4370 		{		
  4371 		if (r == KErrNone)
  4372 			{
  4373 			GetAllPageInfo();
  4374 			if (gZoneUtilArray[index].iAllocMovable != 0)
  4375 				{
  4376 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4377 										index, 0, index2, KIndex2Alloc);
  4378 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4379 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4380 				CLEANUP(Ldd.FreeFromAllZones());
  4381 				TEST_FAIL;		
  4382 				}
  4383 			
  4384 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4385 			// if there is free space elsewhere
  4386 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4387 			
  4388 			GetAllPageInfo();
  4389 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages - 1 &&
  4390 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
  4391 				{
  4392 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4393 										index, 0, index2, 0);
  4394 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4395 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4396 				CLEANUP(Ldd.FreeFromAllZones());
  4397 				TEST_FAIL;		
  4398 				}
  4399 			else
  4400 				{
  4401 				test.Printf(_L("Passed...\n"));
  4402 				}
  4403 			}
  4404 		else 
  4405 			{
  4406 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4407 			}	
  4408 		
  4409 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4410 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
  4411 		}
  4412 	Ldd.FreeFromAllZones();
  4413 	TestEnd();
  4414 
  4415 
  4416 	test.Next(_L("Test4: Call DefragRam aMaxPages = number of movable pages in least preferable zone "));	
  4417 	TestStart();	
  4418 	gChunkArray1 = new RChunk;
  4419 	gChunkArraySize1 = 1;
  4420 	gChunkArray2 = new RChunk;
  4421 	gChunkArraySize2 = 1;
  4422 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages, index, index2);
  4423 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4424 		{// Cannot perform test with fixed pages in least preferable zones
  4425 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4426 		}
  4427 	else
  4428 		{		
  4429 		if (r == KErrNone)
  4430 			{
  4431 			GetAllPageInfo();
  4432 			if (gZoneUtilArray[index].iAllocMovable != 0)
  4433 				{
  4434 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4435 										index, 0, index2, KIndex2Alloc);
  4436 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4437 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4438 				CLEANUP(Ldd.FreeFromAllZones());
  4439 				TEST_FAIL;		
  4440 				}
  4441 
  4442 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4443 			// if there is free space elsewhere
  4444 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4445 			
  4446 			GetAllPageInfo();
  4447 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages &&
  4448 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
  4449 				{
  4450 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4451 										index, 0, index2, 0);
  4452 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4453 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
  4454 				CLEANUP(Ldd.FreeFromAllZones());
  4455 				TEST_FAIL;		
  4456 				}
  4457 			else 
  4458 				{
  4459 				test.Printf(_L("Passed...\n"));
  4460 				}
  4461 			}
  4462 		else 
  4463 			{
  4464 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4465 			}
  4466 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4467 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
  4468 		}
  4469 	Ldd.FreeFromAllZones();
  4470 	TestEnd();
  4471 	
  4472 	test.Next(_L("Test5: Call DefragRam aMaxPages < number of discardable pages in least preferable zone "));	
  4473 	TestStart();	
  4474 	if (gPagedRom)
  4475 		{
  4476 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages + 1, index, index2);
  4477 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4478 			{// Cannot perform test with fixed pages in least preferable zones
  4479 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4480 			}
  4481 		else
  4482 			{
  4483 			if (r == KErrNone)
  4484 				{
  4485 				GetAllPageInfo();
  4486 				if (gZoneUtilArray[index].iAllocDiscardable < KTestMaxPages + 1 ||
  4487 					gZoneUtilArray[index2].iAllocDiscardable < KIndex2Alloc)
  4488 					{
  4489 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4490 											index, KTestMaxPages + 1, index2, KIndex2Alloc);
  4491 					CLEANUP(ResetDPCache());
  4492 					CLEANUP(Ldd.FreeFromAllZones());
  4493 					TEST_FAIL;		
  4494 					}
  4495 				
  4496 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4497 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4498 				
  4499 				GetAllPageInfo();
  4500 				if (gZoneUtilArray[index].iAllocDiscardable != 0 ||
  4501 					gZoneUtilArray[index2].iAllocDiscardable != 0)
  4502 					{
  4503 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4504 											index, 0, index2, 0);
  4505 					CLEANUP(ResetDPCache());
  4506 					CLEANUP(Ldd.FreeFromAllZones());
  4507 					TEST_FAIL;		
  4508 					}
  4509 				else
  4510 					{
  4511 					test.Printf(_L("Passed...\n"));
  4512 					}
  4513 				}
  4514 			else 
  4515 				{
  4516 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4517 				}
  4518 			ResetDPCache();
  4519 			}
  4520 		Ldd.FreeFromAllZones();
  4521 		}
  4522 	else
  4523 		{
  4524 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
  4525 		}
  4526 	TestEnd();
  4527 
  4528 	test.Next(_L("Test6: Call DefragRam aMaxPages > number of discardable pages in least preferable zone "));	
  4529 	TestStart();	
  4530 	
  4531 	if (gPagedRom)
  4532 		{
  4533 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages - 1, index, index2);
  4534 		
  4535 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4536 			{// Cannot perform test with fixed pages in least preferable zones
  4537 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4538 			}
  4539 		else
  4540 			{	
  4541 			if (r == KErrNone)
  4542 				{
  4543 				GetAllPageInfo();
  4544 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
  4545 					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
  4546 					{
  4547 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4548 											index, 0, index2, KIndex2Alloc);
  4549 					CLEANUP(ResetDPCache());
  4550 					CLEANUP(Ldd.FreeFromAllZones());
  4551 					TEST_FAIL;		
  4552 					}
  4553 				
  4554 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4555 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4556 				GetAllPageInfo();
  4557 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
  4558 					gZoneUtilArray[index2].iAllocDiscardable != 0)
  4559 					{
  4560 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4561 											index, 0, index2, 0);
  4562 					CLEANUP(ResetDPCache());
  4563 					CLEANUP(Ldd.FreeFromAllZones());
  4564 					TEST_FAIL;		
  4565 					}
  4566 				else
  4567 					{
  4568 					test.Printf(_L("Passed...\n"));
  4569 					}
  4570 				}
  4571 			else 
  4572 				{
  4573 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4574 				}	
  4575 			ResetDPCache();
  4576 			}
  4577 		Ldd.FreeFromAllZones();
  4578 		}
  4579 	else
  4580 		{
  4581 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
  4582 		}
  4583 	TestEnd();
  4584 
  4585 
  4586 	test.Next(_L("Test4: Call DefragRam aMaxPages = number of discardable pages in least preferable zone "));	
  4587 	TestStart();	
  4588 	
  4589 	if(gPagedRom)
  4590 		{
  4591 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages, index, index2);
  4592 		
  4593 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
  4594 			{// Cannot perform test with fixed pages in least preferable zones
  4595 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
  4596 			}
  4597 		else
  4598 			{	
  4599 			if (r == KErrNone)
  4600 				{
  4601 				GetAllPageInfo();
  4602 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
  4603 					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
  4604 					{
  4605 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4606 											index, 0, index2, KIndex2Alloc);
  4607 					CLEANUP(ResetDPCache());
  4608 					CLEANUP(Ldd.FreeFromAllZones());
  4609 					TEST_FAIL;		
  4610 					}
  4611 
  4612 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
  4613 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4614 				GetAllPageInfo();
  4615 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
  4616 					gZoneUtilArray[index2].iAllocDiscardable != 0)
  4617 					{
  4618 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
  4619 											index, 0, index2, 0);
  4620 					CLEANUP(ResetDPCache());
  4621 					CLEANUP(Ldd.FreeFromAllZones());
  4622 					TEST_FAIL;		
  4623 					}
  4624 				else
  4625 					{
  4626 					test.Printf(_L("Passed...\n"));
  4627 					}
  4628 				}
  4629 			else 
  4630 				{
  4631 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
  4632 				}
  4633 			ResetDPCache();
  4634 			}
  4635 		Ldd.FreeFromAllZones();
  4636 		}
  4637 	else
  4638 		{
  4639 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
  4640 		}
  4641 	TestEnd();
  4642 
  4643 	test.End();
  4644 	return KErrNone;
  4645 	}
  4646 
  4647 
  4648 //
  4649 // TestEmptyRamZone
  4650 //
  4651 //---------------------------------------------------------------------------------------------------------------------
  4652 //! @SYMTestCaseID				KBASE-t_ramdefrag-0531
  4653 //! @SYMTestType				CIT
  4654 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()
  4655 //! @SYMPREQ					PREQ308
  4656 //! @SYMTestPriority			High
  4657 //! @SYMTestActions				
  4658 //! 	1.	Fragment the memory. Call function with a valid aID, 
  4659 //! 		i.e. a zone which lies within the range of zones. 
  4660 //! 	2.	Fragment the memory. Call function with an invalid aID
  4661 //! 		i.e. a zone which does not exist within the range of zones. 
  4662 //! 	3.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
  4663 //! 		Call the zone specific defrag again on the same zone whilst the other defrag is still 
  4664 //!			running on that zone. 
  4665 //! 	4.	Fragment the memory. Following this, call the TRamDefragRequest::DefragRam() to perform general defrag. 
  4666 //! 		Following this call zone specific defrag whilst the general defrag is still running. 
  4667 //! 	5.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
  4668 //! 		Whilst the zone defrag is running, call a general defrag
  4669 //! 	6:  Fragment the memory. Following this, call the function on specifc zone and 
  4670 //! 		at the same time allocate pages to the zone
  4671 //! 	7.	Fragment the memory. Call EmptyRamZone() on every zone one after the other. 
  4672 //! 	8.	Fragment the memory. Allocate a couple of fixed pages to a zone that contains movable pages. 
  4673 //! 		Following this, Call EmptyRamZone() on that zone. 
  4674 //! 
  4675 //! @SYMTestExpectedResults
  4676 //! 	1.	KErrNone
  4677 //! 	2.	KErrArgument
  4678 //! 	3.	KErrNone and the zone has been emptied
  4679 //! 	4.	Zone has not been emptied, as the general defrag has already placed pages in the zone
  4680 //! 	5.	Zone has been emptied
  4681 //! 	6.	KErrNone and pages have been allocated in the zone
  4682 //! 	7.	One of the following scenarios should occur:
  4683 //!			a.	If the cache has not reached its minimum size, then all the movable pages should 
  4684 //!				be moved from the zone if there are enough free pages in the other zones. 
  4685 //!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
  4686 //!				are enough free pages in the other zones. 
  4687 //!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is 
  4688 //!				not less than the minimum cache size, then all the discardable pages should be discarded of. 
  4689 //!				However, when demand paging is off, all discardable pages should be removed from 
  4690 //!				the zone. 
  4691 //! 	8.	KErrNoMemory, however all movable and discardable pages have been moved from the zone 
  4692 //---------------------------------------------------------------------------------------------------------------------
  4693 TInt TestEmptyRamZone()
  4694 	{
  4695 	TInt r = KErrNone;
  4696 	TInt r2 = KErrNone;
  4697 
  4698 	test.Start(_L("Test1: Call EmptyRamZone with valid aID "));	
  4699 	TestStart();	
  4700 	
  4701 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  4702 	FreeMovable(gChunkArray1, gChunkArraySize1);
  4703 	
  4704 	GetAllPageInfo();
  4705 	TUint index = gZoneCount - 1;
  4706 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  4707 		{
  4708 		-- index;
  4709 		}
  4710 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
  4711 	test.Printf(_L("Zone ID = %d\n"), defragZoneID);
  4712 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  4713 		{
  4714 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  4715 		}
  4716 	else
  4717 		{
  4718 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
  4719 		GetAllPageInfo();
  4720 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  4721 		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
  4722 			{
  4723 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  4724 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4725 			TEST_FAIL;
  4726 			}
  4727 		else
  4728 			{
  4729 			test.Printf(_L("Passed...\n"));	
  4730 			}
  4731 		}
  4732 
  4733 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  4734 	TestEnd();
  4735 	
  4736 	
  4737 	test.Next(_L("Test2: Call EmptyRamZone with invalid aID "));	
  4738 	TestStart();	
  4739 	gChunkArray1 = new RChunk;
  4740 	gChunkArraySize1 = 1;
  4741 	defragZoneID = KInvalidZoneID; 
  4742 
  4743 	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
  4744 	
  4745 	if (r != KErrArgument)
  4746 		{
  4747 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  4748 		TEST_FAIL;
  4749 		}
  4750 	else
  4751 		{
  4752 		test.Printf(_L("Passed...\n"));	
  4753 		}
  4754 	
  4755 	TestEnd();
  4756 	
  4757 	
  4758 	test.Next(_L("Test3: Call EmptyRamZone twice at the same time "));	
  4759 	TestStart();	
  4760 	
  4761 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  4762 	FreeMovable(gChunkArray1, gChunkArraySize1);
  4763 
  4764 	GetAllPageInfo();
  4765 	index = gZoneCount - 1;
  4766 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  4767 		{
  4768 		-- index;
  4769 		}
  4770 	defragZoneID = gZoneConfigArray[index].iZoneId;
  4771 	
  4772 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  4773 		{
  4774 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  4775 		}
  4776 	else
  4777 		{
  4778 		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny *)defragZoneID);
  4779 		if (r != KErrNone)
  4780 			{
  4781 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  4782 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4783 			TEST_FAIL;
  4784 			}		
  4785 				
  4786 		gTestThread.Logon(status);
  4787 		gTestThread.Resume();
  4788 		
  4789 		User::After(10);
  4790 
  4791 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
  4792 		
  4793 		User::WaitForRequest(status);
  4794 		r2 = status.Int();
  4795 
  4796 		GetAllPageInfo();
  4797 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  4798 		if (r != r2 || 
  4799 			(freeInOthers && (r != KErrNone || r2 != KErrNone || !CheckZoneIsOff(index))))
  4800 			{
  4801 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d has not been emptied\n"), 
  4802 									r, r2, KErrNone, defragZoneID);
  4803 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
  4804 			TEST_FAIL;
  4805 			}	
  4806 		else
  4807 			{
  4808 			test.Printf(_L("Passed...\n"));	
  4809 			}
  4810 		
  4811 		gTestThread.Close();	
  4812 		}
  4813 	
  4814 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  4815 	TestEnd();
  4816 
  4817 		
  4818 	test.Next(_L("Test4: Call general defrag and zone defrag at the same time "));	
  4819 	TestStart();	
  4820 		
  4821 	TInt waitTime = 10000;
  4822 	
  4823 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  4824 	FreeMovable(gChunkArray1, gChunkArraySize1);
  4825 
  4826 	GetAllPageInfo();
  4827 	index = gZoneCount - 1;
  4828 	while (index > 0 && gZoneUtilArray[index].iAllocFixed > 0)
  4829 		{
  4830 		index--;
  4831 		}
  4832 	defragZoneID = gZoneConfigArray[index].iZoneId;
  4833 	
  4834 	if (index == 0 && gZoneUtilArray[index].iAllocFixed > 0)
  4835 		{
  4836 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  4837 		}
  4838 	else
  4839 		{
  4840 		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  4841 		if (r != KErrNone)
  4842 			{
  4843 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  4844 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4845 			TEST_FAIL;
  4846 			}		
  4847 		gTestThread.Logon(status);
  4848 				
  4849 		gTestThread.Resume();
  4850 		User::After(waitTime);
  4851 		
  4852 		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
  4853 
  4854 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
  4855 
  4856 		User::WaitForRequest(status);
  4857 		r2 = status.Int();
  4858 
  4859 		GetAllPageInfo();
  4860 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  4861 		if (r2 != KErrNone || 
  4862 			(freeInOthers && (r != KErrNone || !CheckZoneIsOff(index))))
  4863 			{
  4864 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d is on unexpectedly\n"),
  4865 							r, r2, KErrNone, defragZoneID);
  4866 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
  4867 			TEST_FAIL;
  4868 			}	
  4869 		else
  4870 			{
  4871 			test.Printf(_L("Passed...\n"));	
  4872 			}	
  4873 
  4874 		gTestThread.Close();	
  4875 		}
  4876 
  4877 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4878 	TestEnd();
  4879 	
  4880 	
  4881 	test.Next(_L("Test5: Call zone defrag and general defrag at the same time "));	
  4882 	TestStart();	
  4883 	
  4884 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  4885 	FreeMovable(gChunkArray1, gChunkArraySize1);
  4886 
  4887 	GetAllPageInfo();
  4888 	index = gZoneCount - 1;
  4889 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  4890 		{
  4891 		-- index;
  4892 		}
  4893 	defragZoneID = gZoneConfigArray[index].iZoneId;
  4894 
  4895 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  4896 		{
  4897 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  4898 		}
  4899 	else
  4900 		{
  4901 		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
  4902 		if (r != KErrNone)
  4903 			{
  4904 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  4905 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4906 			TEST_FAIL;
  4907 			}		
  4908 		gTestThread.Logon(status);
  4909 				
  4910 		gTestThread.Resume();
  4911 		User::After(0);
  4912 		
  4913 		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
  4914 		
  4915 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
  4916 
  4917 		User::WaitForRequest(status);
  4918 		r2 = status.Int();
  4919 
  4920 		GetAllPageInfo();
  4921 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  4922 		if (r != KErrNone ||
  4923 			(freeInOthers && (r2 != KErrNone || !CheckZoneIsOff(index))))
  4924 			{
  4925 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone 0x%x is on unexpectedly\n"),
  4926 							r, r2, KErrNone, defragZoneID);
  4927 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4928 			TEST_FAIL;
  4929 			}
  4930 		else
  4931 			{
  4932 			test.Printf(_L("Passed...\n"));	
  4933 			}
  4934 
  4935 		gTestThread.Close();	
  4936 		}
  4937 			
  4938 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  4939 	TestEnd();
  4940 
  4941 
  4942 	test.Next(_L("Test6: Call function TRamDefragRequest::EmptyRamZone on specifc zone and at the same time allocate pages to the zone"));	
  4943 	TestStart();	
  4944 	
  4945 	TInt pagesAlloc = 1; // Try and allocate just one page whilst trying to empty the zone. 
  4946 
  4947 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  4948 	FreeMovable(gChunkArray1, gChunkArraySize1);
  4949 
  4950 	GetAllPageInfo();
  4951 	index = gZoneCount - 1;
  4952 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  4953 		{
  4954 		-- index;
  4955 		}
  4956 	defragZoneID = gZoneConfigArray[index].iZoneId;
  4957 	
  4958 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  4959 		{
  4960 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  4961 		}
  4962 	else
  4963 		{
  4964 		TInt numPassed = 0;
  4965 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
  4966 			{
  4967 			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
  4968 			if (r != KErrNone)
  4969 				{
  4970 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  4971 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4972 				TEST_FAIL;
  4973 				}
  4974 			gTestThread.Logon(status);
  4975 			
  4976 			gTestThread.Resume();
  4977 			User::After(waitTime);
  4978 			
  4979 			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
  4980 
  4981 			User::WaitForRequest(status);
  4982 			r2 = status.Int();
  4983 			
  4984 			if (r2 != KErrNone)
  4985 				{
  4986 				test.Printf(_L("Empty was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
  4987 				}
  4988 			else if (r != KErrNone)
  4989 				{
  4990 				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone = 0x%x\n"), 
  4991 										r, KErrNone, r2, KErrNone, defragZoneID);
  4992 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  4993 				CLEANUP(Ldd.FreeAllFixedPages());
  4994 				TEST_FAIL;
  4995 				}
  4996 			else
  4997 				{
  4998 				numPassed ++;
  4999 				}
  5000 							
  5001 			Ldd.FreeAllFixedPages();
  5002 
  5003 			gTestThread.Close();	
  5004 			}
  5005 		if (numPassed > 0)
  5006 			{
  5007 			test.Printf(_L("Passed...\n"));	
  5008 			}
  5009 		}
  5010 			
  5011 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5012 	TestEnd();
  5013 
  5014 
  5015 	test.Next(_L("Test7: Call function TRamDefragRequest::EmptyRamZone on every zone one after the other"));	
  5016 	TestStart();
  5017 		
  5018 	Ldd.ResetDriver();
  5019 			
  5020 	for (index = 0; index < gZoneCount; index ++)
  5021 		{
  5022 		// Variables for DP ROM cache sizes
  5023 		TUint minCacheSize = 0;
  5024 		TUint maxCacheSize = 0;
  5025 		TUint currentCacheSize = 0;
  5026 
  5027 		if (gPagedRom)
  5028 			{
  5029 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  5030 			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5031 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5032 			// Calculate the maximum size available for the DP cache.
  5033 			TUint minZoneFree = KMaxTUint;
  5034 			TUint maxDisFillBytes = 0;
  5035 			GetAllPageInfo();
  5036 			for (TUint i = 0; i < gZoneCount; i++)
  5037 				{
  5038 				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
  5039 				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
  5040 				if (minZoneFree > gZoneUtilArray[i].iFreePages)
  5041 					minZoneFree = gZoneUtilArray[i].iFreePages;
  5042 				}
  5043 			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
  5044 			maxDisFillBytes <<= gPageShift;
  5045 
  5046 			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
  5047 			if (r != KErrNone)
  5048 				{
  5049 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5050 				CLEANUP(ResetDPCache());
  5051 				TEST_FAIL;
  5052 				}
  5053 			
  5054 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  5055 			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5056 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5057 			if (currentCacheSize != maxDisFillBytes)
  5058 				{
  5059 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5060 				CLEANUP(ResetDPCache());
  5061 				TEST_FAIL;
  5062 				}
  5063 			
  5064 			GetAllPageInfo();
  5065 			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
  5066 
  5067 			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
  5068 			if (r != KErrNone)
  5069 				{
  5070 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5071 				CLEANUP(ResetDPCache());
  5072 				TEST_FAIL;
  5073 				}
  5074 			
  5075 			// Check all zones have no free pages.
  5076 			GetAllPageInfo();
  5077 			for (TUint i = 0; i < gZoneCount; i++)
  5078 				{
  5079 				if (gZoneUtilArray[i].iFreePages != 0)
  5080 					{
  5081 					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5082 					CLEANUP(ResetDPCache());
  5083 					TEST_FAIL;
  5084 					}
  5085 				}
  5086 			}
  5087 		else
  5088 			{
  5089 			// Fragment the RAM with some movable pages
  5090 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
  5091 			FreeMovable(gChunkArray1, gChunkArraySize1);
  5092 			}
  5093 		
  5094 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  5095 		
  5096 		GetAllPageInfo();
  5097 		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
  5098 		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
  5099 		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
  5100 
  5101 		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
  5102 						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
  5103 						origMovInZone, origFreeInZone);
  5104 
  5105 
  5106 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
  5107 
  5108 		GetAllPageInfo();		
  5109 		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
  5110 		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
  5111 		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
  5112 	
  5113 		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5114 		if(movablePages && freeInOtherZones)
  5115 			{
  5116 			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
  5117 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5118 			CLEANUP(ResetDPCache());
  5119 			TEST_FAIL;
  5120 			}
  5121 
  5122 		if (origUnknownInZone != unknownPages)
  5123 			{
  5124 			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
  5125 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5126 			CLEANUP(ResetDPCache());
  5127 			TEST_FAIL;
  5128 			}
  5129 
  5130 		if (!gPagedRom)
  5131 			{
  5132 			if (discPages && freeInOtherZones)
  5133 				{
  5134 				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
  5135 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5136 				CLEANUP(ResetDPCache());
  5137 				TEST_FAIL;
  5138 				}
  5139 			}
  5140 		else
  5141 			{
  5142 			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
  5143 			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5144 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5145 			// The the discardable pages should have been discarded or moved unless 
  5146 			// there is no room in the other zones and the cache size is already at its minimum
  5147 			if (discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
  5148 				{
  5149 				test.Printf(_L("Fail: Zone 0x%x all the discardable pages haven't been moved\n"), zoneID);
  5150 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5151 				CLEANUP(ResetDPCache());
  5152 				TEST_FAIL;
  5153 				}
  5154 			}
  5155 		test.Printf(_L("Passed...\n"));
  5156 		}
  5157 	// TestEnd() willl cleanup what's required
  5158 	TestEnd();
  5159 
  5160 	test.Next(_L("Test8: Call function TRamDefragRequest::EmptyRamZone on a zone that contains fixed and movable pages"));
  5161 	TestStart();
  5162 	
  5163 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5164 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5165 
  5166 	GetAllPageInfo();	
  5167 	index = gZoneCount - 1;
  5168 	while (index > 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
  5169 		{	
  5170 		-- index;
  5171 		}
  5172 	defragZoneID = gZoneConfigArray[index].iZoneId;
  5173 	
  5174 	if (index == 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
  5175 		{
  5176 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5177 		}
  5178 	else
  5179 		{
  5180 		test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID);		
  5181 		test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
  5182 						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);	
  5183 
  5184 		
  5185 		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
  5186 		
  5187 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
  5188 		
  5189 		GetAllPageInfo();
  5190 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5191 		if (r != KErrNoMemory || 
  5192 			(freeInOthers && 
  5193 			(gZoneUtilArray[index].iAllocMovable ||	
  5194 			gZoneUtilArray[index].iAllocDiscardable)))
  5195 			{
  5196 			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
  5197 							r, KErrNoMemory);
  5198 			CLEANUP(Ldd.FreeAllFixedPages());
  5199 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5200 			TEST_FAIL;
  5201 			}
  5202 		else
  5203 			{
  5204 			test.Printf(_L("Passed..."));
  5205 			}
  5206 		
  5207 		Ldd.FreeAllFixedPages();
  5208 		}
  5209 	
  5210 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  5211 	TestEnd();
  5212 
  5213 	test.End();
  5214 	return r;
  5215 	}
  5216 
  5217 
  5218 //
  5219 // TestClaimRamZone
  5220 //
  5221 //---------------------------------------------------------------------------------------------------------------------
  5222 //! @SYMTestCaseID				KBASE-t_ramdefrag-0532
  5223 //! @SYMTestType				CIT
  5224 //! @SYMTestCaseDesc			Verifying the implemntation of the function TRamDefragRequest::ClaimRamZone()
  5225 //! @SYMPREQ					PREQ308
  5226 //! @SYMTestPriority			High
  5227 //! @SYMTestActions				
  5228 //! 	1.	Fragment the memory. Following this, call function with a valid aID
  5229 //! 	2.	Fragment the memory. Following this, call function with an invalid aID
  5230 //! 	3.	Fragment the memory. Following this, start a zone defrag and whilst this 
  5231 //! 		is running, call function on the same zone. 
  5232 //! 	4.	Fragment the memory. Following this, start a general defrag and whilst this 
  5233 //! 		is running, call function on the same zone.
  5234 //! 	5.	Fragment the memory. Following this, call ClaimRamZone() on a specific zone, whilst at the
  5235 //! 		same time calling ClaimRamZone() on another zone. 
  5236 //! 	6:	Fragment the memory. Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the 
  5237 //! 		same time allocate pages to the zone
  5238 //!		7.	Fragment the memory. Following this allocate fixed pages to a zone and attempt to claim
  5239 //!			the zone. 
  5240 //!		8.	Fragment the memory. Following this call ClaimRamZone() on every zone, one after the other
  5241 //! 
  5242 //! @SYMTestExpectedResults
  5243 //! 	1.	KErrNone
  5244 //! 	2.	KErrArgument
  5245 //! 	3.	KErrNone and the zone has been claimed
  5246 //! 	4.	KErrNone and the zone has been claimed
  5247 //! 	5.	KErrNone and both zones have been claimed
  5248 //! 	6.	KErrNoMemory
  5249 //!		7.	KErrNoMemory, all movable and discardable pages have been moved
  5250 //!		8.	One of the following scenarios should occur:
  5251 //!			a.	If the cache has not reached its minimum size and there are no unknown pages in 
  5252 //!				the zone, then all the movable pages should be moved from the zone if there are enough free pages
  5253 //!				in the other zones. 
  5254 //!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
  5255 //!				are enough free pages in the other zones and if there are no unknown pages in the zone. 
  5256 //!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is not 
  5257 //!				less than the minimum cache size and there are no unknown pages in the zone, 
  5258 //!				then all the discardable pages should be discarded of. 
  5259 //!				However, when demand paging is off, all discardable pages should be removed from 
  5260 //!				the zone if there are no unknown pages in the zone. 
  5261 //---------------------------------------------------------------------------------------------------------------------
  5262 TInt TestClaimRamZone()
  5263 	{
  5264 	TInt r = 0;
  5265 	TInt r2 = 0;
  5266 	TUint32 addr = 0;
  5267 	TInt waitTime = 1000;
  5268 	
  5269 	test.Start(_L("Test1: Call function TRamDefragRequest::ClaimRamZone with a valid aID"));	
  5270 	TestStart();	
  5271 	
  5272 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5273 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5274 
  5275 	GetAllPageInfo();
  5276 	TUint index = gZoneCount - 1;
  5277 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  5278 		{
  5279 		-- index;
  5280 		}
  5281 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
  5282 	
  5283 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  5284 		{
  5285 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5286 		}
  5287 	else
  5288 		{
  5289 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
  5290 		GetAllPageInfo();
  5291 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5292 		if (freeInOthers && r != KErrNone && 
  5293 			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
  5294 			{
  5295 			test.Printf(_L("Fail: Zone 0x%x has not been claimed r %d expected %d\n"), defragZoneID, r, KErrNone);
  5296 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5297 			TEST_FAIL;
  5298 			}
  5299 		else
  5300 			{
  5301 			test.Printf(_L("Passed...\n"));	
  5302 			}
  5303 
  5304 		addr = gZoneConfigArray[index].iPhysBase;
  5305 		if (r == KErrNone)
  5306 			{
  5307 			r = Ldd.FreeAllFixedPages();
  5308 			}
  5309 		}
  5310 	
  5311 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  5312 	TestEnd();
  5313 
  5314 
  5315 	test.Next(_L("Test2: Call function TRamDefragRequest::ClaimRamZone with an invalid aID"));	
  5316 	TestStart();	
  5317 	gChunkArray1 = new RChunk;
  5318 	gChunkArraySize1 = 1;
  5319 	defragZoneID = KInvalidZoneID;
  5320 	
  5321 	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);	
  5322 	if (r != KErrArgument)
  5323 		{
  5324 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  5325 		TEST_FAIL;
  5326 		}
  5327 	else
  5328 		{
  5329 		test.Printf(_L("Passed...\n"));	
  5330 		}	
  5331 	
  5332 	TestEnd();
  5333 
  5334 
  5335 	test.Next(_L("Test3: Call function TRamDefragRequest::ClaimRamZone when a EmptyRamZone is already running"));	
  5336 	TestStart();	
  5337 	
  5338 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5339 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5340 
  5341 	GetAllPageInfo();
  5342 	index = gZoneCount - 1;
  5343 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  5344 		{
  5345 		-- index;
  5346 		}
  5347 	defragZoneID = gZoneConfigArray[index].iZoneId;
  5348 
  5349 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  5350 		{
  5351 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5352 		}
  5353 	else
  5354 		{
  5355 		TInt numPassed = 0;
  5356 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
  5357 			{
  5358 			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
  5359 			if (r != KErrNone)
  5360 				{
  5361 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5362 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5363 				TEST_FAIL;
  5364 				}
  5365 
  5366 			gTestThread.Logon(status);
  5367 			gTestThread.Resume();
  5368 			User::After(waitTime);
  5369 			
  5370 			r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
  5371 
  5372 			User::WaitForRequest(status);
  5373 			r2 = status.Int();
  5374 			
  5375 			GetAllPageInfo();
  5376 			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5377 			if (freeInOthers && r != KErrNone && r2 != KErrNone)
  5378 				{
  5379 				test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone ID 0x%x has not been claimed\n"), 
  5380 										r, r2, KErrNone, defragZoneID);
  5381 				if (r == KErrNone)
  5382 					{
  5383 					CLEANUP(r = Ldd.FreeAllFixedPages());
  5384 					}	
  5385 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5386 				TEST_FAIL;
  5387 				}
  5388 			else
  5389 				{
  5390 				numPassed ++;
  5391 				}
  5392 			
  5393 			if (r == KErrNone)
  5394 				{
  5395 				r = Ldd.FreeAllFixedPages();
  5396 				}		
  5397 			
  5398 			gTestThread.Close();	
  5399 			}
  5400 		if (numPassed > 0)
  5401 			{
  5402 			test.Printf(_L("Passed...\n"));	
  5403 			}
  5404 		}
  5405 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5406 	TestEnd();
  5407 
  5408 
  5409 	test.Next(_L("Test4: Call function TRamDefragRequest::ClaimRamZone when a general defrag is already running"));	
  5410 	TestStart();	
  5411 	
  5412 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5413 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5414 
  5415 	GetAllPageInfo();
  5416 	index = gZoneCount - 1;
  5417 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  5418 		{
  5419 		-- index;
  5420 		}
  5421 
  5422 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  5423 		{
  5424 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5425 		}
  5426 	else
  5427 		{
  5428 		defragZoneID = gZoneConfigArray[index].iZoneId;
  5429 
  5430 		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
  5431 		if (r != KErrNone)
  5432 			{
  5433 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5434 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5435 			TEST_FAIL;
  5436 			}
  5437 		
  5438 		gTestThread.Logon(status);
  5439 		gTestThread.Resume();
  5440 		User::After(waitTime);
  5441 
  5442 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
  5443 		
  5444 		User::WaitForRequest(status);
  5445 		r2 = status.Int();
  5446 		
  5447 		if (r != KErrNone)
  5448 			{
  5449 			test.Printf(_L("ClaimZone: r = %d, expected = %d\n"), r, KErrNone);
  5450 			}	
  5451 		
  5452 		if (r2 != KErrNone)
  5453 			{
  5454 			test.Printf(_L("General: r2 = %d, expected = %d\n"), r, KErrNone);
  5455 			}
  5456 
  5457 		GetAllPageInfo();
  5458 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5459 		if (freeInOthers && gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
  5460 			{
  5461 			test.Printf(_L("Fail: Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
  5462 										defragZoneID, r, r2, KErrNone);
  5463 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
  5464 			TEST_FAIL;
  5465 			}
  5466 		else
  5467 			{
  5468 			test.Printf(_L("Passed...\n"));	
  5469 			}
  5470 
  5471 		if (r == KErrNone)
  5472 			{
  5473 			r = Ldd.FreeAllFixedPages();
  5474 			}
  5475 					
  5476 		gTestThread.Close();	
  5477 		}
  5478 	
  5479 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  5480 	TestEnd();
  5481 
  5482 
  5483 
  5484 	test.Next(_L("Test5: Call function TRamDefragRequest::ClaimRamZone on specifc zone at the same time as calling on another zone"));	
  5485 	TestStart();	
  5486 	
  5487 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5488 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5489 	
  5490 	GetAllPageInfo();
  5491 	index = gZoneCount - 1;
  5492 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  5493 		{
  5494 		-- index;
  5495 		}
  5496 	defragZoneID = gZoneConfigArray[index].iZoneId;
  5497 	test.Printf(_L("index = 0x%x "), index);
  5498 
  5499 	TUint index2 = gZoneCount - 1;
  5500 	while (	index2 > 0 &&  
  5501 			(index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0))
  5502 		{
  5503 		-- index2;
  5504 		}
  5505 	TUint defragZoneID2 = gZoneConfigArray[index2].iZoneId;
  5506 	test.Printf(_L("index2 = %d\n"), index2);
  5507 
  5508 	if ((index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) || 
  5509 		(index2 == 0 && (index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0)))
  5510 		{
  5511 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5512 		}
  5513 	else
  5514 		{
  5515 		r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)(defragZoneID2));
  5516 		if (r != KErrNone)
  5517 			{
  5518 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5519 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5520 			TEST_FAIL;
  5521 			}
  5522 
  5523 		gTestThread.Logon(status);
  5524 		gTestThread.Resume();
  5525 		User::After(waitTime);
  5526 		
  5527 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
  5528 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  5529 
  5530 		User::WaitForRequest(status);
  5531 		r2 = status.Int();
  5532 		
  5533 		GetAllPageInfo();
  5534 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5535 		TUint freeInOthers2 = gTotalPageCount.iFreePages - gZoneUtilArray[index2].iFreePages;
  5536 		if ((freeInOthers && r != KErrNone && 
  5537 			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages) ||
  5538 			(freeInOthers2 && r != KErrNone && 
  5539 			gZoneUtilArray[index2].iAllocFixed != gZoneUtilArray[index2].iPhysPages))
  5540 			{
  5541 			test.Printf(_L("Fail: Zone ID 0x%x or Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
  5542 									defragZoneID, defragZoneID2, r, r2, KErrNone);
  5543 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5544 			TEST_FAIL;
  5545 			}
  5546 		else
  5547 			{
  5548 			test.Printf(_L("Passed...\n"));	
  5549 			}
  5550 		
  5551 		if (r == KErrNone)
  5552 			{
  5553 			r = Ldd.FreeAllFixedPages();
  5554 			}
  5555 
  5556 		if (r2 == KErrNone)
  5557 			{// Have to free from specific address as RAM zone claimed by other channel.
  5558 			addr = gZoneConfigArray[index2].iPhysBase;
  5559 			r = Ldd.FreeFromAddr(gZoneUtilArray[index2].iAllocFixed, addr);
  5560 			}		
  5561 
  5562 		gTestThread.Close();	
  5563 		}
  5564 	
  5565 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  5566 	TestEnd();
  5567 
  5568 
  5569 	test.Next(_L("Test6: Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the same time allocate pages to the zone"));	
  5570 	TestStart();		
  5571 	
  5572 	TInt pagesAlloc = 1; // Try and allocate just one page whilst attempting to claim the zone
  5573 
  5574 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5575 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5576 
  5577 	GetAllPageInfo();
  5578 	index = gZoneCount - 1;
  5579 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  5580 		{
  5581 		-- index;
  5582 		}
  5583 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  5584 		{
  5585 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5586 		}
  5587 	else
  5588 		{
  5589 		GetAllPageInfo();
  5590 		defragZoneID = gZoneConfigArray[index].iZoneId;
  5591 		TInt numPassed = 0;
  5592 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
  5593 			{
  5594 			r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
  5595 			if (r != KErrNone)
  5596 			{
  5597 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5598 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5599 			TEST_FAIL;
  5600 			}
  5601 
  5602 			gTestThread.Logon(status);
  5603 			gTestThread.Resume();
  5604 			test(status.Int() == KRequestPending);
  5605 			User::After(waitTime);
  5606 			
  5607 			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
  5608 
  5609 			TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  5610 			
  5611 			User::WaitForRequest(status);
  5612 			r2 = status.Int();
  5613 				
  5614 			GetAllPageInfo();
  5615 			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5616 			if (freeInOthers && r2 != KErrNone)
  5617 				{
  5618 				test.Printf(_L("Claim was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
  5619 				}
  5620 			else if (r2 == KErrNone && r != KErrNoMemory)
  5621 				{
  5622 				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone ID = 0x%x\n"), 
  5623 										r, KErrNoMemory, r2, KErrNone, defragZoneID);
  5624 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5625 				CLEANUP(Ldd.FreeAllFixedPages());
  5626 				// Free from address as a different channel claimed the RAM zone.
  5627 				addr = gZoneConfigArray[index].iPhysBase;
  5628 				CLEANUP(Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr));
  5629 				TEST_FAIL;
  5630 				}
  5631 			else
  5632 				{
  5633 				numPassed ++;
  5634 				}
  5635 			
  5636 			Ldd.FreeAllFixedPages();
  5637 		
  5638 			GetAllPageInfo();
  5639 			if (r2 == KErrNone)
  5640 				{// Free from address as a different channel claimed the RAM zone.
  5641 				addr = gZoneConfigArray[index].iPhysBase;
  5642 				r = Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr);
  5643 				}
  5644 			gTestThread.Close();
  5645 			}
  5646 		if (numPassed > 0)
  5647 			{
  5648 			test.Printf(_L("Passed...\n"));	
  5649 			}	
  5650 		}	
  5651 	
  5652 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5653 	TestEnd();
  5654 
  5655 
  5656 
  5657 	test.Next(_L("Test7: Call function TRamDefragRequest::ClaimRamZone on a zone that contains fixed and movable pages"));
  5658 	TestStart();
  5659 	
  5660 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5661 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5662 
  5663 	GetAllPageInfo();
  5664 	index = gZoneCount - 1;
  5665 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
  5666 			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
  5667 			{
  5668 			-- index;
  5669 			}
  5670 
  5671 	if (index == 0 && ((gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
  5672 			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3)))
  5673 		{
  5674 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5675 		}
  5676 	else
  5677 		{
  5678 		defragZoneID = gZoneConfigArray[index].iZoneId;
  5679 		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID));		
  5680 		TESTDEBUG(test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
  5681 						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable));	
  5682 
  5683 		
  5684 		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
  5685 		
  5686 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
  5687 		GetAllPageInfo();
  5688 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5689 		if (r != KErrNoMemory || 
  5690 			(freeInOthers && 
  5691 			(gZoneUtilArray[index].iAllocMovable || 
  5692 			gZoneUtilArray[index].iAllocDiscardable)))
  5693 			{
  5694 			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
  5695 							r, KErrNoMemory);
  5696 			CLEANUP(Ldd.FreeAllFixedPages());
  5697 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5698 			TEST_FAIL;
  5699 			}
  5700 		else
  5701 			{
  5702 			test.Printf(_L("Passed...\n"));
  5703 			}
  5704 		
  5705 		Ldd.FreeAllFixedPages();
  5706 		}
  5707 
  5708 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5709 	TestEnd();
  5710 
  5711 	test.Next(_L("Test8: Call function TRamDefragRequest::ClaimRamZone on every zone one after the other"));
  5712 	TestStart();
  5713 		
  5714 	Ldd.ResetDriver();
  5715 			
  5716 	for (index = 0; index < gZoneCount; index ++)
  5717 		{
  5718 		// Variables for DP ROM cache sizes
  5719 		TUint minCacheSize = 0;
  5720 		TUint maxCacheSize = 0;
  5721 		TUint currentCacheSize = 0;
  5722 
  5723 		if (gPagedRom)
  5724 			{
  5725 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  5726 			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5727 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5728 			// Calculate the maximum size of the DP cache 
  5729 			TUint minZoneFree = KMaxTUint;
  5730 			TUint maxDisFillBytes = 0;
  5731 			GetAllPageInfo();
  5732 			for (TUint i = 0; i < gZoneCount; i++)
  5733 				{
  5734 				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
  5735 				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
  5736 				if (minZoneFree > gZoneUtilArray[i].iFreePages)
  5737 					minZoneFree = gZoneUtilArray[i].iFreePages;
  5738 				}
  5739 			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
  5740 			maxDisFillBytes <<= gPageShift;
  5741 
  5742 			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
  5743 			if (r != KErrNone)
  5744 				{
  5745 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5746 				CLEANUP(ResetDPCache());
  5747 				TEST_FAIL;
  5748 				}
  5749 			
  5750 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  5751 			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5752 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5753 			if (currentCacheSize != maxDisFillBytes)
  5754 				{
  5755 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5756 				CLEANUP(ResetDPCache());
  5757 				TEST_FAIL;
  5758 				}
  5759 			
  5760 			GetAllPageInfo();
  5761 			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
  5762 
  5763 			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
  5764 			if (r != KErrNone)
  5765 				{
  5766 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5767 				CLEANUP(ResetDPCache());
  5768 				TEST_FAIL;
  5769 				}
  5770 			
  5771 			// Check all zones have no free pages.
  5772 			GetAllPageInfo();
  5773 			for (TUint i = 0; i < gZoneCount; i++)
  5774 				{
  5775 				if (gZoneUtilArray[i].iFreePages != 0)
  5776 					{
  5777 					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  5778 					CLEANUP(ResetDPCache());
  5779 					TEST_FAIL;
  5780 					}
  5781 				}
  5782 			}
  5783 		else
  5784 			{
  5785 			// Fragment the RAM with some movable pages
  5786 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
  5787 			FreeMovable(gChunkArray1, gChunkArraySize1);
  5788 			}
  5789 		
  5790 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  5791 		
  5792 		GetAllPageInfo();
  5793 		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
  5794 		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
  5795 		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
  5796 
  5797 		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
  5798 						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
  5799 						origMovInZone, origFreeInZone);
  5800 
  5801 
  5802 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
  5803 
  5804 		GetAllPageInfo();		
  5805 		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
  5806 		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
  5807 		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
  5808 	
  5809 		if (r == KErrNone)
  5810 			{
  5811 			r = Ldd.FreeAllFixedPages();
  5812 			}
  5813 		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  5814 		if (!unknownPages && movablePages && freeInOtherZones)
  5815 			{
  5816 			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
  5817 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5818 			CLEANUP(ResetDPCache());
  5819 			TEST_FAIL;
  5820 			}
  5821 
  5822 		if (origUnknownInZone != unknownPages)
  5823 			{
  5824 			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
  5825 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5826 			CLEANUP(ResetDPCache());
  5827 			TEST_FAIL;
  5828 			}
  5829 
  5830 		if (!gPagedRom)
  5831 			{
  5832 			if (!unknownPages && freeInOtherZones && discPages)
  5833 				{
  5834 				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
  5835 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5836 				CLEANUP(ResetDPCache());
  5837 				TEST_FAIL;
  5838 				}
  5839 			}
  5840 		else
  5841 			{
  5842 			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
  5843 			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  5844 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  5845 			// The the discardable pages should have been discarded or moved unless 
  5846 			// there is no room in the other zones and the cache size is already at its minimum
  5847 			if (!unknownPages && discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
  5848 				{
  5849 				test.Printf(_L("Fail: Zone ID 0x%x all the discardable pages haven't been moved\n"), zoneID);
  5850 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5851 				CLEANUP(ResetDPCache());
  5852 				TEST_FAIL;
  5853 				}
  5854 			}
  5855 		test.Printf(_L("Passed...\n"));
  5856 		}
  5857 	// TestEnd() will cleanup what's required.
  5858 	TestEnd();
  5859 	test.End();
  5860 	return 0;
  5861 	}
  5862 
  5863 
  5864 //
  5865 // TestCancelDefrag
  5866 //
  5867 //-----------------------------------------------------------------------------------------------------------------------------------------
  5868 //! @SYMTestCaseID				KBASE-t_ramdefrag-0533
  5869 //! @SYMTestType				CIT
  5870 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::CancelDefrag()
  5871 //! @SYMPREQ					PREQ308
  5872 //! @SYMTestPriority			High
  5873 //! @SYMTestActions				
  5874 //! 	1.	Fragment the memory. Following this, start a general defrag and cancel it
  5875 //! 	2.	Fragment the memory. Following this, start a zone defrag and cancel it 
  5876 //! 	3.	Fragment the memory. Following this, start a claim zone and cancel it
  5877 //! 
  5878 //! @SYMTestExpectedResults
  5879 //! 	1.	KErrCancel
  5880 //! 	2.	KErrCancel
  5881 //! 	3.	KErrCancel
  5882 //-----------------------------------------------------------------------------------------------------------------------------------------
  5883 TInt TestCancelDefrag()
  5884 	{
  5885 	TInt r = 0;
  5886 	TUint defragZoneID = 0;
  5887 
  5888 	test.Start(_L("Test1: Call general defrag and cancel it"));	
  5889 	TestStart();	
  5890 	
  5891 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5892 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5893 	
  5894 	r = Ldd.CheckCancel(DEFRAG_TYPE_GEN);
  5895 
  5896 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  5897 	if (r != KErrCancel)
  5898 		{
  5899 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
  5900 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5901 		TEST_FAIL;
  5902 		}
  5903 	else
  5904 		{
  5905 		test.Printf(_L("Passed...\n"));	
  5906 		}
  5907 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5908 	TestEnd();
  5909 
  5910 
  5911 	test.Next(_L("Test2: Call zone defrag and cancel it"));	
  5912 	TestStart();		
  5913 	
  5914 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5915 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5916 
  5917 	GetAllPageInfo();
  5918 	TUint index = gZoneCount - 1;
  5919 	while (index > 0 && 
  5920 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
  5921 			gZoneUtilArray[index].iAllocFixed !=0 ||
  5922 			gZoneUtilArray[index].iAllocUnknown !=0))
  5923 		{	
  5924 		-- index; 
  5925 		}
  5926 	if (index == 0 && 
  5927 		(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
  5928 		gZoneUtilArray[index].iAllocFixed !=0 ||
  5929 		gZoneUtilArray[index].iAllocUnknown !=0))
  5930 		{
  5931 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5932 		}
  5933 	else
  5934 		{		
  5935 		defragZoneID = gZoneConfigArray[index].iZoneId;
  5936 		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
  5937 
  5938 		r = Ldd.CheckCancel(DEFRAG_TYPE_EMPTY, defragZoneID);
  5939 		
  5940 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  5941 		if (r != KErrCancel)
  5942 			{
  5943 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
  5944 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5945 			TEST_FAIL;
  5946 			}	
  5947 		else
  5948 			{
  5949 			test.Printf(_L("Passed...\n"));	
  5950 			}
  5951 		}	
  5952 
  5953 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  5954 	TestEnd();
  5955 
  5956 
  5957 	test.Next(_L("Test3: Call Claim RAM Zone and cancel it"));	
  5958 	TestStart();
  5959 	
  5960 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  5961 	FreeMovable(gChunkArray1, gChunkArraySize1);
  5962 
  5963 	GetAllPageInfo();
  5964 	index = gZoneCount - 1;	
  5965 	while (index > 0 && 
  5966 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
  5967 			gZoneUtilArray[index].iAllocFixed !=0 ||
  5968 			gZoneUtilArray[index].iAllocUnknown !=0))
  5969 		{
  5970 		-- index;
  5971 		}
  5972 	defragZoneID = gZoneConfigArray[index].iZoneId;
  5973 	TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
  5974 
  5975 	if (index == 0 && 
  5976 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
  5977 			gZoneUtilArray[index].iAllocFixed !=0 ||
  5978 			gZoneUtilArray[index].iAllocUnknown !=0))
  5979 		{
  5980 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  5981 		}
  5982 	else
  5983 		{
  5984 		r = Ldd.CheckCancel(DEFRAG_TYPE_CLAIM, defragZoneID);
  5985 
  5986 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  5987 		if (r != KErrCancel)
  5988 			{
  5989 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
  5990 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  5991 			TEST_FAIL;
  5992 			}	
  5993 		else
  5994 			{
  5995 			test.Printf(_L("Passed...\n"));	
  5996 			}
  5997 		}
  5998 
  5999 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  6000 	TestEnd(); 
  6001 
  6002 	test.End();
  6003 	return 0;
  6004 	}
  6005 
  6006 //
  6007 // TestZoneAllocNoAffect
  6008 //
  6009 // Ensures that fixed page allocations do not affect the movable
  6010 // or discardable page allocations
  6011 //
  6012 TInt TestZoneAllocNoAffect(TInt aZoneAllocType, TInt aPageType)
  6013 	{
  6014 	TInt retVal = KErrNone;
  6015 	TInt r = KErrNone;
  6016 	TInt mostPrefIndex = 0;
  6017 	TUint leastPrefIndex = 0;
  6018 	TUint leastPrefZoneID = 0;
  6019 
  6020 	TestStart();
  6021 	gChunkArray1 = new RChunk;
  6022 	gChunkArraySize1 = 1;
  6023 	switch(aPageType)
  6024 		{
  6025 		case BEST_MOVABLE:
  6026 			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of movable pages\n"));
  6027 			break;
  6028 
  6029 		case BEST_DISCARDABLE:
  6030 			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of discardable pages\n"));
  6031 			if (!gPagedRom)
  6032 				{
  6033 				test.Printf(_L("Not a paged ROM - Skipping...\n"));
  6034 				goto skipSetup;
  6035 				}
  6036 			break;
  6037 		}
  6038 	// Fist find the zone that movable page allocations should go into
  6039 	// Getting the best movable will be the same for discardable as well
  6040 	mostPrefIndex = GetBestZone(aPageType);
  6041 	if (mostPrefIndex == KErrNotFound)
  6042 		{
  6043 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  6044 		goto skipSetup;
  6045 		}
  6046 
  6047 	// Get the least pref zone to zone alloc into and ensure it has free pages
  6048 	GetPrefOrder();
  6049 	leastPrefIndex = gPrefArray[gZoneCount - 1];
  6050 	if (gZoneConfigArray[mostPrefIndex].iPref == gZoneConfigArray[leastPrefIndex].iPref)
  6051 		{
  6052 		test.Printf(_L("Zones with same preference - Skipping...\n"));
  6053 		goto skipSetup;
  6054 		}
  6055 	if (leastPrefIndex == (TUint)mostPrefIndex ||
  6056 		gZoneUtilArray[leastPrefIndex].iFreePages == 0)
  6057 		{
  6058 		test.Printf(_L("leastPrefIndex = mostPrefIndex or leastPrefIndex(%d) / mostPrefIndex(%d) has 0 free - Skipping test step...\n"), 
  6059 						leastPrefIndex, mostPrefIndex);
  6060 		goto skipSetup;
  6061 		}
  6062 
  6063 
  6064 	// Zone alloc 1 fixed page into the least preferable zone
  6065 	leastPrefZoneID = gZoneConfigArray[leastPrefIndex].iZoneId;	
  6066 	GetOriginalPageCount();
  6067 	switch(aZoneAllocType)
  6068 		{
  6069 		case Z_ALLOC_DISC:
  6070 			{
  6071 			r = Ldd.ZoneAllocDiscontiguous(leastPrefZoneID, 1);
  6072 			break;
  6073 			}
  6074 
  6075 		case Z_ALLOC_CONTIG:
  6076 			{			
  6077 			r = Ldd.ZoneAllocContiguous(leastPrefZoneID, gPageSize);
  6078 			break;
  6079 			}
  6080 		}
  6081 	GetAllPageInfo();
  6082 	if (r != KErrNone ||
  6083 		gZoneUtilArray[leastPrefIndex].iAllocFixed <= gOriginalPageCountArray[leastPrefIndex].iAllocFixed)
  6084 		{
  6085 		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d\n"), leastPrefIndex, r);
  6086 		goto skipSetup;
  6087 		}
  6088 	test.Printf(_L("leastPrefIndex = %d mostPrefIndex = %d\n"), leastPrefIndex, mostPrefIndex);
  6089 	switch(aPageType)
  6090 		{
  6091 		case BEST_MOVABLE:
  6092 			// now allocate 1 movable page
  6093 			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
  6094 			if (r != KErrNone)
  6095 				{
  6096 				test.Printf(_L("Failed to allocate 1 movable page r = %d\n"), r);
  6097 				goto skipSetup;
  6098 				}
  6099 			break;
  6100 			
  6101 		case BEST_DISCARDABLE:
  6102 			// now allocate 1 discardable page
  6103 			TInt disPages;					
  6104 			TUint disBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
  6105 			r = AllocDiscardable(disPages, disBytes);
  6106 			if (r != KErrNone)
  6107 				{
  6108 				test.Printf(_L("Failed to allocate 1 discardable page r = %d\n"), r);
  6109 				goto skipSetup;
  6110 				}
  6111 			break;
  6112 		}
  6113 	
  6114 	GetAllPageInfo();
  6115 	switch(aPageType)
  6116 		{
  6117 		case BEST_MOVABLE:
  6118 			if (gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
  6119 				gOriginalPageCountArray[leastPrefIndex].iAllocMovable < gZoneUtilArray[leastPrefIndex].iAllocMovable)
  6120 				{
  6121 				test.Printf(_L("FAIL: mostPrefIndex(%d) origMov 0x%x curMov 0x%x leastPrefIndex(%d) origMov 0x%x curMov 0x%x\n"), 
  6122 								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
  6123 								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable);
  6124 				CLEANUP(Ldd.FreeAllFixedPages());
  6125 				retVal = KErrGeneral;
  6126 				}
  6127 			else
  6128 				{
  6129 				test.Printf(_L("Passed...\n"));
  6130 				}
  6131 			break;
  6132 
  6133 			
  6134 		case BEST_DISCARDABLE:
  6135 			if (gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
  6136 				gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[leastPrefIndex].iAllocDiscardable)
  6137 				{
  6138 				test.Printf(_L("FAIL: mostPrefIndex(%d) origDis 0x%x curDis 0x%x leastPrefIndex(%d) origDis 0x%x curDis 0x%x\n"), 
  6139 								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable, gZoneUtilArray[mostPrefIndex].iAllocDiscardable, 
  6140 								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
  6141 				CLEANUP(Ldd.FreeAllFixedPages());
  6142 				retVal = KErrGeneral;
  6143 				}
  6144 			else
  6145 				{
  6146 				test.Printf(_L("Passed...\n"));
  6147 				}
  6148 			break;
  6149 		}
  6150 	
  6151 	// This will clean up any fixed pages allocated.
  6152 skipSetup:
  6153 	TestEnd();
  6154 	return retVal;
  6155 	}
  6156 		
  6157 //
  6158 // TestZoneAllocContiguous
  6159 //
  6160 //---------------------------------------------------------------------------------------------------------------------
  6161 //! @SYMTestCaseID				KBASE-t_ramdefrag-0535
  6162 //! @SYMTestType				CIT
  6163 //! @SYMTestCaseDesc			Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam().
  6164 //! @SYMPREQ					PREQ308
  6165 //! @SYMTestPriority			High
  6166 //! @SYMTestActions				
  6167 //! 	1.	Call function with a valid aZoneID.  
  6168 //! 	2.	Call function with an invalid aZoneID
  6169 //! 	3.	Call function with aSize > zone size 
  6170 //!		4.	Call multiple RAM zone overload of the function with a mix of valid and invalid IDs.
  6171 //!		5.	Call multiple RAM zone overload of the function with contiguous RAM zones and attempt
  6172 //!			to allocate over both RAM zones.
  6173 //!		6.	Call function on a RAM zone that has some non-DP pages allocated into it and ask
  6174 //!			for the whole RAM zone to be filled with fixed pages.
  6175 //!		7.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 contiguous fixed page 
  6176 //!			in the least preferable zone. Following this allocate 1 movable page
  6177 //!		8.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 contiguous fixed 
  6178 //!			page in the least preferable zone. Following this allocate 1 discardable page
  6179 //! 
  6180 //! @SYMTestExpectedResults
  6181 //! 	1.	KErrNone
  6182 //! 	2.	KErrArgument 
  6183 //! 	3.	KErrArgument
  6184 //!		4.	KErrArgument
  6185 //!		5.	KErrNone
  6186 //!		6.	KErrNoMemory
  6187 //!		7.	Movable pages are allocated into zone mostPref.
  6188 //!		8.	Discardable pages are allocated into zone mostPref
  6189 //---------------------------------------------------------------------------------------------------------------------
  6190 TInt TestZoneAllocContiguous()
  6191 	{
  6192 	TInt r = 0;
  6193 	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
  6194 	TestStart();	
  6195 		
  6196 	const TUint KAllocPages = 2;
  6197 	
  6198 	GetOriginalPageCount();
  6199 	GetAllPageInfo();
  6200 	TUint index = gZoneCount - 1;
  6201 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
  6202 		{
  6203 		-- index;
  6204 		}
  6205 	TUint zoneID = gZoneConfigArray[index].iZoneId;
  6206 	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
  6207 		{
  6208 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  6209 		}
  6210 	else
  6211 		{
  6212 		TUint allocBytes = KAllocPages << gPageShift;
  6213 		r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
  6214 		GetAllPageInfo();
  6215 		if (r == KErrNone && 
  6216 			gZoneUtilArray[index].iAllocFixed - gOriginalPageCountArray[index].iAllocFixed == KAllocPages)
  6217 			{
  6218 			test.Printf(_L("Pass: Correct number of fixed pages allocated to zone ID 0x%x\n"),zoneID);
  6219 			}
  6220 		else
  6221 			{
  6222 			test.Printf(_L("Fail: r = %d, expected = %d, or number of pages allocated is not expected\n"), 
  6223 							r, KErrNone);
  6224 			CLEANUP(Ldd.FreeAllFixedPages());
  6225 			TEST_FAIL;
  6226 			}
  6227 
  6228 		Ldd.FreeAllFixedPages();
  6229 		}
  6230 	TestEnd();
  6231 
  6232 
  6233 	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
  6234 	TestStart();	
  6235 	
  6236 	zoneID = KInvalidZoneID; 
  6237  
  6238 	TESTDEBUG(test.Printf(_L("zoneID = 0x%x\n"), zoneID));
  6239 	
  6240 	r = Ldd.ZoneAllocContiguous(zoneID, gPageSize);
  6241 	GetAllPageInfo();
  6242 	if (r == KErrArgument)
  6243 		{
  6244 		test.Printf(_L("Pass: Correct return value\n"));
  6245 		}
  6246 	else
  6247 		{
  6248 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  6249 		CLEANUP(Ldd.FreeAllFixedPages());
  6250 		TEST_FAIL;
  6251 		}
  6252 	Ldd.FreeAllFixedPages();
  6253 	TestEnd();
  6254 
  6255 
  6256 	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() with aSize > zone size"));	
  6257 	TestStart();	
  6258 	
  6259 	index = gZoneCount - 1;
  6260 	zoneID = gZoneConfigArray[index].iZoneId;
  6261 
  6262 	TUint allocBytes = (gZoneUtilArray[index].iPhysPages + 5)  << gPageShift;
  6263 	
  6264 	r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
  6265 	GetAllPageInfo();
  6266 	if (r == KErrArgument)
  6267 		{
  6268 		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
  6269 		}
  6270 	else
  6271 		{
  6272 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  6273 		CLEANUP(Ldd.FreeAllFixedPages());
  6274 		TEST_FAIL;
  6275 		}
  6276 	Ldd.FreeAllFixedPages();
  6277 	TestEnd();
  6278 
  6279 	const TUint KMultiZoneIds = 10;
  6280 	TUint* zoneIdArray = new TUint[KMultiZoneIds];
  6281 	test_NotNull(zoneIdArray);
  6282 
  6283 	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
  6284 	TestStart();
  6285 
  6286 	index = gZoneCount - 1;
  6287 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
  6288 		{
  6289 		-- index;
  6290 		}
  6291 	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
  6292 		{
  6293 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  6294 		}
  6295 	else
  6296 		{
  6297 		TUint zoneIdSize = 2;
  6298 		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
  6299 		zoneIdArray[1] = KInvalidZoneID;
  6300 		TUint allocBytes = KAllocPages << gPageShift;
  6301 		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIdSize, allocBytes);
  6302 		if (r != KErrArgument)
  6303 			{
  6304 			CLEANUP(Ldd.FreeAllFixedPages());
  6305 			TEST_FAIL;
  6306 			}
  6307 		else
  6308 			{
  6309 			test.Printf(_L("Pass: Correct return value\n"));
  6310 			}
  6311 		}
  6312 
  6313 	TestEnd();
  6314 
  6315 	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
  6316 	TestStart();
  6317 	// Attempt to find to physically contiguous RAM zones where higher addressed
  6318 	// one is empty, relies on RAM zones are returned by HAL functions in 
  6319 	// ascending physical address order.
  6320 	GetAllPageInfo();
  6321 	TBool zonesFound = EFalse;
  6322 	index = gZoneCount - 1;
  6323 	for (; index > 1; index--)
  6324 		{
  6325 		if (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages &&
  6326 			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages &&
  6327 			gZoneConfigArray[index].iPhysBase - 1 == gZoneConfigArray[index-1].iPhysEnd)
  6328 			{
  6329 			zonesFound = ETrue;
  6330 			break;
  6331 			}
  6332 		}
  6333 
  6334 	if (!zonesFound)
  6335 		{
  6336 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6337 		}
  6338 	else
  6339 		{
  6340 		// Allocate one page more than the first RAM zone to force the allocation
  6341 		// to spread over both RAM zones.
  6342 		TUint allocPages = gZoneUtilArray[index-1].iPhysPages + 1;
  6343 		TUint allocBytes = allocPages << gPageShift;
  6344 
  6345 		// Attempt to find a zone that has less free pages than the allocation
  6346 		// size so we can test that the method continues past too full RAM zones.
  6347 		zonesFound = EFalse;
  6348 		TUint noAllocZone = 0;
  6349 		for (; noAllocZone < gZoneCount; noAllocZone++)
  6350 			{
  6351 			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
  6352 				noAllocZone != index && noAllocZone != index-1)
  6353 				{
  6354 				zonesFound = ETrue;
  6355 				break;
  6356 				} 
  6357 			}
  6358 		TUint zoneIds = 2;
  6359 		if (!zonesFound)
  6360 			{
  6361 			zoneIdArray[0] = gZoneConfigArray[index-1].iZoneId;
  6362 			zoneIdArray[1] = gZoneConfigArray[index].iZoneId;
  6363 			}
  6364 		else
  6365 			{// Have a zone that won't meet the allocation so use it
  6366 			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
  6367 			zoneIds++;
  6368 			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
  6369 			zoneIdArray[1] = gZoneConfigArray[index-1].iZoneId;
  6370 			zoneIdArray[2] = gZoneConfigArray[index].iZoneId;
  6371 			}
  6372 
  6373 		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIds, allocBytes);
  6374 
  6375 		GetAllPageInfo();
  6376 		if (r != KErrNone || 
  6377 			gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
  6378 			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages)
  6379 			{// The allocation failed.
  6380 			CLEANUP(Ldd.FreeAllFixedPages());
  6381 			TEST_FAIL;
  6382 			}
  6383 		else
  6384 			{
  6385 			test.Printf(_L("Passed...\n"));
  6386 			}
  6387 		}
  6388 	// TestEnd() will free the allocation of fixed pages.
  6389 	TestEnd();
  6390 
  6391 	test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when appropriate"));
  6392 	TestStart();
  6393 	// Attempt to find a RAM zone with some non-discarable pages allocated into it.
  6394 	// (At time of writing discardable pages wouldn't be discarded on demand by 
  6395 	// this function but this may be changed in the future as discontiguous case does that).
  6396 	GetAllPageInfo();
  6397 	zonesFound = EFalse;
  6398 	index = gZoneCount - 1;
  6399 	for (; index > 0; index--)
  6400 		{
  6401 		if (gZoneUtilArray[index].iFreePages && 
  6402 			(gZoneUtilArray[index].iAllocMovable || gZoneUtilArray[index].iAllocFixed))
  6403 			{
  6404 			zonesFound = ETrue;
  6405 			break;
  6406 			}
  6407 		}
  6408 	
  6409 	if (!zonesFound)
  6410 		{
  6411 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));		
  6412 		}
  6413 	else
  6414 		{
  6415 		// Attempt to allocate the whole of the RAM zone.
  6416 		GetOriginalPageCount();
  6417 		TUint allocBytes = gZoneConfigArray[index].iPhysPages << gPageShift;
  6418 		r = Ldd.ZoneAllocContiguous(gZoneConfigArray[index].iZoneId, allocBytes);
  6419 
  6420 		// The allocation should have failed and no pages should have
  6421 		// been allocated.
  6422 		GetAllPageInfo();
  6423 		if (r != KErrNoMemory ||
  6424 			gOriginalPageCountArray[index].iFreePages > gZoneUtilArray[index].iFreePages)
  6425 			{
  6426 			CLEANUP(Ldd.FreeAllFixedPages());
  6427 			TEST_FAIL;
  6428 			}
  6429 		else
  6430 			{
  6431 			test.Printf(_L("Pass: Correct return value\n"));
  6432 			}
  6433 		}
  6434 	// TestEnd() will free the allocation of fixed pages.
  6435 	TestEnd();
  6436 	delete[] zoneIdArray;
  6437 
  6438 	
  6439 	test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of movable pages"));
  6440 	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_MOVABLE);
  6441 	if (r != KErrNone)
  6442 		{
  6443 		TEST_FAIL;
  6444 		}
  6445 
  6446 	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of discardable pages"));
  6447 	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_DISCARDABLE);
  6448 	if (r != KErrNone)
  6449 		{
  6450 		TEST_FAIL;
  6451 		}
  6452 
  6453 	test.End();
  6454 	return KErrNone;
  6455 	}
  6456 
  6457 
  6458 //
  6459 // TestZoneAllocDiscontiguous
  6460 //
  6461 //---------------------------------------------------------------------------------------------------------------------
  6462 //! @SYMTestCaseID				KBASE-t_ramdefrag-0536
  6463 //! @SYMTestType				CIT
  6464 //! @SYMTestCaseDesc			Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam().
  6465 //! @SYMPREQ					PREQ308
  6466 //! @SYMTestPriority			High
  6467 //! @SYMTestActions				
  6468 //! 	1.	Call function with a valid aZoneID.  
  6469 //! 	2.	Call function with an invalid aZoneID
  6470 //! 	3.	Call function with aNumPages > zone size 
  6471 //!		4.	Call multiple RAM zone overload of function with a mix of valid and invalid IDs
  6472 //!		5.	Call multiple RAM zone overload of function so that the allocation will have to
  6473 //!			span multiple RAM zones.
  6474 //!		6.	Call function with memory full with DP cache that has reached it's 
  6475 //!			minimum cache size.
  6476 //!		7.	Call function with memory not quite full with DP cache that has reached it's 
  6477 //!			minimum cache size and with the specified RAM zone full.
  6478 //!		8.	Call function to allocate a whole RAM zone on a RAM zone that has non-discardable
  6479 //!			pages already allocated into it.
  6480 //!		9.	Call function to allocate one less than the whole RAM zone on a RAM zone that has movable 
  6481 //!			pages allocated.
  6482 //!		10.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 discontiguous fixed page 
  6483 //!			in the least preferable zone. Following this allocate 1 movable page
  6484 //!		11.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 discontiguous fixed 
  6485 //!			page in the least preferable zone. Following this allocate 1 discardable page
  6486 //! 
  6487 //! @SYMTestExpectedResults
  6488 //! 	1.	KErrNone
  6489 //! 	2.	KErrArgument
  6490 //! 	3.	KErrArgument
  6491 //!		4.	KErrArgument
  6492 //!		5.	KErrNone
  6493 //!		6.	KErrNoMemory
  6494 //!		7.	KErrNone
  6495 //!		8. 	KErrNoMemory
  6496 //!		9.	KErrNone (i.e. the movable pages are shifted out of the way but only those that need to be moved).
  6497 //!		10.	Movable pages are allocated into zone mostPref.
  6498 //!		11.	Discardable pages are allocated into zone mostPref
  6499 //---------------------------------------------------------------------------------------------------------------------
  6500 TInt TestZoneAllocDiscontiguous()
  6501 	{
  6502 	TInt r = KErrNone;
  6503 	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
  6504 	TestStart();	
  6505 	
  6506 	const TUint KAllocPages = 5;
  6507 	// Detemine how many extra pages the kernel heap may grow by
  6508 	// as these may need to be accounted for.
  6509 	TUint fixedOverhead = Ldd.GetAllocDiff(KAllocPages);
  6510 
  6511 	GetOriginalPageCount();
  6512 	GetAllPageInfo();
  6513 	TUint index = gZoneCount - 1;
  6514 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
  6515 		{
  6516 		-- index;
  6517 		}
  6518 
  6519 	if (gZoneUtilArray[index].iFreePages < KAllocPages || gTotalPageCount.iFreePages < KAllocPages + fixedOverhead)
  6520 		{
  6521 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  6522 		}
  6523 	else
  6524 		{
  6525 		// Allocate KAllocPages discontiguous fixed pages into RAM zone zoneID 
  6526 		// and verfiy that the pages were allocated to the correct zone, allow for
  6527 		// extra fixed pages to be allocated as the kernel heap may grow.
  6528 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  6529 		r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
  6530 
  6531 		GetAllPageInfo();
  6532 		if ((r == KErrNone) && 
  6533 			(gZoneUtilArray[index].iAllocFixed >= gOriginalPageCountArray[index].iAllocFixed + KAllocPages))
  6534 			{
  6535 			test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
  6536 			}
  6537 		else
  6538 			{
  6539 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  6540 			CLEANUP(Ldd.FreeAllFixedPages());
  6541 			TEST_FAIL;
  6542 			}
  6543 		Ldd.FreeAllFixedPages();
  6544 		}	
  6545 	TestEnd();
  6546 
  6547 
  6548 	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
  6549 	TestStart();	
  6550 	
  6551 	TUint zoneID = KInvalidZoneID;
  6552 	
  6553 	r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
  6554 	GetAllPageInfo();
  6555 	if (r == KErrArgument)
  6556 		{
  6557 		test.Printf(_L("Pass: Correct return value\n"));
  6558 		}
  6559 	else
  6560 		{
  6561 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  6562 		CLEANUP(Ldd.FreeAllFixedPages());
  6563 		TEST_FAIL;
  6564 		}
  6565 	Ldd.FreeAllFixedPages();
  6566 	TestEnd();
  6567 
  6568 	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() when aNumPages > zone size"));	
  6569 	TestStart();	
  6570 	
  6571 	GetAllPageInfo();
  6572 	index = gZoneCount - 1;
  6573 	zoneID = gZoneConfigArray[index].iZoneId;
  6574 	
  6575 	TUint allocPages = gZoneUtilArray[index].iPhysPages + 1;
  6576 	r = Ldd.ZoneAllocDiscontiguous(zoneID, allocPages);
  6577 	
  6578 	GetAllPageInfo();
  6579 	if (r == KErrArgument)
  6580 		{
  6581 		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
  6582 		}
  6583 	else
  6584 		{
  6585 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
  6586 		CLEANUP(Ldd.FreeAllFixedPages());
  6587 		TEST_FAIL;
  6588 		}
  6589 	Ldd.FreeAllFixedPages();
  6590 	TestEnd();
  6591 
  6592 	const TUint KMultiZoneIds = 10;
  6593 	TUint* zoneIdArray = new TUint[KMultiZoneIds];
  6594 	test_NotNull(zoneIdArray);
  6595 
  6596 	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
  6597 	TestStart();
  6598 
  6599 	TBool zonesFound = EFalse;
  6600 	index = gZoneCount - 1;
  6601 	for (; index > 0; index--)
  6602 		{
  6603 		if (gZoneUtilArray[index].iFreePages >= KAllocPages)
  6604 			{
  6605 			zonesFound = ETrue;
  6606 			break;
  6607 			}
  6608 		}
  6609 	if (!zonesFound)
  6610 		{
  6611 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  6612 		}
  6613 	else
  6614 		{
  6615 		TUint zoneIdSize = 2;
  6616 		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
  6617 		zoneIdArray[1] = KInvalidZoneID;
  6618 		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIdSize, KAllocPages);
  6619 		if (r != KErrArgument)
  6620 			{// Make sure we cleanup.
  6621 			CLEANUP(Ldd.FreeAllFixedPages());
  6622 			TEST_FAIL;
  6623 			}
  6624 		else
  6625 			{
  6626 			test.Printf(_L("Passed...\n"));	
  6627 			}
  6628 		}
  6629 
  6630 	TestEnd();
  6631 
  6632 	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
  6633 	TestStart();
  6634 	zonesFound = EFalse;
  6635 	TUint zonesCount = 0;
  6636 	const TUint KTest5Zones = 2;
  6637 	TUint zoneIndices[KTest5Zones];
  6638 	allocPages = 0;
  6639 
  6640 	// Attempt to find KTest5Zones RAM zones with some free pages,
  6641 	// search in reverse preference order to reduce chances of kernel heap pages
  6642 	// being allocated into the RAM zones under test.
  6643 	GetPrefOrder();
  6644 	index = gZoneCount - 1;
  6645 	for (; index > 0; index--)
  6646 		{
  6647 		TUint prefIndex = gPrefArray[index];
  6648 		if (gZoneUtilArray[prefIndex].iFreePages != 0)
  6649 			{
  6650 			allocPages += gZoneUtilArray[prefIndex].iFreePages;
  6651 			zoneIndices[zonesCount++] = prefIndex;
  6652 			if (zonesCount == KTest5Zones)
  6653 				{
  6654 				zonesFound = ETrue;
  6655 				break;
  6656 				}
  6657 			}
  6658 		}
  6659 
  6660 	if (!zonesFound)
  6661 		{
  6662 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6663 		}
  6664 	else
  6665 		{
  6666 		// Attempt to find a zone that has less free pages than the allocation
  6667 		// size so we can test that the method continues past too full RAM zones.
  6668 		zonesFound = EFalse;
  6669 		TUint noAllocZone = 0;
  6670 		for (; noAllocZone < gZoneCount; noAllocZone++)
  6671 			{
  6672 			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
  6673 				noAllocZone != zoneIndices[0] && noAllocZone != zoneIndices[1])
  6674 				{
  6675 				zonesFound = ETrue;
  6676 				break;
  6677 				}
  6678 			}
  6679 		
  6680 		if (gPagedRom)
  6681 			{// Fill memory with DP pages to test the allocation will discard
  6682 			// pages when necessary.
  6683 			TInt discard;
  6684 			r = AllocDiscardable(discard);
  6685 			GetAllPageInfo();
  6686 			if (r != KErrNone ||
  6687 				gZoneUtilArray[zoneIndices[0]].iFreePages != 0 || 
  6688 				gZoneUtilArray[zoneIndices[1]].iFreePages != 0)
  6689 				{
  6690 				test.Printf(_L("r %d\n"), r);
  6691 				CLEANUP(ResetDPCache());
  6692 				TEST_FAIL;
  6693 				}			
  6694 			}
  6695 
  6696 		TUint zoneIds = KTest5Zones;
  6697 		if (!zonesFound)
  6698 			{
  6699 			zoneIdArray[0] = gZoneConfigArray[zoneIndices[0]].iZoneId;
  6700 			zoneIdArray[1] = gZoneConfigArray[zoneIndices[1]].iZoneId;
  6701 			}
  6702 		else
  6703 			{// Have a zone that won't meet the allocation so use it
  6704 			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
  6705 			zoneIds++;
  6706 			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
  6707 			zoneIdArray[1] = gZoneConfigArray[zoneIndices[0]].iZoneId;
  6708 			zoneIdArray[2] = gZoneConfigArray[zoneIndices[1]].iZoneId;
  6709 			}
  6710 
  6711 		// Adjust the allocation size for any kernel heap pages that may be
  6712 		// required as they may get allocated into the RAM zones under test.
  6713 		allocPages -= Ldd.GetAllocDiff(allocPages);
  6714 
  6715 		GetOriginalPageCount();
  6716 		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIds, allocPages);
  6717 
  6718 		GetAllPageInfo();
  6719 		if (r != KErrNone ||
  6720 			gZoneUtilArray[zoneIndices[0]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[0]].iAllocFixed ||
  6721 			gZoneUtilArray[zoneIndices[1]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[1]].iAllocFixed)
  6722 			{// The allocation failed.
  6723 			CLEANUP(Ldd.FreeAllFixedPages());
  6724 			TEST_FAIL;
  6725 			}
  6726 		else
  6727 			{
  6728 			test.Printf(_L("Passed...\n"));	
  6729 			}
  6730 		}
  6731 	// TestEnd() will free the allocation of fixed pages.
  6732 	TestEnd();
  6733 
  6734 
  6735 	if (gPagedRom)
  6736 		{
  6737 		test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when DP minimum cache size is hit"));
  6738 		TestStart();
  6739 		GetPrefOrder();
  6740 		TUint zoneFreePages = 0;
  6741 		index = gZoneCount - 1;
  6742 		for (; index > 0; index--)
  6743 			{
  6744 			TUint prefIndex = gPrefArray[index];
  6745 			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
  6746 			if (zoneFreePages > 2)
  6747 				{
  6748 				index = prefIndex;
  6749 				zonesFound = ETrue;
  6750 				break;
  6751 				}
  6752 			}
  6753 
  6754 		if (!zonesFound)
  6755 			{
  6756 			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6757 			}
  6758 		else
  6759 			{
  6760 			// Fill the RAM with DP cache pages and up the minimum cache size so
  6761 			// that the allocation will fail.
  6762 			TInt discardBytes;
  6763 			TInt r = AllocDiscardable(discardBytes, KMaxTUint64, (zoneFreePages - 1) << gPageShift);
  6764 			test_KErrNone(r);
  6765 
  6766 			// Ensure that the RAM zone under test is full.
  6767 			GetAllPageInfo();
  6768 			if (gZoneUtilArray[index].iFreePages != 0)
  6769 				{
  6770 				CLEANUP(ResetDPCache());
  6771 				TEST_FAIL;
  6772 				}
  6773 
  6774 			r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, zoneFreePages);
  6775 
  6776 			if (r != KErrNoMemory)
  6777 				{
  6778 				CLEANUP(Ldd.FreeAllFixedPages());
  6779 				TEST_FAIL;
  6780 				}
  6781 			else
  6782 				{
  6783 				test.Printf(_L("Passed...\n"));	
  6784 				}
  6785 			}
  6786 		TestEnd();
  6787 
  6788 		test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() replaces DP cache when DP minimum cache size is hit"));
  6789 		TestStart();
  6790 		gChunkArray1 = new RChunk;
  6791 		gChunkArraySize1 = 1;
  6792 		GetPrefOrder();
  6793 		zonesFound = EFalse;
  6794 		for (index = gZoneCount - 1; index > 0 && !zonesFound; index--)
  6795 			{
  6796 			TUint prefIndex = gPrefArray[index];
  6797 			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
  6798 			if (zoneFreePages > 1)
  6799 				{
  6800 				// Check there is at least one free page in the other RAM zones.
  6801 				TUint i = 0;
  6802 				for (; i < gZoneCount; i++)
  6803 					{
  6804 					if (i != prefIndex && gZoneUtilArray[i].iFreePages != 0)
  6805 						{
  6806 						index = prefIndex;
  6807 						zonesFound = ETrue;
  6808 						break;
  6809 						}
  6810 					}
  6811 				}
  6812 			}
  6813 		if (!zonesFound)
  6814 			{
  6815 			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6816 			}
  6817 		else
  6818 			{
  6819 			index++;
  6820 			// Attempt to allocate a movable page to create a gap for the DP cache 
  6821 			// page to be replaced with.
  6822 			GetOriginalPageCount();
  6823 			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
  6824 
  6825 			GetAllPageInfo();
  6826 			TESTDEBUG(test.Printf(_L("index %d prev free 0x%x cur free 0x%x\n"), 
  6827 										index, gOriginalPageCountArray[index].iFreePages, gZoneUtilArray[index].iFreePages));
  6828 
  6829 			if (r != KErrNone || 
  6830 				gOriginalPageCountArray[index].iFreePages != gZoneUtilArray[index].iFreePages)
  6831 				{// The gap was allocated into the RAM zone under test so can't continue as
  6832 				// the DP cache will attempt to be reallocated into the same RAM zone.
  6833 				test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6834 				}
  6835 			else
  6836 				{
  6837 				// Get the ldd to create the array for the fixed page addresses here
  6838 				// so that any kernel heap allocations have already occurred before 
  6839 				// memory is filled etc.  Make allocation really large so it will always be enough.
  6840 				r = Ldd.AllocFixedArray(50);
  6841 
  6842 				// Fill RAM with DP cache pages and free the gap.
  6843 				TInt discardBytes;
  6844 				r = AllocDiscardable(discardBytes, KMaxTUint64, 0);
  6845 				if (r != KErrNone)	
  6846 					{
  6847 					CLEANUP(ResetDPCache());
  6848 					TEST_FAIL;
  6849 					}
  6850 				UpdateRamInfo();
  6851 				TInt prevFreeBytes = gFreeRam;
  6852 				RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  6853 				UpdateRamInfo();
  6854 				TInt freedPages = (gFreeRam - prevFreeBytes) >> gPageShift;
  6855 				if (freedPages < 1)
  6856 					{// Something went wrong as should have freed at least one page
  6857 					CLEANUP(ResetDPCache());
  6858 					TEST_FAIL;
  6859 					}
  6860 				TUint extraFreePages = freedPages - 1;
  6861 
  6862 				// Ensure that the RAM zone under test is full.
  6863 				GetAllPageInfo();
  6864 				if (gZoneUtilArray[index].iFreePages != 0)
  6865 					{
  6866 					CLEANUP(ResetDPCache());
  6867 					TEST_FAIL;
  6868 					}
  6869 
  6870 				// Allocate from the RAM zone which should force a DP cache 
  6871 				// page to be allocated.
  6872 				GetOriginalPageCount();
  6873 				TUint fixedAllocPages = 1 + extraFreePages;
  6874 				r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, fixedAllocPages);
  6875 			
  6876 				GetAllPageInfo();
  6877 				if (r != KErrNone ||
  6878 					gOriginalPageCountArray[index].iAllocFixed + fixedAllocPages != gZoneUtilArray[index].iAllocFixed)
  6879 					{
  6880 					test.Printf(_L("r %d index %d alloc 0x%x prevFixed 0x%x curFixed 0x%x\n"), r, index, fixedAllocPages, 
  6881 								gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
  6882 					CLEANUP(Ldd.FreeAllFixedPages());
  6883 					TEST_FAIL;
  6884 					}
  6885 				else
  6886 					{
  6887 					test.Printf(_L("Passed...\n"));						
  6888 					}
  6889 				}
  6890 			}
  6891 		// This should cleanup any fixed pages allocated.
  6892 		TestEnd();
  6893 		}
  6894 
  6895 	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() return KErrNoMemory when appropriate"));
  6896 	TestStart();
  6897 	// Search for a RAM zone that has some immovable pages allocated into 
  6898 	// it but isn't totally full.
  6899 	GetAllPageInfo();
  6900 	zonesFound = EFalse;
  6901 	for (index = 0; index < gZoneCount; index++)
  6902 		{
  6903 		if (gZoneUtilArray[index].iFreePages && 
  6904 			(gZoneUtilArray[index].iAllocUnknown || gZoneUtilArray[index].iAllocFixed))
  6905 			{
  6906 			zonesFound = ETrue;
  6907 			break;
  6908 			}
  6909 		}
  6910 	if (!zonesFound)
  6911 		{
  6912 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
  6913 		}
  6914 	else
  6915 		{
  6916 		// Attempt to allocate the whole RAM zone.
  6917 		r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, gZoneUtilArray[index].iPhysPages);
  6918 		
  6919 		if (r != KErrNoMemory)
  6920 			{
  6921 			test.Printf(_L("FAIL: r %d index %d\n"), r, index);
  6922 			CLEANUP(Ldd.FreeAllFixedPages());
  6923 			TEST_FAIL;
  6924 			}
  6925 		else
  6926 			{
  6927 			test.Printf(_L("Passed...\n"));
  6928 			}
  6929 		}
  6930 	TestEnd();
  6931 
  6932 	test.Next(_L("Test9: Test Epoc::ZoneAllocPhysicalRam() moves the required number of movable pages"));
  6933 	TestStart();
  6934 	TUint allocFixedPages;
  6935 	// Search for a RAM zone that has at least 2 movable pages allocated into it.
  6936 	// Need 2 so that we can move one and leave one.
  6937 	GetAllPageInfo();
  6938 	zonesFound = EFalse;
  6939 	for (index = 0; index < gZoneCount; index++)
  6940 		{
  6941 		if (gZoneUtilArray[index].iAllocMovable > 1)
  6942 			{
  6943 			// Only use this zone if the other RAM zones have enough free space for 
  6944 			// the movable page in this zone to be moved to.
  6945 			TUint freeInOther = 0;
  6946 			for (TUint i = 0; i < gZoneCount && !zonesFound; i++)
  6947 				{
  6948 				if (i != index)
  6949 					{
  6950 					freeInOther += gZoneUtilArray[i].iFreePages;
  6951 					}
  6952 				}
  6953 			if (freeInOther >= gZoneUtilArray[index].iAllocMovable)
  6954 				{
  6955 				zonesFound = ETrue;
  6956 				break;
  6957 				}
  6958 			}
  6959 		}
  6960 	
  6961 	if (!zonesFound)
  6962 		{
  6963 		test.Printf(_L("No suitable RAM zone could be found - Skipping...\n"));
  6964 		goto skipTest9;
  6965 		}
  6966 
  6967 	// Allocate up to one less than the RAM zone size. Do 2 stage fixed allocation
  6968 	// to avoid kernel heap allocations spoiling test setup.
  6969 	r = Ldd.AllocFixedArray(gZoneConfigArray[index].iPhysPages);
  6970 	if (r != KErrNone)
  6971 		{
  6972 		test.Printf(_L("Not enough free RAM to perform test - Skipping...\n"));
  6973 		goto skipTest9;
  6974 		}
  6975 
  6976 	GetAllPageInfo();
  6977 	if (gZoneUtilArray[index].iAllocMovable < 2)
  6978 		{
  6979 		test.Printf(_L("Expanding kernel heap for phys address array spoiled RAM zone - Skipping...\n"));
  6980 		goto skipTest9;
  6981 		}
  6982 	allocFixedPages =	gZoneUtilArray[index].iAllocMovable + 
  6983 						gZoneUtilArray[index].iAllocDiscardable +
  6984 						gZoneUtilArray[index].iFreePages - 1;
  6985 	r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, allocFixedPages);
  6986 
  6987 	if (r != KErrNone || !gZoneUtilArray[index].iAllocMovable)
  6988 		{
  6989 		test.Printf(_L("Fixed not allocated or too many movable moved RAM zone ID%x\n"), 
  6990 					gZoneConfigArray[index].iZoneId);
  6991 		CLEANUP(Ldd.FreeAllFixedPages());
  6992 		TEST_FAIL;
  6993 		}
  6994 
  6995 skipTest9 :
  6996 	// This will clean up any fixed pages allocated.
  6997 	TestEnd();
  6998 
  6999 	delete[] zoneIdArray;
  7000 	
  7001 	test.Next(_L("Test10: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of movable pages"));
  7002 	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_MOVABLE);
  7003 	if (r != KErrNone)
  7004 		{
  7005 		TEST_FAIL;
  7006 		}
  7007 
  7008 	test.Next(_L("Test11: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of discardable pages"));
  7009 	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_DISCARDABLE);
  7010 	if (r != KErrNone)
  7011 		{
  7012 		TEST_FAIL;
  7013 		}
  7014 
  7015 	test.End();
  7016 	return KErrNone;
  7017 	}
  7018 
  7019 
  7020 //
  7021 // TestFreeZone
  7022 //
  7023 //---------------------------------------------------------------------------------------------------------------------
  7024 //! @SYMTestCaseID				KBASE-t_ramdefrag-0537
  7025 //! @SYMTestType				CIT
  7026 //! @SYMTestCaseDesc			Verifying the function Epoc::FreePhysicalRam()
  7027 //! @SYMPREQ					PREQ308
  7028 //! @SYMTestPriority			High
  7029 //! @SYMTestActions				
  7030 //! 	1.	Allocate fixed pages and call function to free all fixed pages allocated.  
  7031 //! 
  7032 //! @SYMTestExpectedResults
  7033 //! 	1.	KErrNone
  7034 //---------------------------------------------------------------------------------------------------------------------
  7035 TInt TestFreeZone()
  7036 	{
  7037 	TInt r = 0;
  7038 	TUint zoneID = 0;
  7039 	test.Start(_L("Test1: Free allocated pages"));	
  7040 	TestStart();	
  7041 	
  7042 	TInt pages = 50;
  7043 
  7044 	GetAllPageInfo();
  7045 	TUint index = gZoneCount - 1;
  7046 	while (index > 0 && 
  7047 			(gZoneUtilArray[index].iAllocFixed != 0 || 
  7048 			gZoneUtilArray[index].iAllocUnknown != 0 || 
  7049 			(TInt)gZoneUtilArray[index].iFreePages < pages)) 
  7050 		{
  7051 		-- index;
  7052 		}
  7053 	zoneID = gZoneConfigArray[index].iZoneId;
  7054 	
  7055 	if (index == 0 && 
  7056 		(gZoneUtilArray[index].iAllocFixed != 0 || 
  7057 		gZoneUtilArray[index].iAllocUnknown != 0 || 
  7058 		(TInt)gZoneUtilArray[index].iFreePages < pages))
  7059 		{
  7060 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  7061 		}
  7062 	else
  7063 		{
  7064 		TESTDEBUG(test.Printf(_L("Allocating 0x%x pages to zone ID 0x%x\n"), pages, zoneID));
  7065 		r = Ldd.ZoneAllocDiscontiguous(zoneID, pages);
  7066 		GetAllPageInfo();
  7067 
  7068 		TESTDEBUG(test.Printf(_L("Freeing 0x%x fixed pages\n"), pages));
  7069 		if (r == KErrNone)
  7070 			{
  7071 			r = Ldd.FreeZone(pages);
  7072 			}
  7073 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  7074 		if (r == KErrNone)
  7075 			{
  7076 			test.Printf(_L("Pass: Correct return value\n"));
  7077 			}
  7078 		else
  7079 			{
  7080 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
  7081 			TEST_FAIL;
  7082 			}
  7083 		}
  7084 	TestEnd();
  7085 
  7086 	test.End();
  7087 	return KErrNone;
  7088 	}
  7089 
  7090 
  7091 //
  7092 // TestDefragSemMethod
  7093 //
  7094 //---------------------------------------------------------------------------------------------------------------------
  7095 //! @SYMTestCaseID				KBASE-t_ramdefrag-0538
  7096 //! @SYMTestType				CIT
  7097 //! @SYMTestCaseDesc			Verifying the semaphore versions of the various defrag methods
  7098 //! @SYMPREQ					PREQ308
  7099 //! @SYMTestPriority			High
  7100 //! @SYMTestActions				
  7101 //! 	1.	Fragment the memory. Following this, call the semaphore variation of DefragRam.  
  7102 //! 	2.	Fragment the memory. Following this, call the semaphore variation of EmptyRamZone 
  7103 //! 	3.	Fragment the memory. Following this, call the semaphore variation of ClaimRamZone 
  7104 //! 
  7105 //! @SYMTestExpectedResults
  7106 //! 	1.	1 or more zones have been emptied
  7107 //! 	2.	Zone specified has been emptied
  7108 //! 	3.	Zone has been claimed
  7109 //---------------------------------------------------------------------------------------------------------------------
  7110 TInt TestDefragSemMethod()
  7111 	{
  7112 	TInt r = 0;
  7113 	
  7114 	test.Start(_L("Test1: Call semaphore method of DefragRam"));	
  7115 	TestStart();	
  7116 	
  7117 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7118 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7119 
  7120 	GetOriginalPageCount();
  7121 	TBool genSucceed = CanGenSucceed();
  7122 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SEM);
  7123 	
  7124 	
  7125 	if (r != KErrNone ||(genSucceed && CheckZonesSwitchedOff() == EFalse))
  7126 		{
  7127 		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
  7128 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7129 		TEST_FAIL;
  7130 		}
  7131 	else
  7132 		{
  7133 		test.Printf(_L("Passed...\n"));	
  7134 		}
  7135 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  7136 	TestEnd();
  7137 
  7138 
  7139 	test.Next(_L("Test2: Call semaphore method of EmptyRamZone"));	
  7140 	TestStart();	
  7141 	
  7142 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7143 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7144 
  7145 	GetAllPageInfo();
  7146 	TUint index = gZoneCount - 1;
  7147 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  7148 		{
  7149 		-- index;
  7150 		}
  7151 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
  7152 	
  7153 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  7154 		{
  7155 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  7156 		}
  7157 	else
  7158 		{
  7159 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SEM, defragZoneID);
  7160 		
  7161 		GetAllPageInfo();
  7162 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  7163 		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
  7164 			{
  7165 			test.Printf(_L("Fail: r = %d, or zones has not been swtiched off\n"), r);
  7166 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7167 			TEST_FAIL;
  7168 			}
  7169 		else
  7170 			{
  7171 			test.Printf(_L("Passed...\n"));	
  7172 			}
  7173 		}
  7174 	// This will free any allocated memory.			
  7175 	TestEnd();
  7176 
  7177 	test.Next(_L("Test3: Call semaphore method of ClaimRamZone"));	
  7178 	TestStart();	
  7179 	
  7180 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7181 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7182 
  7183 	GetAllPageInfo();
  7184 	index = gZoneCount - 2;
  7185 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  7186 		{
  7187 		-- index;
  7188 		}
  7189 	defragZoneID = gZoneConfigArray[index].iZoneId;
  7190 	
  7191 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  7192 		{
  7193 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  7194 		}
  7195 	else
  7196 		{
  7197 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SEM, defragZoneID);
  7198 
  7199 		GetAllPageInfo();
  7200 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  7201 		if (CheckZoneIsOff(index) ||
  7202 			(freeInOthers && ( r != KErrNone ||
  7203 			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
  7204 			{
  7205 			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
  7206 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7207 			TEST_FAIL;
  7208 			}
  7209 		else
  7210 			{
  7211 			test.Printf(_L("Passed...\n"));	
  7212 			}
  7213 
  7214 		r = Ldd.FreeAllFixedPages();
  7215 		}
  7216 
  7217 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  7218 	TestEnd();
  7219 
  7220 	test.End();
  7221 	return KErrNone;
  7222 
  7223 	}
  7224 
  7225 
  7226 //
  7227 // TestDefragDfcMethod
  7228 //
  7229 //---------------------------------------------------------------------------------------------------------------------
  7230 //! @SYMTestCaseID				KBASE-t_ramdefrag-0539
  7231 //! @SYMTestType				CIT
  7232 //! @SYMTestCaseDesc			Verifying the Dfc versions of the various defrag methods
  7233 //! @SYMPREQ					PREQ308
  7234 //! @SYMTestPriority			High
  7235 //! @SYMTestActions				
  7236 //! 	1.	Fragment the memory. Following this, call the Dfc variation of DefragRam.  
  7237 //! 	2.	Fragment the memory. Following this, call the Dfc variation of EmptyRamZone 
  7238 //! 	3.	Fragment the memory. Following this, call the Dfc variation of ClaimRamZone 
  7239 //! 
  7240 //! @SYMTestExpectedResults
  7241 //! 	1.	1 or more zones have been emptied
  7242 //! 	2.	Zone specified has been emptied
  7243 //! 	3.	Zone has been claimed
  7244 //---------------------------------------------------------------------------------------------------------------------
  7245 TInt TestDefragDfcMethod()
  7246 	{
  7247 	TInt r = 0;
  7248 	TRequestStatus req;
  7249 	test.Start(_L("Test1: Call Dfc method of DefragRam"));	
  7250 	TestStart();	
  7251 	
  7252 	GetAllPageInfo();
  7253 	
  7254 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7255 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7256 
  7257 	GetOriginalPageCount();
  7258 	TBool genSucceed = CanGenSucceed();
  7259 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_DFC, 0, 0, -1, &req);
  7260 	TESTDEBUG(test.Printf(_L("After queueing defrag r = %d\n"), r));
  7261 	User::WaitForRequest(req);
  7262 	r = req.Int();
  7263 	
  7264 	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
  7265 		{
  7266 		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
  7267 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7268 		TEST_FAIL;
  7269 		}
  7270 	else
  7271 		{
  7272 		test.Printf(_L("Passed...\n"));	
  7273 		}
  7274 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  7275 	TestEnd();
  7276 
  7277 
  7278 	test.Next(_L("Test2: Call Dfc method of EmptyRamZone"));	
  7279 	TestStart();	
  7280 	
  7281 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7282 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7283 
  7284 	GetAllPageInfo();
  7285 	TUint index = gZoneCount - 1;
  7286 	while (index > 0 && 
  7287 			(gZoneUtilArray[index].iAllocMovable < 10 ||
  7288 			gZoneUtilArray[index].iAllocFixed != 0 || 
  7289 			gZoneUtilArray[index].iAllocUnknown != 0)) 
  7290 		{
  7291 		-- index;
  7292 		}
  7293 
  7294 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
  7295 	test.Printf(_L("zone ID = 0x%x\n"), defragZoneID);
  7296 	if (index == 0 && 
  7297 		(gZoneUtilArray[index].iAllocMovable < 10 ||
  7298 		gZoneUtilArray[index].iAllocFixed != 0 || 
  7299 		gZoneUtilArray[index].iAllocUnknown != 0))
  7300 		{
  7301 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  7302 		}
  7303 	else
  7304 		{
  7305 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
  7306 		
  7307 		User::WaitForRequest(req);
  7308 		r = req.Int();
  7309 
  7310 		GetAllPageInfo();
  7311 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  7312 		if (freeInOthers && (r != KErrNone || CheckZoneIsOff(index) == EFalse))
  7313 			{
  7314 			test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
  7315 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7316 			TEST_FAIL;
  7317 			}
  7318 		else
  7319 			{
  7320 			test.Printf(_L("Passed...\n"));	
  7321 			}
  7322 		}
  7323 	// This will free any allocated memory
  7324 	TestEnd();
  7325 	
  7326 
  7327 	test.Next(_L("Test3: Call Dfc method of ClaimRamZone"));	
  7328 	TestStart();	
  7329 	
  7330 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7331 	FreeMovable(gChunkArray1, gChunkArraySize1);
  7332 
  7333 	GetAllPageInfo();
  7334 	index = gZoneCount - 1;
  7335 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
  7336 		{
  7337 		-- index;
  7338 		}
  7339 	defragZoneID = gZoneConfigArray[index].iZoneId;
  7340 
  7341 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  7342 		{
  7343 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
  7344 		}
  7345 	else
  7346 		{
  7347 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
  7348 		User::WaitForRequest(req);
  7349 		r = req.Int();
  7350 		
  7351 		GetAllPageInfo();
  7352 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  7353 		if (CheckZoneIsOff(index) || 
  7354 			(freeInOthers && (r != KErrNone || 
  7355 			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
  7356 			{
  7357 			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
  7358 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7359 			if (r == KErrNone)
  7360 				{
  7361 				CLEANUP(Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
  7362 											gZoneConfigArray[index].iPhysBase));
  7363 				}
  7364 			TEST_FAIL;
  7365 			}
  7366 		else
  7367 			{
  7368 			test.Printf(_L("Passed...\n"));	
  7369 			}
  7370 		
  7371 		if (r == KErrNone)
  7372 			{
  7373 			Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
  7374 								gZoneConfigArray[index].iPhysBase);
  7375 			}
  7376 		}
  7377 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
  7378 	TestEnd();
  7379 
  7380 	test.End();
  7381 	return KErrNone;
  7382 	}
  7383 
  7384 
  7385 //
  7386 // TestPriorities
  7387 //
  7388 //---------------------------------------------------------------------------------------------------------------------
  7389 //! @SYMTestCaseID				KBASE-t_ramdefrag-0540
  7390 //! @SYMTestType				CIT
  7391 //! @SYMTestCaseDesc			Verifying the priorities of the defrag methods
  7392 //! @SYMPREQ					PREQ308
  7393 //! @SYMTestPriority			High
  7394 //! @SYMTestActions				
  7395 //! 	1.	Call DefragRam with a lower invalid priority value, e.g. -2  
  7396 //! 	2.	Call DefragRam with a lower invalid priority value, e.g. 100
  7397 //! 	3.	Queue three asynchronous defrags using the Dfc method:
  7398 //! 			a.	First one with the lowest priority, this will start the straight away 
  7399 //! 				and will busy the defrag method, causing any other defrag requests to 
  7400 //! 				be queued. 
  7401 //! 			b.	Queue a defrag with a relatively low priority
  7402 //! 			c.	Queue a defrag with a higher priority than the one queued in (b)
  7403 //! 		Record the order in which the defrags are completed
  7404 //! 
  7405 //! @SYMTestExpectedResults
  7406 //! 	1.	KErrArgument
  7407 //! 	2.	KErrArgument
  7408 //! 	3.	(a) will complete first as it started straight away. 
  7409 //! 		(b) and (c) were both queued whilst (a) was running, 
  7410 //! 		however as (c) has a higher priority, it will complete first - 
  7411 //! 		therefore the order returned would be "a,c,b"
  7412 //---------------------------------------------------------------------------------------------------------------------
  7413 TInt TestPriorities()
  7414 	{
  7415 	test.Start(_L("Test1: Call defrag with an invalid lower priority"));	
  7416 	TestStart();	
  7417 	
  7418 	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, -2);
  7419 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  7420 	if (r != KErrArgument)
  7421 		{
  7422 		test.Printf(_L("Fail: r = %d, not expected\n"), r);
  7423 		TEST_FAIL;
  7424 		}
  7425 	else
  7426 		{
  7427 		test.Printf(_L("Passed...\n"));	
  7428 		}
  7429 	
  7430 	TestEnd();
  7431 
  7432 	test.Next(_L("Test2: Call defrag with an invalid higher priority"));	
  7433 	TestStart();	
  7434 	
  7435 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, 100);
  7436 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  7437 	if (r != KErrArgument)
  7438 		{
  7439 		test.Printf(_L("Fail: r = %d, not expected\n"), r);
  7440 		TEST_FAIL;
  7441 		}
  7442 	else
  7443 		{
  7444 		test.Printf(_L("Passed...\n"));	
  7445 		}
  7446 	TestEnd();
  7447 
  7448 	if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) == 1)
  7449 		{// Only test priorities on single core system as this test is not smp safe.
  7450 		test.Next(_L("Test3: Call Dfc method of EmptyRamZone to test priorities"));	
  7451 		TestStart();
  7452 		
  7453 		
  7454 		TRequestStatus req;
  7455 		TRequestStatus req2;
  7456 		TRequestStatus req3;	
  7457 		TInt expectedOrder = 132; // Priorities set in Device driver 
  7458 		
  7459 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  7460 		FreeMovable(gChunkArray1, gChunkArraySize1);
  7461 
  7462 		TUint index = (gZoneCount + 1) / 2;
  7463 		TUint defragZoneID = gZoneConfigArray[index].iZoneId;
  7464 
  7465 		r = Ldd.CheckPriorities(DEFRAG_TYPE_EMPTY, defragZoneID, &req, &req2, &req3);
  7466 
  7467 		User::WaitForRequest(req);
  7468 		User::WaitForRequest(req2);
  7469 		User::WaitForRequest(req3);
  7470 
  7471 		TInt order = Ldd.GetDefragOrder();
  7472 		if (order != expectedOrder)
  7473 			{
  7474 			test.Printf(_L("Fail: order = %d. expected = %d\n"), order, expectedOrder);
  7475 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7476 			TEST_FAIL;
  7477 			}
  7478 		else
  7479 			{
  7480 			test.Printf(_L("Passed...\n"));	
  7481 			}
  7482 		// This will free any allocated memory
  7483 		TestEnd();
  7484 		}
  7485 
  7486 	test.End();
  7487 	return KErrNone;
  7488 	}
  7489 
  7490 
  7491 //
  7492 // TestFlags
  7493 //
  7494 //---------------------------------------------------------------------------------------------------------------------
  7495 //! @SYMTestCaseID				KBASE-t_ramdefrag-0541
  7496 //! @SYMTestType				CIT
  7497 //! @SYMTestCaseDesc			Verifying that when certain flags are set, 
  7498 //! 							only certain types of pages can be allocated to the zone. 
  7499 //! @SYMPREQ					PREQ308
  7500 //! @SYMTestPriority			High
  7501 //! @SYMTestActions				
  7502 //! 	1.	Set the NoFixed flag in a zone and allocate movable pages
  7503 //! 	2.	Set the NoFixed flag in a zone and allocate fixed pages
  7504 //! 	3.	Set the NoFixed flag in a zone and allocate discardable pages
  7505 //! 	4.	Set the NoFixed flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
  7506 //! 	5.	Set the NoFixed flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
  7507 //! 	6.	Set the NoMovable flag in a zone and allocate movable pages
  7508 //! 	7.	Set the NoMovable flag in a zone and allocate fixed pages
  7509 //! 	8.	Set the NoMovable flag in a zone and allocate discardable pages
  7510 //! 	9.	Set the NoDiscardable flag in a zone and allocate movable pages
  7511 //! 	10.	Set the NoDiscardable flag in a zone and allocate fixed pages
  7512 //! 	11.	Set the NoDiscardable flag in a zone and allocate discardable pages
  7513 //! 	12.	Set the OnlyDiscardable flag in a zone and allocate movable pages
  7514 //! 	13.	Set the OnlyDiscardable flag in a zone and allocate fixed pages
  7515 //! 	14.	Set the OnlyDiscardable flag in a zone and allocate discardable pages
  7516 //! 	15.	Set the OnlyDiscardable flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
  7517 //! 	16.	Set the OnlyDiscardable flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
  7518 //! 	17.	Set the NoFurtherAlloc flag in a zone and allocate movable pages
  7519 //! 	18.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages
  7520 //! 	19.	Set the NoFurtherAlloc flag in a zone and allocate discardable pages
  7521 //! 	20.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
  7522 //! 	21.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
  7523 //!		22.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most  
  7524 //!			preferable zone. Ensure that the discardable pages cannot be discarded and so must be moved. Now set the flag on  
  7525 //!			all zones barring the most preferable zone to KRamZoneFlagNoMovable, ensuring that most pref has no flags set. 
  7526 //!			Following this call a general defrag. 
  7527 //!		23.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most 
  7528 //!			preferable RAM zone. Ensure that the discardable pages cannot be discarded and so must be moved. 
  7529 //!			Now set the flag on all zones barring the most preferable zone to KRamZoneFlagNoDiscard, ensuring that 
  7530 //!			most preferable RAM zone has no flags set. Following this call a general defrag. 
  7531 //!		24. Set up memory so that the least preferable RAM zone has movable pages and discardable pages. Set all the zone 
  7532 //!			flags to KRamZoneFlagNoMovable. Following this call a general defrag. 
  7533 //!
  7534 //! @SYMTestExpectedResults
  7535 //! 	1.	Movable pages are allocated and no fixed pages allocated
  7536 //! 	2.	No fixed pages have been allocated
  7537 //! 	3.	Discardable pages are allocated and no fixed pages allocated
  7538 //!		4.	KErrNone, flag is ignored with zone specific allocation
  7539 //!		5.	KErrNone, flag is ignored when claiming a zone
  7540 //! 	6.	No movable pages have been allocated
  7541 //! 	7.	Fixed pages allocated, no movable allocated
  7542 //! 	8.	Discardable pages allocated, no movable pages allocated
  7543 //! 	9.	Movable pages allocated, no discardable pages allocated
  7544 //! 	10.	Fixed pages allocated, no discardable allocated
  7545 //! 	11.	No discardable pages allocated
  7546 //! 	12.	No movable pages allocated
  7547 //! 	13.	No fixed pages allocated
  7548 //! 	14.	Discardable pages allocated, no movable or fixed allocated
  7549 //!		15.	KErrNone, flag is ignored with zone specific allocation
  7550 //!		16.	KErrNone, flag is ignored when claiming a zone
  7551 //! 	17.	No moving, fixed or discardable pages allocated
  7552 //! 	18.	No moving, fixed or discardable pages allocated
  7553 //! 	19.	No moving, fixed or discardable pages allocated
  7554 //!		20.	KErrNoMemory, flag is obeyed with zone specific allocation
  7555 //!		21.	KErrNone, flag is ignored when claiming a zone
  7556 //!		22.	Movable pages moved to the most preferable zone, discardable pages moved to next most preferable zone
  7557 //!		23.	Discardable pages moved to most preferable zone, movable pages moved to next next most preferable zone
  7558 //!		24.	No pages are moved from the least preferable zone zone
  7559 //---------------------------------------------------------------------------------------------------------------------
  7560 TInt TestFlags()
  7561 	{
  7562 	TInt r = 0;
  7563 	TUint zoneDefragID = 0;
  7564 
  7565 	test.Start(_L("Test1: No Fixed Flag, Alloc Movable"));	
  7566 	TestStart();	
  7567 	
  7568 	TInt index = GetBestZone(BEST_MOVABLE);
  7569 
  7570 	if (index == KErrNotFound)
  7571 		{
  7572 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7573 		}
  7574 	else
  7575 		{
  7576 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7577 		if (SpaceAvailForPageTables(index, KFillAllMovable))
  7578 			{
  7579 			GetOriginalPageCount();
  7580 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  7581 			test_KErrNone(r);		
  7582 
  7583 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
  7584 			GetAllPageInfo();
  7585 			if (gZoneUtilArray[index].iAllocMovable == gOriginalPageCountArray[index].iAllocMovable || 
  7586 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  7587 				{
  7588 				test.Printf(_L("Fail: Either fixed pages have been allocated or movable pages have not been allocated to zone ID %x\n"), gZoneConfigArray[index].iZoneId);
  7589 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7590 				TEST_FAIL;
  7591 				}
  7592 			else
  7593 				{
  7594 				test.Printf(_L("Passed...\n"));	
  7595 				}
  7596 			}
  7597 		else
  7598 			{
  7599 			test.Printf(_L("Skipping...\n"));
  7600 			}
  7601 		}
  7602 	TestEnd();
  7603 
  7604 
  7605 	test.Next(_L("Test2: No Fixed Flag, Alloc Fixed"));	
  7606 	TestStart();
  7607 
  7608 	if(gPagedRom)
  7609 		{
  7610 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  7611 		test_KErrNone(r);
  7612 		}
  7613 
  7614 	// find a zone that has free pages in it to block it
  7615 	GetAllPageInfo();
  7616 	TUint i = 0;
  7617 	for (; i < gZoneCount; i++)
  7618 		{
  7619 		if (gZoneUtilArray[i].iFreePages > 0)
  7620 			{
  7621 			index = i;
  7622 			break;
  7623 			}
  7624 		}
  7625 	
  7626 	if (i == gZoneCount)
  7627 		{
  7628 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7629 		}
  7630 	else
  7631 		{
  7632 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7633 
  7634 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  7635 		test_KErrNone(r);
  7636 
  7637 		GetOriginalPageCount();
  7638 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  7639 
  7640 		GetAllPageInfo();
  7641 		// Ensure that either zone does not contain extra fixed pages 
  7642 		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  7643 			{
  7644 			test.Printf(_L("Fail: Fixed pages have been allocated into the zone ID 0x%x r = %d\n"), zoneDefragID, r);
  7645 			CLEANUP(Ldd.FreeAllFixedPages());
  7646 			TEST_FAIL;
  7647 			}
  7648 		else
  7649 			{
  7650 			test.Printf(_L("Passed...\n"));	
  7651 			}
  7652 		}	
  7653 	TestEnd();
  7654 
  7655 
  7656 	test.Next(_L("Test3: No Fixed Flag, Alloc Discardable"));	
  7657 	TestStart();
  7658 	if (gPagedRom)
  7659 		{
  7660 		index = GetBestZone(BEST_DISCARDABLE);
  7661 			
  7662 		if (index == KErrNotFound)
  7663 			{
  7664 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7665 			}
  7666 		else
  7667 			{
  7668 			zoneDefragID = gZoneConfigArray[index].iZoneId;
  7669 			GetOriginalPageCount();
  7670 			
  7671 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  7672 			test_KErrNone(r);
  7673 
  7674 			UpdateRamInfo();
  7675 			TInt discardablePages;
  7676 			r = AllocDiscardable(discardablePages);
  7677 			if (r != KErrNone)
  7678 				{
  7679 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  7680 				TEST_FAIL;
  7681 				}
  7682 			
  7683 			GetAllPageInfo();
  7684 			if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  7685 				{
  7686 				test.Printf(_L("Fail: Fixed pages have been allocated\n"));
  7687 				CLEANUP(ResetDPCache());
  7688 				TEST_FAIL;
  7689 				}
  7690 			else
  7691 				{
  7692 				test.Printf(_L("Passed...\n"));	
  7693 				}
  7694 			}		
  7695 		}
  7696 	else
  7697 		{
  7698 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  7699 		}
  7700 	TestEnd();
  7701 	
  7702 	test.Next(_L("Test4: No Fixed Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
  7703 	TestStart();
  7704 	
  7705 	index = GetBestZone(BEST_FIXED);
  7706 	
  7707 	if (index == KErrNotFound)
  7708 		{
  7709 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7710 		}
  7711 	else
  7712 		{
  7713 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7714 
  7715 		GetOriginalPageCount();
  7716 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  7717 		test_KErrNone(r);		
  7718 
  7719 		// Just need to try and allocate one page
  7720 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
  7721 
  7722 		GetAllPageInfo();
  7723 		if (r != KErrNone)
  7724 			{
  7725 			test.Printf(_L("Fail: Fixed pages have not been allocated into the zone\n"));
  7726 			CLEANUP(Ldd.FreeAllFixedPages());
  7727 			TEST_FAIL;
  7728 			}
  7729 		else
  7730 			{
  7731 			test.Printf(_L("Passed...\n"));	
  7732 			}
  7733 		}	
  7734 	TestEnd();
  7735 
  7736 	test.Next(_L("Test5: No Fixed Flag, Alloc Fixed by attempting to claim zone"));	
  7737 	TestStart();
  7738 	
  7739 	GetAllPageInfo();
  7740 	index = 0;	
  7741 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  7742 		{
  7743 		index++;
  7744 		}
  7745 	
  7746 	if ((TUint)index == gZoneCount)
  7747 		{
  7748 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7749 		}
  7750 	else
  7751 		{
  7752 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7753 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
  7754 		test_KErrNone(r);		
  7755 
  7756 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
  7757 		GetAllPageInfo();
  7758 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  7759 		if (freeInOthers && r != KErrNone)
  7760 			{
  7761 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
  7762 			TEST_FAIL;
  7763 			}
  7764 		else
  7765 			{
  7766 			test.Printf(_L("Passed...\n"));	
  7767 			}			
  7768 		}	
  7769 	TestEnd();
  7770 	
  7771 
  7772 
  7773 //------------------------------------------------------------
  7774 	test.Next(_L("Test6: No Movable Flag, Alloc Movable"));	
  7775 	TestStart();
  7776 	
  7777 	index = GetBestZone(BEST_MOVABLE);
  7778 	
  7779 	if (index == KErrNotFound)
  7780 		{
  7781 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7782 		}
  7783 	else
  7784 		{
  7785 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7786 
  7787 		GetOriginalPageCount();
  7788 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
  7789 		test_KErrNone(r);
  7790 		
  7791 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
  7792 
  7793 		GetAllPageInfo();
  7794 		if (gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable)
  7795 			{
  7796 			test.Printf(_L("Fail: Movable pages have been allocated in the zone\n"));
  7797 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7798 			TEST_FAIL;
  7799 			}
  7800 		else
  7801 			{
  7802 			test.Printf(_L("Passed...\n"));	
  7803 			}		
  7804 		}
  7805 	TestEnd();
  7806 
  7807 
  7808 	test.Next(_L("Test7: No Movable Flag, Alloc Fixed"));	
  7809 	TestStart();
  7810 	
  7811 	if(gPagedRom)
  7812 		{
  7813 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  7814 		test_KErrNone(r);
  7815 		}
  7816 
  7817 	index = GetBestZone(BEST_FIXED);
  7818 		
  7819 	if (index == KErrNotFound)
  7820 		{
  7821 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7822 		}
  7823 	else
  7824 		{ 
  7825 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7826 
  7827 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
  7828 		test_KErrNone(r);
  7829 
  7830 		GetOriginalPageCount();
  7831 		// Fill up all of RAM with fixed pages.
  7832 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  7833 		test.Printf(_L("r = %d\n"), r);
  7834 
  7835 		GetAllPageInfo();
  7836 		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable || 
  7837 			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
  7838 			{
  7839 			test.Printf(_L("Fail: orig mov 0x%x fix 0x%x current mov 0x%x fix 0x%x\n"), gOriginalPageCountArray[index].iAllocMovable,
  7840 						gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocFixed);
  7841 			CLEANUP(Ldd.FreeAllFixedPages());
  7842 			TEST_FAIL;
  7843 			}
  7844 		else
  7845 			{
  7846 			test.Printf(_L("Passed...\n"));	
  7847 			}
  7848 		}
  7849 	TestEnd();
  7850 
  7851 
  7852 	test.Next(_L("Test8: No Movable Flag, Alloc Discardable"));	
  7853 	TestStart();
  7854 	if (gPagedRom)
  7855 		{	
  7856 		index = GetBestZone(BEST_DISCARDABLE);
  7857 		
  7858 		if (index == KErrNotFound)
  7859 			{
  7860 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7861 			}
  7862 		else
  7863 			{
  7864 			zoneDefragID = gZoneConfigArray[index].iZoneId;
  7865 
  7866 			GetOriginalPageCount();
  7867 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
  7868 			test_KErrNone(r)
  7869 			UpdateRamInfo();
  7870 			TInt discardablePages;
  7871 			r = AllocDiscardable(discardablePages);
  7872 			if (r != KErrNone)
  7873 				{
  7874 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  7875 				TEST_FAIL;
  7876 				}
  7877 
  7878 			GetAllPageInfo();
  7879 			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
  7880 				{
  7881 				test.Printf(_L("Fail: Movable pages have been allocated into the zone \n"));
  7882 				CLEANUP(ResetDPCache());
  7883 				TEST_FAIL;
  7884 				}
  7885 			else
  7886 				{
  7887 				test.Printf(_L("Passed...\n"));	
  7888 				}
  7889 			}
  7890 		}
  7891 	else
  7892 		{
  7893 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  7894 		}
  7895 	TestEnd();
  7896 
  7897 //-----------------------------------------------------------------------------------------------
  7898 
  7899 	test.Next(_L("Test9: No Discardable Flag, Alloc Movable"));	
  7900 	TestStart();
  7901 	
  7902 	index = GetBestZone(BEST_MOVABLE);
  7903 
  7904 	if (index == KErrNotFound)
  7905 		{
  7906 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7907 		}
  7908 	else
  7909 		{
  7910 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7911 
  7912 		GetOriginalPageCount();
  7913 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
  7914 		test_KErrNone(r);
  7915 
  7916 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
  7917 		GetAllPageInfo();
  7918 
  7919 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
  7920 			(gZoneUtilArray[index].iAllocMovable <=  gOriginalPageCountArray[index].iAllocMovable &&
  7921 			gZoneUtilArray[index].iAllocFixed ==  gOriginalPageCountArray[index].iAllocFixed))
  7922 			{
  7923 			test.Printf(_L("Fail: Either discardable pages have been allocated or movable pages have not been allocated\n"));
  7924 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  7925 			TEST_FAIL;
  7926 			}
  7927 		else
  7928 			{
  7929 			test.Printf(_L("Passed...\n"));	
  7930 			}
  7931 		}
  7932 	TestEnd();
  7933 
  7934 
  7935 	test.Next(_L("Test10: No Discardable Flag, Alloc Fixed"));	
  7936 	TestStart();
  7937 	
  7938 	if(gPagedRom)
  7939 		{
  7940 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  7941 		test_KErrNone(r);
  7942 		}
  7943 
  7944 	index = GetBestZone(BEST_FIXED);
  7945 			
  7946 	if (index == KErrNotFound)
  7947 		{
  7948 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7949 		}
  7950 	else
  7951 		{ 
  7952 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  7953 
  7954 		GetOriginalPageCount();
  7955 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
  7956 		test_KErrNone(r);
  7957 		// Fill up all of RAM with fixed pages.
  7958 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  7959 
  7960 		GetAllPageInfo();
  7961 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
  7962 			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
  7963 			{
  7964 			test.Printf(_L("Fail: Discardable pages have been allocated or fixed pages have not been allocated\n"));
  7965 			CLEANUP(Ldd.FreeAllFixedPages());
  7966 			TEST_FAIL;
  7967 			}
  7968 		else
  7969 			{
  7970 			test.Printf(_L("Passed...\n"));	
  7971 			}
  7972 		}
  7973 	TestEnd();
  7974 
  7975 
  7976 	test.Next(_L("Test11: No Discardable Flag, Alloc Discardable"));	
  7977 	TestStart();
  7978 	if (gPagedRom)
  7979 		{
  7980 		index = GetBestZone(BEST_DISCARDABLE);
  7981 			
  7982 		if (index == KErrNotFound)
  7983 			{
  7984 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  7985 			}
  7986 		else
  7987 			{
  7988 			zoneDefragID = gZoneConfigArray[index].iZoneId;
  7989 			GetOriginalPageCount();
  7990 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
  7991 			test_KErrNone(r);
  7992 			UpdateRamInfo();
  7993 			
  7994 			TInt discardablePages;
  7995 			r = AllocDiscardable(discardablePages);
  7996 			if (r != KErrNoMemory)
  7997 				{// Allocation should fail as no dis flag is set
  7998 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  7999 				TEST_FAIL;
  8000 				}
  8001 			GetAllPageInfo();
  8002 
  8003 			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable)
  8004 				{
  8005 				test.Printf(_L("Fail: Discardable pages have been allocated into the zone\n"));
  8006 				CLEANUP(ResetDPCache());
  8007 				TEST_FAIL;
  8008 				}
  8009 			else
  8010 				{
  8011 				test.Printf(_L("Passed...\n"));	
  8012 				}
  8013 			}
  8014 		}
  8015 	else
  8016 		{
  8017 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  8018 		}
  8019 	TestEnd();
  8020 
  8021 //-----------------------------------------------------------------------------------------------
  8022 
  8023 	test.Next(_L("Test12: Only Discardable Flag, Alloc Movable"));	
  8024 	TestStart();
  8025 	
  8026 	index = GetBestZone(BEST_MOVABLE);
  8027 
  8028 	if (index == KErrNotFound)
  8029 		{
  8030 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8031 		}
  8032 	else
  8033 		{
  8034 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8035 
  8036 		GetOriginalPageCount();
  8037 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  8038 		test_KErrNone(r);
  8039 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
  8040 		GetAllPageInfo();
  8041 
  8042 		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
  8043 			{
  8044 			test.Printf(_L("Fail: Movable pages have been allocated\n"));
  8045 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  8046 			TEST_FAIL;
  8047 			}
  8048 		else
  8049 			{
  8050 			test.Printf(_L("Passed...\n"));	
  8051 			}
  8052 		}
  8053 	TestEnd();
  8054 
  8055 
  8056 	test.Next(_L("Test13: Only Discardable Flag, Alloc Fixed"));	
  8057 	TestStart();
  8058 	
  8059 	if(gPagedRom)
  8060 		{
  8061 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  8062 		test_KErrNone(r);
  8063 		}
  8064 
  8065 	index = GetBestZone(BEST_FIXED);
  8066 
  8067 	if (index == KErrNotFound)
  8068 		{
  8069 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8070 		}
  8071 	else
  8072 		{
  8073 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8074 
  8075 		GetOriginalPageCount();
  8076 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  8077 		test_KErrNone(r);
  8078 
  8079 		// Fill up all of RAM with fixed pages.
  8080 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  8081 
  8082 		GetAllPageInfo();
  8083 		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  8084 			{
  8085 			test.Printf(_L("Fail: Fixed pages have been allocated\n"));
  8086 			CLEANUP(Ldd.FreeAllFixedPages());
  8087 			TEST_FAIL;
  8088 			}
  8089 		else
  8090 			{
  8091 			test.Printf(_L("Passed...\n"));	
  8092 			}	
  8093 		}
  8094 	TestEnd();
  8095 
  8096 
  8097 	test.Next(_L("Test14: Only Discardable Flag, Alloc Discardable"));	
  8098 	TestStart();
  8099 	if (gPagedRom)
  8100 		{
  8101 		index = GetBestZone(BEST_DISCARDABLE);
  8102 
  8103 		if (index == KErrNotFound)
  8104 			{
  8105 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8106 			}
  8107 		else
  8108 			{
  8109 			zoneDefragID = gZoneConfigArray[index].iZoneId;
  8110 
  8111 			GetOriginalPageCount();
  8112 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  8113 			test_KErrNone(r);
  8114 			UpdateRamInfo();
  8115 			
  8116 			TInt discardablePages;
  8117 			r = AllocDiscardable(discardablePages);
  8118 			if (r != KErrNone)
  8119 				{
  8120 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  8121 				TEST_FAIL;
  8122 				}
  8123 			GetAllPageInfo();
  8124 
  8125 			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
  8126 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  8127 				{
  8128 				test.Printf(_L("Fail: Pages other than discardable have been allocated\n"));
  8129 				CLEANUP(ResetDPCache());
  8130 				TEST_FAIL;
  8131 				}
  8132 			else
  8133 				{
  8134 				test.Printf(_L("Passed...\n"));	
  8135 				}
  8136 			}
  8137 		}
  8138 	else
  8139 		{
  8140 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  8141 		}
  8142 	TestEnd();
  8143 
  8144 	test.Next(_L("Test15: Only Discardable Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
  8145 	TestStart();
  8146 	
  8147 	index = GetBestZone(BEST_FIXED);
  8148 	
  8149 	if (index == KErrNotFound)
  8150 		{
  8151 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8152 		}
  8153 	else
  8154 		{
  8155 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8156 
  8157 		GetOriginalPageCount();
  8158 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  8159 		test_KErrNone(r);		
  8160 
  8161 		// Just need to try and allocate one page
  8162 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
  8163 
  8164 		GetAllPageInfo();
  8165 		if (r != KErrNone)
  8166 			{
  8167 			test.Printf(_L("Fail: Fixed pages have not been allocated into zone ID 0x%x\n"), zoneDefragID);
  8168 			CLEANUP(Ldd.FreeAllFixedPages());
  8169 			TEST_FAIL;
  8170 			}
  8171 		else
  8172 			{
  8173 			test.Printf(_L("Passed...\n"));	
  8174 			}	
  8175 		}	
  8176 	TestEnd();
  8177 
  8178 	test.Next(_L("Test16: Only Discardable Flag, Alloc Fixed by attempting to claim zone"));	
  8179 	TestStart();
  8180 	
  8181 	GetAllPageInfo();
  8182 	index = 0;	
  8183 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  8184 		{
  8185 		index++;
  8186 		}
  8187 	
  8188 	if ((TUint)index == gZoneCount)
  8189 		{
  8190 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8191 		}
  8192 	else
  8193 		{
  8194 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8195 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
  8196 		test_KErrNone(r);		
  8197 
  8198 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
  8199 		GetAllPageInfo();
  8200 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  8201 		if (freeInOthers && r != KErrNone)
  8202 			{
  8203 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
  8204 			TEST_FAIL;
  8205 			}
  8206 		else
  8207 			{
  8208 			test.Printf(_L("Passed...\n"));	
  8209 			}		
  8210 		}	
  8211 	TestEnd();
  8212 //-----------------------------------------------------------------------------------------------
  8213 
  8214 	test.Next(_L("Test17: No further alloc Flag, Alloc Movable"));	
  8215 	TestStart();
  8216 	
  8217 	index = GetBestZone(BEST_MOVABLE);
  8218 
  8219 	
  8220 	if (index == KErrNotFound)
  8221 		{
  8222 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8223 		}
  8224 	else
  8225 		{
  8226 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8227 
  8228 		GetOriginalPageCount();
  8229 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
  8230 		test_KErrNone(r);
  8231 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
  8232 		GetAllPageInfo();
  8233 
  8234 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
  8235 			gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
  8236 			gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  8237 			{
  8238 			test.Printf(_L("Fail: Pages been allocated\n"));
  8239 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  8240 			TEST_FAIL;
  8241 			}
  8242 		else
  8243 			{
  8244 			test.Printf(_L("Passed...\n"));	
  8245 			}
  8246 		}
  8247 	TestEnd();
  8248 
  8249 
  8250 	test.Next(_L("Test18: No further alloc Flag, Alloc Fixed"));	
  8251 	TestStart();
  8252 	
  8253 	if(gPagedRom)
  8254 		{
  8255 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
  8256 		test_KErrNone(r);
  8257 		}
  8258 
  8259 	// Find a zone with free pages and set the flag on this zone 
  8260 	// as it will ensure that you cannot fill all of free RAM with fixed pages
  8261 	index = KErrNotFound;
  8262 	GetAllPageInfo();
  8263 	for (TUint i = 0; i < gZoneCount; i++)
  8264 		{
  8265 		if (gZoneUtilArray[i].iFreePages != 0)
  8266 			{
  8267 			index = i;
  8268 			break;
  8269 			}
  8270 		}
  8271 	if (index == KErrNotFound)
  8272 		{
  8273 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8274 		}
  8275 	else
  8276 		{ 
  8277 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8278 
  8279 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
  8280 		test_KErrNone(r);
  8281 
  8282 		GetOriginalPageCount();
  8283 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
  8284 		
  8285 		// Ensure memory wasn't filled as it should have hit the blocked zone.
  8286 		GetAllPageInfo();
  8287 		if (r != KErrNoMemory || gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  8288 			{
  8289 			test.Printf(_L("Fail: orig fix 0x%x current fix 0x%x\n"), gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
  8290 			CLEANUP(Ldd.FreeAllFixedPages());
  8291 			TEST_FAIL;
  8292 			}
  8293 		else
  8294 			{
  8295 			test.Printf(_L("Passed...\n"));	
  8296 			}
  8297 		}
  8298 	TestEnd();
  8299 
  8300 
  8301 	test.Next(_L("Test19: No further alloc Flag, Alloc Discardable"));	
  8302 	TestStart();
  8303 	if (gPagedRom)
  8304 		{
  8305 		index = GetBestZone(BEST_DISCARDABLE);
  8306 		
  8307 		if (index == KErrNotFound)
  8308 			{
  8309 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8310 			}
  8311 		else
  8312 			{
  8313 			zoneDefragID = gZoneConfigArray[index].iZoneId;
  8314 
  8315 			GetOriginalPageCount();
  8316 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
  8317 			test_KErrNone(r);
  8318 			UpdateRamInfo();
  8319 			TInt discardablePages;
  8320 			r = AllocDiscardable(discardablePages);
  8321 			if (r != KErrNoMemory)
  8322 				{// Allocation should fail as no alloc flag is set
  8323 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  8324 				TEST_FAIL;
  8325 				}
  8326 			GetAllPageInfo();
  8327 
  8328 			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
  8329 				gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
  8330 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
  8331 				{
  8332 				test.Printf(_L("Fail: Pages have been allocated into the zone\n"));
  8333 				CLEANUP(ResetDPCache());
  8334 				TEST_FAIL;
  8335 				}
  8336 			else
  8337 				{
  8338 				test.Printf(_L("Passed...\n"));	
  8339 				}
  8340 			}
  8341 		}
  8342 	else
  8343 		{
  8344 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
  8345 		}
  8346 	TestEnd();
  8347 
  8348 	
  8349 	test.Next(_L("Test20: No Further Alloc Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
  8350 	TestStart();
  8351 	
  8352 	GetAllPageInfo();
  8353 	index = 0;
  8354 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iFreePages == 0 || 
  8355 			gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  8356 		{
  8357 		index++;
  8358 		}
  8359 	
  8360 	if ((TUint)index == gZoneCount)
  8361 		{
  8362 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8363 		}
  8364 	else
  8365 		{
  8366 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8367 
  8368 		GetOriginalPageCount();
  8369 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
  8370 		test_KErrNone(r);		
  8371 
  8372 		// Just need to try and allocate one page
  8373 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
  8374 
  8375 		GetAllPageInfo();
  8376 		if (r != KErrNoMemory)
  8377 			{
  8378 			test.Printf(_L("Fail: Fixed pages have been allocated into zone ID 0x%x\n"), zoneDefragID);
  8379 			CLEANUP(Ldd.FreeAllFixedPages());
  8380 			TEST_FAIL;
  8381 			}
  8382 		else
  8383 			{
  8384 			test.Printf(_L("Passed...\n"));	
  8385 			}
  8386 		}	
  8387 	TestEnd();
  8388 
  8389 	test.Next(_L("Test21: No Further Alloc Flag, Alloc Fixed by attempting to claim zone"));	
  8390 	TestStart();
  8391 	
  8392 	GetAllPageInfo();
  8393 	index = 0;	
  8394 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
  8395 		{
  8396 		index++;
  8397 		}
  8398 	
  8399 	if ((TUint)index == gZoneCount)
  8400 		{
  8401 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
  8402 		}
  8403 	else
  8404 		{
  8405 		zoneDefragID = gZoneConfigArray[index].iZoneId;
  8406 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
  8407 		test_KErrNone(r);		
  8408 
  8409 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
  8410 		GetAllPageInfo();
  8411 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
  8412 		if (freeInOthers && r != KErrNone)
  8413 			{
  8414 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
  8415 			CLEANUP(ResetRamZoneFlags());
  8416 			TEST_FAIL;
  8417 			}
  8418 		else
  8419 			{
  8420 			test.Printf(_L("Passed...\n"));	
  8421 			}
  8422 		}	
  8423 	TestEnd();
  8424 
  8425 	TUint mostPrefArrayIndex = 0;
  8426 	TUint mostPrefIndex = 0;
  8427 	TUint leastPrefIndex = 0;
  8428 	const TUint KFreeMostPref = 10;
  8429 	TUint flag = 0;
  8430 	TUint prefIndex;
  8431 	TUint totalFree;
  8432 
  8433 
  8434 	for (TUint testStep = 0; testStep < 2; testStep++)
  8435 		{
  8436 		switch(testStep)
  8437 			{
  8438 			case 0:
  8439 				test.Next(_L("Test22: Ensure that the General Defrag looks at the flags 1"));
  8440 				break;
  8441 
  8442 			case 1:
  8443 				test.Next(_L("Test23: Ensure that the General Defrag looks at the flags 2"));
  8444 				break;
  8445 			}
  8446 			
  8447 		TestStart();
  8448 		gChunkArray1 = new RChunk;	
  8449 		gChunkArraySize1 = 1;
  8450 		TUint minCacheSize = 0;
  8451 		TUint maxCacheSize = 0;
  8452 		TUint currentCacheSize = 0;
  8453 		TUint freeNeededInMostPref = 0;
  8454 		if (!gPagedRom)
  8455 			{
  8456 			test.Printf(_L("Not a paged ROM - Skipping...\n"));
  8457 			goto skipTest22;
  8458 			}
  8459 
  8460 		// Find the most pref zone with free pages
  8461 		r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
  8462 		if (r != KErrNone)
  8463 			{
  8464 			test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
  8465 			goto skipTest22;
  8466 			}
  8467 
  8468 		// Ensure that the least pref zone is empty
  8469 		leastPrefIndex = gPrefArray[gZoneCount - 1];
  8470 		if (gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
  8471 			{
  8472 			test.Printf(_L("Least pref zone is not empty - Skipping...\n"));
  8473 			goto skipTest22;
  8474 			}
  8475 
  8476 		// Allocate 1 movable page to the least preferable zone
  8477 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
  8478 		if (r != KErrNone)
  8479 			{			
  8480 			test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
  8481 			goto skipTest22;
  8482 			}
  8483 		
  8484 		// Allocate 1 discardable page to the least preferable zone
  8485 		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
  8486 			{
  8487 			TInt disPages;
  8488 			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
  8489 			if (r != KErrNone)
  8490 				{			
  8491 				test.Printf(_L("Failed to allocate discardable pages r = %d - Skipping...\n"), r);
  8492 				goto skipTest22;
  8493 				}
  8494 
  8495 			// up the minimum cache size so that the pages have to be moved - not discarded
  8496 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  8497 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
  8498 			if (r != KErrNone)
  8499 				{			
  8500 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
  8501 				goto skipTest22;
  8502 				}
  8503 			}
  8504 		else
  8505 			{
  8506 			test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
  8507 			goto skipTest22;
  8508 			}
  8509 
  8510 		// Check that the least pref zone has movable and discardable pages in it
  8511 		GetAllPageInfo();
  8512 		if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
  8513 			gZoneUtilArray[leastPrefIndex].iAllocDiscardable == 0)
  8514 			{
  8515 			test.Printf(_L("No movable or discardable in least pref zone\n"));
  8516 			PrintPageInfo();
  8517 			goto skipTest22;
  8518 			}
  8519 
  8520 		ResetRamZoneFlags();
  8521 		// if most pref zone has too many free pages fill up with fixed
  8522 		if (gZoneUtilArray[mostPrefIndex].iFreePages > KFreeMostPref)
  8523 			{
  8524 			TUint allocPages = gZoneUtilArray[mostPrefIndex].iFreePages - KFreeMostPref;
  8525 			r = Ldd.ZoneAllocToMany(mostPrefIndex, allocPages);
  8526 			if (r != KErrNone)
  8527 				{	
  8528 				test.Printf(_L("Failed allocate 0x%x fixed to index %d r = %d - Skipping...\n"), 
  8529 								allocPages, mostPrefIndex,r);
  8530 				PrintPageInfo();
  8531 				goto skipTest22;
  8532 				}
  8533 			}
  8534 		
  8535 		
  8536 		// if the no. of discardable pages is less than free in most pref increase the min cache size
  8537 		GetAllPageInfo();
  8538 		if (gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[mostPrefIndex].iFreePages)
  8539 			{
  8540 			TUint discDiff = gZoneUtilArray[mostPrefIndex].iFreePages - gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
  8541 			test.Printf(_L("discDiff = 0x%x\n"), discDiff);
  8542 			TInt disPages;
  8543 			if (ZoneAllocDiscard(leastPrefIndex, discDiff, disPages) != KErrNone)
  8544 				{			
  8545 				test.Printf(_L("Failed allocate discardable to zone index %d- Skipping...\n"), leastPrefIndex);
  8546 				goto skipTest22;
  8547 				}
  8548 			// up the minimum cache size by the difference as we don't want these pages to be discarded
  8549 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  8550 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
  8551 			test.Printf(_L("r = %d\n"), r);
  8552 			if (r != KErrNone)
  8553 				{
  8554 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
  8555 				goto skipTest22;
  8556 				}
  8557 			}
  8558 		ResetRamZoneFlags();
  8559 
  8560 		// if the rest of the zones are either full or empty put a fixed page into the next most pref empty zone
  8561 		GetPrefOrder();
  8562 		prefIndex = 0;
  8563 		totalFree = 0;
  8564 		for (; prefIndex < gZoneCount && totalFree < gZoneUtilArray[leastPrefIndex].iAllocDiscardable; prefIndex++)
  8565 			{// Look for zone that has enough free pages to fit all the discardable
  8566 			TUint zoneIndex = gPrefArray[prefIndex];
  8567 			if (zoneIndex != mostPrefIndex && zoneIndex != leastPrefIndex)
  8568 				{
  8569 				if (gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
  8570 					{
  8571 					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
  8572 					}
  8573 				else
  8574 					{
  8575 					r = Ldd.ZoneAllocToMany(zoneIndex, 1);
  8576 					if (r != KErrNone)
  8577 						{
  8578 						test.Printf(_L("Couldn't alloc fixed to zone index %d - r = %d\n"), zoneIndex, r);
  8579 						goto skipTest22;
  8580 						}
  8581 					GetAllPageInfo();
  8582 					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
  8583 					}
  8584 				}
  8585 			}
  8586 
  8587 		if (prefIndex >= gZoneCount)
  8588 			{
  8589 			test.Printf(_L("Couldn't find zone\n"));
  8590 			goto skipTest22;
  8591 			}
  8592 
  8593 
  8594 		
  8595 		// If the most preferable zone does not have enough free pages, skip
  8596 		freeNeededInMostPref = (testStep == 0) ? gZoneUtilArray[leastPrefIndex].iAllocMovable : gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
  8597 		if (gZoneUtilArray[mostPrefIndex].iFreePages < freeNeededInMostPref)
  8598 			{
  8599 			test.Printf(_L("Free needed in mostPref(%d) = %d, Free available in mostPref = %d - skipping...\n"), 
  8600 							mostPrefIndex,freeNeededInMostPref,gZoneUtilArray[mostPrefIndex].iFreePages);
  8601 			goto skipTest22;
  8602 			}
  8603 
  8604 		GetAllPageInfo();
  8605 		// Set up the RAM zone flags for the test
  8606 		flag = (testStep == 0)? NO_MOVE_FLAG: NO_DISCARD_FLAG;
  8607 		// Set all zones except most pref to KRamZoneFlagNoMovable	or KRamZoneFlagNoDiscard
  8608 		for (TUint index = 0; index < gZoneCount; index++)
  8609 			{
  8610 			TUint zoneID = gZoneConfigArray[index].iZoneId;
  8611 			if (index != mostPrefIndex)
  8612 				{
  8613 				r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, flag);
  8614 				if (r != KErrNone)
  8615 					{			
  8616 					test.Printf(_L("Failed to set flag r = %d - Skipping...\n"), r);
  8617 					goto skipTest22;
  8618 					}
  8619 				}
  8620 			}
  8621 		
  8622 		GetOriginalPageCount();
  8623 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  8624 		
  8625 		GetAllPageInfo();
  8626 		switch(testStep)
  8627 			{
  8628 			case 0:
  8629 				if (r != KErrNone ||
  8630 					gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
  8631 					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
  8632 					{
  8633 					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
  8634 									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
  8635 									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
  8636 									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
  8637 					CLEANUP(Ldd.FreeAllFixedPages());
  8638 					TEST_FAIL;
  8639 					}
  8640 				else
  8641 					{
  8642 					test.Printf(_L("Passed...\n"));
  8643 					}
  8644 				break;
  8645 
  8646 			case 1:
  8647 				if (r != KErrNone ||
  8648 					gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
  8649 					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
  8650 					{
  8651 					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
  8652 									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
  8653 									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
  8654 									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
  8655 					CLEANUP(Ldd.FreeAllFixedPages());
  8656 					TEST_FAIL;
  8657 					}
  8658 				else
  8659 					{
  8660 					test.Printf(_L("Passed...\n"));
  8661 					}
  8662 				break;
  8663 			}
  8664 
  8665 	skipTest22:
  8666 		TestEnd();
  8667 		}
  8668 
  8669 	test.Next(_L("Test24: Ensure that the General Defrag doesnt move or discard pages if KRamZoneFlagNoMovable set on all zones "));	
  8670 	TestStart();
  8671 	gChunkArray1 = new RChunk;
  8672 	gChunkArraySize1 = 1;
  8673 	// Find the most pref zone with free pages
  8674 	GetPrefOrder();
  8675 	TInt disPages = 0;	
  8676 	if (FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex) != KErrNone)
  8677 		{
  8678 		test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
  8679 		goto skipTest24;
  8680 		}
  8681 
  8682 	// Ensure that the least pref zone has free pages in it
  8683 	leastPrefIndex = gPrefArray[gZoneCount-1];
  8684 	if (gZoneUtilArray[leastPrefIndex].iFreePages == 0)
  8685 		{
  8686 		test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
  8687 		goto skipTest24;
  8688 		}
  8689 
  8690 	// Allocate 1 movable page to the least preferable zone
  8691 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
  8692 	if (r != KErrNone)
  8693 		{			
  8694 		test.Printf(_L("Failed to allocate movable page r = %d - Skipping...\n"), r);
  8695 		goto skipTest24;
  8696 		}
  8697 	
  8698 	if (gPagedRom)
  8699 		{
  8700 		
  8701 		TUint minCacheSize = 0;
  8702 		TUint maxCacheSize = 0;
  8703 		TUint currentCacheSize = 0;
  8704 		
  8705 		// Allocate 1 discardable page to the least preferable zone
  8706 		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
  8707 			{
  8708 			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
  8709 			if (r != KErrNone)
  8710 				{
  8711 				test.Printf(_L("Discardable pages not allocated r= %d\n"), r);
  8712 				}
  8713 			
  8714 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  8715 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
  8716 			if (r != KErrNone)
  8717 				{			
  8718 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
  8719 				goto skipTest24;
  8720 				}
  8721 			}	
  8722 		}
  8723 
  8724 	
  8725 	// Check that the least pref zone has movable and discardable (if allocated) pages in it
  8726 	GetAllPageInfo();
  8727 	if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
  8728 		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < (TUint)disPages ||
  8729 		gZoneUtilArray[leastPrefIndex].iAllocMovable > gTotalPageCount.iFreePages - gZoneUtilArray[leastPrefIndex].iFreePages)
  8730 		{
  8731 		test.Printf(_L("No movable in least pref zone or no space for moveable pages to be moved to\n"));
  8732 		PrintPageInfo();
  8733 		goto skipTest24;
  8734 		}
  8735 
  8736 	ResetRamZoneFlags();
  8737 	GetAllPageInfo();
  8738 	// Now set all zones to KRamZoneFlagNoMovable
  8739 	for (TUint index = 0; index < gZoneCount; index++)
  8740 		{
  8741 		TUint zoneID = gZoneConfigArray[index].iZoneId;
  8742 		r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
  8743 		if (r != KErrNone)
  8744 			{			
  8745 			test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
  8746 			goto skipTest24;
  8747 			}
  8748 		}
  8749 
  8750 	GetOriginalPageCount();
  8751 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  8752 	GetAllPageInfo();
  8753 	if (r != KErrNone ||
  8754 		gOriginalPageCountArray[leastPrefIndex].iAllocMovable != gZoneUtilArray[leastPrefIndex].iAllocMovable ||
  8755 		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable)
  8756 		{
  8757 		test.Printf(_L("FAIL:r=%d LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
  8758 						r,leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
  8759 						gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
  8760 		CLEANUP(ResetRamZoneFlags());
  8761 		TEST_FAIL;
  8762 		}
  8763 	else
  8764 		{
  8765 		test.Printf(_L("Passed...\n"));
  8766 		}
  8767 skipTest24:
  8768 	TestEnd();
  8769 
  8770 	test.End();
  8771 	return KErrNone;
  8772 	}
  8773 
  8774 
  8775 //
  8776 // Template functions encapsulating ControlIo magic
  8777 //
  8778 template <class C>
  8779 TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
  8780 	{
  8781     TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
  8782 
  8783     TInt r = fs.ControlIo(drv, fkn, ptrC);
  8784 
  8785     return r;
  8786 	}
  8787 
  8788 //
  8789 // FormatMMC
  8790 //
  8791 // Formats the MMC card
  8792 //
  8793 void FormatMMC()
  8794 	{	
  8795 	test.Printf(_L("Formatting MMC...\n"));
  8796 
  8797 	RFs theFs;
  8798 	TBuf<4> driveBuf = _L("D:\\");
  8799 	RFormat format;
  8800 	TInt count;
  8801 	TChar driveLet;
  8802 
  8803 	TInt r = theFs.Connect();
  8804 	test_KErrNone(r);
  8805 	
  8806 	r = theFs.DriveToChar(gDrive, driveLet);
  8807 	test_KErrNone(r);
  8808 
  8809 	driveBuf[0] = driveLet;
  8810 	test.Printf(_L("Formatting Drive: %C\n"),(TInt)driveLet);
  8811 	
  8812 	r = format.Open(theFs,driveBuf,EFullFormat,count);
  8813 	test_KErrNone(r);
  8814 	
  8815 	while(count)
  8816 		{
  8817 		TInt r = format.Next(count);
  8818 		test_KErrNone(r);
  8819 		}
  8820 	format.Close();
  8821 	theFs.Close();
  8822 	}
  8823 
  8824 
  8825 //
  8826 // FileNameGen
  8827 //
  8828 // Generates file names to create the files
  8829 //
  8830 void FileNameGen(TDes16& aBuffer, TInt aLong, TInt aPos) 
  8831 	{
  8832 	TInt padding;
  8833 	TInt i = 0;
  8834 	TBuf16<10> tempbuf;
  8835 
  8836 	_LIT(KNumber,"%d");
  8837 	tempbuf.Format(KNumber,aPos);
  8838 	
  8839 	padding = aLong-tempbuf.Size()/2;
  8840 	aBuffer = _L("");
  8841 	
  8842 	while(i < padding)
  8843 		{
  8844 		aBuffer.Append('F');
  8845 		i++;
  8846 		}
  8847 	aBuffer.Append(tempbuf);
  8848 
  8849 	_LIT(KExtension1, ".TXT");
  8850 	aBuffer.Append(KExtension1);
  8851 	}
  8852 
  8853 
  8854 //
  8855 // CreateFiles
  8856 //
  8857 // Creates the files to fill part of the read cache
  8858 //
  8859 void CreateFiles(TInt aFiles, TInt aFileSize)
  8860 	{
  8861 	TInt i = 0, r = 0;
  8862 	RFile file;
  8863 	TBuf16<50> directory;
  8864 	
  8865 	TBuf16<50> path;
  8866 	TBuf16<50> buffer(50); 	
  8867 	
  8868 	directory = gSessionPath;
  8869 	
  8870 	test.Printf(_L("Creating %d files for filling the cache (size %d)\n"), aFiles, aFileSize);
  8871 
  8872 	// create a big buffer to speed things up
  8873 	HBufC8* bigBuf = NULL;
  8874 	const TInt KBigBufferSize = 32 * 1024;
  8875 	TRAPD(res,bigBuf = HBufC8::NewL(KBigBufferSize));
  8876 	test(res == KErrNone && bigBuf != NULL);
  8877 		
  8878 	TPtr8 bigBufWritePtr(NULL, 0);	
  8879 	bigBufWritePtr.Set(bigBuf->Des());
  8880 
  8881 	// Fill the buffer
  8882 	TChar aC = 'A';
  8883 	for(i = 0; i < KBigBufferSize; i++)
  8884 		{
  8885 		bigBufWritePtr.Append((i%32) + aC);
  8886 		}
  8887 	
  8888 
  8889 	i = 0;		
  8890 	while(i < aFiles) 
  8891 		{
  8892 		if (i % 10 == 0)
  8893 			test.Printf(_L("Creating file %d of %d...\r"), i, aFiles);
  8894 		FileNameGen(buffer, 8, i+3) ;
  8895 		path = directory;
  8896 		path.Append(buffer);
  8897 
  8898 		// delete file first to ensure it's contents are not in the cache (file may be on the closed file queue)
  8899 		r = gTheFs.Delete(path);
  8900 		test(r == KErrNone || r == KErrNotFound);
  8901 
  8902 		r = file.Create(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
  8903 		if(r == KErrAlreadyExists) 
  8904 			r = file.Open(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
  8905 		TInt j = 0;
  8906 		i++;
  8907 		while(j < aFileSize)
  8908 			{
  8909 			bigBufWritePtr.SetLength(Min(KBigBufferSize, aFileSize - j));
  8910 			r = file.Write(bigBufWritePtr);
  8911 			// Running out of disk space is expected for the last file.
  8912 			// Premature "disk full" conditions need to abort.
  8913 			if (r == KErrDiskFull)
  8914 				{
  8915 				test(i == aFiles);
  8916 				break;
  8917 				}
  8918 			test_KErrNone(r);
  8919 			j += bigBufWritePtr.Length();
  8920 			}					
  8921 
  8922 		file.Close();
  8923 		}
  8924 	test.Printf(_L("\nFiles created\n"));
  8925 	delete bigBuf;
  8926 	}
  8927 
  8928 
  8929 //
  8930 // FillCache
  8931 //
  8932 // Allocate discardable pages using file system caching
  8933 //
  8934 TInt FillCache(TInt aFiles, TInt aFileSize)
  8935 	{
  8936 	// Fail if files already open
  8937 	test(!gFileCacheRun);
  8938 
  8939 
  8940 	TInt i = 0, r = 0;
  8941 	TBuf16<50> directory;
  8942 	
  8943 	TBuf16<50> path;
  8944 	TBuf16<50> buffer(50); 	
  8945 	HBufC8* buf = NULL;
  8946 	TPtr8 bufPtr(NULL, 0);	
  8947 	
  8948 	TRAPD(res,buf = HBufC8::NewL(2));
  8949 	test(res == KErrNone && buf != NULL);
  8950 	bufPtr.Set(buf->Des());
  8951 	
  8952 	TESTDEBUG(test.Printf(_L("Filling the cache\n")));
  8953 
  8954 	directory = gSessionPath;
  8955 	
  8956 	i = 0;		
  8957 
  8958 	GetAllPageInfo();
  8959 	TESTDEBUG(test.Printf(_L("total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
  8960 	
  8961 
  8962 	while(i < aFiles) 
  8963 		{
  8964 		FileNameGen(buffer, 8, i+3) ;
  8965 		path = directory;
  8966 		path.Append(buffer);
  8967 		r = gFile[i].Open(gTheFs,path,EFileShareAny|EFileRead|EFileReadBuffered|EFileReadAheadOff);
  8968 		test_KErrNone(r);
  8969 		
  8970 		TInt j = 0;
  8971 		while(j < aFileSize)
  8972 			{
  8973 			r = gFile[i].Read(j,bufPtr);
  8974 			test_KErrNone(r);
  8975 			j += 4 * 1024;
  8976 			}					
  8977 		i++;
  8978 		}
  8979 	gFileCacheRun = ETrue;
  8980 	GetAllPageInfo();
  8981 	TESTDEBUG(test.Printf(_L("after - total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
  8982 	delete buf;
  8983 	TESTDEBUG(test.Printf(_L("Cache filled\n")));
  8984 	
  8985 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
  8986 	// get number of items on Page Cache
  8987 	TFileCacheStats startPageCacheStats;
  8988 
  8989 	r = controlIo(gTheFs,gDrive, KControlIoFileCacheStats, startPageCacheStats);
  8990 	test.Printf(_L("control stats r= %d\n"), r);
  8991 	test(r==KErrNone || r == KErrNotSupported);
  8992 	
  8993 	TESTDEBUG(test.Printf(_L("Allocated segment count=%d\n"),startPageCacheStats.iAllocatedSegmentCount));
  8994 #endif
  8995 	// if we do not have any discardable pages then something went
  8996 	// wrong with file caching
  8997 	if (gTotalPageCount.iDiscardablePages == 0)
  8998 		return KErrNotSupported;
  8999 		
  9000 	return KErrNone;
  9001 	}
  9002 	
  9003 
  9004 
  9005 //
  9006 // SetUpMMC
  9007 //
  9008 // Sets up the MMC to be used by the test by creating files on the MMC
  9009 //
  9010 void SetUpMMC()
  9011 	{
  9012 	FormatMMC();
  9013 
  9014 	TInt r = 0;
  9015 
  9016 	TChar driveToTest;	
  9017 	
  9018 	TVolumeInfo volInfo;
  9019 	
  9020 	r = gTheFs.DriveToChar(gDrive, driveToTest);
  9021 	test_KErrNone(r);
  9022 					
  9023 	r = gTheFs.CharToDrive(driveToTest,gDrive);
  9024 	test_KErrNone(r);
  9025 
  9026 	gSessionPath = _L("?:\\F32-TST\\");
  9027 	gSessionPath[0] = (TUint16) driveToTest;
  9028 	test.Printf(_L("Drive Letter=%C\n"),(TInt)driveToTest);
  9029 
  9030 	TDriveInfo info;
  9031 	r = gTheFs.Drive(info,gDrive);
  9032 	test_KErrNone(r);
  9033 	r = gTheFs.SetSessionPath(gSessionPath);
  9034 	test_KErrNone(r);
  9035 	r = gTheFs.MkDirAll(gSessionPath);
  9036 	if (r != KErrNone && r != KErrAlreadyExists)
  9037 		{
  9038 		test_KErrNone(r);
  9039 		}
  9040 	
  9041 	r = gTheFs.Volume(volInfo, gDrive);
  9042 	test_KErrNone(r);
  9043 	TInt64 gMediaSize = volInfo.iSize;
  9044 
  9045 	// This calculation is approximate because the client cannot know
  9046 	// internal allocation mechanisms of the filesystem, i.e. how much
  9047 	// metadata is associated with a file of name X / size Y, whether
  9048 	// space used by such metadata is reflected in TDriveInfo::iSize and
  9049 	// what block/clustersize the filesystem will round filesizes to.
  9050 	// The last file that fills up the drive may therefore be partial
  9051 	// (smaller than this calculation predicts).
  9052 	TInt maxPossibleFiles = gFilesNeededToFillCache;
  9053 	test.Printf(_L("Original files needed = %d\n"), maxPossibleFiles);
  9054 	if(gMediaSize < (KDefaultCacheSize * maxPossibleFiles))
  9055 		{
  9056 		maxPossibleFiles = (gMediaSize - 10) / KDefaultCacheSize;
  9057 		test.Printf(_L("Disk size is smaller - files needed = %d\n"), maxPossibleFiles);
  9058 		}
  9059 	gFilesNeededToFillCache = maxPossibleFiles;
  9060 	CreateFiles(gFilesNeededToFillCache, KDefaultCacheSize);
  9061 	}
  9062 
  9063 
  9064 //
  9065 // TestFileCaching
  9066 //
  9067 //---------------------------------------------------------------------------------------------------------------------
  9068 //! @SYMTestCaseID				KBASE-t_ramdefrag-0599
  9069 //! @SYMTestType				CIT
  9070 //! @SYMTestCaseDesc			Verifying that when File System Caching allocates dicardable pages, 
  9071 //! 							Defrag and allocation of fixed pages happens correctly. 
  9072 //! @SYMPREQ					PREQ308
  9073 //! @SYMTestPriority			High
  9074 //! @SYMTestActions				
  9075 //! 	1.	Fill the file system cache to allocate discardable pages
  9076 //! 		following this call EmptyRamZone() in a zone with discardable pages. 
  9077 //! 	2.	Fill the file system cache to allocate discardable pages
  9078 //! 		folling this allocate discontiguous fixed pages to a zone
  9079 //! 	3.	Fill the file system cache to allocate discardable pages
  9080 //! 	.	following this allocate discontuguous fixed pages
  9081 //! 	4.	Fill the file system cache to allocate discardable pages
  9082 //! 		following this allocate less than 16 contiguous fixed pages
  9083 //! 	5.	Fill the file system cache to allocate discardable pages
  9084 //! 		following this allocate more than 16 contiguous fixed pages
  9085 //!
  9086 //! @SYMTestExpectedResults
  9087 //! 	1.	Discardable pages are removed
  9088 //! 	2.	KErrNone
  9089 //! 	3.	KErrNone
  9090 //! 	4.	KErrNone and numDiscardablePages != 0
  9091 //! 	5.	KErrNone and numDiscardablePages = 0
  9092 //---------------------------------------------------------------------------------------------------------------------
  9093 TInt TestFileCaching()
  9094 	{
  9095 	const TUint KDisPagesReq = 1;
  9096 	TInt r = KErrNone;
  9097 	TInt allocSize = 0;
  9098 	TUint zoneID = 0;
  9099 	TUint index = 0;
  9100 	TUint minDiscardPages = 0;
  9101 	if (gDrive == KNoRemovableDrive)
  9102 		{
  9103 		test.Start(_L("Cannot find drive to write files to - Skipping FS Caching Tests\n"));
  9104 		test.End();
  9105 		return 0;
  9106 		}
  9107 	
  9108 	r = gTheFs.Connect();
  9109 	test_KErrNone(r);
  9110 
  9111 	SetUpMMC();
  9112 	
  9113 	RRamDefragFuncTestLdd Ldd2;
  9114 	r = Ldd2.Open();
  9115 	test_KErrNone(r);
  9116 	
  9117 	test.Start(_L("Test1: Test EmptyRamZone() clears file server cache pages"));	
  9118 	TestStart();	
  9119 	
  9120 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9121 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9122 
  9123 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9124 	if (r != KErrNone)
  9125 		{
  9126 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
  9127 		goto skipFileCacheTests;
  9128 		}
  9129 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  9130 	
  9131 	GetAllPageInfo();
  9132 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
  9133 		{
  9134 		++ index;
  9135 		}
  9136 	
  9137 	if (index == gZoneCount)  
  9138 		{
  9139 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
  9140 		}
  9141 	else
  9142 		{
  9143 		zoneID = gZoneConfigArray[index].iZoneId;
  9144 
  9145 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
  9146 		
  9147 		GetAllPageInfo();
  9148 		if (gZoneUtilArray[index].iAllocDiscardable != 0)
  9149 			{
  9150 			test.Printf(_L("Fail: Zone ID 0x%x has 0x%x discardable pages\n"), zoneID, gZoneUtilArray[index].iAllocDiscardable);
  9151 			TEST_FAIL;
  9152 			}
  9153 		else
  9154 			{
  9155 			test.Printf(_L("Passed...\n"));
  9156 			}
  9157 		}
  9158 	TestEnd();
  9159 
  9160 	test.Next(_L("Test2: Filling the FS Cache and allocating fixed pages to a zone"));	
  9161 	TestStart();
  9162 	
  9163 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9164 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9165 
  9166 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9167 	if (r != KErrNone)
  9168 		{
  9169 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
  9170 		goto skipFileCacheTests;
  9171 		}
  9172 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  9173 
  9174 	GetAllPageInfo();	
  9175 	index = 0;
  9176 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
  9177 		{
  9178 		++ index;
  9179 		} 
  9180 
  9181 	if (index == gZoneCount)  
  9182 		{
  9183 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
  9184 		}
  9185 	else
  9186 		{
  9187 		zoneID = gZoneConfigArray[index].iZoneId;
  9188 
  9189 		// Just need to attempt to allocate one more page than there is free in the zone
  9190 		allocSize = gZoneUtilArray[index].iFreePages + 1;
  9191 		
  9192 		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
  9193 		r = Ldd.ZoneAllocDiscontiguous(zoneID, allocSize);
  9194 		test.Printf(_L("r = %d\n"), r);
  9195 
  9196 		GetAllPageInfo();
  9197 		if (r != KErrNone)
  9198 			{
  9199 			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
  9200 			TEST_FAIL;
  9201 			}
  9202 		else
  9203 			{
  9204 			test.Printf(_L("Passed...\n"));
  9205 			}
  9206 		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
  9207 		Ldd.FreeAllFixedPages();
  9208 		}	
  9209 	TestEnd();
  9210 
  9211 
  9212 	test.Next(_L("Test3: Filling the FS Cache and allocating fixed pages"));	
  9213 	TestStart();
  9214 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9215 	if (r != KErrNone)
  9216 		{
  9217 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
  9218 		goto skipFileCacheTests;
  9219 		}
  9220 
  9221 	GetAllPageInfo();	
  9222 	index = 0;
  9223 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
  9224 		{
  9225 		++ index;
  9226 		}
  9227 		
  9228 	if (index == gZoneCount)  
  9229 		{
  9230 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
  9231 		}
  9232 	else
  9233 		{ 
  9234 		zoneID = gZoneConfigArray[index].iZoneId;
  9235 
  9236 		allocSize = 14; 
  9237 
  9238 		TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
  9239 		Ldd.AllocateFixed(gTotalPageCount.iFreePages);
  9240 		
  9241 		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
  9242 		r = Ldd2.AllocateFixed(allocSize);
  9243 		
  9244 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  9245 		
  9246 		GetAllPageInfo();
  9247 		if (r != KErrNone)
  9248 			{
  9249 			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
  9250 			CLEANUP(Ldd.FreeAllFixedPages());
  9251 			CLEANUP(Ldd2.FreeAllFixedPages());
  9252 			TEST_FAIL;
  9253 			}
  9254 		else
  9255 			{
  9256 			test.Printf(_L("Passed...\n"));
  9257 			}
  9258 		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
  9259 		Ldd2.FreeAllFixedPages();
  9260 		}
  9261 	TestEnd();
  9262 
  9263 	test.Next(_L("Test4: Filling the FS Cache and allocating less than 16 contiguous fixed pages"));	
  9264 	TestStart();
  9265 	
  9266 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);	
  9267 	if (r != KErrNone)
  9268 		{
  9269 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
  9270 		goto skipFileCacheTests;
  9271 		}
  9272 	allocSize = 14 << gPageShift; 
  9273 	
  9274 	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
  9275 	GetAllPageInfo();
  9276 	// Allocate the fixed array before getting any page counts
  9277 	for (TUint index = 0; index < gZoneCount; index++)
  9278 		{
  9279 		GetAllPageInfo();
  9280 		if (gZoneUtilArray[index].iFreePages)
  9281 			{
  9282 			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
  9283 			if (r != KErrNone)
  9284 				{
  9285 				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
  9286 				goto SkipTest4;
  9287 				}
  9288 			}
  9289 		}
  9290 
  9291 	// Now fill all zones with fixed pages, 1 zone at a time
  9292 	// to avoid the discardable pages being disturbed
  9293 	for (TUint index = 0; index < gZoneCount; index++)
  9294 		{
  9295 		GetAllPageInfo();
  9296 		if (gZoneUtilArray[index].iFreePages)
  9297 			{
  9298 			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
  9299 			if (r != KErrNone)
  9300 				{
  9301 				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
  9302 													gZoneUtilArray[index].iFreePages, index,  r);
  9303 				goto SkipTest4;
  9304 				}
  9305 			}
  9306 		}
  9307 	
  9308 	GetAllPageInfo();
  9309 	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
  9310 	if (gTotalPageCount.iFreePages || 
  9311 		gTotalPageCount.iDiscardablePages <= (TUint)(allocSize >> gPageShift))
  9312 		{
  9313 		test.Printf(_L("Setup failed - Skipping...\n"));
  9314 		goto SkipTest4;
  9315 		}
  9316 
  9317 	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
  9318 	r = Ldd2.AllocContiguous(allocSize);
  9319 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  9320 	
  9321 	GetAllPageInfo();
  9322 	if (r != KErrNone || !gTotalPageCount.iDiscardablePages)
  9323 		{
  9324 		test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
  9325 		CLEANUP(Ldd.FreeAllFixedPages());
  9326 		CLEANUP(Ldd2.FreeAllFixedPages());
  9327 		TEST_FAIL;
  9328 		}
  9329 	else
  9330 		{
  9331 		test.Printf(_L("Passed...\n"));
  9332 		}
  9333 
  9334 SkipTest4:
  9335 	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
  9336 	Ldd2.FreeAllFixedPages();
  9337 	TestEnd();
  9338 
  9339 	test.Next(_L("Test5: Filling the FS Cache and allocating more than 16 contiguous fixed pages"));	
  9340 	TestStart();
  9341 	// TestEnd() will have reduced any cache pages to minimum so just get current 
  9342 	// count of discardable pages.
  9343 	GetAllPageInfo();
  9344 	minDiscardPages = gTotalPageCount.iDiscardablePages;
  9345 	
  9346 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9347 	if (r != KErrNone)
  9348 		{
  9349 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
  9350 		goto skipFileCacheTests;
  9351 		}
  9352 	
  9353 	allocSize = 18 << gPageShift; 
  9354 
  9355 	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
  9356 	GetAllPageInfo();
  9357 	
  9358 	// Allocate the fixed array before getting any page counts
  9359 	for (TUint index = 0; index < gZoneCount; index++)
  9360 		{
  9361 		GetAllPageInfo();
  9362 		if (gZoneUtilArray[index].iFreePages)
  9363 			{
  9364 			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
  9365 			if (r != KErrNone)
  9366 				{
  9367 				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
  9368 				goto SkipTest5;
  9369 				}
  9370 			}
  9371 		}
  9372 
  9373 	// Now fill all zones with fixed pages, 1 zone at a time
  9374 	// to avoid the discardable pages being disturbed
  9375 	for (TUint index = 0; index < gZoneCount; index++)
  9376 		{
  9377 		GetAllPageInfo();
  9378 		if (gZoneUtilArray[index].iFreePages)
  9379 			{
  9380 			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
  9381 			if (r != KErrNone)
  9382 				{
  9383 				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
  9384 													gZoneUtilArray[index].iFreePages, index,  r);
  9385 				goto SkipTest5;
  9386 				}
  9387 			}
  9388 		}
  9389 	
  9390 	GetAllPageInfo();
  9391 	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
  9392 	if (gTotalPageCount.iFreePages)
  9393 		{
  9394 		test.Printf(_L("Setup failed - Skipping...\n"));
  9395 		goto SkipTest5;
  9396 		}
  9397 	
  9398 	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
  9399 	r = Ldd2.AllocContiguous(allocSize);
  9400 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
  9401 
  9402 	GetAllPageInfo();
  9403 	if (r != KErrNone || gTotalPageCount.iDiscardablePages != minDiscardPages)
  9404 		{
  9405 		test.Printf(_L("Fail: r = %d, expected = %d - Discardable Pages = 0x%x, expected = %d\n"), 
  9406 								r, KErrNone, gTotalPageCount.iDiscardablePages, minDiscardPages);
  9407 		CLEANUP(Ldd.FreeAllFixedPages());
  9408 		CLEANUP(Ldd2.FreeAllFixedPages());
  9409 		TEST_FAIL;
  9410 		}
  9411 	else
  9412 		{
  9413 		test.Printf(_L("Passed...\n"));
  9414 		}
  9415 SkipTest5:
  9416 
  9417 skipFileCacheTests:
  9418 	TestEnd();
  9419 	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
  9420 	Ldd2.FreeAllFixedPages();
  9421 	Ldd2.Close();
  9422 	gTheFs.Close();
  9423 	FormatMMC();
  9424 	test.End();
  9425 	return KErrNone;
  9426 
  9427 	}
  9428 
  9429 
  9430 //
  9431 // TestOneZoneConfig
  9432 //
  9433 //---------------------------------------------------------------------------------------------------------------------
  9434 //! @SYMTestCaseID				KBASE-t_ramdefrag-0600
  9435 //! @SYMTestType				CIT
  9436 //! @SYMTestCaseDesc			Verifying that when only 1 zone is cofigured in the variant, that
  9437 //!								the defrag and allocation of fixed pages happend correctly
  9438 //! @SYMPREQ					PREQ308
  9439 //! @SYMTestPriority			High
  9440 //! @SYMTestActions				
  9441 //! 	1.	Fragment the memory and perform a DefragRam
  9442 //! 	2.	Fragment the memory and perform an EmptyZone
  9443 //! 	3.	Fragment the memory and perform a ClaimZone
  9444 //! 	4.	Call ZoneAllocPhysicalRam to allocate discontiguous fixed pages
  9445 //! 	5.	Call ZoneAllocPhysicalRam to allocate contiguous fixed pages
  9446 //!
  9447 //! @SYMTestExpectedResults
  9448 //! 	1.	Number of pages does not differ from the original
  9449 //! 	2.	KErrNoMemroy and discardable pages are discarded of
  9450 //! 	3.	KErrNoMemory
  9451 //! 	4.	KErrNone
  9452 //! 	5.	KErrNone
  9453 //---------------------------------------------------------------------------------------------------------------------
  9454 TInt TestOneZoneConfig()
  9455 	{
  9456 	TInt r = gTheFs.Connect();
  9457 	test_KErrNone(r);
  9458 	
  9459 	if (gDrive != KNoRemovableDrive)
  9460 		{	
  9461 		SetUpMMC();
  9462 		}
  9463 
  9464 	TUint index = 0;
  9465 	GetAllPageInfo();
  9466 	TUint zoneID = gZoneConfigArray[index].iZoneId;
  9467 	test.Printf(_L("Zone ID = 0x%x\n"), zoneID);
  9468 	TUint minCacheSize = 0;
  9469 	TUint maxCacheSize = 0;
  9470 	TUint currentCacheSize = 0;
  9471 	
  9472 
  9473 	TUint origFree = 0;
  9474 	TUint origUnknown = 0;
  9475 	TUint origFixed = 0;
  9476 	TUint origMovable = 0;
  9477 	TUint origDiscard = 0;
  9478 	TUint origOther = 0;
  9479 
  9480 	if (gPagedRom)
  9481 		{
  9482 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  9483 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  9484 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  9485 
  9486 		TUint setMin = 60 << gPageShift;
  9487 		TInt setMax = -1;
  9488 		TInt r = DPTest::SetCacheSize(setMin, setMax);
  9489 		if (r != KErrNone)
  9490 			{
  9491 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
  9492 			CLEANUP(ResetDPCache());
  9493 			TEST_FAIL;
  9494 			}
  9495 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
  9496 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
  9497 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
  9498 		}
  9499 
  9500 	test.Start(_L("Test1: Fragmenting the memory and performing a general defrag"));	
  9501 	TestStart();	
  9502 	
  9503 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9504 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9505 	
  9506 	if (gDrive != KNoRemovableDrive)
  9507 		{
  9508 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9509 		}
  9510 	else
  9511 		{
  9512 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
  9513 		}
  9514 
  9515 	GetAllPageInfo();
  9516 	origFree = gTotalPageCount.iFreePages;
  9517 	origUnknown = gTotalPageCount.iUnknownPages;
  9518 	origFixed = gTotalPageCount.iFixedPages;
  9519 	origMovable = gTotalPageCount.iMovablePages;
  9520 	origDiscard = gTotalPageCount.iDiscardablePages;
  9521 	origOther = gTotalPageCount.iOtherPages;
  9522 	
  9523 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  9524 	
  9525 	GetAllPageInfo();
  9526 
  9527 	if ((origUnknown != gTotalPageCount.iUnknownPages) ||
  9528 		(origFixed != gTotalPageCount.iFixedPages) ||
  9529 		(origMovable != gTotalPageCount.iMovablePages) ||
  9530 		(origDiscard != gTotalPageCount.iDiscardablePages) ||
  9531 		(origOther != gTotalPageCount.iOtherPages))
  9532 		{
  9533 		test.Printf(_L("Fail: Pages after defrag are not equal to those before"));
  9534 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  9535 		TEST_FAIL;
  9536 		}
  9537 	else
  9538 		{
  9539 		test.Printf(_L("Passed..."));
  9540 		}
  9541 	// This will free any allocated memory
  9542 	TestEnd();
  9543 
  9544 
  9545 	test.Next(_L("Test2: Fragmenting the memory and performing an EmptyZone"));	
  9546 	TestStart();	
  9547 	
  9548 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9549 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9550 	
  9551 	if (gDrive != KNoRemovableDrive)
  9552 		{
  9553 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9554 		}
  9555 	else
  9556 		{
  9557 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
  9558 		}	
  9559 	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
  9560 
  9561 	if (r != KErrNoMemory || CheckZoneIsOff(index))
  9562 		{
  9563 		test.Printf(_L("Fail: r = %d, expected = -4"), r);
  9564 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  9565 		TEST_FAIL;
  9566 		}
  9567 	else
  9568 		{
  9569 		test.Printf(_L("Passed..."));
  9570 		}
  9571 	// This will free any allocated memory
  9572 	TestEnd();
  9573 
  9574 	test.Next(_L("Test3: Fragmenting the memory and performing a ClaimZone"));	
  9575 	TestStart();	
  9576 	
  9577 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9578 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9579 
  9580 	if (gDrive != KNoRemovableDrive)
  9581 		{
  9582 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
  9583 		}
  9584 	else
  9585 		{
  9586 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
  9587 		}
  9588 	
  9589 	GetAllPageInfo();
  9590 	origMovable = gTotalPageCount.iMovablePages;
  9591 		
  9592 	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
  9593 
  9594 	GetAllPageInfo();
  9595 	if (r != KErrNoMemory || origMovable != gTotalPageCount.iMovablePages)
  9596 		{
  9597 		test.Printf(_L("Fail: r = %d, expected = -4"), r);
  9598 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  9599 		TEST_FAIL;
  9600 		}
  9601 	else
  9602 		{
  9603 		test.Printf(_L("Passed..."));
  9604 		}
  9605 	// This will free any allocated memory.
  9606 	TestEnd();
  9607 
  9608 	test.Next(_L("Test4: Calling ZoneAllocPhysicalRam to allocate discontiguous fixed pages"));	
  9609 	TestStart();	
  9610 	
  9611 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9612 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9613 	
  9614 	GetAllPageInfo();	
  9615 	origFree = gTotalPageCount.iFreePages;
  9616 	origUnknown = gTotalPageCount.iUnknownPages;
  9617 	origFixed = gTotalPageCount.iFixedPages;
  9618 	origMovable = gTotalPageCount.iMovablePages;
  9619 	origDiscard = gTotalPageCount.iDiscardablePages;
  9620 	origOther = gTotalPageCount.iOtherPages;
  9621 	
  9622 	r = Ldd.ZoneAllocDiscontiguous(zoneID, (TInt)(origFree / 2));
  9623 
  9624 	GetAllPageInfo();
  9625 	if (gTotalPageCount.iFixedPages < (origFixed + (origFree / 2)))
  9626 		{
  9627 		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
  9628 						gTotalPageCount.iFixedPages, (origFixed + (origFree / 2)));
  9629 		CLEANUP(Ldd.FreeAllFixedPages());
  9630 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  9631 		TEST_FAIL;
  9632 		}
  9633 	else
  9634 		{
  9635 		test.Printf(_L("Passed..."));
  9636 		}
  9637 	Ldd.FreeAllFixedPages();
  9638 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  9639 	TestEnd();
  9640 
  9641 
  9642 	test.Next(_L("Test5: Calling ZoneAllocPhysicalRam to allocate contiguous fixed pages"));	
  9643 	TestStart();	
  9644 	
  9645 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
  9646 	FreeMovable(gChunkArray1, gChunkArraySize1);
  9647 
  9648 	GetAllPageInfo();
  9649 	origFree = gTotalPageCount.iFreePages;
  9650 	origUnknown = gTotalPageCount.iUnknownPages;
  9651 	origFixed = gTotalPageCount.iFixedPages;
  9652 	origMovable = gTotalPageCount.iMovablePages;
  9653 	origDiscard = gTotalPageCount.iDiscardablePages;
  9654 	origOther = gTotalPageCount.iOtherPages;
  9655 	
  9656 	TInt allocSize = 50 << gPageShift; 
  9657 	
  9658 	r = Ldd.ZoneAllocContiguous(zoneID, allocSize);
  9659 
  9660 	GetAllPageInfo();
  9661 	if (gTotalPageCount.iFixedPages < (origFixed + (allocSize >> gPageShift)))
  9662 		{
  9663 		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
  9664 							gTotalPageCount.iFixedPages, (origFixed + (allocSize >> gPageShift)));
  9665 		CLEANUP(Ldd.FreeAllFixedPages());
  9666 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
  9667 		TEST_FAIL;
  9668 		}
  9669 	else
  9670 		{
  9671 		test.Printf(_L("Passed..."));
  9672 		}
  9673 	Ldd.FreeAllFixedPages();
  9674 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
  9675 	TestEnd();
  9676 
  9677 	if (gPagedRom)
  9678 		{
  9679 		test_KErrNone(DPTest::FlushCache());
  9680 		ResetDPCache();
  9681 		}
  9682 
  9683 	gTheFs.Close();
  9684 	if (gDrive != KNoRemovableDrive)
  9685 		{
  9686 		FormatMMC();
  9687 		}
  9688 	
  9689 	test.End();
  9690 	return KErrNone;
  9691 	}
  9692 
  9693 
  9694 //
  9695 // RunDefragTests
  9696 //
  9697 // List of defrag tests to be run 
  9698 //
  9699 void RunDefragTests()
  9700 	{
  9701 	test.Start(_L("Testing the moving of pages in a defrag"));
  9702 	TestMovPgsDefrag();
  9703 
  9704 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::DefragRam() arg aMaxPages"));
  9705 	TestDefragRamMaxPages();
  9706 
  9707 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()"));
  9708 	TestEmptyRamZone();
  9709 
  9710 	test.Next(_L("Verifying the implementation of the function Epoc::GetRamZonePageCount()"));
  9711 	TestGetRamZonePageCount();
  9712 
  9713 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::ClaimRamZone()"));
  9714 	TestClaimRamZone();
  9715 
  9716 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::Cancel()"));
  9717 	TestCancelDefrag();
  9718 
  9719 	test.Next(_L("Verifying that pages are moved correctly"));
  9720 	TestMovingPages();
  9721 
  9722 	test.Next(_L("Verifying Semaphore Methods of the Defrag"));
  9723 	TestDefragSemMethod();
  9724 
  9725 	test.Next(_L("Verifying Dfc Methods of the Defrag"));
  9726 	TestDefragDfcMethod();
  9727 
  9728 	test.Next(_L("Testing priorities"));
  9729 	TestPriorities();
  9730 
  9731 	test.Next(_L("Testing File System Caching"));
  9732 	if (!gPagedRom)
  9733 		{
  9734 		TestFileCaching();
  9735 		}
  9736 	else
  9737 		{
  9738 		test.Printf(_L("Skipping... \n"));
  9739 		}
  9740 	
  9741 	test.Next(_L("Testing general RAM defrag implementation"));
  9742 	TestGenDefrag();
  9743 
  9744 	test.End();
  9745 	}
  9746 
  9747 
  9748 //
  9749 // RunAllocTests
  9750 //
  9751 // List of allocating tests to be run 
  9752 // These tests only need to be executed once
  9753 //
  9754 void RunAllocTests()
  9755 	{
  9756 	test.Start(_L("Verifying the allocating strategies"));
  9757 	TestAllocStrategies();
  9758 
  9759 	test.Next(_L("Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam()"));
  9760 	TestZoneAllocContiguous();
  9761 
  9762 	test.Next(_L("Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam()"));
  9763 	TestZoneAllocDiscontiguous();
  9764 	
  9765 	test.Next(_L("Test Free Zone"));
  9766 	TestFreeZone();
  9767 
  9768 	test.Next(_L("Testing zone flags"));
  9769 	TestFlags();
  9770 
  9771 	test.End();
  9772 	}
  9773 
  9774 
  9775 //
  9776 // E32Main
  9777 //
  9778 // Main entry point.
  9779 //
  9780 TInt E32Main()
  9781 	{
  9782 	test.Title();
  9783 	DeviceDriver(TEST_DRIVER_OPEN);
  9784 	gTotalRamLost = 0;
  9785 	
  9786 	TInt r = TestSetup();
  9787 	if (r != KErrNone)
  9788 		{
  9789 		test.Printf(_L("Test Setup failed, r = %d\n"), r);
  9790 		TestCleanup();
  9791 		return r;
  9792 		}
  9793 
  9794 	if (gZoneCount == 1)
  9795 		{
  9796 		GetAllPageInfo();
  9797 
  9798 		test.Start(_L("Zone Count 1..."));
  9799 		TestOneZoneConfig();
  9800 		}
  9801 	else
  9802 		{
  9803 		test.Start(_L("Running Alloc tests"));
  9804 		RunAllocTests();
  9805 
  9806 		Ldd.ResetDriver();
  9807 		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  9808 		
  9809 
  9810 		test.Next(_L("Running Defrag tests"));
  9811 		RunDefragTests();
  9812 
  9813 		Ldd.ResetDriver();
  9814 		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
  9815 		}
  9816 	
  9817 
  9818 	test.Printf(_L("The total number of test steps failed = %d\n"), gTestStepFailed);
  9819 	test_Equal(KErrNone, gTestStepFailed);
  9820 	
  9821 	TestCleanup();
  9822 	
  9823 	DeviceDriver(TEST_DRIVER_CLOSE);
  9824 	test.End();
  9825 	test.Close();
  9826 	
  9827 	return 0;
  9828 	}