stdio.h 5.0 KB

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