stdio.h 5.4 KB

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