mirror of https://git.musl-libc.org/git/musl
Browse Source
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.rs-1.0
37 changed files with 258 additions and 281 deletions
@ -0,0 +1,10 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) |
|||
{ |
|||
struct termios tio; |
|||
f->write = __stdio_write; |
|||
if (!(f->flags & F_SVB) && syscall(SYS_ioctl, f->fd, TCGETS, &tio)) |
|||
f->lbf = -1; |
|||
return __stdio_write(f, buf, len); |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
#include <stdio_impl.h> |
|||
|
|||
int __toread(FILE *f) |
|||
{ |
|||
f->mode |= f->mode-1; |
|||
if (f->wpos > f->buf) f->write(f, 0, 0); |
|||
f->wpos = f->wbase = f->wend = 0; |
|||
if (f->flags & (F_EOF|F_NORD)) { |
|||
if (f->flags & F_NORD) f->flags |= F_ERR; |
|||
return EOF; |
|||
} |
|||
f->rpos = f->rend = f->buf; |
|||
return 0; |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int __towrite(FILE *f) |
|||
{ |
|||
f->mode |= f->mode-1; |
|||
if (f->flags & (F_NOWR)) { |
|||
f->flags |= F_ERR; |
|||
return EOF; |
|||
} |
|||
/* Clear read buffer (easier than summoning nasal demons) */ |
|||
f->rpos = f->rend = 0; |
|||
|
|||
/* Activate write through the buffer. */ |
|||
f->wpos = f->wbase = f->buf; |
|||
f->wend = f->buf + f->buf_size; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
/* Link flush-on-exit code iff any stdio write functions are linked. */ |
|||
int (*const __fflush_on_exit)(FILE *) = fflush; |
|||
@ -1,7 +1,11 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
/* This function will never be called if there is already data
|
|||
* buffered for reading. Thus we can get by with very few branches. */ |
|||
|
|||
int __uflow(FILE *f) |
|||
{ |
|||
if (__underflow(f) < 0) return EOF; |
|||
else return *f->rpos++; |
|||
unsigned char c = EOF; |
|||
if (f->rend || !__toread(f)) f->read(f, &c, 1); |
|||
return c; |
|||
} |
|||
|
|||
@ -1,38 +0,0 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int __underflow(FILE *f) |
|||
{ |
|||
ssize_t cnt; |
|||
|
|||
/* Read from buffer (Do we ever get called when this is true??) */ |
|||
if (f->rpos < f->rstop) return *f->rpos; |
|||
|
|||
/* Initialize if we're not already reading */ |
|||
if (!f->rstop) { |
|||
/* Fail immediately if unreadable, eof, or error state. */ |
|||
if (f->flags & (F_EOF|F_ERR|F_NORD)) return EOF; |
|||
|
|||
/* Set byte orientation -1,0=>-1; 1=>1 */ |
|||
f->mode |= f->mode-1; |
|||
|
|||
/* Flush any unwritten output; fail on error. */ |
|||
if (f->wpos > f->buf && __oflow(f)) return EOF; |
|||
|
|||
/* Disallow writes to buffer. */ |
|||
f->wstop = 0; |
|||
} |
|||
|
|||
/* Perform the underlying read operation */ |
|||
if ((cnt=f->read(f, f->buf, f->buf_size)) + 1 <= 1) { |
|||
/* Set flags and leave read mode */ |
|||
f->flags |= F_EOF | (cnt & F_ERR); |
|||
f->rpos = f->rend = f->rstop = 0; |
|||
return EOF; |
|||
} |
|||
|
|||
/* Setup buffer pointers for reading from buffer */ |
|||
f->rpos = f->buf; |
|||
f->rend = f->rstop = f->buf + cnt; |
|||
|
|||
return *f->rpos; |
|||
} |
|||
@ -1,6 +0,0 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int getc(FILE *f) |
|||
{ |
|||
return fgetc(f); |
|||
} |
|||
@ -1,8 +1,8 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int getc_unlocked(FILE *f) |
|||
int (getc_unlocked)(FILE *f) |
|||
{ |
|||
return f->rpos < f->rstop ? *f->rpos++ : __uflow(f); |
|||
return getc_unlocked(f); |
|||
} |
|||
|
|||
weak_alias (getc_unlocked, fgetc_unlocked); |
|||
|
|||
@ -1,8 +0,0 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int putc(int c, FILE *f) |
|||
{ |
|||
return fputc(c, f); |
|||
} |
|||
|
|||
weak_alias(putc, _IO_putc); |
|||
@ -1,8 +1,8 @@ |
|||
#include "stdio_impl.h" |
|||
|
|||
int putc_unlocked(int c, FILE *f) |
|||
int (putc_unlocked)(int c, FILE *f) |
|||
{ |
|||
return f->wpos < f->wstop ? (*f->wpos++ = c) : __overflow(f, c); |
|||
return putc_unlocked(c, f); |
|||
} |
|||
|
|||
weak_alias(putc_unlocked, fputc_unlocked); |
|||
|
|||
Loading…
Reference in new issue