stdio.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Optimizing macros and inline functions for stdio functions.
  2. Copyright (C) 1998, 2000, 2001, 2004, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _STDIO_H
  17. # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
  18. #endif
  19. #ifndef __extern_inline
  20. # define __STDIO_INLINE inline
  21. #else
  22. # define __STDIO_INLINE __extern_inline
  23. #endif
  24. #ifdef __USE_EXTERN_INLINES
  25. /* For -D_FORTIFY_SOURCE{,=2} bits/stdio2.h will define a different
  26. inline. */
  27. # if !(__USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline)
  28. /* Write formatted output to stdout from argument list ARG. */
  29. __STDIO_INLINE int
  30. vprintf (__const char *__restrict __fmt, _G_va_list __arg)
  31. {
  32. return vfprintf (stdout, __fmt, __arg);
  33. }
  34. # endif
  35. /* Read a character from stdin. */
  36. __STDIO_INLINE int
  37. getchar (void)
  38. {
  39. return _IO_getc (stdin);
  40. }
  41. # ifdef __USE_MISC
  42. /* Faster version when locking is not necessary. */
  43. __STDIO_INLINE int
  44. fgetc_unlocked (FILE *__fp)
  45. {
  46. return _IO_getc_unlocked (__fp);
  47. }
  48. # endif /* misc */
  49. # if defined __USE_POSIX || defined __USE_MISC
  50. /* This is defined in POSIX.1:1996. */
  51. __STDIO_INLINE int
  52. getc_unlocked (FILE *__fp)
  53. {
  54. return _IO_getc_unlocked (__fp);
  55. }
  56. /* This is defined in POSIX.1:1996. */
  57. __STDIO_INLINE int
  58. getchar_unlocked (void)
  59. {
  60. return _IO_getc_unlocked (stdin);
  61. }
  62. # endif /* POSIX || misc */
  63. /* Write a character to stdout. */
  64. __STDIO_INLINE int
  65. putchar (int __c)
  66. {
  67. return _IO_putc (__c, stdout);
  68. }
  69. # ifdef __USE_MISC
  70. /* Faster version when locking is not necessary. */
  71. __STDIO_INLINE int
  72. fputc_unlocked (int __c, FILE *__stream)
  73. {
  74. return _IO_putc_unlocked (__c, __stream);
  75. }
  76. # endif /* misc */
  77. # if defined __USE_POSIX || defined __USE_MISC
  78. /* This is defined in POSIX.1:1996. */
  79. __STDIO_INLINE int
  80. putc_unlocked (int __c, FILE *__stream)
  81. {
  82. return _IO_putc_unlocked (__c, __stream);
  83. }
  84. /* This is defined in POSIX.1:1996. */
  85. __STDIO_INLINE int
  86. putchar_unlocked (int __c)
  87. {
  88. return _IO_putc_unlocked (__c, stdout);
  89. }
  90. # endif /* POSIX || misc */
  91. # ifdef __USE_GNU
  92. /* Like `getdelim', but reads up to a newline. */
  93. __STDIO_INLINE _IO_ssize_t
  94. getline (char **__lineptr, size_t *__n, FILE *__stream)
  95. {
  96. return __getdelim (__lineptr, __n, '\n', __stream);
  97. }
  98. # endif /* GNU */
  99. # ifdef __USE_MISC
  100. /* Faster versions when locking is not required. */
  101. __STDIO_INLINE int
  102. __NTH (feof_unlocked (FILE *__stream))
  103. {
  104. return _IO_feof_unlocked (__stream);
  105. }
  106. /* Faster versions when locking is not required. */
  107. __STDIO_INLINE int
  108. __NTH (ferror_unlocked (FILE *__stream))
  109. {
  110. return _IO_ferror_unlocked (__stream);
  111. }
  112. # endif /* misc */
  113. #endif /* Use extern inlines. */
  114. #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
  115. && !defined __cplusplus
  116. /* Perform some simple optimizations. */
  117. # define fread_unlocked(ptr, size, n, stream) \
  118. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  119. && (size_t) (size) * (size_t) (n) <= 8 \
  120. && (size_t) (size) != 0) \
  121. ? ({ char *__ptr = (char *) (ptr); \
  122. FILE *__stream = (stream); \
  123. size_t __cnt; \
  124. for (__cnt = (size_t) (size) * (size_t) (n); \
  125. __cnt > 0; --__cnt) \
  126. { \
  127. int __c = _IO_getc_unlocked (__stream); \
  128. if (__c == EOF) \
  129. break; \
  130. *__ptr++ = __c; \
  131. } \
  132. ((size_t) (size) * (size_t) (n) - __cnt) \
  133. / (size_t) (size); }) \
  134. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  135. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  136. /* Evaluate all parameters once. */ \
  137. ? ((void) (ptr), (void) (stream), (void) (size), \
  138. (void) (n), (size_t) 0) \
  139. : fread_unlocked (ptr, size, n, stream))))
  140. # define fwrite_unlocked(ptr, size, n, stream) \
  141. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  142. && (size_t) (size) * (size_t) (n) <= 8 \
  143. && (size_t) (size) != 0) \
  144. ? ({ const char *__ptr = (const char *) (ptr); \
  145. FILE *__stream = (stream); \
  146. size_t __cnt; \
  147. for (__cnt = (size_t) (size) * (size_t) (n); \
  148. __cnt > 0; --__cnt) \
  149. if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \
  150. break; \
  151. ((size_t) (size) * (size_t) (n) - __cnt) \
  152. / (size_t) (size); }) \
  153. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  154. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  155. /* Evaluate all parameters once. */ \
  156. ? ((void) (ptr), (void) (stream), (void) (size), \
  157. (void) (n), (size_t) 0) \
  158. : fwrite_unlocked (ptr, size, n, stream))))
  159. #endif
  160. /* Define helper macro. */
  161. #undef __STDIO_INLINE