]>
Commit | Line | Data |
---|---|---|
085320f5 | 1 | /* Optimizing macros and inline functions for stdio functions. |
0ecb606c | 2 | Copyright (C) 1998, 2000, 2001, 2004, 2007 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
085320f5 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
085320f5 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
085320f5 | 14 | |
41bdb6e2 AJ |
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. */ | |
085320f5 UD |
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 | |
d9997a45 | 34 | vprintf (__const char *__restrict __fmt, _G_va_list __arg) |
085320f5 UD |
35 | { |
36 | return vfprintf (stdout, __fmt, __arg); | |
37 | } | |
38 | ||
39 | /* Read a character from stdin. */ | |
40 | __STDIO_INLINE int | |
d9997a45 | 41 | getchar (void) |
085320f5 UD |
42 | { |
43 | return _IO_getc (stdin); | |
44 | } | |
45 | ||
46 | ||
0ecb606c JJ |
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 | ||
085320f5 UD |
57 | # if defined __USE_POSIX || defined __USE_MISC |
58 | /* This is defined in POSIX.1:1996. */ | |
59 | __STDIO_INLINE int | |
d9997a45 | 60 | getc_unlocked (FILE *__fp) |
085320f5 UD |
61 | { |
62 | return _IO_getc_unlocked (__fp); | |
63 | } | |
64 | ||
65 | /* This is defined in POSIX.1:1996. */ | |
66 | __STDIO_INLINE int | |
d9997a45 | 67 | getchar_unlocked (void) |
085320f5 UD |
68 | { |
69 | return _IO_getc_unlocked (stdin); | |
70 | } | |
71 | # endif /* POSIX || misc */ | |
72 | ||
73 | ||
74 | /* Write a character to stdout. */ | |
75 | __STDIO_INLINE int | |
d9997a45 | 76 | putchar (int __c) |
085320f5 UD |
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 | |
d9997a45 | 85 | fputc_unlocked (int __c, FILE *__stream) |
085320f5 UD |
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 | |
d9997a45 | 95 | putc_unlocked (int __c, FILE *__stream) |
085320f5 UD |
96 | { |
97 | return _IO_putc_unlocked (__c, __stream); | |
98 | } | |
99 | ||
100 | /* This is defined in POSIX.1:1996. */ | |
101 | __STDIO_INLINE int | |
d9997a45 | 102 | putchar_unlocked (int __c) |
085320f5 UD |
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 | |
d9997a45 | 112 | getline (char **__lineptr, size_t *__n, FILE *__stream) |
085320f5 UD |
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 | |
f377d022 | 122 | __NTH (feof_unlocked (FILE *__stream)) |
085320f5 UD |
123 | { |
124 | return _IO_feof_unlocked (__stream); | |
125 | } | |
126 | ||
127 | /* Faster versions when locking is not required. */ | |
128 | __STDIO_INLINE int | |
f377d022 | 129 | __NTH (ferror_unlocked (FILE *__stream)) |
085320f5 UD |
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) \ | |
8b7fb588 | 141 | (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \ |
8799d935 UD |
142 | && (size_t) (size) * (size_t) (n) <= 8 \ |
143 | && (size_t) (size) != 0) \ | |
085320f5 UD |
144 | ? ({ char *__ptr = (char *) (ptr); \ |
145 | FILE *__stream = (stream); \ | |
8b7fb588 | 146 | size_t __cnt; \ |
8799d935 UD |
147 | for (__cnt = (size_t) (size) * (size_t) (n); \ |
148 | __cnt > 0; --__cnt) \ | |
085320f5 | 149 | { \ |
7f2a1e3c | 150 | int __c = _IO_getc_unlocked (__stream); \ |
085320f5 | 151 | if (__c == EOF) \ |
8b7fb588 | 152 | break; \ |
085320f5 UD |
153 | *__ptr++ = __c; \ |
154 | } \ | |
8799d935 UD |
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)) \ | |
8b7fb588 UD |
159 | /* Evaluate all parameters once. */ \ |
160 | ? ((void) (ptr), (void) (stream), (void) (size), \ | |
f98ca075 | 161 | (void) (n), (size_t) 0) \ |
8b7fb588 | 162 | : fread_unlocked (ptr, size, n, stream)))) |
085320f5 UD |
163 | |
164 | # define fwrite_unlocked(ptr, size, n, stream) \ | |
8b7fb588 | 165 | (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \ |
f98ca075 RM |
166 | && (size_t) (size) * (size_t) (n) <= 8 \ |
167 | && (size_t) (size) != 0) \ | |
085320f5 UD |
168 | ? ({ const char *__ptr = (const char *) (ptr); \ |
169 | FILE *__stream = (stream); \ | |
8b7fb588 | 170 | size_t __cnt; \ |
8799d935 UD |
171 | for (__cnt = (size_t) (size) * (size_t) (n); \ |
172 | __cnt > 0; --__cnt) \ | |
085320f5 | 173 | if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \ |
8b7fb588 | 174 | break; \ |
8799d935 UD |
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)) \ | |
8b7fb588 | 179 | /* Evaluate all parameters once. */ \ |
8799d935 | 180 | ? ((void) (ptr), (void) (stream), (void) (size), \ |
f98ca075 | 181 | (void) (n), (size_t) 0) \ |
8b7fb588 | 182 | : fwrite_unlocked (ptr, size, n, stream)))) |
085320f5 UD |
183 | #endif |
184 | ||
185 | /* Define helper macro. */ | |
186 | #undef __STDIO_INLINE |