2011-02-12 00:22:29 -05:00
|
|
|
#ifndef _STDIO_IMPL_H
|
|
|
|
#define _STDIO_IMPL_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "libc.h"
|
|
|
|
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
#define UNGET 8
|
2011-02-12 00:22:29 -05:00
|
|
|
|
2012-11-01 23:46:39 -04:00
|
|
|
#define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0)
|
2011-07-30 08:02:14 -04:00
|
|
|
#define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0)
|
|
|
|
#define FUNLOCK(f) if (__need_unlock) __unlockfile((f)); else
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
#define F_PERM 1
|
|
|
|
#define F_NORD 4
|
|
|
|
#define F_NOWR 8
|
|
|
|
#define F_EOF 16
|
|
|
|
#define F_ERR 32
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
#define F_SVB 64
|
2014-02-07 00:57:50 -05:00
|
|
|
#define F_APP 128
|
2011-02-12 00:22:29 -05:00
|
|
|
|
refactor headers, especially alltypes.h, and improve C++ ABI compat
the arch-specific bits/alltypes.h.sh has been replaced with a generic
alltypes.h.in and minimal arch-specific bits/alltypes.h.in.
this commit is intended to have no functional changes except:
- exposing additional symbols that POSIX allows but does not require
- changing the C++ name mangling for some types
- fixing the signedness of blksize_t on powerpc (POSIX requires signed)
- fixing the limit macros for sig_atomic_t on x86_64
- making dev_t an unsigned type (ABI matching goal, and more logical)
in addition, some types that were wrongly defined with long on 32-bit
archs were changed to int, and vice versa; this change is
non-functional except for the possibility of making pointer types
mismatch, and only affects programs that were using them incorrectly,
and only at build-time, not runtime.
the following changes were made in the interest of moving
non-arch-specific types out of the alltypes system and into the
headers they're associated with, and also will tend to improve
application compatibility:
- netdb.h now includes netinet/in.h (for socklen_t and uint32_t)
- netinet/in.h now includes sys/socket.h and inttypes.h
- sys/resource.h now includes sys/time.h (for struct timeval)
- sys/wait.h now includes signal.h (for siginfo_t)
- langinfo.h now includes nl_types.h (for nl_item)
for the types in stdint.h:
- types which are of no interest to other headers were moved out of
the alltypes system.
- fast types for 8- and 64-bit are hard-coded (at least for now); only
the 16- and 32-bit ones have reason to vary by arch.
and the following types have been changed for C++ ABI purposes;
- mbstate_t now has a struct tag, __mbstate_t
- FILE's struct tag has been changed to _IO_FILE
- DIR's struct tag has been changed to __dirstream
- locale_t's struct tag has been changed to __locale_struct
- pthread_t is defined as unsigned long in C++ mode only
- fpos_t now has a struct tag, _G_fpos64_t
- fsid_t's struct tag has been changed to __fsid_t
- idtype_t has been made an enum type (also required by POSIX)
- nl_catd has been changed from long to void *
- siginfo_t's struct tag has been removed
- sigset_t's has been given a struct tag, __sigset_t
- stack_t has been given a struct tag, sigaltstack
- suseconds_t has been changed to long on 32-bit archs
- [u]intptr_t have been changed from long to int rank on 32-bit archs
- dev_t has been made unsigned
summary of tests that have been performed against these changes:
- nsz's libc-test (diff -u before and after)
- C++ ABI check symbol dump (diff -u before, after, glibc)
- grepped for __NEED, made sure types needed are still in alltypes
- built gcc 3.4.6
2013-07-22 11:22:36 -04:00
|
|
|
struct _IO_FILE {
|
2011-02-12 00:22:29 -05:00
|
|
|
unsigned flags;
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
unsigned char *rpos, *rend;
|
|
|
|
int (*close)(FILE *);
|
|
|
|
unsigned char *wend, *wpos;
|
|
|
|
unsigned char *mustbezero_1;
|
2011-02-12 00:22:29 -05:00
|
|
|
unsigned char *wbase;
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
size_t (*read)(FILE *, unsigned char *, size_t);
|
|
|
|
size_t (*write)(FILE *, const unsigned char *, size_t);
|
|
|
|
off_t (*seek)(FILE *, off_t, int);
|
2011-02-12 00:22:29 -05:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_size;
|
|
|
|
FILE *prev, *next;
|
|
|
|
int fd;
|
|
|
|
int pipe_pid;
|
2011-07-30 08:02:14 -04:00
|
|
|
long lockcount;
|
2011-02-12 00:22:29 -05:00
|
|
|
short dummy3;
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
signed char mode;
|
2011-02-12 00:22:29 -05:00
|
|
|
signed char lbf;
|
make all objects used with atomic operations volatile
the memory model we use internally for atomics permits plain loads of
values which may be subject to concurrent modification without
requiring that a special load function be used. since a compiler is
free to make transformations that alter the number of loads or the way
in which loads are performed, the compiler is theoretically free to
break this usage. the most obvious concern is with atomic cas
constructs: something of the form tmp=*p;a_cas(p,tmp,f(tmp)); could be
transformed to a_cas(p,*p,f(*p)); where the latter is intended to show
multiple loads of *p whose resulting values might fail to be equal;
this would break the atomicity of the whole operation. but even more
fundamental breakage is possible.
with the changes being made now, objects that may be modified by
atomics are modeled as volatile, and the atomic operations performed
on them by other threads are modeled as asynchronous stores by
hardware which happens to be acting on the request of another thread.
such modeling of course does not itself address memory synchronization
between cores/cpus, but that aspect was already handled. this all
seems less than ideal, but it's the best we can do without mandating a
C11 compiler and using the C11 model for atomics.
in the case of pthread_once_t, the ABI type of the underlying object
is not volatile-qualified. so we are assuming that accessing the
object through a volatile-qualified lvalue via casts yields volatile
access semantics. the language of the C standard is somewhat unclear
on this matter, but this is an assumption the linux kernel also makes,
and seems to be the correct interpretation of the standard.
2015-03-03 22:50:02 -05:00
|
|
|
volatile int lock;
|
|
|
|
volatile int waiters;
|
2011-03-18 09:19:09 -04:00
|
|
|
void *cookie;
|
2011-02-12 00:22:29 -05:00
|
|
|
off_t off;
|
2012-08-11 18:10:38 -04:00
|
|
|
char *getln_buf;
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
void *mustbezero_2;
|
2012-04-10 21:47:37 -04:00
|
|
|
unsigned char *shend;
|
|
|
|
off_t shlim, shcnt;
|
2014-08-23 23:35:10 -04:00
|
|
|
FILE *prev_locked, *next_locked;
|
byte-based C locale, phase 2: stdio and iconv (multibyte callers)
this patch adjusts libc components which use the multibyte functions
internally, and which depend on them operating in a particular
encoding, to make the appropriate locale changes before calling them
and restore the calling thread's locale afterwards. activating the
byte-based C locale without these changes would cause regressions in
stdio and iconv.
in the case of iconv, the current implementation was simply using the
multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
locale for the duration of the iconv operation allows the code to
continue working.
in the case of stdio, POSIX requires that FILE streams have an
encoding rule bound at the time of setting wide orientation. as long
as all locales, including the C locale, used the same encoding,
treating high bytes as UTF-8, there was no need to store an encoding
rule as part of the stream's state.
a new locale field in the FILE structure points to the locale that
should be made active during fgetwc/fputwc/ungetwc on the stream. it
cannot point to the locale active at the time the stream becomes
oriented, because this locale could be mutable (the global locale) or
could be destroyed (locale_t objects produced by newlocale) before the
stream is closed. instead, a pointer to the static C or C.UTF-8 locale
object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
is used. this is valid since categories other than LC_CTYPE will not
affect these functions.
2015-06-16 05:35:31 +00:00
|
|
|
struct __locale_struct *locale;
|
2011-02-12 00:22:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t __stdio_read(FILE *, unsigned char *, size_t);
|
|
|
|
size_t __stdio_write(FILE *, const unsigned char *, size_t);
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
size_t __stdout_write(FILE *, const unsigned char *, size_t);
|
2011-02-12 00:22:29 -05:00
|
|
|
off_t __stdio_seek(FILE *, off_t, int);
|
|
|
|
int __stdio_close(FILE *);
|
|
|
|
|
2012-04-16 16:03:45 -04:00
|
|
|
size_t __string_read(FILE *, unsigned char *, size_t);
|
|
|
|
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
int __toread(FILE *);
|
|
|
|
int __towrite(FILE *);
|
|
|
|
|
use explicit visibility to optimize a few hot-path function calls
on x86 and some other archs, functions which make function calls which
might go through a PLT incur a significant overhead cost loading the
GOT register prior to making the call. this load is utterly useless in
musl, since all calls are bound at library-creation time using
-Bsymbolic-functions, but the compiler has no way of knowing this, and
attempts to set the default visibility to protected have failed due to
bugs in GCC and binutils.
this commit simply manually assigns hidden/protected visibility, as
appropriate, to a few internal-use-only functions which have many
callers, or which have callers that are hot paths like getc/putc. it
shaves about 5k off the i386 libc.so with -Os. many of the
improvements are in syscall wrappers, where the benefit is just size
and performance improvement is unmeasurable noise amid the syscall
overhead. however, stdio may be measurably faster.
if in the future there are toolchains that can do the same thing
globally without introducing linking bugs, it might be worth
considering removing these workarounds.
2012-10-25 15:40:58 -04:00
|
|
|
#if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303)
|
|
|
|
__attribute__((visibility("protected")))
|
|
|
|
#endif
|
|
|
|
int __overflow(FILE *, int), __uflow(FILE *);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
int __fseeko(FILE *, off_t, int);
|
|
|
|
int __fseeko_unlocked(FILE *, off_t, int);
|
|
|
|
off_t __ftello(FILE *);
|
|
|
|
off_t __ftello_unlocked(FILE *);
|
|
|
|
size_t __fwritex(const unsigned char *, size_t, FILE *);
|
|
|
|
int __putc_unlocked(int, FILE *);
|
|
|
|
|
|
|
|
FILE *__fdopen(int, const char *);
|
2012-10-24 21:16:06 -04:00
|
|
|
int __fmodeflags(const char *);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
2015-06-16 07:11:19 +00:00
|
|
|
FILE *__ofl_add(FILE *f);
|
|
|
|
FILE **__ofl_lock(void);
|
|
|
|
void __ofl_unlock(void);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
#define feof(f) ((f)->flags & F_EOF)
|
|
|
|
#define ferror(f) ((f)->flags & F_ERR)
|
|
|
|
|
major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.
making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:
- the implementation no longer assumes that further io will fail
following errors, and no longer blocks io when the error flag is set
(though the latter could easily be changed back if desired)
- unbuffered mode is no longer implemented as a one-byte buffer. as a
consequence, scanf unreading has to use ungetc, to the unget buffer
has been enlarged to hold at least 2 wide characters.
- the FILE structure has been rearranged to maintain the locations of
the fields that might be used in glibc getc/putc type macros, while
shrinking the structure to save some space.
- error cases for fflush, fseek, etc. should be more correct.
- library-internal macros are used for getc_unlocked and putc_unlocked
now, eliminating some ugly code duplication. __uflow and __overflow
are no longer used anywhere but these macros. switch to read or
write mode is also separated so the code can be better shared, e.g.
with ungetc.
- lots of other small things.
2011-03-28 01:14:44 -04:00
|
|
|
#define getc_unlocked(f) \
|
|
|
|
( ((f)->rpos < (f)->rend) ? *(f)->rpos++ : __uflow((f)) )
|
|
|
|
|
|
|
|
#define putc_unlocked(c, f) ( ((c)!=(f)->lbf && (f)->wpos<(f)->wend) \
|
|
|
|
? *(f)->wpos++ = (c) : __overflow((f),(c)) )
|
|
|
|
|
2011-02-12 00:22:29 -05:00
|
|
|
/* Caller-allocated FILE * operations */
|
|
|
|
FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
|
|
|
|
int __fclose_ca(FILE *);
|
|
|
|
|
|
|
|
#endif
|