[ac7d5ef0] | 1 | /* |
---|
[98e4ebf5] | 2 | * The license and distribution terms for this file may be |
---|
| 3 | * found in the file LICENSE in this distribution or at |
---|
[0eae36c7] | 4 | * http://www.rtems.com/license/LICENSE. |
---|
[ac7d5ef0] | 5 | * |
---|
[3235ad9] | 6 | * $Id$ |
---|
[ac7d5ef0] | 7 | * |
---|
| 8 | */ |
---|
| 9 | |
---|
[9c49db4] | 10 | #if HAVE_CONFIG_H |
---|
| 11 | #include "config.h" |
---|
| 12 | #endif |
---|
| 13 | |
---|
[1a8fde6c] | 14 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
[ac7d5ef0] | 15 | #include <rtems.h> |
---|
[dcec5a4] | 16 | |
---|
| 17 | #if defined(RTEMS_NEWLIB) |
---|
[3ba74c73] | 18 | #include <rtems/libcsupport.h> |
---|
[07e9194e] | 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 | |
---|
[ac7d5ef0] | 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) */ |
---|
[8d7a1d76] | 29 | #include <errno.h> |
---|
[ac7d5ef0] | 30 | |
---|
[1f94ed6b] | 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 | */ |
---|
[50f32b11] | 38 | |
---|
[1f94ed6b] | 39 | #if defined(RTEMS_UNIX) && !defined(hpux) |
---|
| 40 | #define NEED_SETVBUF |
---|
| 41 | #endif |
---|
[50f32b11] | 42 | |
---|
[1f94ed6b] | 43 | #include <stdio.h> |
---|
[eaa442fc] | 44 | |
---|
[c9a4da01] | 45 | int _fwalk(struct _reent *ptr, int (*function) (FILE *) ); |
---|
| 46 | |
---|
[d40da79b] | 47 | extern struct _reent * const _global_impure_ptr __ATTRIBUTE_IMPURE_PTR__; |
---|
[50f32b11] | 48 | /* |
---|
[6cff0f8] | 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 | */ |
---|
[d40da79b] | 55 | bool newlib_create_hook( |
---|
[2e8737a] | 56 | rtems_tcb *current_task __attribute__((unused)), |
---|
[7edb9281] | 57 | rtems_tcb *creating_task |
---|
| 58 | ) |
---|
[ac7d5ef0] | 59 | { |
---|
[7edb9281] | 60 | struct _reent *ptr; |
---|
[ac7d5ef0] | 61 | |
---|
[d40da79b] | 62 | if (_Thread_libc_reent == 0) |
---|
| 63 | { |
---|
| 64 | _REENT = _global_impure_ptr; |
---|
| 65 | |
---|
| 66 | _Thread_Set_libc_reent (&_REENT); |
---|
| 67 | } |
---|
| 68 | |
---|
[7edb9281] | 69 | /* NOTE: The RTEMS malloc is reentrant without a reent ptr since |
---|
| 70 | * it is based on the Classic API Region Manager. |
---|
| 71 | */ |
---|
[ac7d5ef0] | 72 | |
---|
[c3db01d0] | 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 |
---|
[ac7d5ef0] | 82 | |
---|
[c3db01d0] | 83 | if (ptr) { |
---|
[d40da79b] | 84 | _REENT_INIT_PTR((ptr)); /* GCC extension: structure constants */ |
---|
| 85 | creating_task->libc_reent = ptr; |
---|
| 86 | return TRUE; |
---|
[07e9194e] | 87 | } |
---|
[d40da79b] | 88 | |
---|
| 89 | return FALSE; |
---|
[ac7d5ef0] | 90 | } |
---|
| 91 | |
---|
[eaa442fc] | 92 | /* |
---|
[8cbbe312] | 93 | * Called for all user TASKS (system tasks are MPCI Receive Server and IDLE) |
---|
[eaa442fc] | 94 | */ |
---|
[50f32b11] | 95 | |
---|
[8b2ecf85] | 96 | #ifdef NEED_SETVBUF |
---|
[d40da79b] | 97 | rtems_extension newlib_begin_hook(rtems_tcb *current_task) |
---|
[eaa442fc] | 98 | { |
---|
| 99 | setvbuf( stdout, NULL, _IOLBF, BUFSIZ ); |
---|
| 100 | } |
---|
| 101 | #endif |
---|
| 102 | |
---|
[ac7d5ef0] | 103 | /* |
---|
[c3db01d0] | 104 | * Called when a task is deleted. |
---|
| 105 | * Must restore the new lib reentrancy state for the new current |
---|
| 106 | * task. |
---|
[ac7d5ef0] | 107 | * |
---|
| 108 | */ |
---|
[7edb9281] | 109 | |
---|
[c9a4da01] | 110 | int 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 | |
---|
[d40da79b] | 130 | rtems_extension newlib_delete_hook( |
---|
[7edb9281] | 131 | rtems_tcb *current_task, |
---|
| 132 | rtems_tcb *deleted_task |
---|
| 133 | ) |
---|
[ac7d5ef0] | 134 | { |
---|
[7edb9281] | 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 { |
---|
[7bd88b54] | 144 | ptr = deleted_task->libc_reent; |
---|
[7edb9281] | 145 | } |
---|
| 146 | |
---|
[d40da79b] | 147 | if (ptr && ptr != _global_impure_ptr) { |
---|
[c9a4da01] | 148 | /* |
---|
[7edb9281] | 149 | _wrapup_reent(ptr); |
---|
| 150 | _reclaim_reent(ptr); |
---|
[c9a4da01] | 151 | */ |
---|
| 152 | /* |
---|
| 153 | * Just in case there are some buffers lying around. |
---|
[50f32b11] | 154 | */ |
---|
[c9a4da01] | 155 | _fwalk(ptr, newlib_free_buffers); |
---|
[6cff0f8] | 156 | #if REENT_MALLOCED |
---|
[7edb9281] | 157 | free(ptr); |
---|
[6cff0f8] | 158 | #else |
---|
[07e9194e] | 159 | _Workspace_Free(ptr); |
---|
[6cff0f8] | 160 | #endif |
---|
[7edb9281] | 161 | } |
---|
| 162 | |
---|
[c0d4abe6] | 163 | deleted_task->libc_reent = NULL; |
---|
[7edb9281] | 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 | } |
---|
[ac7d5ef0] | 172 | } |
---|
| 173 | |
---|
| 174 | #endif |
---|