Correct and improve fread_unlocked and fwrite_unlocked optimizations.
[glibc.git] / libio / bits / stdio.h
bloba3e3d6fc8d997357ce2824c93463098aa111ffe7
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) \
131 && ((size) == 0 || __builtin_constant_p (n))) \
132 || (__builtin_constant_p (n) && (n) == 0)) \
133 && (size_t) ((size) * (n)) <= 8) \
134 ? ({ char *__ptr = (char *) (ptr); \
135 FILE *__stream = (stream); \
136 size_t __size = (size); \
137 size_t __n = (n); \
138 size_t __readres = __n; \
139 size_t __cnt = __size * __n + 1; \
140 while (--__cnt > 0) \
142 int __c = _IO_getc_unlocked (__stream); \
143 if (__c == EOF) \
145 __readres = (__size * __n - __cnt) / __size; \
146 break; \
148 *__ptr++ = __c; \
150 __readres; }) \
151 : fread_unlocked (ptr, size, n, stream)))
153 # define fwrite_unlocked(ptr, size, n, stream) \
154 (__extension__ ((((__builtin_constant_p (size) \
155 && ((size) == 0 || __builtin_constant_p (n))) \
156 || (__builtin_constant_p (n) && (n) == 0)) \
157 && (size_t) ((size) * (n)) <= 8) \
158 ? ({ const char *__ptr = (const char *) (ptr); \
159 FILE *__stream = (stream); \
160 size_t __size = (size); \
161 size_t __n = (n); \
162 size_t __writeres = __n; \
163 size_t __cnt = __size * __n + 1; \
164 while (--__cnt > 0) \
165 if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \
167 __writeres = (__size * __n - __cnt) / __size; \
168 break; \
170 __writeres; }) \
171 : fwrite_unlocked (ptr, size, n, stream)))
172 #endif
174 /* Define helper macro. */
175 #undef __STDIO_INLINE