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