reent.h 24 KB

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