kerneltest/e32test/mmu/t_chunk2.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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\mmu\t_chunk2.cpp
       
    15 // Overview:
       
    16 // Test RChunk class
       
    17 // API Information:
       
    18 // RChunk
       
    19 // Details:
       
    20 // - Test creating local chunks and moving chunks home address, verify
       
    21 // results are as expected.
       
    22 // - Create numerous local chunks, test allocating more space by 
       
    23 // adjusting chunk size, check for allocation failure, verify results 
       
    24 // are as expected.
       
    25 // Platforms/Drives/Compatibility:
       
    26 // All.
       
    27 // Assumptions/Requirement/Pre-requisites:
       
    28 // Failures and causes:
       
    29 // Base Port information:
       
    30 // 
       
    31 //
       
    32 
       
    33 #define __E32TEST_EXTENSION__
       
    34 #include <e32test.h>
       
    35 #include <e32hal.h>
       
    36 #include <e32panic.h>
       
    37 #include "mmudetect.h"
       
    38 #include "d_gobble.h"
       
    39 #include "freeram.h"
       
    40 
       
    41 TInt GlobalValue=299792458;
       
    42 TInt *GlobalPtr=&GlobalValue;
       
    43 
       
    44 LOCAL_D RTest test(_L("T_CHUNK2"));
       
    45 LOCAL_D RTest t(_L("ShareThread"));
       
    46 LOCAL_D RChunk gChunk;
       
    47 
       
    48 LOCAL_D TPtr nullPtr(NULL,0);
       
    49 
       
    50 LOCAL_C void TestAllocFailure(RChunk& c1, RChunk& c2)
       
    51 	{
       
    52 	TInt free=FreeRam();
       
    53 	test.Printf(_L("Free RAM %dK\n"),free/1024);
       
    54 	TInt size1=free-0x80000;	// 512K less than available
       
    55 	TInt size2=0xFF000;			// 255 pages
       
    56 	test.Printf(_L("Adjusting chunk 1 to size %dK\n"),size1/1024);
       
    57 	TInt r=c1.Adjust(size1);
       
    58 	test(r==KErrNone);
       
    59 	test.Printf(_L("Attempting to adjust chunk 2 to size %dK\n"),size2/1024);
       
    60 	r=c2.Adjust(size2);
       
    61 	test(r==KErrNoMemory);
       
    62 	TInt size3=FreeRam();
       
    63 	test.Printf(_L("Attempting to adjust chunk 2 to size %dK\n"),size3/1024);
       
    64 	r=c2.Adjust(size3);
       
    65 	TInt free2=FreeRam();
       
    66 	if (r==KErrNone)
       
    67 		{
       
    68 		test.Printf(_L("Succeeded - free RAM now %dK\n"),free2/1024);
       
    69 		test.Printf(_L("Freeing chunk 2\n"));
       
    70 		r=c2.Adjust(0);
       
    71 		test(r==KErrNone);
       
    72 		}
       
    73 	else
       
    74 		test.Printf(_L("Failed - free RAM now %dK\n"),free2/1024);
       
    75 	test.Printf(_L("Freeing chunk 1\n"));
       
    76 	r=c1.Adjust(0);
       
    77 	test(r==KErrNone);
       
    78 	test.Printf(_L("Checking free RAM\n"));
       
    79 	UserSvr::HalFunction(EHalGroupKernel, EKernelHalSupervisorBarrier, 0, 0);
       
    80 	free2=FreeRam();
       
    81 	test.Printf(_L("Free RAM %dK\n"),free2/1024);
       
    82 	test(free==free2);
       
    83 	}
       
    84 
       
    85 void TestInterleavedAlloc()
       
    86 	{
       
    87 	TInt pageSize;
       
    88 	RChunk c1;
       
    89 	RChunk c2;
       
    90 	TInt r;
       
    91 	r=UserHal::PageSizeInBytes(pageSize);
       
    92 	test(r==KErrNone);
       
    93 	r=c1.CreateLocal(0,0x100000);
       
    94 	test(r==KErrNone);
       
    95 	r=c2.CreateLocal(0,0x100000);
       
    96 	test(r==KErrNone);
       
    97 	TInt step;
       
    98 	for (step=1; step<=32; ++step)
       
    99 		{
       
   100 		test.Printf(_L("Step size %x\n"),step*pageSize);
       
   101 		while (c1.Size()<64*pageSize)
       
   102 			{
       
   103 			r=c1.Adjust(c1.Size()+step*pageSize);
       
   104 			test(r==KErrNone);
       
   105 			r=c2.Adjust(c2.Size()+step*pageSize);
       
   106 			test(r==KErrNone);
       
   107 			}
       
   108 		c1.Adjust(0);
       
   109 		c2.Adjust(0);
       
   110 		}
       
   111 	c1.Close();
       
   112 	c2.Close();
       
   113 	}
       
   114 
       
   115 TInt E32Main()
       
   116 //
       
   117 //	Test RChunk class
       
   118 //
       
   119 	{
       
   120 
       
   121 	test.Title();
       
   122 	if (!HaveVirtMem())
       
   123 		{
       
   124 		test.Printf(_L("This test requires an MMU\n"));
       
   125 		return KErrNone;
       
   126 		}
       
   127 	TestInterleavedAlloc();
       
   128 	test.Start(_L("Test moving chunks home addresses"));
       
   129 	test.Printf(_L("GlobalValue=%d\n"),*GlobalPtr);
       
   130 	++*GlobalPtr;
       
   131 	test.Printf(_L("GlobalValue=%d\n"),GlobalValue);
       
   132 
       
   133 	test.Next(_L("Load gobbler LDD"));
       
   134 	TInt r = User::LoadLogicalDevice(KGobblerLddFileName);
       
   135 	test(r==KErrNone || r==KErrAlreadyExists);
       
   136 	RGobbler gobbler;
       
   137 	r = gobbler.Open();
       
   138 	test(r==KErrNone);
       
   139 	TUint32 taken = gobbler.GobbleRAM(128*1024*1024);
       
   140 	test.Printf(_L("Gobbled: %dK\n"), taken/1024);
       
   141 
       
   142 	TInt free=FreeRam();
       
   143 	test.Printf(_L("Free RAM 0x%08X bytes\n"),free);
       
   144 
       
   145 	RChunk chunk1;
       
   146 	r=chunk1.CreateLocal(0x400,0x800000);
       
   147 	test (r==KErrNone);
       
   148 	TInt i;
       
   149 	TInt *p=(TInt*)chunk1.Base();
       
   150 	test.Printf(_L("Chunk 1 Base %08X\n"),p);
       
   151 	for (i=0; i<0x100; i++)
       
   152 		*p++=i*i+41;
       
   153 	RChunk chunk2;
       
   154 	r=chunk2.CreateLocal(0x400,0x800000);
       
   155 	test (r==KErrNone);
       
   156 	TInt *p2=(TInt*)chunk2.Base();
       
   157 	test.Printf(_L("Chunk 2 Base %08X\n"),p2);
       
   158 	for (i=0; i<0x100; i++)
       
   159 		*p2++=i*i*i+487;
       
   160 	r=chunk1.Adjust(0x120000);
       
   161 	test (r==KErrNone);
       
   162 	for (i=0x100; i<0x48000; i++)
       
   163 		*p++=i*i+41;
       
   164 	r=chunk2.Adjust(0x120000);
       
   165 	test (r==KErrNone);
       
   166 	for (i=0x100; i<0x48000; i++)
       
   167 		*p2++=i*i*i+487;
       
   168 	p=(TInt*)chunk1.Base();
       
   169 	p2=(TInt*)chunk2.Base();
       
   170 	for(i=0; i<0x48000; i++)
       
   171 		{
       
   172 		TInt read1=*p++;
       
   173 		TInt read2=*p2++;
       
   174 		if (read1 != (i*i+41))
       
   175 			{
       
   176 			test.Printf(_L("Chunk 1 i=%X, read %08X expected %08X\n"),i,read1,i*i+41);
       
   177 			//test.Getch();
       
   178 			}
       
   179 		if (read2 != (i*i*i+487))
       
   180 			{
       
   181 			test.Printf(_L("Chunk 2 i=%X, read %08X expected %08X\n"),i,read2,i*i*i+487);
       
   182 			//test.Getch();
       
   183 			}
       
   184 		}
       
   185 	chunk1.Close();
       
   186 	chunk2.Close();
       
   187 	
       
   188 	TInt free2=FreeRam();
       
   189 	test.Printf(_L("Free RAM 0x%08X bytes\n"),free2);
       
   190 	test(free2==free);
       
   191 
       
   192 	// Chunks must not be paged otherwise they will not effect the amount 
       
   193 	// of free ram reported plus on h4 swap size is less than the total ram.
       
   194 	TChunkCreateInfo createInfo;
       
   195 	createInfo.SetNormal(0, free+2097152);
       
   196 	createInfo.SetPaging(TChunkCreateInfo::EUnpaged);
       
   197 	RChunk c1;
       
   198 	test_KErrNone(c1.Create(createInfo));
       
   199 	createInfo.SetNormal(0, 0x1000000);
       
   200 	RChunk c2;
       
   201 	RChunk c3;
       
   202 	RChunk c4;
       
   203 	RChunk c5;
       
   204 	RChunk c6;
       
   205 	test_KErrNone(c2.Create(createInfo));
       
   206 	test_KErrNone(c3.Create(createInfo));
       
   207 	test_KErrNone(c4.Create(createInfo));
       
   208 	test_KErrNone(c5.Create(createInfo));
       
   209 	test_KErrNone(c6.Create(createInfo));
       
   210 
       
   211 	TestAllocFailure(c1,c2);
       
   212 	r=c3.Adjust(1024);
       
   213 	test(r==KErrNone);
       
   214 	TestAllocFailure(c1,c2);
       
   215 	r=c4.Adjust(1024);
       
   216 	test(r==KErrNone);
       
   217 	TestAllocFailure(c1,c2);
       
   218 	r=c5.Adjust(1024);
       
   219 	test(r==KErrNone);
       
   220 	TestAllocFailure(c1,c2);
       
   221 	r=c6.Adjust(1024);
       
   222 	test(r==KErrNone);
       
   223 	TestAllocFailure(c1,c2);
       
   224 
       
   225 	c1.Close();
       
   226 	c2.Close();
       
   227 	c3.Close();
       
   228 	c4.Close();
       
   229 	c5.Close();
       
   230 	c6.Close();
       
   231 	gobbler.Close();
       
   232 
       
   233 	test.End();
       
   234 	test.Close();
       
   235 	return(KErrNone);
       
   236 	}
       
   237 
       
   238