]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/bits/stdio.h
.
[thirdparty/glibc.git] / libio / bits / stdio.h
1 /* Optimizing macros and inline functions for stdio functions.
2 Copyright (C) 1998, 2000, 2001, 2004, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #ifndef _STDIO_H
21 # error "Never include <bits/stdio.h> directly; use <stdio.h> instead."
22 #endif
23
24 #ifdef __cplusplus
25 # define __STDIO_INLINE inline
26 #else
27 # define __STDIO_INLINE extern __inline
28 #endif
29
30
31 #ifdef __USE_EXTERN_INLINES
32 /* Write formatted output to stdout from argument list ARG. */
33 __STDIO_INLINE int
34 vprintf (__const char *__restrict __fmt, _G_va_list __arg)
35 {
36 return vfprintf (stdout, __fmt, __arg);
37 }
38
39 /* Read a character from stdin. */
40 __STDIO_INLINE int
41 getchar (void)
42 {
43 return _IO_getc (stdin);
44 }
45
46
47 # ifdef __USE_MISC
48 /* Faster version when locking is not necessary. */
49 __STDIO_INLINE int
50 fgetc_unlocked (FILE *__fp)
51 {
52 return _IO_getc_unlocked (__fp);
53 }
54 # endif /* misc */
55
56
57 # if defined __USE_POSIX || defined __USE_MISC
58 /* This is defined in POSIX.1:1996. */
59 __STDIO_INLINE int
60 getc_unlocked (FILE *__fp)
61 {
62 return _IO_getc_unlocked (__fp);
63 }
64
65 /* This is defined in POSIX.1:1996. */
66 __STDIO_INLINE int
67 getchar_unlocked (void)
68 {
69 return _IO_getc_unlocked (stdin);
70 }
71 # endif /* POSIX || misc */
72
73
74 /* Write a character to stdout. */
75 __STDIO_INLINE int
76 putchar (int __c)
77 {
78 return _IO_putc (__c, stdout);
79 }
80
81
82 # ifdef __USE_MISC
83 /* Faster version when locking is not necessary. */
84 __STDIO_INLINE int
85 fputc_unlocked (int __c, FILE *__stream)
86 {
87 return _IO_putc_unlocked (__c, __stream);
88 }
89 # endif /* misc */
90
91
92 # if defined __USE_POSIX || defined __USE_MISC
93 /* This is defined in POSIX.1:1996. */
94 __STDIO_INLINE int
95 putc_unlocked (int __c, FILE *__stream)
96 {
97 return _IO_putc_unlocked (__c, __stream);
98 }
99
100 /* This is defined in POSIX.1:1996. */
101 __STDIO_INLINE int
102 putchar_unlocked (int __c)
103 {
104 return _IO_putc_unlocked (__c, stdout);
105 }
106 # endif /* POSIX || misc */
107
108
109 # ifdef __USE_GNU
110 /* Like `getdelim', but reads up to a newline. */
111 __STDIO_INLINE _IO_ssize_t
112 getline (char **__lineptr, size_t *__n, FILE *__stream)
113 {
114 return __getdelim (__lineptr, __n, '\n', __stream);
115 }
116 # endif /* GNU */
117
118
119 # ifdef __USE_MISC
120 /* Faster versions when locking is not required. */
121 __STDIO_INLINE int
122 __NTH (feof_unlocked (FILE *__stream))
123 {
124 return _IO_feof_unlocked (__stream);
125 }
126
127 /* Faster versions when locking is not required. */
128 __STDIO_INLINE int
129 __NTH (ferror_unlocked (FILE *__stream))
130 {
131 return _IO_ferror_unlocked (__stream);
132 }
133 # endif /* misc */
134
135 #endif /* Use extern inlines. */
136
137
138 #if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__
139 /* Perform some simple optimizations. */
140 # define fread_unlocked(ptr, size, n, stream) \
141 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
142 && (size_t) (size) * (size_t) (n) <= 8 \
143 && (size_t) (size) != 0) \
144 ? ({ char *__ptr = (char *) (ptr); \
145 FILE *__stream = (stream); \
146 size_t __cnt; \
147 for (__cnt = (size_t) (size) * (size_t) (n); \
148 __cnt > 0; --__cnt) \
149 { \
150 int __c = _IO_getc_unlocked (__stream); \
151 if (__c == EOF) \
152 break; \
153 *__ptr++ = __c; \
154 } \
155 ((size_t) (size) * (size_t) (n) - __cnt) \
156 / (size_t) (size); }) \
157 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
158 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
159 /* Evaluate all parameters once. */ \
160 ? ((void) (ptr), (void) (stream), (void) (size), \
161 (void) (n), (size_t) 0) \
162 : fread_unlocked (ptr, size, n, stream))))
163
164 # define fwrite_unlocked(ptr, size, n, stream) \
165 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
166 && (size_t) (size) * (size_t) (n) <= 8 \
167 && (size_t) (size) != 0) \
168 ? ({ const char *__ptr = (const char *) (ptr); \
169 FILE *__stream = (stream); \
170 size_t __cnt; \
171 for (__cnt = (size_t) (size) * (size_t) (n); \
172 __cnt > 0; --__cnt) \
173 if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \
174 break; \
175 ((size_t) (size) * (size_t) (n) - __cnt) \
176 / (size_t) (size); }) \
177 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
178 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
179 /* Evaluate all parameters once. */ \
180 ? ((void) (ptr), (void) (stream), (void) (size), \
181 (void) (n), (size_t) 0) \
182 : fwrite_unlocked (ptr, size, n, stream))))
183 #endif
184
185 /* Define helper macro. */
186 #undef __STDIO_INLINE