stdio.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Optimizing macros and inline functions for stdio functions.
  2. Copyright (C) 1998-2019 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _BITS_STDIO_H
  16. #define _BITS_STDIO_H 1
  17. #ifndef _STDIO_H
  18. # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
  19. #endif
  20. #ifndef __extern_inline
  21. # define __STDIO_INLINE inline
  22. #else
  23. # define __STDIO_INLINE __extern_inline
  24. #endif
  25. #ifdef __USE_EXTERN_INLINES
  26. /* For -D_FORTIFY_SOURCE{,=2} bits/stdio2.h will define a different
  27. inline. */
  28. # if !(__USE_FORTIFY_LEVEL > 0 && defined __fortify_function)
  29. /* Write formatted output to stdout from argument list ARG. */
  30. __STDIO_INLINE int
  31. vprintf (const char *__restrict __fmt, __gnuc_va_list __arg)
  32. {
  33. return vfprintf (stdout, __fmt, __arg);
  34. }
  35. # endif
  36. /* Read a character from stdin. */
  37. __STDIO_INLINE int
  38. getchar (void)
  39. {
  40. return getc (stdin);
  41. }
  42. # ifdef __USE_MISC
  43. /* Faster version when locking is not necessary. */
  44. __STDIO_INLINE int
  45. fgetc_unlocked (FILE *__fp)
  46. {
  47. return __getc_unlocked_body (__fp);
  48. }
  49. # endif /* misc */
  50. # ifdef __USE_POSIX
  51. /* This is defined in POSIX.1:1996. */
  52. __STDIO_INLINE int
  53. getc_unlocked (FILE *__fp)
  54. {
  55. return __getc_unlocked_body (__fp);
  56. }
  57. /* This is defined in POSIX.1:1996. */
  58. __STDIO_INLINE int
  59. getchar_unlocked (void)
  60. {
  61. return __getc_unlocked_body (stdin);
  62. }
  63. # endif /* POSIX */
  64. /* Write a character to stdout. */
  65. __STDIO_INLINE int
  66. putchar (int __c)
  67. {
  68. return putc (__c, stdout);
  69. }
  70. # ifdef __USE_MISC
  71. /* Faster version when locking is not necessary. */
  72. __STDIO_INLINE int
  73. fputc_unlocked (int __c, FILE *__stream)
  74. {
  75. return __putc_unlocked_body (__c, __stream);
  76. }
  77. # endif /* misc */
  78. # ifdef __USE_POSIX
  79. /* This is defined in POSIX.1:1996. */
  80. __STDIO_INLINE int
  81. putc_unlocked (int __c, FILE *__stream)
  82. {
  83. return __putc_unlocked_body (__c, __stream);
  84. }
  85. /* This is defined in POSIX.1:1996. */
  86. __STDIO_INLINE int
  87. putchar_unlocked (int __c)
  88. {
  89. return __putc_unlocked_body (__c, stdout);
  90. }
  91. # endif /* POSIX */
  92. # ifdef __USE_GNU
  93. /* Like `getdelim', but reads up to a newline. */
  94. __STDIO_INLINE __ssize_t
  95. getline (char **__lineptr, size_t *__n, FILE *__stream)
  96. {
  97. return __getdelim (__lineptr, __n, '\n', __stream);
  98. }
  99. # endif /* GNU */
  100. # ifdef __USE_MISC
  101. /* Faster versions when locking is not required. */
  102. __STDIO_INLINE int
  103. __NTH (feof_unlocked (FILE *__stream))
  104. {
  105. return __feof_unlocked_body (__stream);
  106. }
  107. /* Faster versions when locking is not required. */
  108. __STDIO_INLINE int
  109. __NTH (ferror_unlocked (FILE *__stream))
  110. {
  111. return __ferror_unlocked_body (__stream);
  112. }
  113. # endif /* misc */
  114. #endif /* Use extern inlines. */
  115. #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
  116. && !defined __cplusplus
  117. /* Perform some simple optimizations. */
  118. # define fread_unlocked(ptr, size, n, stream) \
  119. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  120. && (size_t) (size) * (size_t) (n) <= 8 \
  121. && (size_t) (size) != 0) \
  122. ? ({ char *__ptr = (char *) (ptr); \
  123. FILE *__stream = (stream); \
  124. size_t __cnt; \
  125. for (__cnt = (size_t) (size) * (size_t) (n); \
  126. __cnt > 0; --__cnt) \
  127. { \
  128. int __c = getc_unlocked (__stream); \
  129. if (__c == EOF) \
  130. break; \
  131. *__ptr++ = __c; \
  132. } \
  133. ((size_t) (size) * (size_t) (n) - __cnt) \
  134. / (size_t) (size); }) \
  135. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  136. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  137. /* Evaluate all parameters once. */ \
  138. ? ((void) (ptr), (void) (stream), (void) (size), \
  139. (void) (n), (size_t) 0) \
  140. : fread_unlocked (ptr, size, n, stream))))
  141. # define fwrite_unlocked(ptr, size, n, stream) \
  142. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  143. && (size_t) (size) * (size_t) (n) <= 8 \
  144. && (size_t) (size) != 0) \
  145. ? ({ const char *__ptr = (const char *) (ptr); \
  146. FILE *__stream = (stream); \
  147. size_t __cnt; \
  148. for (__cnt = (size_t) (size) * (size_t) (n); \
  149. __cnt > 0; --__cnt) \
  150. if (putc_unlocked (*__ptr++, __stream) == EOF) \
  151. break; \
  152. ((size_t) (size) * (size_t) (n) - __cnt) \
  153. / (size_t) (size); }) \
  154. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  155. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  156. /* Evaluate all parameters once. */ \
  157. ? ((void) (ptr), (void) (stream), (void) (size), \
  158. (void) (n), (size_t) 0) \
  159. : fwrite_unlocked (ptr, size, n, stream))))
  160. #endif
  161. /* Define helper macro. */
  162. #undef __STDIO_INLINE
  163. #endif /* bits/stdio.h. */