]> sourceware.org Git - glibc.git/blame - libio/bits/stdio.h
[BZ #309]
[glibc.git] / libio / bits / stdio.h
CommitLineData
085320f5 1/* Optimizing macros and inline functions for stdio functions.
8799d935 2 Copyright (C) 1998, 2000, 2001, 2004 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 34vprintf (__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 41getchar (void)
085320f5
UD
42{
43 return _IO_getc (stdin);
44}
45
46
47# if defined __USE_POSIX || defined __USE_MISC
48/* This is defined in POSIX.1:1996. */
49__STDIO_INLINE int
d9997a45 50getc_unlocked (FILE *__fp)
085320f5
UD
51{
52 return _IO_getc_unlocked (__fp);
53}
54
55/* This is defined in POSIX.1:1996. */
56__STDIO_INLINE int
d9997a45 57getchar_unlocked (void)
085320f5
UD
58{
59 return _IO_getc_unlocked (stdin);
60}
61# endif /* POSIX || misc */
62
63
64/* Write a character to stdout. */
65__STDIO_INLINE int
d9997a45 66putchar (int __c)
085320f5
UD
67{
68 return _IO_putc (__c, stdout);
69}
70
71
72# ifdef __USE_MISC
73/* Faster version when locking is not necessary. */
74__STDIO_INLINE int
d9997a45 75fputc_unlocked (int __c, FILE *__stream)
085320f5
UD
76{
77 return _IO_putc_unlocked (__c, __stream);
78}
79# endif /* misc */
80
81
82# if defined __USE_POSIX || defined __USE_MISC
83/* This is defined in POSIX.1:1996. */
84__STDIO_INLINE int
d9997a45 85putc_unlocked (int __c, FILE *__stream)
085320f5
UD
86{
87 return _IO_putc_unlocked (__c, __stream);
88}
89
90/* This is defined in POSIX.1:1996. */
91__STDIO_INLINE int
d9997a45 92putchar_unlocked (int __c)
085320f5
UD
93{
94 return _IO_putc_unlocked (__c, stdout);
95}
96# endif /* POSIX || misc */
97
98
99# ifdef __USE_GNU
100/* Like `getdelim', but reads up to a newline. */
101__STDIO_INLINE _IO_ssize_t
d9997a45 102getline (char **__lineptr, size_t *__n, FILE *__stream)
085320f5
UD
103{
104 return __getdelim (__lineptr, __n, '\n', __stream);
105}
106# endif /* GNU */
107
108
109# ifdef __USE_MISC
110/* Faster versions when locking is not required. */
111__STDIO_INLINE int
112feof_unlocked (FILE *__stream) __THROW
113{
114 return _IO_feof_unlocked (__stream);
115}
116
117/* Faster versions when locking is not required. */
118__STDIO_INLINE int
119ferror_unlocked (FILE *__stream) __THROW
120{
121 return _IO_ferror_unlocked (__stream);
122}
123# endif /* misc */
124
125#endif /* Use extern inlines. */
126
127
128#if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__
129/* Perform some simple optimizations. */
130# define fread_unlocked(ptr, size, n, stream) \
8b7fb588 131 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
8799d935
UD
132 && (size_t) (size) * (size_t) (n) <= 8 \
133 && (size_t) (size) != 0) \
085320f5
UD
134 ? ({ char *__ptr = (char *) (ptr); \
135 FILE *__stream = (stream); \
8b7fb588 136 size_t __cnt; \
8799d935
UD
137 for (__cnt = (size_t) (size) * (size_t) (n); \
138 __cnt > 0; --__cnt) \
085320f5 139 { \
7f2a1e3c 140 int __c = _IO_getc_unlocked (__stream); \
085320f5 141 if (__c == EOF) \
8b7fb588 142 break; \
085320f5
UD
143 *__ptr++ = __c; \
144 } \
8799d935
UD
145 ((size_t) (size) * (size_t) (n) - __cnt) \
146 / (size_t) (size); }) \
147 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
148 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
8b7fb588
UD
149 /* Evaluate all parameters once. */ \
150 ? ((void) (ptr), (void) (stream), (void) (size), \
0155a773 151 (void) (n), 0) \
8b7fb588 152 : fread_unlocked (ptr, size, n, stream))))
085320f5
UD
153
154# define fwrite_unlocked(ptr, size, n, stream) \
8b7fb588 155 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \
8799d935 156 && (size_t) ((size) * (n)) <= 8 && (size_t) (size) != 0) \
085320f5
UD
157 ? ({ const char *__ptr = (const char *) (ptr); \
158 FILE *__stream = (stream); \
8b7fb588 159 size_t __cnt; \
8799d935
UD
160 for (__cnt = (size_t) (size) * (size_t) (n); \
161 __cnt > 0; --__cnt) \
085320f5 162 if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \
8b7fb588 163 break; \
8799d935
UD
164 ((size_t) (size) * (size_t) (n) - __cnt) \
165 / (size_t) (size); }) \
166 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \
167 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \
8b7fb588 168 /* Evaluate all parameters once. */ \
8799d935
UD
169 ? ((void) (ptr), (void) (stream), (void) (size), \
170 (size_t) n) \
8b7fb588 171 : fwrite_unlocked (ptr, size, n, stream))))
085320f5
UD
172#endif
173
174/* Define helper macro. */
175#undef __STDIO_INLINE
This page took 0.219829 seconds and 5 git commands to generate.