sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\mmu\t_chunk2.cpp sl@0: // Overview: sl@0: // Test RChunk class sl@0: // API Information: sl@0: // RChunk sl@0: // Details: sl@0: // - Test creating local chunks and moving chunks home address, verify sl@0: // results are as expected. sl@0: // - Create numerous local chunks, test allocating more space by sl@0: // adjusting chunk size, check for allocation failure, verify results sl@0: // are as expected. sl@0: // Platforms/Drives/Compatibility: sl@0: // All. sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #define __E32TEST_EXTENSION__ sl@0: #include sl@0: #include sl@0: #include sl@0: #include "mmudetect.h" sl@0: #include "d_gobble.h" sl@0: #include "freeram.h" sl@0: sl@0: TInt GlobalValue=299792458; sl@0: TInt *GlobalPtr=&GlobalValue; sl@0: sl@0: LOCAL_D RTest test(_L("T_CHUNK2")); sl@0: LOCAL_D RTest t(_L("ShareThread")); sl@0: LOCAL_D RChunk gChunk; sl@0: sl@0: LOCAL_D TPtr nullPtr(NULL,0); sl@0: sl@0: LOCAL_C void TestAllocFailure(RChunk& c1, RChunk& c2) sl@0: { sl@0: TInt free=FreeRam(); sl@0: test.Printf(_L("Free RAM %dK\n"),free/1024); sl@0: TInt size1=free-0x80000; // 512K less than available sl@0: TInt size2=0xFF000; // 255 pages sl@0: test.Printf(_L("Adjusting chunk 1 to size %dK\n"),size1/1024); sl@0: TInt r=c1.Adjust(size1); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Attempting to adjust chunk 2 to size %dK\n"),size2/1024); sl@0: r=c2.Adjust(size2); sl@0: test(r==KErrNoMemory); sl@0: TInt size3=FreeRam(); sl@0: test.Printf(_L("Attempting to adjust chunk 2 to size %dK\n"),size3/1024); sl@0: r=c2.Adjust(size3); sl@0: TInt free2=FreeRam(); sl@0: if (r==KErrNone) sl@0: { sl@0: test.Printf(_L("Succeeded - free RAM now %dK\n"),free2/1024); sl@0: test.Printf(_L("Freeing chunk 2\n")); sl@0: r=c2.Adjust(0); sl@0: test(r==KErrNone); sl@0: } sl@0: else sl@0: test.Printf(_L("Failed - free RAM now %dK\n"),free2/1024); sl@0: test.Printf(_L("Freeing chunk 1\n")); sl@0: r=c1.Adjust(0); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("Checking free RAM\n")); sl@0: UserSvr::HalFunction(EHalGroupKernel, EKernelHalSupervisorBarrier, 0, 0); sl@0: free2=FreeRam(); sl@0: test.Printf(_L("Free RAM %dK\n"),free2/1024); sl@0: test(free==free2); sl@0: } sl@0: sl@0: void TestInterleavedAlloc() sl@0: { sl@0: TInt pageSize; sl@0: RChunk c1; sl@0: RChunk c2; sl@0: TInt r; sl@0: r=UserHal::PageSizeInBytes(pageSize); sl@0: test(r==KErrNone); sl@0: r=c1.CreateLocal(0,0x100000); sl@0: test(r==KErrNone); sl@0: r=c2.CreateLocal(0,0x100000); sl@0: test(r==KErrNone); sl@0: TInt step; sl@0: for (step=1; step<=32; ++step) sl@0: { sl@0: test.Printf(_L("Step size %x\n"),step*pageSize); sl@0: while (c1.Size()<64*pageSize) sl@0: { sl@0: r=c1.Adjust(c1.Size()+step*pageSize); sl@0: test(r==KErrNone); sl@0: r=c2.Adjust(c2.Size()+step*pageSize); sl@0: test(r==KErrNone); sl@0: } sl@0: c1.Adjust(0); sl@0: c2.Adjust(0); sl@0: } sl@0: c1.Close(); sl@0: c2.Close(); sl@0: } sl@0: sl@0: TInt E32Main() sl@0: // sl@0: // Test RChunk class sl@0: // sl@0: { sl@0: sl@0: test.Title(); sl@0: if (!HaveVirtMem()) sl@0: { sl@0: test.Printf(_L("This test requires an MMU\n")); sl@0: return KErrNone; sl@0: } sl@0: TestInterleavedAlloc(); sl@0: test.Start(_L("Test moving chunks home addresses")); sl@0: test.Printf(_L("GlobalValue=%d\n"),*GlobalPtr); sl@0: ++*GlobalPtr; sl@0: test.Printf(_L("GlobalValue=%d\n"),GlobalValue); sl@0: sl@0: test.Next(_L("Load gobbler LDD")); sl@0: TInt r = User::LoadLogicalDevice(KGobblerLddFileName); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: RGobbler gobbler; sl@0: r = gobbler.Open(); sl@0: test(r==KErrNone); sl@0: TUint32 taken = gobbler.GobbleRAM(128*1024*1024); sl@0: test.Printf(_L("Gobbled: %dK\n"), taken/1024); sl@0: sl@0: TInt free=FreeRam(); sl@0: test.Printf(_L("Free RAM 0x%08X bytes\n"),free); sl@0: sl@0: RChunk chunk1; sl@0: r=chunk1.CreateLocal(0x400,0x800000); sl@0: test (r==KErrNone); sl@0: TInt i; sl@0: TInt *p=(TInt*)chunk1.Base(); sl@0: test.Printf(_L("Chunk 1 Base %08X\n"),p); sl@0: for (i=0; i<0x100; i++) sl@0: *p++=i*i+41; sl@0: RChunk chunk2; sl@0: r=chunk2.CreateLocal(0x400,0x800000); sl@0: test (r==KErrNone); sl@0: TInt *p2=(TInt*)chunk2.Base(); sl@0: test.Printf(_L("Chunk 2 Base %08X\n"),p2); sl@0: for (i=0; i<0x100; i++) sl@0: *p2++=i*i*i+487; sl@0: r=chunk1.Adjust(0x120000); sl@0: test (r==KErrNone); sl@0: for (i=0x100; i<0x48000; i++) sl@0: *p++=i*i+41; sl@0: r=chunk2.Adjust(0x120000); sl@0: test (r==KErrNone); sl@0: for (i=0x100; i<0x48000; i++) sl@0: *p2++=i*i*i+487; sl@0: p=(TInt*)chunk1.Base(); sl@0: p2=(TInt*)chunk2.Base(); sl@0: for(i=0; i<0x48000; i++) sl@0: { sl@0: TInt read1=*p++; sl@0: TInt read2=*p2++; sl@0: if (read1 != (i*i+41)) sl@0: { sl@0: test.Printf(_L("Chunk 1 i=%X, read %08X expected %08X\n"),i,read1,i*i+41); sl@0: //test.Getch(); sl@0: } sl@0: if (read2 != (i*i*i+487)) sl@0: { sl@0: test.Printf(_L("Chunk 2 i=%X, read %08X expected %08X\n"),i,read2,i*i*i+487); sl@0: //test.Getch(); sl@0: } sl@0: } sl@0: chunk1.Close(); sl@0: chunk2.Close(); sl@0: sl@0: TInt free2=FreeRam(); sl@0: test.Printf(_L("Free RAM 0x%08X bytes\n"),free2); sl@0: test(free2==free); sl@0: sl@0: // Chunks must not be paged otherwise they will not effect the amount sl@0: // of free ram reported plus on h4 swap size is less than the total ram. sl@0: TChunkCreateInfo createInfo; sl@0: createInfo.SetNormal(0, free+2097152); sl@0: createInfo.SetPaging(TChunkCreateInfo::EUnpaged); sl@0: RChunk c1; sl@0: test_KErrNone(c1.Create(createInfo)); sl@0: createInfo.SetNormal(0, 0x1000000); sl@0: RChunk c2; sl@0: RChunk c3; sl@0: RChunk c4; sl@0: RChunk c5; sl@0: RChunk c6; sl@0: test_KErrNone(c2.Create(createInfo)); sl@0: test_KErrNone(c3.Create(createInfo)); sl@0: test_KErrNone(c4.Create(createInfo)); sl@0: test_KErrNone(c5.Create(createInfo)); sl@0: test_KErrNone(c6.Create(createInfo)); sl@0: sl@0: TestAllocFailure(c1,c2); sl@0: r=c3.Adjust(1024); sl@0: test(r==KErrNone); sl@0: TestAllocFailure(c1,c2); sl@0: r=c4.Adjust(1024); sl@0: test(r==KErrNone); sl@0: TestAllocFailure(c1,c2); sl@0: r=c5.Adjust(1024); sl@0: test(r==KErrNone); sl@0: TestAllocFailure(c1,c2); sl@0: r=c6.Adjust(1024); sl@0: test(r==KErrNone); sl@0: TestAllocFailure(c1,c2); sl@0: sl@0: c1.Close(); sl@0: c2.Close(); sl@0: c3.Close(); sl@0: c4.Close(); sl@0: c5.Close(); sl@0: c6.Close(); sl@0: gobbler.Close(); sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: return(KErrNone); sl@0: } sl@0: sl@0: