source: rtems/cpukit/libcsupport/src/newlibc_reent.c @ 2e8737a

4.104.115
Last change on this file since 2e8737a was 2e8737a, checked in by Ralf Corsepius <ralf.corsepius@…>, on Sep 15, 2009 at 9:42:46 AM

Add attribute((unused)) to function arguments.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  The license and distribution terms for this file may be
3 *  found in the file LICENSE in this distribution or at
4 *  http://www.rtems.com/license/LICENSE.
5 *
6 *  $Id$
7 *
8 */
9
10#if HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
15#include <rtems.h>
16
17#if defined(RTEMS_NEWLIB)
18#include <rtems/libcsupport.h>
19
20/* Since we compile with strict ANSI we need to undef it to get
21 * prototypes for extensions
22 */
23#undef __STRICT_ANSI__
24
25#include <stdlib.h>             /* for free() */
26#include <string.h>             /* for memset() */
27
28#include <sys/reent.h>          /* for extern of _REENT (aka _impure_ptr) */
29#include <errno.h>
30
31/*
32 *  NOTE:
33 *        There is some problem with doing this on the hpux version
34 *        of the UNIX simulator (symptom is printf core dumps), so
35 *        we just don't for now.
36 *        Not sure if this is a problem with hpux, newlib, or something else.
37 */
38
39#if defined(RTEMS_UNIX) && !defined(hpux)
40#define NEED_SETVBUF
41#endif
42
43#include <stdio.h>
44
45int _fwalk(struct _reent *ptr, int (*function) (FILE *) );
46
47extern struct _reent * const _global_impure_ptr __ATTRIBUTE_IMPURE_PTR__;
48/*
49 * reent struct allocation moved here from libc_start_hook() to avoid
50 * mutual exclusion problems when memory is allocated from the start hook.
51 *
52 * Memory is also now allocated from the workspace rather than the heap.
53 *  -- ptorre 9/30/03
54 */
55bool newlib_create_hook(
56  rtems_tcb *current_task __attribute__((unused)),
57  rtems_tcb *creating_task
58)
59{
60  struct _reent *ptr;
61
62  if (_Thread_libc_reent == 0)
63  {
64    _REENT = _global_impure_ptr;
65
66    _Thread_Set_libc_reent (&_REENT);
67  }
68
69  /*  NOTE: The RTEMS malloc is reentrant without a reent ptr since
70   *        it is based on the Classic API Region Manager.
71   */
72
73  #define REENT_MALLOCED 0
74  #if REENT_MALLOCED
75    ptr = (struct _reent *) calloc(1, sizeof(struct _reent));
76  #else
77    /* It is OK to allocate from the workspace because these
78     * hooks run with thread dispatching disabled.
79     */
80    ptr = (struct _reent *) _Workspace_Allocate(sizeof(struct _reent));
81  #endif
82
83  if (ptr) {
84    _REENT_INIT_PTR((ptr)); /* GCC extension: structure constants */
85    creating_task->libc_reent = ptr;
86    return TRUE;
87  }
88
89  return FALSE;
90}
91
92/*
93 * Called for all user TASKS (system tasks are MPCI Receive Server and IDLE)
94 */
95
96#ifdef NEED_SETVBUF
97rtems_extension newlib_begin_hook(rtems_tcb *current_task)
98{
99  setvbuf( stdout, NULL, _IOLBF, BUFSIZ );
100}
101#endif
102
103/*
104 *  Called when a task is deleted.
105 *  Must restore the new lib reentrancy state for the new current
106 *  task.
107 *
108 */
109
110int newlib_free_buffers(
111  FILE *fp
112)
113{
114  switch ( fileno(fp) ) {
115    case 0:
116    case 1:
117    case 2:
118      if (fp->_flags & __SMBF) {
119        free( fp->_bf._base );
120        fp->_flags &= ~__SMBF;
121        fp->_bf._base = fp->_p = (unsigned char *) NULL;
122      }
123      break;
124    default:
125     fclose(fp);
126  }
127  return 0;
128}
129
130rtems_extension newlib_delete_hook(
131  rtems_tcb *current_task,
132  rtems_tcb *deleted_task
133)
134{
135  struct _reent *ptr;
136
137  /*
138   * The reentrancy structure was allocated by newlib using malloc()
139   */
140
141  if (current_task == deleted_task) {
142    ptr = _REENT;
143  } else {
144    ptr = deleted_task->libc_reent;
145  }
146
147  if (ptr && ptr != _global_impure_ptr) {
148/*
149    _wrapup_reent(ptr);
150    _reclaim_reent(ptr);
151*/
152    /*
153     *  Just in case there are some buffers lying around.
154     */
155    _fwalk(ptr, newlib_free_buffers);
156#if REENT_MALLOCED
157    free(ptr);
158#else
159    _Workspace_Free(ptr);
160#endif
161  }
162
163  deleted_task->libc_reent = NULL;
164
165  /*
166   * Require the switch back to another task to install its own
167   */
168
169  if ( current_task == deleted_task ) {
170    _REENT = 0;
171  }
172}
173
174#endif
Note: See TracBrowser for help on using the repository browser.