reent.h 25 KB

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