Subversion Repositories Kolibri OS

Rev

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