os/kernelhwsrv/kerneltest/e32test/mmu/d_memorytest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/mmu/d_memorytest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,690 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\mmu\d_memorytest.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <kernel/kern_priv.h>
    1.22 +#include <kernel/cache.h>
    1.23 +#include "d_memorytest.h"
    1.24 +
    1.25 +//
    1.26 +// Class definitions
    1.27 +//
    1.28 +
    1.29 +class DMemoryTestFactory : public DLogicalDevice
    1.30 +	{
    1.31 +public:
    1.32 +	~DMemoryTestFactory();
    1.33 +	virtual TInt Install();
    1.34 +	virtual void GetCaps(TDes8& aDes) const;
    1.35 +	virtual TInt Create(DLogicalChannelBase*& aChannel);
    1.36 +	};
    1.37 +
    1.38 +class DMemoryTestChannel : public DLogicalChannelBase
    1.39 +	{
    1.40 +public:
    1.41 +	DMemoryTestChannel();
    1.42 +	~DMemoryTestChannel();
    1.43 +	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
    1.44 +	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
    1.45 +private:
    1.46 +	TInt TestAllocZerosMemory();
    1.47 +	TInt TestReAllocZerosMemory();
    1.48 +	TInt AllocTest1();
    1.49 +	TInt ReAllocTest1();
    1.50 +	TInt ReAllocTest2(TUint8*& mem1, TUint8*& mem2, TUint8*& mem3);
    1.51 +	TInt AllocPhysTest(TUint32 aIters, TUint32 aSize); 
    1.52 +	TInt AllocPhysTest1(TUint32 aIters, TUint32 aSize); 
    1.53 +public:
    1.54 +	DMemoryTestFactory*	iFactory;
    1.55 +	TVirtualPinObject* iVirtualPinObject;
    1.56 +	
    1.57 +	struct{
    1.58 +		TPhysicalPinObject* iObject;
    1.59 +		TPhysAddr iPhysAddr;
    1.60 +		TPhysAddr iPhysPageList[KUCPageCount];
    1.61 +		TUint 	iColour;
    1.62 +		TUint32 iActualMapAttr;
    1.63 +		}iPhysicalPinning;
    1.64 +
    1.65 +	struct{
    1.66 +		TKernelMapObject* iObject;
    1.67 +		TPhysAddr iPhysPageList[KUCPageCount];
    1.68 +		TLinAddr iLinAddr;
    1.69 +		}iKernelMapping;
    1.70 +
    1.71 +	TUint32 iPageSize;
    1.72 +	};
    1.73 +
    1.74 +//
    1.75 +// DMemoryTestFactory
    1.76 +//
    1.77 +
    1.78 +TInt DMemoryTestFactory::Install()
    1.79 +	{
    1.80 +	return SetName(&KMemoryTestLddName);
    1.81 +	}
    1.82 +
    1.83 +DMemoryTestFactory::~DMemoryTestFactory()
    1.84 +	{
    1.85 +	}
    1.86 +
    1.87 +void DMemoryTestFactory::GetCaps(TDes8& /*aDes*/) const
    1.88 +	{
    1.89 +	// Not used but required as DLogicalDevice::GetCaps is pure virtual
    1.90 +	}
    1.91 +
    1.92 +TInt DMemoryTestFactory::Create(DLogicalChannelBase*& aChannel)
    1.93 +	{
    1.94 +	aChannel = NULL;
    1.95 +	DMemoryTestChannel* channel=new DMemoryTestChannel;
    1.96 +	if(!channel)
    1.97 +		return KErrNoMemory;
    1.98 +	channel->iFactory = this;
    1.99 +	aChannel = channel;
   1.100 +	return KErrNone;
   1.101 +	}
   1.102 +
   1.103 +DECLARE_STANDARD_LDD()
   1.104 +	{
   1.105 +	return new DMemoryTestFactory;
   1.106 +	}
   1.107 +
   1.108 +//
   1.109 +// DMemoryTestChannel
   1.110 +//
   1.111 +
   1.112 +TInt DMemoryTestChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
   1.113 +	{
   1.114 +	return KErrNone;
   1.115 +	}
   1.116 +
   1.117 +DMemoryTestChannel::DMemoryTestChannel()
   1.118 +	{
   1.119 +	iPageSize = Kern::RoundToPageSize(1);
   1.120 +	}
   1.121 +
   1.122 +DMemoryTestChannel::~DMemoryTestChannel()
   1.123 +	{
   1.124 +	Kern::DestroyVirtualPinObject(iVirtualPinObject);
   1.125 +	}
   1.126 +
   1.127 +
   1.128 +TInt DMemoryTestChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
   1.129 +	{
   1.130 +	TInt r=KErrNotSupported;
   1.131 +
   1.132 +	switch(aFunction)
   1.133 +		{
   1.134 +	case RMemoryTestLdd::EReadWriteMemory:
   1.135 +	case RMemoryTestLdd::EReadMemory:
   1.136 +	case RMemoryTestLdd::EWriteMemory:
   1.137 +		{
   1.138 +		TUint32 value=(TUint32)a2;
   1.139 +#ifdef _DEBUG
   1.140 +		TInt debugMask = Kern::CurrentThread().iDebugMask;
   1.141 +		Kern::CurrentThread().iDebugMask = debugMask&~(1<<KPANIC);
   1.142 +#endif
   1.143 +		XTRAP(r, XT_DEFAULT,
   1.144 +			if(aFunction==RMemoryTestLdd::EReadWriteMemory)
   1.145 +				{
   1.146 +				kumemget32(&value,a1,4);
   1.147 +				kumemput32(a1,&value,4);
   1.148 +				}
   1.149 +			else if(aFunction==RMemoryTestLdd::EReadMemory)
   1.150 +				kumemget32(&value,a1,4);
   1.151 +			else if(aFunction==RMemoryTestLdd::EWriteMemory)
   1.152 +				kumemput32(a1,&value,4);
   1.153 +			);
   1.154 +#ifdef _DEBUG
   1.155 +		Kern::CurrentThread().iDebugMask = debugMask;
   1.156 +#endif
   1.157 +		if(aFunction==RMemoryTestLdd::EReadMemory)
   1.158 +			kumemput32(a2,&value,sizeof(value));
   1.159 +
   1.160 +		return r;
   1.161 +		}
   1.162 +
   1.163 +	case RMemoryTestLdd::ETestAllocZerosMemory:
   1.164 +	case RMemoryTestLdd::ETestReAllocZerosMemory:
   1.165 +		{
   1.166 +		NKern::ThreadEnterCS();		
   1.167 +		TInt r;
   1.168 +		if (aFunction==RMemoryTestLdd::ETestAllocZerosMemory)
   1.169 +			r=TestAllocZerosMemory();
   1.170 +		else
   1.171 +			r=TestReAllocZerosMemory();
   1.172 +		NKern::ThreadLeaveCS();
   1.173 +		return r;
   1.174 +		}
   1.175 +
   1.176 +	case RMemoryTestLdd::ETestAllocPhysTest:
   1.177 +		{
   1.178 +		NKern::ThreadEnterCS();
   1.179 +		r=AllocPhysTest((TUint32)a1,(TUint32)a2);
   1.180 +		NKern::ThreadLeaveCS();
   1.181 +		return r;
   1.182 +		}
   1.183 +
   1.184 +	case RMemoryTestLdd::ETestAllocPhysTest1:
   1.185 +		{
   1.186 +		NKern::ThreadEnterCS();
   1.187 +		r=AllocPhysTest1((TUint32)a1,(TUint32)a2);
   1.188 +		NKern::ThreadLeaveCS();
   1.189 +		return r;
   1.190 +		}
   1.191 +
   1.192 +	case RMemoryTestLdd::ECreateVirtualPinObject:
   1.193 +		{
   1.194 +		NKern::ThreadEnterCS();
   1.195 +		r=Kern::CreateVirtualPinObject(iVirtualPinObject);
   1.196 +		NKern::ThreadLeaveCS();
   1.197 +		return r;
   1.198 +		}
   1.199 +		
   1.200 +	case RMemoryTestLdd::EPinVirtualMemory:
   1.201 +		return Kern::PinVirtualMemory(iVirtualPinObject, (TLinAddr)a1, (TUint)a2);
   1.202 +
   1.203 +	case RMemoryTestLdd::EUnpinVirtualMemory:
   1.204 +		Kern::UnpinVirtualMemory(iVirtualPinObject);
   1.205 +		return KErrNone;
   1.206 +
   1.207 +	case RMemoryTestLdd::EDestroyVirtualPinObject:
   1.208 +		{
   1.209 +		NKern::ThreadEnterCS();
   1.210 +		Kern::DestroyVirtualPinObject(iVirtualPinObject);
   1.211 +		NKern::ThreadLeaveCS();
   1.212 +		return KErrNone;
   1.213 +		}
   1.214 +
   1.215 +	case RMemoryTestLdd::ESetPanicTrace:
   1.216 +		{
   1.217 +		TBool old = false;
   1.218 +#ifdef _DEBUG
   1.219 +		DThread& thread = Kern::CurrentThread();
   1.220 +		TInt debugMask = thread.iDebugMask;
   1.221 +		if(debugMask&(1<<KPANIC))
   1.222 +			old = true;
   1.223 +		if(a1)
   1.224 +			debugMask |= (1<<KPANIC);
   1.225 +		else
   1.226 +			debugMask &= ~(1<<KPANIC);
   1.227 +		thread.iDebugMask = debugMask;
   1.228 +#endif
   1.229 +		return old;
   1.230 +		}
   1.231 +
   1.232 +	case RMemoryTestLdd::EIsMemoryPresent:
   1.233 +#ifndef __WINS__
   1.234 +		return Epoc::LinearToPhysical((TLinAddr)a1) != KPhysAddrInvalid;
   1.235 +#else
   1.236 +		Kern::PanicCurrentThread(_L("IsMemoryPresent should not be used on the emulator"), KErrNotSupported);
   1.237 +		return KErrNotSupported;
   1.238 +#endif
   1.239 +
   1.240 +	case RMemoryTestLdd::ECreatePhysicalPinObject:
   1.241 +		{
   1.242 +		NKern::ThreadEnterCS();
   1.243 +		r=Kern::CreatePhysicalPinObject(iPhysicalPinning.iObject);
   1.244 +		NKern::ThreadLeaveCS();
   1.245 +		return r;
   1.246 +		}
   1.247 +
   1.248 +	case RMemoryTestLdd::EPinPhysicalMemory:
   1.249 +		return Kern::PinPhysicalMemory(iPhysicalPinning.iObject, (TLinAddr)a1, (TUint)a2, EFalse, iPhysicalPinning.iPhysAddr,
   1.250 +							iPhysicalPinning.iPhysPageList, iPhysicalPinning.iActualMapAttr, iPhysicalPinning.iColour, NULL);
   1.251 +
   1.252 +	case RMemoryTestLdd::EPinPhysicalMemoryRO:
   1.253 +		return Kern::PinPhysicalMemory(iPhysicalPinning.iObject, (TLinAddr)a1, (TUint)a2, ETrue, iPhysicalPinning.iPhysAddr,
   1.254 +							iPhysicalPinning.iPhysPageList, iPhysicalPinning.iActualMapAttr, iPhysicalPinning.iColour, NULL);
   1.255 +
   1.256 +	case RMemoryTestLdd::ECheckPageList:
   1.257 +		{
   1.258 +#ifdef __WINS__
   1.259 +		return KErrNotSupported;
   1.260 +#else
   1.261 +		TInt i;
   1.262 +		for (i=0;i<KUCPageCount; i++)
   1.263 +			{
   1.264 +			TPhysAddr addr = Epoc::LinearToPhysical((TLinAddr)a1 + i*iPageSize);
   1.265 +			if (addr==KPhysAddrInvalid) 				 return KErrGeneral;
   1.266 +			if (addr!=iPhysicalPinning.iPhysPageList[i]) return KErrNotFound;
   1.267 +			}
   1.268 +		return KErrNone;
   1.269 +#endif		
   1.270 +		}
   1.271 +
   1.272 +	case RMemoryTestLdd::ESyncPinnedPhysicalMemory:
   1.273 +		return Cache::SyncPhysicalMemoryBeforeDmaWrite(iPhysicalPinning.iPhysPageList,
   1.274 +									iPhysicalPinning.iColour, (TUint)a1, (TUint)a2, iPhysicalPinning.iActualMapAttr);
   1.275 +
   1.276 +	case RMemoryTestLdd::EMovePinnedPhysicalMemory:
   1.277 +		{
   1.278 +#ifdef __WINS__
   1.279 +		return KErrNotSupported;
   1.280 +#else
   1.281 +		TPhysAddr newPage;
   1.282 +		NKern::ThreadEnterCS();
   1.283 +		r = Epoc::MovePhysicalPage(iPhysicalPinning.iPhysPageList[(TUint)a1], newPage);		
   1.284 +		NKern::ThreadLeaveCS();
   1.285 +		return r;
   1.286 +#endif
   1.287 +		}
   1.288 +
   1.289 +	case RMemoryTestLdd::EInvalidatePinnedPhysicalMemory:
   1.290 +		{
   1.291 +		r = Cache::SyncPhysicalMemoryBeforeDmaRead(iPhysicalPinning.iPhysPageList,
   1.292 +									iPhysicalPinning.iColour, (TUint)a1, (TUint)a2, iPhysicalPinning.iActualMapAttr);
   1.293 +		if (r==KErrNone)
   1.294 +			r = Cache::SyncPhysicalMemoryAfterDmaRead(iPhysicalPinning.iPhysPageList,
   1.295 +										iPhysicalPinning.iColour, (TUint)a1, (TUint)a2, iPhysicalPinning.iActualMapAttr);
   1.296 +		return r;	
   1.297 +		}
   1.298 +		
   1.299 +	case RMemoryTestLdd::EUnpinPhysicalMemory:
   1.300 +		return Kern::UnpinPhysicalMemory(iPhysicalPinning.iObject);
   1.301 +
   1.302 +	case RMemoryTestLdd::EDestroyPhysicalPinObject:
   1.303 +		{
   1.304 +		NKern::ThreadEnterCS();
   1.305 +		r=Kern::DestroyPhysicalPinObject(iPhysicalPinning.iObject);
   1.306 +		NKern::ThreadLeaveCS();
   1.307 +		return r;
   1.308 +		}
   1.309 +
   1.310 +	case RMemoryTestLdd::EPinKernelPhysicalMemory:
   1.311 +		{
   1.312 +		TPhysicalPinObject* pinObject;
   1.313 +		TPhysAddr aAddress;
   1.314 +		TPhysAddr aPages[2];
   1.315 +		TUint aColour=0;
   1.316 +		TUint32 actualMemAttr;
   1.317 +		NKern::ThreadEnterCS();
   1.318 +		Kern::CreatePhysicalPinObject(pinObject);
   1.319 +		r = Kern::PinPhysicalMemory(pinObject, (TLinAddr)&aAddress, 4, EFalse, aAddress, aPages, actualMemAttr, aColour, NULL);
   1.320 +		Cache::SyncPhysicalMemoryBeforeDmaWrite(aPages, aColour, 10, 30, actualMemAttr);
   1.321 +		Kern::UnpinPhysicalMemory(pinObject);
   1.322 +		Kern::DestroyPhysicalPinObject(pinObject);
   1.323 +		NKern::ThreadLeaveCS();
   1.324 +		return r;
   1.325 +		}
   1.326 +
   1.327 +	case RMemoryTestLdd::ECreateKernelMapObject:
   1.328 +		{
   1.329 +		NKern::ThreadEnterCS();
   1.330 +		r=Kern::CreateKernelMapObject(iKernelMapping.iObject, (TUint)a1);
   1.331 +		NKern::ThreadLeaveCS();
   1.332 +		return r;
   1.333 +		}
   1.334 +
   1.335 +	case RMemoryTestLdd::EKernelMapMemory:
   1.336 +		return Kern::MapAndPinMemory(	iKernelMapping.iObject, NULL, (TLinAddr)a1, (TUint)a2, 0,
   1.337 +										iKernelMapping.iLinAddr, iKernelMapping.iPhysPageList);
   1.338 +
   1.339 +	case RMemoryTestLdd::EKernelMapMemoryRO:
   1.340 +		return Kern::MapAndPinMemory(	iKernelMapping.iObject, NULL, (TLinAddr)a1, (TUint)a2, Kern::EKernelMap_ReadOnly,
   1.341 +										iKernelMapping.iLinAddr, iKernelMapping.iPhysPageList);
   1.342 +
   1.343 +	case RMemoryTestLdd::EKernelMapMemoryInvalid:
   1.344 +		return Kern::MapAndPinMemory(	iKernelMapping.iObject, NULL, (TLinAddr)a1, (TUint)a2, (TUint)~Kern::EKernelMap_ReadOnly,
   1.345 +										iKernelMapping.iLinAddr, iKernelMapping.iPhysPageList);
   1.346 +
   1.347 +	case RMemoryTestLdd::EKernelMapCheckPageList:
   1.348 +		{
   1.349 +#ifdef __WINS__
   1.350 +		return KErrNotSupported;
   1.351 +#else
   1.352 +		TUint i = 0;
   1.353 +		for (; i < (TUint)KUCPageCount; i++)
   1.354 +			{
   1.355 +			// Compare the user side address to physical addresses
   1.356 +			TPhysAddr addr = Epoc::LinearToPhysical((TLinAddr)a1 + i*iPageSize);
   1.357 +			if (addr == KPhysAddrInvalid) 				 
   1.358 +				return KErrGeneral;
   1.359 +			if (addr != iKernelMapping.iPhysPageList[i]) 
   1.360 +				return KErrNotFound;
   1.361 +			// Compare the kernel side address to physical addresses
   1.362 +			addr = Epoc::LinearToPhysical(iKernelMapping.iLinAddr + i*iPageSize);
   1.363 +			if (addr == KPhysAddrInvalid) 				 
   1.364 +				return KErrGeneral;
   1.365 +			if (addr != iKernelMapping.iPhysPageList[i])
   1.366 +				return KErrNotFound;
   1.367 +			}
   1.368 +		return KErrNone;
   1.369 +#endif		
   1.370 +		}
   1.371 +
   1.372 +	case RMemoryTestLdd::EKernelMapSyncMemory:
   1.373 +		Cache::SyncMemoryBeforeDmaWrite(iKernelMapping.iLinAddr, KUCPageCount*iPageSize);
   1.374 +		return KErrNone;
   1.375 +
   1.376 +	case RMemoryTestLdd::EKernelMapInvalidateMemory:
   1.377 +		{
   1.378 +		Cache::SyncMemoryBeforeDmaRead(iKernelMapping.iLinAddr, KUCPageCount*iPageSize);
   1.379 +		Cache::SyncMemoryAfterDmaRead(iKernelMapping.iLinAddr, KUCPageCount*iPageSize);
   1.380 +		return KErrNone;
   1.381 +		}
   1.382 +
   1.383 +	case RMemoryTestLdd::EKernelMapMoveMemory:
   1.384 +		{
   1.385 +#ifdef __WINS__
   1.386 +		return KErrNotSupported;
   1.387 +#else
   1.388 +		TPhysAddr newPage;
   1.389 +		NKern::ThreadEnterCS();
   1.390 +		r = Epoc::MovePhysicalPage(iKernelMapping.iPhysPageList[(TUint)a1], newPage);		
   1.391 +		NKern::ThreadLeaveCS();
   1.392 +		return r;
   1.393 +#endif
   1.394 +		}
   1.395 +
   1.396 +	case RMemoryTestLdd::EKernelMapReadModifyMemory:
   1.397 +		{
   1.398 +		TUint8* p = (TUint8*)iKernelMapping.iLinAddr;
   1.399 +		// Verify the contents of the data when accessed via the kernel mapping.
   1.400 +		TUint i = 0;
   1.401 +		for (i = 0; i < KUCPageCount*iPageSize; i++)
   1.402 +			{
   1.403 +			if (*p++ != (TUint8)i)
   1.404 +				return KErrCorrupt;
   1.405 +			}
   1.406 +		// Modify the data via the kernel mapping.
   1.407 +		p = (TUint8*)iKernelMapping.iLinAddr;
   1.408 +		for (i = 0; i < KUCPageCount*iPageSize; i++)
   1.409 +			{
   1.410 +			*p++ = (TUint8)(i + 1);
   1.411 +			}
   1.412 +		return KErrNone;
   1.413 +		}
   1.414 +		
   1.415 +	case RMemoryTestLdd::EKernelUnmapMemory:
   1.416 +		Kern::UnmapAndUnpinMemory(iKernelMapping.iObject);
   1.417 +		return KErrNone;
   1.418 +
   1.419 +	case RMemoryTestLdd::EDestroyKernelMapObject:
   1.420 +		{
   1.421 +		NKern::ThreadEnterCS();
   1.422 +		Kern::DestroyKernelMapObject(iKernelMapping.iObject);
   1.423 +		NKern::ThreadLeaveCS();
   1.424 +		return KErrNone;
   1.425 +		}
   1.426 +		
   1.427 +	default:
   1.428 +		return KErrNotSupported;
   1.429 +		}
   1.430 +	}
   1.431 +
   1.432 +// Fail a test by returning an error code indicating the problem
   1.433 +#define FAIL_ALLOC_TEST(testIndex, byteOffset, unexepectedValue) \
   1.434 +	err = ((testIndex) << 16) | ((byteOffset) << 8) | (unexepectedValue)
   1.435 +
   1.436 +TInt DMemoryTestChannel::TestAllocZerosMemory()
   1.437 +	{
   1.438 +	TInt count = 100;
   1.439 +	TInt r = KErrNotSupported;
   1.440 +
   1.441 +	do	{	//re-try up to 100 times if memory conditions are not correct
   1.442 +		r=AllocTest1();
   1.443 +		} while(((r == KErrNoMemory)||(r == KErrUnknown)) && --count);
   1.444 +
   1.445 +	return r;
   1.446 +	}
   1.447 +
   1.448 +TInt DMemoryTestChannel::AllocTest1()
   1.449 +	{
   1.450 +	const TInt KSize = 256;
   1.451 +	TInt err = KErrNone;
   1.452 +	TUint8* mem1 = (TUint8*)Kern::Alloc(KSize);
   1.453 +	if (!mem1)
   1.454 +		return KErrNoMemory;
   1.455 +	memset(mem1, KSize, 0xff);
   1.456 +	Kern::Free(mem1);
   1.457 +	TUint8* mem2 = (TUint8*)Kern::Alloc(KSize);
   1.458 +	if (!mem2)
   1.459 +		return KErrNoMemory;
   1.460 +	if (mem1 != mem2)
   1.461 +		err = KErrUnknown;	// Test inconclusive, can retry
   1.462 +	for (TInt i = 0 ; i<KSize && err==KErrNone; ++i)
   1.463 +		{
   1.464 +		if (mem2[i] != 0)
   1.465 +			FAIL_ALLOC_TEST(1, i, mem2[i]);
   1.466 +		}
   1.467 +	Kern::Free(mem2);
   1.468 +
   1.469 +	return err;
   1.470 +	}
   1.471 +
   1.472 +TInt DMemoryTestChannel::TestReAllocZerosMemory()
   1.473 +	{
   1.474 +	TInt count = 100;
   1.475 +	TInt r = KErrNotSupported;
   1.476 +
   1.477 +	do	{	//re-try up to 100 times if memory conditions are not correct
   1.478 +		r=ReAllocTest1();
   1.479 +		} while(((r == KErrNoMemory)||(r == KErrUnknown)) && --count);
   1.480 +
   1.481 +	if (r!=KErrNone)	
   1.482 +		return r;
   1.483 +
   1.484 +	count = 100;
   1.485 +	do	{	//	re-try up to 100 times if memory conditions are not correct
   1.486 +		TUint8* mem1 = NULL;
   1.487 +		TUint8* mem2 = NULL;
   1.488 +		TUint8* mem3 = NULL;
   1.489 +		r=ReAllocTest2(mem1, mem2, mem3);
   1.490 +		if (mem1)
   1.491 +			Kern::Free(mem1);
   1.492 +		if (mem2)
   1.493 +			Kern::Free(mem2);
   1.494 +		if (mem3)
   1.495 +			Kern::Free(mem3);
   1.496 +		} while(((r == KErrNoMemory)||(r == KErrUnknown)) && --count);
   1.497 +	
   1.498 +	return r;
   1.499 +	}
   1.500 +
   1.501 +// The actual size of the block allocated given the size requested.
   1.502 +#define ALIGNED_SIZE(aReqSize) (_ALIGN_UP(aReqSize + RHeap::EAllocCellSize, RHeap::ECellAlignment) - RHeap::EAllocCellSize)
   1.503 +
   1.504 +// We only acllocate blocks where the size we get is the size we ask for - this
   1.505 +// just makes testing easier.
   1.506 +const TInt KSize = ALIGNED_SIZE(200), KHalfSize = ALIGNED_SIZE(100), KSmallSize = ALIGNED_SIZE(50);
   1.507 +
   1.508 +TInt DMemoryTestChannel::ReAllocTest1()
   1.509 +	{
   1.510 +	// Test case where cell grows
   1.511 +	// 
   1.512 +	// Expected heap layout:
   1.513 +	//   1: [-mem1-------]
   1.514 +	//   2: [-mem1-]
   1.515 +	//   3: [-mem1-------]
   1.516 +
   1.517 +	TInt err = KErrNone;
   1.518 +	TUint8* mem1 = (TUint8*)Kern::Alloc(KSize); // 1
   1.519 +	if (!mem1)
   1.520 +		return KErrNoMemory;
   1.521 +	memset(mem1, 0xff, KSize);
   1.522 +	TUint8* mem2 = (TUint8*)Kern::ReAlloc(mem1, KHalfSize); // 2
   1.523 +	if (mem1 != mem2)
   1.524 +		{
   1.525 +		mem1 = 0;
   1.526 +		Kern::Free(mem2);
   1.527 +		return KErrUnknown; // Don't expect move on shrink
   1.528 +		}
   1.529 +	mem2 = (TUint8*)Kern::ReAlloc(mem1, KSize); // 3
   1.530 +	if (mem1 != mem2)
   1.531 +		{
   1.532 +		mem1 = 0;
   1.533 +		Kern::Free(mem2);
   1.534 +		return KErrUnknown; // Expect growth into original area
   1.535 +		}
   1.536 +	
   1.537 +	TInt i;
   1.538 +	for (i = 0 ; i<KHalfSize && err==KErrNone; ++i)
   1.539 +		{
   1.540 +		if (mem1[i] != 0xff)
   1.541 +			FAIL_ALLOC_TEST(2, i, mem1[i]);
   1.542 +		}
   1.543 +	for (i = KHalfSize ; i<KSize && err==KErrNone; ++i)
   1.544 +		{
   1.545 +		if (mem1[i] != 0)
   1.546 +			FAIL_ALLOC_TEST(3, i, mem1[i]);
   1.547 +		}
   1.548 +
   1.549 +	Kern::Free(mem1);
   1.550 +	return err;
   1.551 +	}
   1.552 +
   1.553 +TInt DMemoryTestChannel::ReAllocTest2(TUint8*& mem1, TUint8*& mem2, TUint8*& mem3)
   1.554 +	{	
   1.555 +	// Test case where cell is moved
   1.556 +	// 
   1.557 +	// Expected heap layout:
   1.558 +	//   1: [ mem1 ]
   1.559 +	//   2: [ mem1 ] [ mem2 ]
   1.560 +	//   3: [ mem1 ] [ mem2 ] [ mem3       ]
   1.561 +	//   4:          [ mem2 ] [ mem1       ] 
   1.562 +
   1.563 +	mem1 = (TUint8*)Kern::Alloc(KSmallSize); // 1
   1.564 +	if (!mem1)
   1.565 +		return KErrNoMemory;
   1.566 +	memset(mem1, 0xff, KSmallSize);	
   1.567 +	mem2 = (TUint8*)Kern::Alloc(KSmallSize); // 2
   1.568 +	if (!mem2)
   1.569 +		return KErrNoMemory;
   1.570 +	if (mem2 <= (mem1 + KSmallSize))
   1.571 +		return KErrUnknown;	// Expect mem2 higher than mem1
   1.572 +	memset(mem2, 0xee, KSmallSize);		
   1.573 +	mem3 = (TUint8*)Kern::Alloc(KSize); // 3
   1.574 +	if (!mem3)
   1.575 +		return KErrNoMemory;
   1.576 +	if (mem3 <= (mem2 + KSmallSize))
   1.577 +		return KErrUnknown;	// Expect mem3 higher than mem2
   1.578 +	memset(mem3, 0xdd, KSize);
   1.579 +	Kern::Free(mem3);
   1.580 +	TUint8* m3 = mem3;
   1.581 +	mem3 = NULL;
   1.582 +	TUint8* mem4 = (TUint8*)Kern::ReAlloc(mem1, KSize); // 4
   1.583 +	if (!mem4)
   1.584 +		return KErrNoMemory;	
   1.585 +	if (mem4 == mem1)
   1.586 +		return KErrUnknown; // Expect move on grow
   1.587 +	mem1=mem4;
   1.588 +	if (mem4 != m3)
   1.589 +		return KErrUnknown; // Expect to realloc to use old mem3 space
   1.590 +	
   1.591 +	TInt i;
   1.592 +	TInt err = KErrNone;
   1.593 +	for (i = 0 ; i<KSmallSize && err==KErrNone; ++i)
   1.594 +		{
   1.595 +		if (mem1[i] != 0xff)
   1.596 +			FAIL_ALLOC_TEST(4, i, mem1[i]);
   1.597 +		}
   1.598 +	for (i = KSmallSize; i<KSize && err==KErrNone; ++i)
   1.599 +		{
   1.600 +		if (mem1[i] != 0)
   1.601 +			FAIL_ALLOC_TEST(5, i, mem1[i]);
   1.602 +		}
   1.603 +
   1.604 +	return err;
   1.605 +	}
   1.606 +
   1.607 +#ifdef __EPOC32__
   1.608 +#define CHECK(c) { if(!(c)) { Kern::Printf("Fail  %d", __LINE__); ; ret = __LINE__;} }
   1.609 +
   1.610 +TInt DMemoryTestChannel::AllocPhysTest(TUint32 aIters, TUint32 aSize)
   1.611 +	{
   1.612 +	TInt	ret = KErrNone;
   1.613 +	TUint32	index;
   1.614 +
   1.615 +	TUint32  pageSize = 0;
   1.616 +	CHECK(Kern::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0)==KErrNone);
   1.617 +	TUint32  numPages = aSize / pageSize;
   1.618 +	TUint32  pageIndex;
   1.619 +	TPhysAddr*	 addrArray = (TPhysAddr *)Kern::AllocZ(sizeof(TPhysAddr) * numPages);
   1.620 +	CHECK(addrArray);
   1.621 +	if(!addrArray)
   1.622 +		{
   1.623 +		return KErrNoMemory;
   1.624 +		}
   1.625 +
   1.626 +	for (index = 0; index < aIters; index ++)
   1.627 +		{
   1.628 +		for (pageIndex = 0; pageIndex < numPages; pageIndex ++)
   1.629 +			{
   1.630 +			ret = Epoc::AllocPhysicalRam(pageSize, addrArray[pageIndex], 0);
   1.631 +			if (ret != KErrNone)
   1.632 +				{
   1.633 +				break;
   1.634 +				}
   1.635 +			}
   1.636 +		for (pageIndex = 0; pageIndex < numPages; pageIndex ++)
   1.637 +			{
   1.638 +			if (addrArray[pageIndex])
   1.639 +				{
   1.640 +				Epoc::FreePhysicalRam(addrArray[pageIndex], pageSize);
   1.641 +				addrArray[pageIndex] = NULL;
   1.642 +				}
   1.643 +			}
   1.644 +		if (ret != KErrNone)
   1.645 +			{
   1.646 +			break;
   1.647 +			}
   1.648 +		}
   1.649 +
   1.650 +	Kern::Free(addrArray);
   1.651 +	return ret;
   1.652 +	}
   1.653 +
   1.654 +#else
   1.655 +
   1.656 +TInt DMemoryTestChannel::AllocPhysTest(TUint32 , TUint32 )
   1.657 +	{
   1.658 +	return KErrNone;
   1.659 +	}
   1.660 +
   1.661 +#endif
   1.662 +
   1.663 +#ifdef __EPOC32__
   1.664 +
   1.665 +TInt DMemoryTestChannel::AllocPhysTest1(TUint32 aIters, TUint32 aSize)
   1.666 +	{
   1.667 +	TInt	ret = KErrNone;
   1.668 +	TUint32	index;
   1.669 +
   1.670 +	TUint32  pageSize = 0;
   1.671 +	CHECK(Kern::HalFunction(EHalGroupKernel,EKernelHalPageSizeInBytes,&pageSize,0)==KErrNone);
   1.672 +	TUint32 numPages = aSize / pageSize;
   1.673 +	TPhysAddr*	 addrArray = (TPhysAddr *)Kern::AllocZ(sizeof(TPhysAddr) * numPages);
   1.674 +	for (index = 0; index < aIters; index ++)
   1.675 +		{
   1.676 +		ret = Epoc::AllocPhysicalRam(numPages, addrArray);
   1.677 +		if (ret != KErrNone)
   1.678 +			{
   1.679 +			break;
   1.680 +			}
   1.681 +		Epoc::FreePhysicalRam(numPages, addrArray);
   1.682 +		}
   1.683 +	Kern::Free(addrArray);
   1.684 +	return ret;
   1.685 +	}
   1.686 +#else
   1.687 +
   1.688 +TInt DMemoryTestChannel::AllocPhysTest1(TUint32 , TUint32 )
   1.689 +	{
   1.690 +	return KErrNone;
   1.691 +	}
   1.692 +
   1.693 +#endif