Subversion Repositories Kolibri OS

Rev

Rev 4921 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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