genericopenlibs/cstdlib/USTLIB/ESTLIB.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "POSIXIF.H"
       
    17 
       
    18 // Category for the panic.
       
    19 _LIT(KEstlibInit, "ESTLIB-INIT");
       
    20 
       
    21 // Access for the outside world
       
    22 extern "C" {
       
    23 
       
    24 EXPORT_C void CloseSTDLIB()
       
    25 	{
       
    26 	struct _reent* p=(struct _reent*)Dll::Tls();
       
    27 	if (p==0)
       
    28 		return;
       
    29 	_reclaim_reent(p);	// Reclaiming calls the atexit processing and generally tries to tidy up
       
    30 	User::Free(p);		// then we give back the struct _reent itself
       
    31 	Dll::SetTls(0);		// ... so we don't free it again when the DLL exits
       
    32 	}
       
    33 
       
    34 /**
       
    35 Allocates memory for the library globals struct and returns a pointer to it. If the
       
    36 memory has been allocated previously then it simply returns a pointer to the memory.
       
    37 Panics if any error/failure occurs.
       
    38 
       
    39 @return On Success, a pointer to the memory containing the library globals.
       
    40 */
       
    41 EXPORT_C struct _reent *ImpurePtr(void)
       
    42 	{
       
    43 	struct _reent* p=(struct _reent*)Dll::Tls();
       
    44 	if (p)
       
    45 		return p; // Memory is already allocated for the library globals.
       
    46 
       
    47 	// First use, so construct the default struct _reent and the associated SystemInterface
       
    48 	p=(struct _reent*)User::Alloc(sizeof(struct _reent));
       
    49 	if(p ==0)
       
    50 		{
       
    51 		User::Panic(KEstlibInit,KErrNoMemory);
       
    52 		}
       
    53 
       
    54 	TInt err= Dll::SetTls(p);
       
    55 
       
    56 	if (err != KErrNone)
       
    57 		{
       
    58 		delete p;
       
    59 		p = 0;
       
    60 		Dll::FreeTls();
       
    61 		User::Panic(KEstlibInit, err);
       
    62 		}
       
    63 
       
    64 	void* sysIf=0;
       
    65 
       
    66 	CProcessSystemInterface* pSysIf=new CProcessSystemInterface;
       
    67 	if (pSysIf && pSysIf->Connect()==KErrNone)
       
    68 		{
       
    69 		sysIf=pSysIf;
       
    70 		}
       
    71 	else
       
    72 		{
       
    73 		delete pSysIf;
       
    74 		pSysIf = 0;
       
    75 
       
    76 		CTrapCleanup *cleanup = NULL;
       
    77 		if(User::TrapHandler() == NULL)
       
    78 			{
       
    79 			cleanup = CTrapCleanup::New();
       
    80 			if(cleanup != NULL)
       
    81 		        {
       
    82 				//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
       
    83 				TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
       
    84 		        }
       
    85 		    else
       
    86 		    	{
       
    87 				err = KErrNoMemory;
       
    88 				}
       
    89 	    	delete cleanup;
       
    90 			}
       
    91 		else
       
    92 			{
       
    93 			//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
       
    94 			TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
       
    95 			}
       
    96 		}
       
    97 
       
    98 	if (err != KErrNone)
       
    99 		{
       
   100 	    delete p;
       
   101 	    p = 0;
       
   102 		Dll::FreeTls();
       
   103 		User::Panic(KEstlibInit, err);
       
   104 		}
       
   105 
       
   106 	Mem::FillZ(p,sizeof(struct _reent));
       
   107 	_init_reent(p,sysIf);
       
   108 
       
   109 	return p;
       
   110 	}
       
   111 
       
   112 /**
       
   113 This is a panic free version of ImpurePtr. It allocates memory for the library globals
       
   114 struct and returns a pointer to it. If the memory has been allocated previously then it
       
   115 simply returns a pointer to the memory.
       
   116 If there is not enough memory available to set up the library globals or other error occurs,
       
   117 a NULL pointer will be returned.
       
   118 
       
   119 @return On Success, a pointer to the memory containing the library globals.
       
   120 		On Failure, a NULL pointer.
       
   121 */
       
   122 EXPORT_C struct _reent * ImpurePtr2(void)
       
   123 	{
       
   124 	struct _reent* p = (struct _reent*)Dll::Tls();
       
   125 	if (p)
       
   126 		return p; // Memory is already allocated for the library globals.
       
   127 
       
   128 	// First use, so construct the default struct _reent and the associated SystemInterface
       
   129 	p =(struct _reent*)User::Alloc(sizeof(struct _reent));
       
   130 	if (p== 0)
       
   131 		return p ; // NULL
       
   132 
       
   133 	if (Dll::SetTls(p))
       
   134 		{
       
   135 		delete p;
       
   136 		p = 0;
       
   137 		Dll::FreeTls();
       
   138 		return p; // NULL
       
   139 		}
       
   140 
       
   141 	void* sysIf=0;
       
   142 	CProcessSystemInterface* pSysIf=new CProcessSystemInterface;
       
   143 	if (pSysIf && pSysIf->Connect()==KErrNone)
       
   144 		{
       
   145 		sysIf=pSysIf;
       
   146 		}
       
   147 	else
       
   148 		{
       
   149 		TInt err=KErrNone;
       
   150 		delete pSysIf;
       
   151 		pSysIf = 0;
       
   152 
       
   153 		CTrapCleanup *cleanup = NULL;
       
   154 		if(User::TrapHandler() == NULL)
       
   155 			{
       
   156 			cleanup = CTrapCleanup::New();
       
   157 			if(cleanup != NULL)
       
   158 		        {
       
   159 				//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
       
   160 				TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
       
   161 		        }
       
   162 	    	delete cleanup;
       
   163 			}
       
   164 		else
       
   165 			{
       
   166 			//use static_cast to perform the valid conversion from CLocalSystemInterface to base class MSystemInterface
       
   167 			TRAP(err, sysIf = static_cast<MSystemInterface*> (CLocalSystemInterface::NewL()));
       
   168 			}
       
   169 		}
       
   170 
       
   171 	if (!sysIf)
       
   172 		{
       
   173 		delete p;
       
   174 		p = 0;
       
   175 		Dll::FreeTls();
       
   176 		return p; // NULL
       
   177 		}
       
   178 
       
   179 	Mem::FillZ(p,sizeof(struct _reent));
       
   180 	_init_reent(p,sysIf);
       
   181 
       
   182 	return p; // Library globals have been set up correctly.
       
   183 	}
       
   184 
       
   185 
       
   186 EXPORT_C int *__errno(void)
       
   187 	{
       
   188 	return &(ImpurePtr()->_errno);
       
   189 	}
       
   190 
       
   191 EXPORT_C void _exit (int status) _ATTRIBUTE((noreturn))
       
   192 	{
       
   193 	struct _reent* p=(struct _reent*)Dll::Tls();
       
   194 	if (p)
       
   195 		{
       
   196 		MSystemInterface& sysIf=Interface(p);
       
   197 		sysIf.TerminateProcess(status);
       
   198 		}
       
   199 	RProcess().Terminate(status);	// just in case
       
   200 	}
       
   201 
       
   202 } // extern "C"
       
   203 
       
   204 #ifndef EKA2
       
   205 GLDEF_C TInt E32Dll(TDllReason)
       
   206 //
       
   207 // DLL entry point
       
   208 //
       
   209 	{
       
   210 
       
   211 	return KErrNone;
       
   212 	}
       
   213 #endif
       
   214