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