Date: Sun, 8 Nov 2015 13:37:16 +0000 (UTC) From: "Andrey A. Chernov" <ache@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r290544 - in stable/10: include lib/libc/stdio Message-ID: <201511081337.tA8DbGEL072196@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: ache Date: Sun Nov 8 13:37:16 2015 New Revision: 290544 URL: https://svnweb.freebsd.org/changeset/base/290544 Log: 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(). Modified: stable/10/include/stdio.h stable/10/lib/libc/stdio/fdopen.c stable/10/lib/libc/stdio/findfp.c stable/10/lib/libc/stdio/fmemopen.c stable/10/lib/libc/stdio/fopen.c stable/10/lib/libc/stdio/freopen.c stable/10/lib/libc/stdio/ftell.c stable/10/lib/libc/stdio/stdio.c Directory Properties: stable/10/ (props changed) Modified: stable/10/include/stdio.h ============================================================================== --- stable/10/include/stdio.h Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/include/stdio.h Sun Nov 8 13:37:16 2015 (r290544) @@ -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 Modified: stable/10/lib/libc/stdio/fdopen.c ============================================================================== --- stable/10/lib/libc/stdio/fdopen.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/fdopen.c Sun Nov 8 13:37:16 2015 (r290544) @@ -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; Modified: stable/10/lib/libc/stdio/findfp.c ============================================================================== --- stable/10/lib/libc/stdio/findfp.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/findfp.c Sun Nov 8 13:37:16 2015 (r290544) @@ -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); } Modified: stable/10/lib/libc/stdio/fmemopen.c ============================================================================== --- stable/10/lib/libc/stdio/fmemopen.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/fmemopen.c Sun Nov 8 13:37:16 2015 (r290544) @@ -149,6 +149,9 @@ fmemopen(void * __restrict buf, size_t s 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. Modified: stable/10/lib/libc/stdio/fopen.c ============================================================================== --- stable/10/lib/libc/stdio/fopen.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/fopen.c Sun Nov 8 13:37:16 2015 (r290544) @@ -91,7 +91,9 @@ fopen(const char * __restrict file, cons * 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); } Modified: stable/10/lib/libc/stdio/freopen.c ============================================================================== --- stable/10/lib/libc/stdio/freopen.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/freopen.c Sun Nov 8 13:37:16 2015 (r290544) @@ -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); } Modified: stable/10/lib/libc/stdio/ftell.c ============================================================================== --- stable/10/lib/libc/stdio/ftell.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/ftell.c Sun Nov 8 13:37:16 2015 (r290544) @@ -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); Modified: stable/10/lib/libc/stdio/stdio.c ============================================================================== --- stable/10/lib/libc/stdio/stdio.c Sun Nov 8 12:24:19 2015 (r290543) +++ stable/10/lib/libc/stdio/stdio.c Sun Nov 8 13:37:16 2015 (r290544) @@ -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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201511081337.tA8DbGEL072196>