1
0

stdio.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Optimizing macros and inline functions for stdio functions.
  17. Copyright (C) 1998-2026 Free Software Foundation, Inc.
  18. This file is part of the GNU C Library.
  19. The GNU C Library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #ifndef _BITS_STDIO_H
  31. #define _BITS_STDIO_H 1
  32. #ifndef _STDIO_H
  33. # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
  34. #endif
  35. #ifndef __extern_inline
  36. # define __STDIO_INLINE inline
  37. #else
  38. # define __STDIO_INLINE __extern_inline
  39. #endif
  40. #ifdef __USE_EXTERN_INLINES
  41. /* For -D_FORTIFY_SOURCE{,=2,=3} bits/stdio2.h will define a different
  42. inline. */
  43. # if !(__USE_FORTIFY_LEVEL > 0 && defined __fortify_function)
  44. /* Write formatted output to stdout from argument list ARG. */
  45. __STDIO_INLINE int
  46. vprintf (const char *__restrict __fmt, __gnuc_va_list __arg)
  47. {
  48. return vfprintf (stdout, __fmt, __arg);
  49. }
  50. # endif
  51. /* Read a character from stdin. */
  52. __STDIO_INLINE int
  53. getchar (void)
  54. {
  55. return getc (stdin);
  56. }
  57. # ifdef __USE_MISC
  58. /* Faster version when locking is not necessary. */
  59. __STDIO_INLINE int
  60. fgetc_unlocked (FILE *__fp)
  61. {
  62. return __getc_unlocked_body (__fp);
  63. }
  64. # endif /* misc */
  65. # ifdef __USE_POSIX199506
  66. /* This is defined in POSIX.1:1996. */
  67. __STDIO_INLINE int
  68. getc_unlocked (FILE *__fp)
  69. {
  70. return __getc_unlocked_body (__fp);
  71. }
  72. /* This is defined in POSIX.1:1996. */
  73. __STDIO_INLINE int
  74. getchar_unlocked (void)
  75. {
  76. return __getc_unlocked_body (stdin);
  77. }
  78. # endif /* POSIX */
  79. /* Write a character to stdout. */
  80. __STDIO_INLINE int
  81. putchar (int __c)
  82. {
  83. return putc (__c, stdout);
  84. }
  85. # ifdef __USE_MISC
  86. /* Faster version when locking is not necessary. */
  87. __STDIO_INLINE int
  88. fputc_unlocked (int __c, FILE *__stream)
  89. {
  90. return __putc_unlocked_body (__c, __stream);
  91. }
  92. # endif /* misc */
  93. # ifdef __USE_POSIX199506
  94. /* This is defined in POSIX.1:1996. */
  95. __STDIO_INLINE int
  96. putc_unlocked (int __c, FILE *__stream)
  97. {
  98. return __putc_unlocked_body (__c, __stream);
  99. }
  100. /* This is defined in POSIX.1:1996. */
  101. __STDIO_INLINE int
  102. putchar_unlocked (int __c)
  103. {
  104. return __putc_unlocked_body (__c, stdout);
  105. }
  106. # endif /* POSIX */
  107. # ifdef __USE_GNU
  108. /* Like `getdelim', but reads up to a newline. */
  109. __STDIO_INLINE __ssize_t
  110. getline (char **__lineptr, size_t *__n, FILE *__stream)
  111. {
  112. return __getdelim (__lineptr, __n, '\n', __stream);
  113. }
  114. # endif /* GNU */
  115. # ifdef __USE_MISC
  116. /* Faster versions when locking is not required. */
  117. __STDIO_INLINE int
  118. __NTH (feof_unlocked (FILE *__stream))
  119. {
  120. return __feof_unlocked_body (__stream);
  121. }
  122. /* Faster versions when locking is not required. */
  123. __STDIO_INLINE int
  124. __NTH (ferror_unlocked (FILE *__stream))
  125. {
  126. return __ferror_unlocked_body (__stream);
  127. }
  128. # endif /* misc */
  129. #endif /* Use extern inlines. */
  130. #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \
  131. && !defined __cplusplus
  132. /* Perform some simple optimizations. */
  133. # define fread_unlocked(ptr, size, n, stream) \
  134. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  135. && (size_t) (size) * (size_t) (n) <= 8 \
  136. && (size_t) (size) != 0) \
  137. ? ({ char *__ptr = (char *) (ptr); \
  138. FILE *__stream = (stream); \
  139. size_t __cnt; \
  140. for (__cnt = (size_t) (size) * (size_t) (n); \
  141. __cnt > 0; --__cnt) \
  142. { \
  143. int __c = getc_unlocked (__stream); \
  144. if (__c == EOF) \
  145. break; \
  146. *__ptr++ = __c; \
  147. } \
  148. ((size_t) (size) * (size_t) (n) - __cnt) \
  149. / (size_t) (size); }) \
  150. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  151. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  152. /* Evaluate all parameters once. */ \
  153. ? ((void) (ptr), (void) (stream), (void) (size), \
  154. (void) (n), (size_t) 0) \
  155. : fread_unlocked (ptr, size, n, stream))))
  156. # define fwrite_unlocked(ptr, size, n, stream) \
  157. (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
  158. && (size_t) (size) * (size_t) (n) <= 8 \
  159. && (size_t) (size) != 0) \
  160. ? ({ const char *__ptr = (const char *) (ptr); \
  161. FILE *__stream = (stream); \
  162. size_t __cnt; \
  163. for (__cnt = (size_t) (size) * (size_t) (n); \
  164. __cnt > 0; --__cnt) \
  165. if (putc_unlocked (*__ptr++, __stream) == EOF) \
  166. break; \
  167. ((size_t) (size) * (size_t) (n) - __cnt) \
  168. / (size_t) (size); }) \
  169. : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
  170. || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
  171. /* Evaluate all parameters once. */ \
  172. ? ((void) (ptr), (void) (stream), (void) (size), \
  173. (void) (n), (size_t) 0) \
  174. : fwrite_unlocked (ptr, size, n, stream))))
  175. #endif
  176. /* Define helper macro. */
  177. #undef __STDIO_INLINE
  178. #endif /* bits/stdio.h. */