sl@0: // Copyright (c) 2006-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\nkernsa\testutils.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: extern "C" { sl@0: TUint32 random(TUint32* aSeed) sl@0: { sl@0: TUint32 x = aSeed[0]; sl@0: TUint32 r3 = x >> 1; sl@0: r3 |= (aSeed[1] << 31); sl@0: aSeed[1] = x & 1; sl@0: r3 ^= (x << 12); sl@0: x = r3 ^ (r3 >> 20); sl@0: aSeed[0] = x; sl@0: return x; sl@0: } sl@0: sl@0: void setup_block(TUint32* aBlock, TInt aNumWords) sl@0: { sl@0: TUint32 seed[2]; sl@0: seed[0] = aBlock[0]; sl@0: seed[1] = 0; sl@0: TInt i; sl@0: for (i=1; iiSlotCount = aSlots; sl@0: p->iGetIndex = 0; sl@0: p->iPutIndex = 0; sl@0: p->iBufBase = (TUint32*)malloc(aSlots*sizeof(TUint32)); sl@0: if (!p->iBufBase) sl@0: { sl@0: delete p; sl@0: p = 0; sl@0: } sl@0: return p; sl@0: } sl@0: sl@0: CircBuf::CircBuf() sl@0: : iLock(TSpinLock::EOrderGenericIrqLow1) sl@0: { sl@0: } sl@0: sl@0: CircBuf::~CircBuf() sl@0: { sl@0: free(iBufBase); sl@0: } sl@0: sl@0: TInt CircBuf::TryGet(TUint32& aOut) sl@0: { sl@0: TInt r = KErrUnderflow; sl@0: TInt irq = __SPIN_LOCK_IRQSAVE(iLock); sl@0: if (iGetIndex != iPutIndex) sl@0: { sl@0: aOut = iBufBase[iGetIndex++]; sl@0: if (iGetIndex == iSlotCount) sl@0: iGetIndex = 0; sl@0: r = KErrNone; sl@0: } sl@0: __SPIN_UNLOCK_IRQRESTORE(iLock,irq); sl@0: return r; sl@0: } sl@0: sl@0: TInt CircBuf::TryPut(TUint32 aIn) sl@0: { sl@0: TInt r = KErrOverflow; sl@0: TInt irq = __SPIN_LOCK_IRQSAVE(iLock); sl@0: TInt nextPut = iPutIndex + 1; sl@0: if (nextPut == iSlotCount) sl@0: nextPut = 0; sl@0: if (iGetIndex != nextPut) sl@0: { sl@0: iBufBase[iPutIndex] = aIn; sl@0: iPutIndex = nextPut; sl@0: r = KErrNone; sl@0: } sl@0: __SPIN_UNLOCK_IRQRESTORE(iLock,irq); sl@0: return r; sl@0: } sl@0: sl@0: TUint32 CircBuf::Get() sl@0: { sl@0: TUint32 x; sl@0: while(TryGet(x)!=KErrNone) sl@0: NKern::Sleep(1); sl@0: return x; sl@0: } sl@0: sl@0: void CircBuf::Put(TUint32 aIn) sl@0: { sl@0: while(TryPut(aIn)!=KErrNone) sl@0: NKern::Sleep(1); sl@0: } sl@0: sl@0: void CircBuf::Reset() sl@0: { sl@0: TInt irq = __SPIN_LOCK_IRQSAVE(iLock); sl@0: iPutIndex = iGetIndex = 0; sl@0: __SPIN_UNLOCK_IRQRESTORE(iLock,irq); sl@0: } sl@0: sl@0: TInt CircBuf::Count() sl@0: { sl@0: TInt irq = __SPIN_LOCK_IRQSAVE(iLock); sl@0: TInt r = iPutIndex - iGetIndex; sl@0: if (r < 0) sl@0: r += iSlotCount; sl@0: __SPIN_UNLOCK_IRQRESTORE(iLock,irq); sl@0: return r; sl@0: }