os/ossrv/genericopenlibs/cstdlib/USTLIB/ESTLIB.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/USTLIB/ESTLIB.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,214 @@
     1.4 +// Copyright (c) 1997-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 "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 +//
    1.18 +
    1.19 +#include "POSIXIF.H"
    1.20 +
    1.21 +// Category for the panic.
    1.22 +_LIT(KEstlibInit, "ESTLIB-INIT");
    1.23 +
    1.24 +// Access for the outside world
    1.25 +extern "C" {
    1.26 +
    1.27 +EXPORT_C void CloseSTDLIB()
    1.28 +	{
    1.29 +	struct _reent* p=(struct _reent*)Dll::Tls();
    1.30 +	if (p==0)
    1.31 +		return;
    1.32 +	_reclaim_reent(p);	// Reclaiming calls the atexit processing and generally tries to tidy up
    1.33 +	User::Free(p);		// then we give back the struct _reent itself
    1.34 +	Dll::SetTls(0);		// ... so we don't free it again when the DLL exits
    1.35 +	}
    1.36 +
    1.37 +/**
    1.38 +Allocates memory for the library globals struct and returns a pointer to it. If the
    1.39 +memory has been allocated previously then it simply returns a pointer to the memory.
    1.40 +Panics if any error/failure occurs.
    1.41 +
    1.42 +@return On Success, a pointer to the memory containing the library globals.
    1.43 +*/
    1.44 +EXPORT_C struct _reent *ImpurePtr(void)
    1.45 +	{
    1.46 +	struct _reent* p=(struct _reent*)Dll::Tls();
    1.47 +	if (p)
    1.48 +		return p; // Memory is already allocated for the library globals.
    1.49 +
    1.50 +	// First use, so construct the default struct _reent and the associated SystemInterface
    1.51 +	p=(struct _reent*)User::Alloc(sizeof(struct _reent));
    1.52 +	if(p ==0)
    1.53 +		{
    1.54 +		User::Panic(KEstlibInit,KErrNoMemory);
    1.55 +		}
    1.56 +
    1.57 +	TInt err= Dll::SetTls(p);
    1.58 +
    1.59 +	if (err != KErrNone)
    1.60 +		{
    1.61 +		delete p;
    1.62 +		p = 0;
    1.63 +		Dll::FreeTls();
    1.64 +		User::Panic(KEstlibInit, err);
    1.65 +		}
    1.66 +
    1.67 +	void* sysIf=0;
    1.68 +
    1.69 +	CProcessSystemInterface* pSysIf=new CProcessSystemInterface;
    1.70 +	if (pSysIf && pSysIf->Connect()==KErrNone)
    1.71 +		{
    1.72 +		sysIf=pSysIf;
    1.73 +		}
    1.74 +	else
    1.75 +		{
    1.76 +		delete pSysIf;
    1.77 +		pSysIf = 0;
    1.78 +
    1.79 +		CTrapCleanup *cleanup = NULL;
    1.80 +		if(User::TrapHandler() == NULL)
    1.81 +			{
    1.82 +			cleanup = CTrapCleanup::New();
    1.83 +			if(cleanup != NULL)
    1.84 +		        {
    1.85 +				//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
    1.86 +				TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
    1.87 +		        }
    1.88 +		    else
    1.89 +		    	{
    1.90 +				err = KErrNoMemory;
    1.91 +				}
    1.92 +	    	delete cleanup;
    1.93 +			}
    1.94 +		else
    1.95 +			{
    1.96 +			//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
    1.97 +			TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
    1.98 +			}
    1.99 +		}
   1.100 +
   1.101 +	if (err != KErrNone)
   1.102 +		{
   1.103 +	    delete p;
   1.104 +	    p = 0;
   1.105 +		Dll::FreeTls();
   1.106 +		User::Panic(KEstlibInit, err);
   1.107 +		}
   1.108 +
   1.109 +	Mem::FillZ(p,sizeof(struct _reent));
   1.110 +	_init_reent(p,sysIf);
   1.111 +
   1.112 +	return p;
   1.113 +	}
   1.114 +
   1.115 +/**
   1.116 +This is a panic free version of ImpurePtr. It allocates memory for the library globals
   1.117 +struct and returns a pointer to it. If the memory has been allocated previously then it
   1.118 +simply returns a pointer to the memory.
   1.119 +If there is not enough memory available to set up the library globals or other error occurs,
   1.120 +a NULL pointer will be returned.
   1.121 +
   1.122 +@return On Success, a pointer to the memory containing the library globals.
   1.123 +		On Failure, a NULL pointer.
   1.124 +*/
   1.125 +EXPORT_C struct _reent * ImpurePtr2(void)
   1.126 +	{
   1.127 +	struct _reent* p = (struct _reent*)Dll::Tls();
   1.128 +	if (p)
   1.129 +		return p; // Memory is already allocated for the library globals.
   1.130 +
   1.131 +	// First use, so construct the default struct _reent and the associated SystemInterface
   1.132 +	p =(struct _reent*)User::Alloc(sizeof(struct _reent));
   1.133 +	if (p== 0)
   1.134 +		return p ; // NULL
   1.135 +
   1.136 +	if (Dll::SetTls(p))
   1.137 +		{
   1.138 +		delete p;
   1.139 +		p = 0;
   1.140 +		Dll::FreeTls();
   1.141 +		return p; // NULL
   1.142 +		}
   1.143 +
   1.144 +	void* sysIf=0;
   1.145 +	CProcessSystemInterface* pSysIf=new CProcessSystemInterface;
   1.146 +	if (pSysIf && pSysIf->Connect()==KErrNone)
   1.147 +		{
   1.148 +		sysIf=pSysIf;
   1.149 +		}
   1.150 +	else
   1.151 +		{
   1.152 +		TInt err=KErrNone;
   1.153 +		delete pSysIf;
   1.154 +		pSysIf = 0;
   1.155 +
   1.156 +		CTrapCleanup *cleanup = NULL;
   1.157 +		if(User::TrapHandler() == NULL)
   1.158 +			{
   1.159 +			cleanup = CTrapCleanup::New();
   1.160 +			if(cleanup != NULL)
   1.161 +		        {
   1.162 +				//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
   1.163 +				TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
   1.164 +		        }
   1.165 +	    	delete cleanup;
   1.166 +			}
   1.167 +		else
   1.168 +			{
   1.169 +			//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
   1.170 +			TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
   1.171 +			}
   1.172 +		}
   1.173 +
   1.174 +	if (!sysIf)
   1.175 +		{
   1.176 +		delete p;
   1.177 +		p = 0;
   1.178 +		Dll::FreeTls();
   1.179 +		return p; // NULL
   1.180 +		}
   1.181 +
   1.182 +	Mem::FillZ(p,sizeof(struct _reent));
   1.183 +	_init_reent(p,sysIf);
   1.184 +
   1.185 +	return p; // Library globals have been set up correctly.
   1.186 +	}
   1.187 +
   1.188 +
   1.189 +EXPORT_C int *__errno(void)
   1.190 +	{
   1.191 +	return &(ImpurePtr()->_errno);
   1.192 +	}
   1.193 +
   1.194 +EXPORT_C void _exit (int status) _ATTRIBUTE((noreturn))
   1.195 +	{
   1.196 +	struct _reent* p=(struct _reent*)Dll::Tls();
   1.197 +	if (p)
   1.198 +		{
   1.199 +		MSystemInterface& sysIf=Interface(p);
   1.200 +		sysIf.TerminateProcess(status);
   1.201 +		}
   1.202 +	RProcess().Terminate(status);	// just in case
   1.203 +	}
   1.204 +
   1.205 +} // extern "C"
   1.206 +
   1.207 +#ifndef EKA2
   1.208 +GLDEF_C TInt E32Dll(TDllReason)
   1.209 +//
   1.210 +// DLL entry point
   1.211 +//
   1.212 +	{
   1.213 +
   1.214 +	return KErrNone;
   1.215 +	}
   1.216 +#endif
   1.217 +