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#ifndef __extern_inline 25# define __STDIO_INLINE inline 26#else 27# define __STDIO_INLINE __extern_inline 28#endif 29 30 31#ifdef __USE_EXTERN_INLINES 32/* For -D_FORTIFY_SOURCE{,=2} bits/stdio2.h will define a different 33 inline. */ 34# if !(__USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline) 35/* Write formatted output to stdout from argument list ARG. */ 36__STDIO_INLINE int 37vprintf (__const char *__restrict __fmt, _G_va_list __arg) 38{ 39 return vfprintf (stdout, __fmt, __arg); 40} 41# endif 42 43/* Read a character from stdin. */ 44__STDIO_INLINE int 45getchar (void) 46{ 47 return _IO_getc (stdin); 48} 49 50 51# ifdef __USE_MISC 52/* Faster version when locking is not necessary. */ 53__STDIO_INLINE int 54fgetc_unlocked (FILE *__fp) 55{ 56 return _IO_getc_unlocked (__fp); 57} 58# endif /* misc */ 59 60 61# if defined __USE_POSIX || defined __USE_MISC 62/* This is defined in POSIX.1:1996. */ 63__STDIO_INLINE int 64getc_unlocked (FILE *__fp) 65{ 66 return _IO_getc_unlocked (__fp); 67} 68 69/* This is defined in POSIX.1:1996. */ 70__STDIO_INLINE int 71getchar_unlocked (void) 72{ 73 return _IO_getc_unlocked (stdin); 74} 75# endif /* POSIX || misc */ 76 77 78/* Write a character to stdout. */ 79__STDIO_INLINE int 80putchar (int __c) 81{ 82 return _IO_putc (__c, stdout); 83} 84 85 86# ifdef __USE_MISC 87/* Faster version when locking is not necessary. */ 88__STDIO_INLINE int 89fputc_unlocked (int __c, FILE *__stream) 90{ 91 return _IO_putc_unlocked (__c, __stream); 92} 93# endif /* misc */ 94 95 96# if defined __USE_POSIX || defined __USE_MISC 97/* This is defined in POSIX.1:1996. */ 98__STDIO_INLINE int 99putc_unlocked (int __c, FILE *__stream) 100{ 101 return _IO_putc_unlocked (__c, __stream); 102} 103 104/* This is defined in POSIX.1:1996. */ 105__STDIO_INLINE int 106putchar_unlocked (int __c) 107{ 108 return _IO_putc_unlocked (__c, stdout); 109} 110# endif /* POSIX || misc */ 111 112 113# ifdef __USE_GNU 114/* Like `getdelim', but reads up to a newline. */ 115__STDIO_INLINE _IO_ssize_t 116getline (char **__lineptr, size_t *__n, FILE *__stream) 117{ 118 return __getdelim (__lineptr, __n, '\n', __stream); 119} 120# endif /* GNU */ 121 122 123# ifdef __USE_MISC 124/* Faster versions when locking is not required. */ 125__STDIO_INLINE int 126__NTH (feof_unlocked (FILE *__stream)) 127{ 128 return _IO_feof_unlocked (__stream); 129} 130 131/* Faster versions when locking is not required. */ 132__STDIO_INLINE int 133__NTH (ferror_unlocked (FILE *__stream)) 134{ 135 return _IO_ferror_unlocked (__stream); 136} 137# endif /* misc */ 138 139#endif /* Use extern inlines. */ 140 141 142#if defined __USE_MISC && defined __GNUC__ && defined __OPTIMIZE__ \ 143 && !defined __cplusplus 144/* Perform some simple optimizations. */ 145# define fread_unlocked(ptr, size, n, stream) \ 146 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \ 147 && (size_t) (size) * (size_t) (n) <= 8 \ 148 && (size_t) (size) != 0) \ 149 ? ({ char *__ptr = (char *) (ptr); \ 150 FILE *__stream = (stream); \ 151 size_t __cnt; \ 152 for (__cnt = (size_t) (size) * (size_t) (n); \ 153 __cnt > 0; --__cnt) \ 154 { \ 155 int __c = _IO_getc_unlocked (__stream); \ 156 if (__c == EOF) \ 157 break; \ 158 *__ptr++ = __c; \ 159 } \ 160 ((size_t) (size) * (size_t) (n) - __cnt) \ 161 / (size_t) (size); }) \ 162 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \ 163 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \ 164 /* Evaluate all parameters once. */ \ 165 ? ((void) (ptr), (void) (stream), (void) (size), \ 166 (void) (n), (size_t) 0) \ 167 : fread_unlocked (ptr, size, n, stream)))) 168 169# define fwrite_unlocked(ptr, size, n, stream) \ 170 (__extension__ ((__builtin_constant_p (size) && __builtin_constant_p (n) \ 171 && (size_t) (size) * (size_t) (n) <= 8 \ 172 && (size_t) (size) != 0) \ 173 ? ({ const char *__ptr = (const char *) (ptr); \ 174 FILE *__stream = (stream); \ 175 size_t __cnt; \ 176 for (__cnt = (size_t) (size) * (size_t) (n); \ 177 __cnt > 0; --__cnt) \ 178 if (_IO_putc_unlocked (*__ptr++, __stream) == EOF) \ 179 break; \ 180 ((size_t) (size) * (size_t) (n) - __cnt) \ 181 / (size_t) (size); }) \ 182 : (((__builtin_constant_p (size) && (size_t) (size) == 0) \ 183 || (__builtin_constant_p (n) && (size_t) (n) == 0)) \ 184 /* Evaluate all parameters once. */ \ 185 ? ((void) (ptr), (void) (stream), (void) (size), \ 186 (void) (n), (size_t) 0) \ 187 : fwrite_unlocked (ptr, size, n, stream)))) 188#endif 189 190/* Define helper macro. */ 191#undef __STDIO_INLINE 192