source: trunk/libs/newlib/src/newlib/libc/reent/reent.c @ 620

Last change on this file since 620 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.7 KB
Line 
1/*
2FUNCTION
3        <<reent>>---definition of impure data.
4       
5INDEX
6        reent
7
8DESCRIPTION
9        This module defines the impure data area used by the
10        non-reentrant functions, such as strtok.
11*/
12
13#include <stdlib.h>
14#include <reent.h>
15
16#ifdef _REENT_ONLY
17#ifndef REENTRANT_SYSCALLS_PROVIDED
18#define REENTRANT_SYSCALLS_PROVIDED
19#endif
20#endif
21
22#ifndef REENTRANT_SYSCALLS_PROVIDED
23
24/* We use the errno variable used by the system dependent layer.  */
25#undef errno
26int errno;
27
28#endif
29
30/* Interim cleanup code */
31
32void
33cleanup_glue (struct _reent *ptr,
34     struct _glue *glue)
35{
36  /* Have to reclaim these in reverse order: */
37  if (glue->_next)
38    cleanup_glue (ptr, glue->_next);
39
40  _free_r (ptr, glue);
41}
42
43void
44_reclaim_reent (struct _reent *ptr)
45{
46  if (ptr != _impure_ptr)
47    {
48      /* used by mprec routines. */
49#ifdef _REENT_SMALL
50      if (ptr->_mp)     /* don't bother allocating it! */
51      {
52#endif
53      if (_REENT_MP_FREELIST(ptr))
54        {
55          int i;
56          for (i = 0; i < _Kmax; i++) 
57            {
58              struct _Bigint *thisone, *nextone;
59       
60              nextone = _REENT_MP_FREELIST(ptr)[i];
61              while (nextone)
62                {
63                  thisone = nextone;
64                  nextone = nextone->_next;
65                  _free_r (ptr, thisone);
66                }
67            }   
68
69          _free_r (ptr, _REENT_MP_FREELIST(ptr));
70        }
71      if (_REENT_MP_RESULT(ptr))
72        _free_r (ptr, _REENT_MP_RESULT(ptr));
73#ifdef _REENT_SMALL
74      }
75#endif
76
77#ifdef _REENT_SMALL
78      if (ptr->_emergency)
79        _free_r (ptr, ptr->_emergency);
80      if (ptr->_mp)
81        _free_r (ptr, ptr->_mp);
82      if (ptr->_r48)
83        _free_r (ptr, ptr->_r48);
84      if (ptr->_localtime_buf)
85        _free_r (ptr, ptr->_localtime_buf);
86      if (ptr->_asctime_buf)
87        _free_r (ptr, ptr->_asctime_buf);
88          if (ptr->_signal_buf)
89        _free_r (ptr, ptr->_signal_buf);
90          if (ptr->_misc)
91        _free_r (ptr, ptr->_misc);
92#endif
93
94#ifndef _REENT_GLOBAL_ATEXIT
95      /* atexit stuff */
96# ifdef _REENT_SMALL
97      if (ptr->_atexit && ptr->_atexit->_on_exit_args_ptr)
98        _free_r (ptr, ptr->_atexit->_on_exit_args_ptr);
99# else
100      if ((ptr->_atexit) && (ptr->_atexit != &ptr->_atexit0))
101        {
102          struct _atexit *p, *q;
103          for (p = ptr->_atexit; p != &ptr->_atexit0;)
104            {
105              q = p;
106              p = p->_next;
107              _free_r (ptr, q);
108            }
109        }
110# endif
111#endif
112
113      if (ptr->_cvtbuf)
114        _free_r (ptr, ptr->_cvtbuf);
115    /* We should free _sig_func to avoid a memory leak, but how to
116           do it safely considering that a signal may be delivered immediately
117           after the free?
118          if (ptr->_sig_func)
119        _free_r (ptr, ptr->_sig_func);*/
120
121      if (ptr->__sdidinit)
122        {
123          /* cleanup won't reclaim memory 'coz usually it's run
124             before the program exits, and who wants to wait for that? */
125          ptr->__cleanup (ptr);
126
127          if (ptr->__sglue._next)
128            cleanup_glue (ptr, ptr->__sglue._next);
129        }
130
131      /* Malloc memory not reclaimed; no good way to return memory anyway. */
132
133    }
134}
Note: See TracBrowser for help on using the repository browser.