genericopenlibs/cstdlib/LSTDLIB/REENT.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1998-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 // FUNCTION
       
    15 // <<reent>>---definition of impure data.
       
    16 // INDEX
       
    17 // reent
       
    18 // This module defines the impure data area used by the
       
    19 // non-rentrant functions, such as strtok.
       
    20 // 
       
    21 //
       
    22 
       
    23 #include "SYSIF.H"
       
    24 #include <sys/reent.h>
       
    25 #include <stdlib.h>
       
    26 
       
    27 /* Interim cleanup code */
       
    28 
       
    29 static void cleanup_glue (struct _glue *glue)
       
    30 	{
       
    31 	/* Have to reclaim these in reverse order: */
       
    32 	if (glue->_next)
       
    33 		cleanup_glue (glue->_next);
       
    34 	free (glue);
       
    35 	}
       
    36 
       
    37 extern "C" {
       
    38 
       
    39 /**
       
    40 The struct _reent is managed on a per-thread basis by EPOC32, so there is no
       
    41 global variable _impure_pointer and everyone has to use _REENT (panics in the
       
    42 event of an error) or REENT2 (which returns a NULL pointer in the event of an
       
    43 error).
       
    44 
       
    45 @return
       
    46 @param ptr
       
    47 */
       
    48 EXPORT_C void
       
    49 _reclaim_reent (struct _reent *ptr)
       
    50 	{
       
    51 	/* atexit stuff */
       
    52 	if ((ptr->_atexit) && (ptr->_atexit != &ptr->_atexit0))
       
    53 		{
       
    54 		struct _atexit *p, *q;
       
    55 		for (p = ptr->_atexit; p != &ptr->_atexit0;)
       
    56 			{
       
    57 			q = p;
       
    58 			p = p->_next;
       
    59 			free(q);
       
    60 			}
       
    61 		}
       
    62 
       
    63 	if (ptr->environ)
       
    64 		free(ptr->environ);
       
    65 
       
    66 	if (ptr->_netdb)
       
    67 		free(ptr->_netdb);
       
    68 
       
    69 	if (ptr->__sdidinit)
       
    70 		{
       
    71 		/* cleanup won't reclaim memory 'coz usually it's run
       
    72 		before the program exits, and who wants to wait for that? */
       
    73 		ptr->__cleanup (ptr);
       
    74 		if (ptr->__sglue._next)
       
    75 			cleanup_glue (ptr->__sglue._next);
       
    76 		}
       
    77 
       
    78 	// narrow environment buffer
       
    79 	if (ptr->_pNarrowEnvBuffer)
       
    80 		{
       
    81 		free(ptr->_pNarrowEnvBuffer);
       
    82 		ptr->_pNarrowEnvBuffer = 0;
       
    83 		}
       
    84 
       
    85 	MSystemInterface& sysIf=Interface(ptr);
       
    86 	sysIf.Release();
       
    87 
       
    88 	ptr->_system=0;
       
    89 	}
       
    90 
       
    91 EXPORT_C void _REENT_INIT(struct _reent *ptr)
       
    92 	{
       
    93 	MSystemInterface& sysIf=Interface(ImpurePtr());
       
    94 	_init_reent(ptr,&sysIf.Clone());
       
    95 	}
       
    96 
       
    97 extern "C" void __sinit(struct _reent*);	// LSTDIO/LOCAL.H
       
    98 
       
    99 EXPORT_C void _init_reent(struct _reent *ptr, void* _system)
       
   100 	{
       
   101 	ptr->_system=_system;
       
   102 	__sinit(ptr);
       
   103 	ptr->_next[0]=1;
       
   104 	ptr->_next[1]=1;
       
   105 
       
   106 	ptr->_pNarrowEnvBuffer = 0;
       
   107 	}
       
   108 
       
   109 } // extern "C"