stdio.h 4.8 KB

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