sl@0: /* sl@0: * Copyright (c) 2003-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: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include "randomshim.h" sl@0: sl@0: _LIT(KThreadRandom, "threadrandom.cpp"); sl@0: sl@0: EXPORT_C void SetThreadRandomL(CRandom* aRNG) sl@0: { sl@0: User::LeaveIfError(Dll::SetTls(aRNG)); sl@0: } sl@0: sl@0: EXPORT_C void SetThreadRandomLC(CRandom* aRNG) sl@0: { sl@0: CleanupStack::PushL(aRNG); sl@0: SetThreadRandomL(aRNG); sl@0: //This pop before the push isn't a problem as the PushL can't fail. sl@0: //We just did a push before this and thus there is enough room on the sl@0: //cleanupstack such that OOM is not possible. sl@0: CleanupStack::Pop(aRNG); sl@0: CleanupStack::PushL(TCleanupItem(&DeleteThreadRandom, Dll::Tls())); sl@0: } sl@0: sl@0: void DeleteThreadRandom(TAny* aPtr) sl@0: { sl@0: CRandom* random = reinterpret_cast(aPtr); sl@0: delete random; sl@0: TInt result = Dll::SetTls(0); sl@0: __ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1)); sl@0: } sl@0: sl@0: EXPORT_C void DestroyThreadRandom(void) sl@0: { sl@0: delete (CRandom*)(Dll::Tls()); sl@0: TInt result = Dll::SetTls(0); sl@0: __ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1)); sl@0: } sl@0: sl@0: EXPORT_C void GenerateRandomBytesL(TDes8& aDest) sl@0: { sl@0: TAny* tls = Dll::Tls(); sl@0: if(tls) sl@0: { sl@0: ((CRandom*)tls)->GenerateBytesL(aDest); sl@0: } sl@0: else sl@0: { sl@0: TRandomShim::RandomL(aDest); sl@0: } sl@0: }