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