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