MFC: r289863,r289931,r290110,r290230,r290231,r290232

r290232:

Microoptimize.

r290231:

Addition to prev. commit.
In some edge cases fp->_p can be changed in _sseek(), recalculate.

r290230:

Don't seek to the end if write buffer is empty (in append modes).
PR:     204156

r290110:

Add _flags2 per jhb@ suggestion since no room left in _flags.
Rewrite O_APPEND flag checking using new __S2OAP flag.

r289931:

According to POSIX, a write operation shall start at the current size of
the stream (if mode had 'a' as the first character).

r289863:

Since no room left in the _flags, reuse __SALC for O_APPEND.
It helps to remove _fcntl() call from _ftello() and optimize seek position
calculation in _swrite().
This commit is contained in:
ache 2015-11-08 13:37:16 +00:00
parent 1df83ba3d3
commit 8a3e4a6db7
8 changed files with 40 additions and 23 deletions

View File

@ -144,6 +144,7 @@ struct __sFILE {
int _fl_count; /* recursive lock count */
int _orientation; /* orientation for fwide() */
__mbstate_t _mbstate; /* multibyte conversion state */
int _flags2; /* additional flags */
};
#ifndef _STDFILE_DECLARED
#define _STDFILE_DECLARED
@ -176,6 +177,8 @@ __END_DECLS
#define __SALC 0x4000 /* allocate string space dynamically */
#define __SIGN 0x8000 /* ignore this file in _fwalk */
#define __S2OAP 0x0001 /* O_APPEND mode is set */
/*
* The following three definitions are for ANSI C, which took them
* from System V, which brilliantly took internal interface macros and

View File

@ -90,7 +90,9 @@ fdopen(int fd, const char *mode)
* O_APPEND bit set, assert __SAPP so that __swrite() caller
* will _sseek() to the end before write.
*/
if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
if (fdflags & O_APPEND)
fp->_flags2 |= __S2OAP;
else if (oflags & O_APPEND)
fp->_flags |= __SAPP;
fp->_file = fd;
fp->_cookie = fp;

View File

@ -155,6 +155,7 @@ found:
/* fp->_fl_mutex = NULL; */ /* once set always set (reused) */
fp->_orientation = 0;
memset(&fp->_mbstate, 0, sizeof(mbstate_t));
fp->_flags2 = 0;
return (fp);
}

View File

@ -149,6 +149,9 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
return (NULL);
}
if (mode[0] == 'a')
f->_flags |= __SAPP;
/*
* Turn off buffering, so a write past the end of the buffer
* correctly returns a short object count.

View File

@ -91,7 +91,9 @@ fopen(const char * __restrict file, const char * __restrict mode)
* we can do about this. (We could set __SAPP and check in
* fseek and ftell.)
*/
if (oflags & O_APPEND)
if (oflags & O_APPEND) {
fp->_flags2 |= __S2OAP;
(void)_sseek(fp, (fpos_t)0, SEEK_END);
}
return (fp);
}

View File

@ -186,6 +186,7 @@ finish:
fp->_lb._size = 0;
fp->_orientation = 0;
memset(&fp->_mbstate, 0, sizeof(mbstate_t));
fp->_flags2 = 0;
if (f < 0) { /* did not get it after all */
if (isopen)
@ -239,8 +240,10 @@ finish:
* we can do about this. (We could set __SAPP and check in
* fseek and ftell.)
*/
if (oflags & O_APPEND)
if (oflags & O_APPEND) {
fp->_flags2 |= __S2OAP;
(void) _sseek(fp, (fpos_t)0, SEEK_END);
}
FUNLOCKFILE(fp);
return (fp);
}

View File

@ -88,7 +88,6 @@ _ftello(FILE *fp, fpos_t *offset)
{
fpos_t pos;
size_t n;
int dflags;
if (fp->_seek == NULL) {
errno = ESPIPE; /* historic practice */
@ -119,29 +118,33 @@ _ftello(FILE *fp, fpos_t *offset)
}
if (HASUB(fp))
pos -= fp->_r; /* Can be negative at this point. */
} else if ((fp->_flags & __SWR) && fp->_p != NULL) {
dflags = 0;
if (fp->_flags & __SAPP)
dflags = O_APPEND;
else if (fp->_file != -1 &&
(dflags = _fcntl(fp->_file, F_GETFL)) < 0)
return (1);
if ((dflags & O_APPEND) &&
(pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
if ((fp->_flags & __SOPT) || __sflush(fp) ||
(pos = _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
return (1);
else {
*offset = pos;
return (0);
}
}
} else if ((fp->_flags & __SWR) && fp->_p != NULL &&
(n = fp->_p - fp->_bf._base) > 0) {
/*
* Writing. Any buffered characters cause the
* position to be greater than that in the
* underlying object.
*/
n = fp->_p - fp->_bf._base;
if ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP)) {
int serrno = errno;
errno = 0;
if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
if (errno == ESPIPE ||
(fp->_flags & __SOPT) || __sflush(fp) ||
(pos =
_sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
return (1);
else {
errno = serrno;
*offset = pos;
return (0);
}
}
errno = serrno;
/* fp->_p can be changed in _sseek(), recalculate. */
n = fp->_p - fp->_bf._base;
}
if (pos > OFF_MAX - n) {
errno = EOVERFLOW;
return (1);

View File

@ -117,7 +117,7 @@ _swrite(FILE *fp, char const *buf, int n)
ret = (*fp->_write)(fp->_cookie, buf, n);
/* __SOFF removed even on success in case O_APPEND mode is set. */
if (ret >= 0) {
if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) &&
if ((fp->_flags & __SOFF) && !(fp->_flags2 & __S2OAP) &&
fp->_offset <= OFF_MAX - ret)
fp->_offset += ret;
else