diff -r 000000000000 -r a41df078684a kerneltest/e32test/nkernsa/testutils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kerneltest/e32test/nkernsa/testutils.cpp Mon Oct 19 15:55:17 2009 +0100 @@ -0,0 +1,212 @@ +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of the License "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// e32test\nkernsa\testutils.cpp +// +// + +#include + +extern "C" { +TUint32 random(TUint32* aSeed) + { + TUint32 x = aSeed[0]; + TUint32 r3 = x >> 1; + r3 |= (aSeed[1] << 31); + aSeed[1] = x & 1; + r3 ^= (x << 12); + x = r3 ^ (r3 >> 20); + aSeed[0] = x; + return x; + } + +void setup_block(TUint32* aBlock, TInt aNumWords) + { + TUint32 seed[2]; + seed[0] = aBlock[0]; + seed[1] = 0; + TInt i; + for (i=1; iiSlotCount = aSlots; + p->iGetIndex = 0; + p->iPutIndex = 0; + p->iBufBase = (TUint32*)malloc(aSlots*sizeof(TUint32)); + if (!p->iBufBase) + { + delete p; + p = 0; + } + return p; + } + +CircBuf::CircBuf() + : iLock(TSpinLock::EOrderGenericIrqLow1) + { + } + +CircBuf::~CircBuf() + { + free(iBufBase); + } + +TInt CircBuf::TryGet(TUint32& aOut) + { + TInt r = KErrUnderflow; + TInt irq = __SPIN_LOCK_IRQSAVE(iLock); + if (iGetIndex != iPutIndex) + { + aOut = iBufBase[iGetIndex++]; + if (iGetIndex == iSlotCount) + iGetIndex = 0; + r = KErrNone; + } + __SPIN_UNLOCK_IRQRESTORE(iLock,irq); + return r; + } + +TInt CircBuf::TryPut(TUint32 aIn) + { + TInt r = KErrOverflow; + TInt irq = __SPIN_LOCK_IRQSAVE(iLock); + TInt nextPut = iPutIndex + 1; + if (nextPut == iSlotCount) + nextPut = 0; + if (iGetIndex != nextPut) + { + iBufBase[iPutIndex] = aIn; + iPutIndex = nextPut; + r = KErrNone; + } + __SPIN_UNLOCK_IRQRESTORE(iLock,irq); + return r; + } + +TUint32 CircBuf::Get() + { + TUint32 x; + while(TryGet(x)!=KErrNone) + NKern::Sleep(1); + return x; + } + +void CircBuf::Put(TUint32 aIn) + { + while(TryPut(aIn)!=KErrNone) + NKern::Sleep(1); + } + +void CircBuf::Reset() + { + TInt irq = __SPIN_LOCK_IRQSAVE(iLock); + iPutIndex = iGetIndex = 0; + __SPIN_UNLOCK_IRQRESTORE(iLock,irq); + } + +TInt CircBuf::Count() + { + TInt irq = __SPIN_LOCK_IRQSAVE(iLock); + TInt r = iPutIndex - iGetIndex; + if (r < 0) + r += iSlotCount; + __SPIN_UNLOCK_IRQRESTORE(iLock,irq); + return r; + }