[444] | 1 | /* This header file provides the reentrancy. */ |
---|
| 2 | |
---|
| 3 | /* WARNING: All identifiers here must begin with an underscore. This file is |
---|
| 4 | included by stdio.h and others and we therefore must only use identifiers |
---|
| 5 | in the namespace allotted to us. */ |
---|
| 6 | |
---|
| 7 | #ifndef _SYS_REENT_H_ |
---|
| 8 | #ifdef __cplusplus |
---|
| 9 | extern "C" { |
---|
| 10 | #endif |
---|
| 11 | #define _SYS_REENT_H_ |
---|
| 12 | |
---|
| 13 | #include <_ansi.h> |
---|
| 14 | #include <stddef.h> |
---|
| 15 | #include <sys/_types.h> |
---|
| 16 | |
---|
| 17 | #define _NULL 0 |
---|
| 18 | |
---|
| 19 | #ifndef __Long |
---|
| 20 | #if __LONG_MAX__ == 2147483647L |
---|
| 21 | #define __Long long |
---|
| 22 | typedef unsigned __Long __ULong; |
---|
| 23 | #elif __INT_MAX__ == 2147483647 |
---|
| 24 | #define __Long int |
---|
| 25 | typedef unsigned __Long __ULong; |
---|
| 26 | #endif |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | #if !defined( __Long) |
---|
| 30 | #include <sys/types.h> |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | #ifndef __Long |
---|
| 34 | #define __Long __int32_t |
---|
| 35 | typedef __uint32_t __ULong; |
---|
| 36 | #endif |
---|
| 37 | |
---|
| 38 | struct _reent; |
---|
| 39 | |
---|
| 40 | struct __locale_t; |
---|
| 41 | |
---|
| 42 | /* |
---|
| 43 | * If _REENT_SMALL is defined, we make struct _reent as small as possible, |
---|
| 44 | * by having nearly everything possible allocated at first use. |
---|
| 45 | */ |
---|
| 46 | |
---|
| 47 | struct _Bigint |
---|
| 48 | { |
---|
| 49 | struct _Bigint *_next; |
---|
| 50 | int _k, _maxwds, _sign, _wds; |
---|
| 51 | __ULong _x[1]; |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | /* needed by reentrant structure */ |
---|
| 55 | struct __tm |
---|
| 56 | { |
---|
| 57 | int __tm_sec; |
---|
| 58 | int __tm_min; |
---|
| 59 | int __tm_hour; |
---|
| 60 | int __tm_mday; |
---|
| 61 | int __tm_mon; |
---|
| 62 | int __tm_year; |
---|
| 63 | int __tm_wday; |
---|
| 64 | int __tm_yday; |
---|
| 65 | int __tm_isdst; |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | /* |
---|
| 69 | * atexit() support. |
---|
| 70 | */ |
---|
| 71 | |
---|
| 72 | #define _ATEXIT_SIZE 32 /* must be at least 32 to guarantee ANSI conformance */ |
---|
| 73 | |
---|
| 74 | struct _on_exit_args { |
---|
| 75 | void * _fnargs[_ATEXIT_SIZE]; /* user fn args */ |
---|
| 76 | void * _dso_handle[_ATEXIT_SIZE]; |
---|
| 77 | /* Bitmask is set if user function takes arguments. */ |
---|
| 78 | __ULong _fntypes; /* type of exit routine - |
---|
| 79 | Must have at least _ATEXIT_SIZE bits */ |
---|
| 80 | /* Bitmask is set if function was registered via __cxa_atexit. */ |
---|
| 81 | __ULong _is_cxa; |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | #ifdef _REENT_SMALL |
---|
| 85 | struct _atexit { |
---|
| 86 | struct _atexit *_next; /* next in list */ |
---|
| 87 | int _ind; /* next index in this table */ |
---|
| 88 | void (*_fns[_ATEXIT_SIZE])(void); /* the table itself */ |
---|
| 89 | struct _on_exit_args * _on_exit_args_ptr; |
---|
| 90 | }; |
---|
| 91 | # define _ATEXIT_INIT {_NULL, 0, {_NULL}, _NULL} |
---|
| 92 | #else |
---|
| 93 | struct _atexit { |
---|
| 94 | struct _atexit *_next; /* next in list */ |
---|
| 95 | int _ind; /* next index in this table */ |
---|
| 96 | /* Some entries may already have been called, and will be NULL. */ |
---|
| 97 | void (*_fns[_ATEXIT_SIZE])(void); /* the table itself */ |
---|
| 98 | struct _on_exit_args _on_exit_args; |
---|
| 99 | }; |
---|
| 100 | # define _ATEXIT_INIT {_NULL, 0, {_NULL}, {{_NULL}, {_NULL}, 0, 0}} |
---|
| 101 | #endif |
---|
| 102 | |
---|
| 103 | #ifdef _REENT_GLOBAL_ATEXIT |
---|
| 104 | # define _REENT_INIT_ATEXIT |
---|
| 105 | #else |
---|
| 106 | # define _REENT_INIT_ATEXIT \ |
---|
| 107 | _NULL, _ATEXIT_INIT, |
---|
| 108 | #endif |
---|
| 109 | |
---|
| 110 | /* |
---|
| 111 | * Stdio buffers. |
---|
| 112 | * |
---|
| 113 | * This and __FILE are defined here because we need them for struct _reent, |
---|
| 114 | * but we don't want stdio.h included when stdlib.h is. |
---|
| 115 | */ |
---|
| 116 | |
---|
| 117 | struct __sbuf { |
---|
| 118 | unsigned char *_base; |
---|
| 119 | int _size; |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | /* |
---|
| 123 | * Stdio state variables. |
---|
| 124 | * |
---|
| 125 | * The following always hold: |
---|
| 126 | * |
---|
| 127 | * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), |
---|
| 128 | * _lbfsize is -_bf._size, else _lbfsize is 0 |
---|
| 129 | * if _flags&__SRD, _w is 0 |
---|
| 130 | * if _flags&__SWR, _r is 0 |
---|
| 131 | * |
---|
| 132 | * This ensures that the getc and putc macros (or inline functions) never |
---|
| 133 | * try to write or read from a file that is in `read' or `write' mode. |
---|
| 134 | * (Moreover, they can, and do, automatically switch from read mode to |
---|
| 135 | * write mode, and back, on "r+" and "w+" files.) |
---|
| 136 | * |
---|
| 137 | * _lbfsize is used only to make the inline line-buffered output stream |
---|
| 138 | * code as compact as possible. |
---|
| 139 | * |
---|
| 140 | * _ub, _up, and _ur are used when ungetc() pushes back more characters |
---|
| 141 | * than fit in the current _bf, or when ungetc() pushes back a character |
---|
| 142 | * that does not match the previous one in _bf. When this happens, |
---|
| 143 | * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff |
---|
| 144 | * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. |
---|
| 145 | */ |
---|
| 146 | |
---|
| 147 | #ifdef _REENT_SMALL |
---|
| 148 | /* |
---|
| 149 | * struct __sFILE_fake is the start of a struct __sFILE, with only the |
---|
| 150 | * minimal fields allocated. In __sinit() we really allocate the 3 |
---|
| 151 | * standard streams, etc., and point away from this fake. |
---|
| 152 | */ |
---|
| 153 | struct __sFILE_fake { |
---|
| 154 | unsigned char *_p; /* current position in (some) buffer */ |
---|
| 155 | int _r; /* read space left for getc() */ |
---|
| 156 | int _w; /* write space left for putc() */ |
---|
| 157 | short _flags; /* flags, below; this FILE is free if 0 */ |
---|
| 158 | short _file; /* fileno, if Unix descriptor, else -1 */ |
---|
| 159 | struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
---|
| 160 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
---|
| 161 | |
---|
| 162 | struct _reent *_data; |
---|
| 163 | }; |
---|
| 164 | |
---|
| 165 | /* Following is needed both in libc/stdio and libc/stdlib so we put it |
---|
| 166 | * here instead of libc/stdio/local.h where it was previously. */ |
---|
| 167 | |
---|
| 168 | extern void __sinit (struct _reent *); |
---|
| 169 | |
---|
| 170 | # define _REENT_SMALL_CHECK_INIT(ptr) \ |
---|
| 171 | do \ |
---|
| 172 | { \ |
---|
| 173 | if ((ptr) && !(ptr)->__sdidinit) \ |
---|
| 174 | __sinit (ptr); \ |
---|
| 175 | } \ |
---|
| 176 | while (0) |
---|
| 177 | #else |
---|
| 178 | # define _REENT_SMALL_CHECK_INIT(ptr) /* nothing */ |
---|
| 179 | #endif |
---|
| 180 | |
---|
| 181 | struct __sFILE { |
---|
| 182 | unsigned char *_p; /* current position in (some) buffer */ |
---|
| 183 | int _r; /* read space left for getc() */ |
---|
| 184 | int _w; /* write space left for putc() */ |
---|
| 185 | short _flags; /* flags, below; this FILE is free if 0 */ |
---|
| 186 | short _file; /* fileno, if Unix descriptor, else -1 */ |
---|
| 187 | struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
---|
| 188 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
---|
| 189 | |
---|
| 190 | #ifdef _REENT_SMALL |
---|
| 191 | struct _reent *_data; |
---|
| 192 | #endif |
---|
| 193 | |
---|
| 194 | /* operations */ |
---|
| 195 | void * _cookie; /* cookie passed to io functions */ |
---|
| 196 | |
---|
| 197 | _READ_WRITE_RETURN_TYPE (*_read) (struct _reent *, void *, |
---|
| 198 | char *, _READ_WRITE_BUFSIZE_TYPE); |
---|
| 199 | _READ_WRITE_RETURN_TYPE (*_write) (struct _reent *, void *, |
---|
| 200 | const char *, |
---|
| 201 | _READ_WRITE_BUFSIZE_TYPE); |
---|
| 202 | _fpos_t (*_seek) (struct _reent *, void *, _fpos_t, int); |
---|
| 203 | int (*_close) (struct _reent *, void *); |
---|
| 204 | |
---|
| 205 | /* separate buffer for long sequences of ungetc() */ |
---|
| 206 | struct __sbuf _ub; /* ungetc buffer */ |
---|
| 207 | unsigned char *_up; /* saved _p when _p is doing ungetc data */ |
---|
| 208 | int _ur; /* saved _r when _r is counting ungetc data */ |
---|
| 209 | |
---|
| 210 | /* tricks to meet minimum requirements even when malloc() fails */ |
---|
| 211 | unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ |
---|
| 212 | unsigned char _nbuf[1]; /* guarantee a getc() buffer */ |
---|
| 213 | |
---|
| 214 | /* separate buffer for fgetline() when line crosses buffer boundary */ |
---|
| 215 | struct __sbuf _lb; /* buffer for fgetline() */ |
---|
| 216 | |
---|
| 217 | /* Unix stdio files get aligned to block boundaries on fseek() */ |
---|
| 218 | int _blksize; /* stat.st_blksize (may be != _bf._size) */ |
---|
| 219 | _off_t _offset; /* current lseek offset */ |
---|
| 220 | |
---|
| 221 | #ifndef _REENT_SMALL |
---|
| 222 | struct _reent *_data; /* Here for binary compatibility? Remove? */ |
---|
| 223 | #endif |
---|
| 224 | |
---|
| 225 | #ifndef __SINGLE_THREAD__ |
---|
| 226 | _flock_t _lock; /* for thread-safety locking */ |
---|
| 227 | #endif |
---|
| 228 | _mbstate_t _mbstate; /* for wide char stdio functions. */ |
---|
| 229 | int _flags2; /* for future use */ |
---|
| 230 | }; |
---|
| 231 | |
---|
| 232 | #ifdef __CUSTOM_FILE_IO__ |
---|
| 233 | |
---|
| 234 | /* Get custom _FILE definition. */ |
---|
| 235 | #include <sys/custom_file.h> |
---|
| 236 | |
---|
| 237 | #else /* !__CUSTOM_FILE_IO__ */ |
---|
| 238 | #ifdef __LARGE64_FILES |
---|
| 239 | struct __sFILE64 { |
---|
| 240 | unsigned char *_p; /* current position in (some) buffer */ |
---|
| 241 | int _r; /* read space left for getc() */ |
---|
| 242 | int _w; /* write space left for putc() */ |
---|
| 243 | short _flags; /* flags, below; this FILE is free if 0 */ |
---|
| 244 | short _file; /* fileno, if Unix descriptor, else -1 */ |
---|
| 245 | struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
---|
| 246 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
---|
| 247 | |
---|
| 248 | struct _reent *_data; |
---|
| 249 | |
---|
| 250 | /* operations */ |
---|
| 251 | void * _cookie; /* cookie passed to io functions */ |
---|
| 252 | |
---|
| 253 | _READ_WRITE_RETURN_TYPE (*_read) (struct _reent *, void *, |
---|
| 254 | char *, _READ_WRITE_BUFSIZE_TYPE); |
---|
| 255 | _READ_WRITE_RETURN_TYPE (*_write) (struct _reent *, void *, |
---|
| 256 | const char *, |
---|
| 257 | _READ_WRITE_BUFSIZE_TYPE); |
---|
| 258 | _fpos_t (*_seek) (struct _reent *, void *, _fpos_t, int); |
---|
| 259 | int (*_close) (struct _reent *, void *); |
---|
| 260 | |
---|
| 261 | /* separate buffer for long sequences of ungetc() */ |
---|
| 262 | struct __sbuf _ub; /* ungetc buffer */ |
---|
| 263 | unsigned char *_up; /* saved _p when _p is doing ungetc data */ |
---|
| 264 | int _ur; /* saved _r when _r is counting ungetc data */ |
---|
| 265 | |
---|
| 266 | /* tricks to meet minimum requirements even when malloc() fails */ |
---|
| 267 | unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ |
---|
| 268 | unsigned char _nbuf[1]; /* guarantee a getc() buffer */ |
---|
| 269 | |
---|
| 270 | /* separate buffer for fgetline() when line crosses buffer boundary */ |
---|
| 271 | struct __sbuf _lb; /* buffer for fgetline() */ |
---|
| 272 | |
---|
| 273 | /* Unix stdio files get aligned to block boundaries on fseek() */ |
---|
| 274 | int _blksize; /* stat.st_blksize (may be != _bf._size) */ |
---|
| 275 | int _flags2; /* for future use */ |
---|
| 276 | |
---|
| 277 | _off64_t _offset; /* current lseek offset */ |
---|
| 278 | _fpos64_t (*_seek64) (struct _reent *, void *, _fpos64_t, int); |
---|
| 279 | |
---|
| 280 | #ifndef __SINGLE_THREAD__ |
---|
| 281 | _flock_t _lock; /* for thread-safety locking */ |
---|
| 282 | #endif |
---|
| 283 | _mbstate_t _mbstate; /* for wide char stdio functions. */ |
---|
| 284 | }; |
---|
| 285 | typedef struct __sFILE64 __FILE; |
---|
| 286 | #else |
---|
| 287 | typedef struct __sFILE __FILE; |
---|
| 288 | #endif /* __LARGE64_FILES */ |
---|
| 289 | #endif /* !__CUSTOM_FILE_IO__ */ |
---|
| 290 | |
---|
| 291 | struct _glue |
---|
| 292 | { |
---|
| 293 | struct _glue *_next; |
---|
| 294 | int _niobs; |
---|
| 295 | __FILE *_iobs; |
---|
| 296 | }; |
---|
| 297 | |
---|
| 298 | /* |
---|
| 299 | * rand48 family support |
---|
| 300 | * |
---|
| 301 | * Copyright (c) 1993 Martin Birgmeier |
---|
| 302 | * All rights reserved. |
---|
| 303 | * |
---|
| 304 | * You may redistribute unmodified or modified versions of this source |
---|
| 305 | * code provided that the above copyright notice and this and the |
---|
| 306 | * following conditions are retained. |
---|
| 307 | * |
---|
| 308 | * This software is provided ``as is'', and comes with no warranties |
---|
| 309 | * of any kind. I shall in no event be liable for anything that happens |
---|
| 310 | * to anyone/anything when using this software. |
---|
| 311 | */ |
---|
| 312 | #define _RAND48_SEED_0 (0x330e) |
---|
| 313 | #define _RAND48_SEED_1 (0xabcd) |
---|
| 314 | #define _RAND48_SEED_2 (0x1234) |
---|
| 315 | #define _RAND48_MULT_0 (0xe66d) |
---|
| 316 | #define _RAND48_MULT_1 (0xdeec) |
---|
| 317 | #define _RAND48_MULT_2 (0x0005) |
---|
| 318 | #define _RAND48_ADD (0x000b) |
---|
| 319 | struct _rand48 { |
---|
| 320 | unsigned short _seed[3]; |
---|
| 321 | unsigned short _mult[3]; |
---|
| 322 | unsigned short _add; |
---|
| 323 | #ifdef _REENT_SMALL |
---|
| 324 | /* Put this in here as well, for good luck. */ |
---|
| 325 | __extension__ unsigned long long _rand_next; |
---|
| 326 | #endif |
---|
| 327 | }; |
---|
| 328 | |
---|
| 329 | /* How big the some arrays are. */ |
---|
| 330 | #define _REENT_EMERGENCY_SIZE 25 |
---|
| 331 | #define _REENT_ASCTIME_SIZE 26 |
---|
| 332 | #define _REENT_SIGNAL_SIZE 24 |
---|
| 333 | |
---|
| 334 | /* |
---|
| 335 | * struct _reent |
---|
| 336 | * |
---|
| 337 | * This structure contains *all* globals needed by the library. |
---|
| 338 | * It's raison d'etre is to facilitate threads by making all library routines |
---|
| 339 | * reentrant. IE: All state information is contained here. |
---|
| 340 | */ |
---|
| 341 | |
---|
| 342 | #ifdef _REENT_SMALL |
---|
| 343 | |
---|
| 344 | struct _mprec |
---|
| 345 | { |
---|
| 346 | /* used by mprec routines */ |
---|
| 347 | struct _Bigint *_result; |
---|
| 348 | int _result_k; |
---|
| 349 | struct _Bigint *_p5s; |
---|
| 350 | struct _Bigint **_freelist; |
---|
| 351 | }; |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | struct _misc_reent |
---|
| 355 | { |
---|
| 356 | /* miscellaneous reentrant data */ |
---|
| 357 | char *_strtok_last; |
---|
| 358 | _mbstate_t _mblen_state; |
---|
| 359 | _mbstate_t _wctomb_state; |
---|
| 360 | _mbstate_t _mbtowc_state; |
---|
| 361 | char _l64a_buf[8]; |
---|
| 362 | int _getdate_err; |
---|
| 363 | _mbstate_t _mbrlen_state; |
---|
| 364 | _mbstate_t _mbrtowc_state; |
---|
| 365 | _mbstate_t _mbsrtowcs_state; |
---|
| 366 | _mbstate_t _wcrtomb_state; |
---|
| 367 | _mbstate_t _wcsrtombs_state; |
---|
| 368 | }; |
---|
| 369 | |
---|
| 370 | /* This version of _reent is laid out with "int"s in pairs, to help |
---|
| 371 | * ports with 16-bit int's but 32-bit pointers, align nicely. */ |
---|
| 372 | struct _reent |
---|
| 373 | { |
---|
| 374 | /* As an exception to the above put _errno first for binary |
---|
| 375 | compatibility with non _REENT_SMALL targets. */ |
---|
| 376 | int _errno; /* local copy of errno */ |
---|
| 377 | |
---|
| 378 | /* FILE is a big struct and may change over time. To try to achieve binary |
---|
| 379 | compatibility with future versions, put stdin,stdout,stderr here. |
---|
| 380 | These are pointers into member __sf defined below. */ |
---|
| 381 | __FILE *_stdin, *_stdout, *_stderr; /* XXX */ |
---|
| 382 | |
---|
| 383 | int _inc; /* used by tmpnam */ |
---|
| 384 | |
---|
| 385 | char *_emergency; |
---|
| 386 | |
---|
| 387 | int __sdidinit; /* 1 means stdio has been init'd */ |
---|
| 388 | |
---|
| 389 | int _unspecified_locale_info; /* unused, reserved for locale stuff */ |
---|
| 390 | struct __locale_t *_locale;/* per-thread locale */ |
---|
| 391 | |
---|
| 392 | struct _mprec *_mp; |
---|
| 393 | |
---|
| 394 | void (*__cleanup) (struct _reent *); |
---|
| 395 | |
---|
| 396 | int _gamma_signgam; |
---|
| 397 | |
---|
| 398 | /* used by some fp conversion routines */ |
---|
| 399 | int _cvtlen; /* should be size_t */ |
---|
| 400 | char *_cvtbuf; |
---|
| 401 | |
---|
| 402 | struct _rand48 *_r48; |
---|
| 403 | struct __tm *_localtime_buf; |
---|
| 404 | char *_asctime_buf; |
---|
| 405 | |
---|
| 406 | /* signal info */ |
---|
| 407 | void (**(_sig_func))(int); |
---|
| 408 | |
---|
| 409 | # ifndef _REENT_GLOBAL_ATEXIT |
---|
| 410 | /* atexit stuff */ |
---|
| 411 | struct _atexit *_atexit; |
---|
| 412 | struct _atexit _atexit0; |
---|
| 413 | # endif |
---|
| 414 | |
---|
| 415 | struct _glue __sglue; /* root of glue chain */ |
---|
| 416 | __FILE *__sf; /* file descriptors */ |
---|
| 417 | struct _misc_reent *_misc; /* strtok, multibyte states */ |
---|
| 418 | char *_signal_buf; /* strsignal */ |
---|
| 419 | }; |
---|
| 420 | |
---|
| 421 | extern const struct __sFILE_fake __sf_fake_stdin; |
---|
| 422 | extern const struct __sFILE_fake __sf_fake_stdout; |
---|
| 423 | extern const struct __sFILE_fake __sf_fake_stderr; |
---|
| 424 | |
---|
| 425 | # define _REENT_INIT(var) \ |
---|
| 426 | { 0, \ |
---|
| 427 | (__FILE *)&__sf_fake_stdin, \ |
---|
| 428 | (__FILE *)&__sf_fake_stdout, \ |
---|
| 429 | (__FILE *)&__sf_fake_stderr, \ |
---|
| 430 | 0, \ |
---|
| 431 | _NULL, \ |
---|
| 432 | 0, \ |
---|
| 433 | 0, \ |
---|
| 434 | _NULL, \ |
---|
| 435 | _NULL, \ |
---|
| 436 | _NULL, \ |
---|
| 437 | 0, \ |
---|
| 438 | 0, \ |
---|
| 439 | _NULL, \ |
---|
| 440 | _NULL, \ |
---|
| 441 | _NULL, \ |
---|
| 442 | _NULL, \ |
---|
| 443 | _NULL, \ |
---|
| 444 | _REENT_INIT_ATEXIT \ |
---|
| 445 | {_NULL, 0, _NULL}, \ |
---|
| 446 | _NULL, \ |
---|
| 447 | _NULL, \ |
---|
| 448 | _NULL \ |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | #define _REENT_INIT_PTR_ZEROED(var) \ |
---|
| 452 | { (var)->_stdin = (__FILE *)&__sf_fake_stdin; \ |
---|
| 453 | (var)->_stdout = (__FILE *)&__sf_fake_stdout; \ |
---|
| 454 | (var)->_stderr = (__FILE *)&__sf_fake_stderr; \ |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | /* Only add assert() calls if we are specified to debug. */ |
---|
| 458 | #ifdef _REENT_CHECK_DEBUG |
---|
| 459 | #include <assert.h> |
---|
| 460 | #define __reent_assert(x) assert(x) |
---|
| 461 | #else |
---|
| 462 | #define __reent_assert(x) ((void)0) |
---|
| 463 | #endif |
---|
| 464 | |
---|
| 465 | #ifdef __CUSTOM_FILE_IO__ |
---|
| 466 | #error Custom FILE I/O and _REENT_SMALL not currently supported. |
---|
| 467 | #endif |
---|
| 468 | |
---|
| 469 | /* Generic _REENT check macro. */ |
---|
| 470 | #define _REENT_CHECK(var, what, type, size, init) do { \ |
---|
| 471 | struct _reent *_r = (var); \ |
---|
| 472 | if (_r->what == NULL) { \ |
---|
| 473 | _r->what = (type)malloc(size); \ |
---|
| 474 | __reent_assert(_r->what); \ |
---|
| 475 | init; \ |
---|
| 476 | } \ |
---|
| 477 | } while (0) |
---|
| 478 | |
---|
| 479 | #define _REENT_CHECK_TM(var) \ |
---|
| 480 | _REENT_CHECK(var, _localtime_buf, struct __tm *, sizeof *((var)->_localtime_buf), \ |
---|
| 481 | /* nothing */) |
---|
| 482 | |
---|
| 483 | #define _REENT_CHECK_ASCTIME_BUF(var) \ |
---|
| 484 | _REENT_CHECK(var, _asctime_buf, char *, _REENT_ASCTIME_SIZE, \ |
---|
| 485 | memset((var)->_asctime_buf, 0, _REENT_ASCTIME_SIZE)) |
---|
| 486 | |
---|
| 487 | /* Handle the dynamically allocated rand48 structure. */ |
---|
| 488 | #define _REENT_INIT_RAND48(var) do { \ |
---|
| 489 | struct _reent *_r = (var); \ |
---|
| 490 | _r->_r48->_seed[0] = _RAND48_SEED_0; \ |
---|
| 491 | _r->_r48->_seed[1] = _RAND48_SEED_1; \ |
---|
| 492 | _r->_r48->_seed[2] = _RAND48_SEED_2; \ |
---|
| 493 | _r->_r48->_mult[0] = _RAND48_MULT_0; \ |
---|
| 494 | _r->_r48->_mult[1] = _RAND48_MULT_1; \ |
---|
| 495 | _r->_r48->_mult[2] = _RAND48_MULT_2; \ |
---|
| 496 | _r->_r48->_add = _RAND48_ADD; \ |
---|
| 497 | _r->_r48->_rand_next = 1; \ |
---|
| 498 | } while (0) |
---|
| 499 | #define _REENT_CHECK_RAND48(var) \ |
---|
| 500 | _REENT_CHECK(var, _r48, struct _rand48 *, sizeof *((var)->_r48), _REENT_INIT_RAND48((var))) |
---|
| 501 | |
---|
| 502 | #define _REENT_INIT_MP(var) do { \ |
---|
| 503 | struct _reent *_r = (var); \ |
---|
| 504 | _r->_mp->_result_k = 0; \ |
---|
| 505 | _r->_mp->_result = _r->_mp->_p5s = _NULL; \ |
---|
| 506 | _r->_mp->_freelist = _NULL; \ |
---|
| 507 | } while (0) |
---|
| 508 | #define _REENT_CHECK_MP(var) \ |
---|
| 509 | _REENT_CHECK(var, _mp, struct _mprec *, sizeof *((var)->_mp), _REENT_INIT_MP(var)) |
---|
| 510 | |
---|
| 511 | #define _REENT_CHECK_EMERGENCY(var) \ |
---|
| 512 | _REENT_CHECK(var, _emergency, char *, _REENT_EMERGENCY_SIZE, /* nothing */) |
---|
| 513 | |
---|
| 514 | #define _REENT_INIT_MISC(var) do { \ |
---|
| 515 | struct _reent *_r = (var); \ |
---|
| 516 | _r->_misc->_strtok_last = _NULL; \ |
---|
| 517 | _r->_misc->_mblen_state.__count = 0; \ |
---|
| 518 | _r->_misc->_mblen_state.__value.__wch = 0; \ |
---|
| 519 | _r->_misc->_wctomb_state.__count = 0; \ |
---|
| 520 | _r->_misc->_wctomb_state.__value.__wch = 0; \ |
---|
| 521 | _r->_misc->_mbtowc_state.__count = 0; \ |
---|
| 522 | _r->_misc->_mbtowc_state.__value.__wch = 0; \ |
---|
| 523 | _r->_misc->_mbrlen_state.__count = 0; \ |
---|
| 524 | _r->_misc->_mbrlen_state.__value.__wch = 0; \ |
---|
| 525 | _r->_misc->_mbrtowc_state.__count = 0; \ |
---|
| 526 | _r->_misc->_mbrtowc_state.__value.__wch = 0; \ |
---|
| 527 | _r->_misc->_mbsrtowcs_state.__count = 0; \ |
---|
| 528 | _r->_misc->_mbsrtowcs_state.__value.__wch = 0; \ |
---|
| 529 | _r->_misc->_wcrtomb_state.__count = 0; \ |
---|
| 530 | _r->_misc->_wcrtomb_state.__value.__wch = 0; \ |
---|
| 531 | _r->_misc->_wcsrtombs_state.__count = 0; \ |
---|
| 532 | _r->_misc->_wcsrtombs_state.__value.__wch = 0; \ |
---|
| 533 | _r->_misc->_l64a_buf[0] = '\0'; \ |
---|
| 534 | _r->_misc->_getdate_err = 0; \ |
---|
| 535 | } while (0) |
---|
| 536 | #define _REENT_CHECK_MISC(var) \ |
---|
| 537 | _REENT_CHECK(var, _misc, struct _misc_reent *, sizeof *((var)->_misc), _REENT_INIT_MISC(var)) |
---|
| 538 | |
---|
| 539 | #define _REENT_CHECK_SIGNAL_BUF(var) \ |
---|
| 540 | _REENT_CHECK(var, _signal_buf, char *, _REENT_SIGNAL_SIZE, /* nothing */) |
---|
| 541 | |
---|
| 542 | #define _REENT_SIGNGAM(ptr) ((ptr)->_gamma_signgam) |
---|
| 543 | #define _REENT_RAND_NEXT(ptr) ((ptr)->_r48->_rand_next) |
---|
| 544 | #define _REENT_RAND48_SEED(ptr) ((ptr)->_r48->_seed) |
---|
| 545 | #define _REENT_RAND48_MULT(ptr) ((ptr)->_r48->_mult) |
---|
| 546 | #define _REENT_RAND48_ADD(ptr) ((ptr)->_r48->_add) |
---|
| 547 | #define _REENT_MP_RESULT(ptr) ((ptr)->_mp->_result) |
---|
| 548 | #define _REENT_MP_RESULT_K(ptr) ((ptr)->_mp->_result_k) |
---|
| 549 | #define _REENT_MP_P5S(ptr) ((ptr)->_mp->_p5s) |
---|
| 550 | #define _REENT_MP_FREELIST(ptr) ((ptr)->_mp->_freelist) |
---|
| 551 | #define _REENT_ASCTIME_BUF(ptr) ((ptr)->_asctime_buf) |
---|
| 552 | #define _REENT_TM(ptr) ((ptr)->_localtime_buf) |
---|
| 553 | #define _REENT_EMERGENCY(ptr) ((ptr)->_emergency) |
---|
| 554 | #define _REENT_STRTOK_LAST(ptr) ((ptr)->_misc->_strtok_last) |
---|
| 555 | #define _REENT_MBLEN_STATE(ptr) ((ptr)->_misc->_mblen_state) |
---|
| 556 | #define _REENT_MBTOWC_STATE(ptr)((ptr)->_misc->_mbtowc_state) |
---|
| 557 | #define _REENT_WCTOMB_STATE(ptr)((ptr)->_misc->_wctomb_state) |
---|
| 558 | #define _REENT_MBRLEN_STATE(ptr) ((ptr)->_misc->_mbrlen_state) |
---|
| 559 | #define _REENT_MBRTOWC_STATE(ptr) ((ptr)->_misc->_mbrtowc_state) |
---|
| 560 | #define _REENT_MBSRTOWCS_STATE(ptr) ((ptr)->_misc->_mbsrtowcs_state) |
---|
| 561 | #define _REENT_WCRTOMB_STATE(ptr) ((ptr)->_misc->_wcrtomb_state) |
---|
| 562 | #define _REENT_WCSRTOMBS_STATE(ptr) ((ptr)->_misc->_wcsrtombs_state) |
---|
| 563 | #define _REENT_L64A_BUF(ptr) ((ptr)->_misc->_l64a_buf) |
---|
| 564 | #define _REENT_GETDATE_ERR_P(ptr) (&((ptr)->_misc->_getdate_err)) |
---|
| 565 | #define _REENT_SIGNAL_BUF(ptr) ((ptr)->_signal_buf) |
---|
| 566 | |
---|
| 567 | #else /* !_REENT_SMALL */ |
---|
| 568 | |
---|
| 569 | struct _reent |
---|
| 570 | { |
---|
| 571 | int _errno; /* local copy of errno */ |
---|
| 572 | |
---|
| 573 | /* FILE is a big struct and may change over time. To try to achieve binary |
---|
| 574 | compatibility with future versions, put stdin,stdout,stderr here. |
---|
| 575 | These are pointers into member __sf defined below. */ |
---|
| 576 | __FILE *_stdin, *_stdout, *_stderr; |
---|
| 577 | |
---|
| 578 | int _inc; /* used by tmpnam */ |
---|
| 579 | char _emergency[_REENT_EMERGENCY_SIZE]; |
---|
| 580 | |
---|
| 581 | /* TODO */ |
---|
| 582 | int _unspecified_locale_info; /* unused, reserved for locale stuff */ |
---|
| 583 | struct __locale_t *_locale;/* per-thread locale */ |
---|
| 584 | |
---|
| 585 | int __sdidinit; /* 1 means stdio has been init'd */ |
---|
| 586 | |
---|
| 587 | void (*__cleanup) (struct _reent *); |
---|
| 588 | |
---|
| 589 | /* used by mprec routines */ |
---|
| 590 | struct _Bigint *_result; |
---|
| 591 | int _result_k; |
---|
| 592 | struct _Bigint *_p5s; |
---|
| 593 | struct _Bigint **_freelist; |
---|
| 594 | |
---|
| 595 | /* used by some fp conversion routines */ |
---|
| 596 | int _cvtlen; /* should be size_t */ |
---|
| 597 | char *_cvtbuf; |
---|
| 598 | |
---|
| 599 | union |
---|
| 600 | { |
---|
| 601 | struct |
---|
| 602 | { |
---|
| 603 | unsigned int _unused_rand; |
---|
| 604 | char * _strtok_last; |
---|
| 605 | char _asctime_buf[_REENT_ASCTIME_SIZE]; |
---|
| 606 | struct __tm _localtime_buf; |
---|
| 607 | int _gamma_signgam; |
---|
| 608 | __extension__ unsigned long long _rand_next; |
---|
| 609 | struct _rand48 _r48; |
---|
| 610 | _mbstate_t _mblen_state; |
---|
| 611 | _mbstate_t _mbtowc_state; |
---|
| 612 | _mbstate_t _wctomb_state; |
---|
| 613 | char _l64a_buf[8]; |
---|
| 614 | char _signal_buf[_REENT_SIGNAL_SIZE]; |
---|
| 615 | int _getdate_err; |
---|
| 616 | _mbstate_t _mbrlen_state; |
---|
| 617 | _mbstate_t _mbrtowc_state; |
---|
| 618 | _mbstate_t _mbsrtowcs_state; |
---|
| 619 | _mbstate_t _wcrtomb_state; |
---|
| 620 | _mbstate_t _wcsrtombs_state; |
---|
| 621 | int _h_errno; |
---|
| 622 | } _reent; |
---|
| 623 | /* Two next two fields were once used by malloc. They are no longer |
---|
| 624 | used. They are used to preserve the space used before so as to |
---|
| 625 | allow addition of new reent fields and keep binary compatibility. */ |
---|
| 626 | struct |
---|
| 627 | { |
---|
| 628 | #define _N_LISTS 30 |
---|
| 629 | unsigned char * _nextf[_N_LISTS]; |
---|
| 630 | unsigned int _nmalloc[_N_LISTS]; |
---|
| 631 | } _unused; |
---|
| 632 | } _new; |
---|
| 633 | |
---|
| 634 | # ifndef _REENT_GLOBAL_ATEXIT |
---|
| 635 | /* atexit stuff */ |
---|
| 636 | struct _atexit *_atexit; /* points to head of LIFO stack */ |
---|
| 637 | struct _atexit _atexit0; /* one guaranteed table, required by ANSI */ |
---|
| 638 | # endif |
---|
| 639 | |
---|
| 640 | /* signal info */ |
---|
| 641 | void (**(_sig_func))(int); |
---|
| 642 | |
---|
| 643 | /* These are here last so that __FILE can grow without changing the offsets |
---|
| 644 | of the above members (on the off chance that future binary compatibility |
---|
| 645 | would be broken otherwise). */ |
---|
| 646 | struct _glue __sglue; /* root of glue chain */ |
---|
| 647 | # ifndef _REENT_GLOBAL_STDIO_STREAMS |
---|
| 648 | __FILE __sf[3]; /* first three file descriptors */ |
---|
| 649 | # endif |
---|
| 650 | }; |
---|
| 651 | |
---|
| 652 | #ifdef _REENT_GLOBAL_STDIO_STREAMS |
---|
| 653 | extern __FILE __sf[3]; |
---|
| 654 | #define _REENT_STDIO_STREAM(var, index) &__sf[index] |
---|
| 655 | #else |
---|
| 656 | #define _REENT_STDIO_STREAM(var, index) &(var)->__sf[index] |
---|
| 657 | #endif |
---|
| 658 | |
---|
| 659 | #define _REENT_INIT(var) \ |
---|
| 660 | { 0, \ |
---|
| 661 | _REENT_STDIO_STREAM(&(var), 0), \ |
---|
| 662 | _REENT_STDIO_STREAM(&(var), 1), \ |
---|
| 663 | _REENT_STDIO_STREAM(&(var), 2), \ |
---|
| 664 | 0, \ |
---|
| 665 | "", \ |
---|
| 666 | 0, \ |
---|
| 667 | _NULL, \ |
---|
| 668 | 0, \ |
---|
| 669 | _NULL, \ |
---|
| 670 | _NULL, \ |
---|
| 671 | 0, \ |
---|
| 672 | _NULL, \ |
---|
| 673 | _NULL, \ |
---|
| 674 | 0, \ |
---|
| 675 | _NULL, \ |
---|
| 676 | { \ |
---|
| 677 | { \ |
---|
| 678 | 0, \ |
---|
| 679 | _NULL, \ |
---|
| 680 | "", \ |
---|
| 681 | {0, 0, 0, 0, 0, 0, 0, 0, 0}, \ |
---|
| 682 | 0, \ |
---|
| 683 | 1, \ |
---|
| 684 | { \ |
---|
| 685 | {_RAND48_SEED_0, _RAND48_SEED_1, _RAND48_SEED_2}, \ |
---|
| 686 | {_RAND48_MULT_0, _RAND48_MULT_1, _RAND48_MULT_2}, \ |
---|
| 687 | _RAND48_ADD \ |
---|
| 688 | }, \ |
---|
| 689 | {0, {0}}, \ |
---|
| 690 | {0, {0}}, \ |
---|
| 691 | {0, {0}}, \ |
---|
| 692 | "", \ |
---|
| 693 | "", \ |
---|
| 694 | 0, \ |
---|
| 695 | {0, {0}}, \ |
---|
| 696 | {0, {0}}, \ |
---|
| 697 | {0, {0}}, \ |
---|
| 698 | {0, {0}}, \ |
---|
| 699 | {0, {0}} \ |
---|
| 700 | } \ |
---|
| 701 | }, \ |
---|
| 702 | _REENT_INIT_ATEXIT \ |
---|
| 703 | _NULL, \ |
---|
| 704 | {_NULL, 0, _NULL} \ |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | #define _REENT_INIT_PTR_ZEROED(var) \ |
---|
| 708 | { (var)->_stdin = _REENT_STDIO_STREAM(var, 0); \ |
---|
| 709 | (var)->_stdout = _REENT_STDIO_STREAM(var, 1); \ |
---|
| 710 | (var)->_stderr = _REENT_STDIO_STREAM(var, 2); \ |
---|
| 711 | (var)->_new._reent._rand_next = 1; \ |
---|
| 712 | (var)->_new._reent._r48._seed[0] = _RAND48_SEED_0; \ |
---|
| 713 | (var)->_new._reent._r48._seed[1] = _RAND48_SEED_1; \ |
---|
| 714 | (var)->_new._reent._r48._seed[2] = _RAND48_SEED_2; \ |
---|
| 715 | (var)->_new._reent._r48._mult[0] = _RAND48_MULT_0; \ |
---|
| 716 | (var)->_new._reent._r48._mult[1] = _RAND48_MULT_1; \ |
---|
| 717 | (var)->_new._reent._r48._mult[2] = _RAND48_MULT_2; \ |
---|
| 718 | (var)->_new._reent._r48._add = _RAND48_ADD; \ |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | #define _REENT_CHECK_RAND48(ptr) /* nothing */ |
---|
| 722 | #define _REENT_CHECK_MP(ptr) /* nothing */ |
---|
| 723 | #define _REENT_CHECK_TM(ptr) /* nothing */ |
---|
| 724 | #define _REENT_CHECK_ASCTIME_BUF(ptr) /* nothing */ |
---|
| 725 | #define _REENT_CHECK_EMERGENCY(ptr) /* nothing */ |
---|
| 726 | #define _REENT_CHECK_MISC(ptr) /* nothing */ |
---|
| 727 | #define _REENT_CHECK_SIGNAL_BUF(ptr) /* nothing */ |
---|
| 728 | |
---|
| 729 | #define _REENT_SIGNGAM(ptr) ((ptr)->_new._reent._gamma_signgam) |
---|
| 730 | #define _REENT_RAND_NEXT(ptr) ((ptr)->_new._reent._rand_next) |
---|
| 731 | #define _REENT_RAND48_SEED(ptr) ((ptr)->_new._reent._r48._seed) |
---|
| 732 | #define _REENT_RAND48_MULT(ptr) ((ptr)->_new._reent._r48._mult) |
---|
| 733 | #define _REENT_RAND48_ADD(ptr) ((ptr)->_new._reent._r48._add) |
---|
| 734 | #define _REENT_MP_RESULT(ptr) ((ptr)->_result) |
---|
| 735 | #define _REENT_MP_RESULT_K(ptr) ((ptr)->_result_k) |
---|
| 736 | #define _REENT_MP_P5S(ptr) ((ptr)->_p5s) |
---|
| 737 | #define _REENT_MP_FREELIST(ptr) ((ptr)->_freelist) |
---|
| 738 | #define _REENT_ASCTIME_BUF(ptr) ((ptr)->_new._reent._asctime_buf) |
---|
| 739 | #define _REENT_TM(ptr) (&(ptr)->_new._reent._localtime_buf) |
---|
| 740 | #define _REENT_EMERGENCY(ptr) ((ptr)->_emergency) |
---|
| 741 | #define _REENT_STRTOK_LAST(ptr) ((ptr)->_new._reent._strtok_last) |
---|
| 742 | #define _REENT_MBLEN_STATE(ptr) ((ptr)->_new._reent._mblen_state) |
---|
| 743 | #define _REENT_MBTOWC_STATE(ptr)((ptr)->_new._reent._mbtowc_state) |
---|
| 744 | #define _REENT_WCTOMB_STATE(ptr)((ptr)->_new._reent._wctomb_state) |
---|
| 745 | #define _REENT_MBRLEN_STATE(ptr)((ptr)->_new._reent._mbrlen_state) |
---|
| 746 | #define _REENT_MBRTOWC_STATE(ptr)((ptr)->_new._reent._mbrtowc_state) |
---|
| 747 | #define _REENT_MBSRTOWCS_STATE(ptr)((ptr)->_new._reent._mbsrtowcs_state) |
---|
| 748 | #define _REENT_WCRTOMB_STATE(ptr)((ptr)->_new._reent._wcrtomb_state) |
---|
| 749 | #define _REENT_WCSRTOMBS_STATE(ptr)((ptr)->_new._reent._wcsrtombs_state) |
---|
| 750 | #define _REENT_L64A_BUF(ptr) ((ptr)->_new._reent._l64a_buf) |
---|
| 751 | #define _REENT_SIGNAL_BUF(ptr) ((ptr)->_new._reent._signal_buf) |
---|
| 752 | #define _REENT_GETDATE_ERR_P(ptr) (&((ptr)->_new._reent._getdate_err)) |
---|
| 753 | |
---|
| 754 | #endif /* !_REENT_SMALL */ |
---|
| 755 | |
---|
| 756 | #define _REENT_INIT_PTR(var) \ |
---|
| 757 | { memset((var), 0, sizeof(*(var))); \ |
---|
| 758 | _REENT_INIT_PTR_ZEROED(var); \ |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | /* This value is used in stdlib/misc.c. reent/reent.c has to know it |
---|
| 762 | as well to make sure the freelist is correctly free'd. Therefore |
---|
| 763 | we define it here, rather than in stdlib/misc.c, as before. */ |
---|
| 764 | #define _Kmax (sizeof (size_t) << 3) |
---|
| 765 | |
---|
| 766 | /* |
---|
| 767 | * All references to struct _reent are via this pointer. |
---|
| 768 | * Internally, newlib routines that need to reference it should use _REENT. |
---|
| 769 | */ |
---|
| 770 | |
---|
| 771 | #ifndef __ATTRIBUTE_IMPURE_PTR__ |
---|
| 772 | #define __ATTRIBUTE_IMPURE_PTR__ |
---|
| 773 | #endif |
---|
| 774 | |
---|
| 775 | extern struct _reent *_impure_ptr __ATTRIBUTE_IMPURE_PTR__; |
---|
| 776 | extern struct _reent *const _global_impure_ptr __ATTRIBUTE_IMPURE_PTR__; |
---|
| 777 | |
---|
| 778 | void _reclaim_reent (struct _reent *); |
---|
| 779 | |
---|
| 780 | /* #define _REENT_ONLY define this to get only reentrant routines */ |
---|
| 781 | |
---|
| 782 | #if defined(__DYNAMIC_REENT__) && !defined(__SINGLE_THREAD__) |
---|
| 783 | #ifndef __getreent |
---|
| 784 | struct _reent * __getreent (void); |
---|
| 785 | #endif |
---|
| 786 | # define _REENT (__getreent()) |
---|
| 787 | #else /* __SINGLE_THREAD__ || !__DYNAMIC_REENT__ */ |
---|
| 788 | # define _REENT _impure_ptr |
---|
| 789 | #endif /* __SINGLE_THREAD__ || !__DYNAMIC_REENT__ */ |
---|
| 790 | |
---|
| 791 | #define _GLOBAL_REENT _global_impure_ptr |
---|
| 792 | |
---|
| 793 | #ifdef _REENT_GLOBAL_ATEXIT |
---|
| 794 | extern struct _atexit *_global_atexit; /* points to head of LIFO stack */ |
---|
| 795 | # define _GLOBAL_ATEXIT _global_atexit |
---|
| 796 | #else |
---|
| 797 | # define _GLOBAL_ATEXIT (_GLOBAL_REENT->_atexit) |
---|
| 798 | #endif |
---|
| 799 | |
---|
| 800 | #ifdef __cplusplus |
---|
| 801 | } |
---|
| 802 | #endif |
---|
| 803 | #endif /* _SYS_REENT_H_ */ |
---|