stdio.h 5.2 KB

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