bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fflush: refactoring


From: Bruno Haible
Subject: fflush: refactoring
Date: Sat, 26 Apr 2008 16:46:38 +0200
User-agent: KMail/1.5.4

In preparation of the DragonflyBSD port, this moves a few system dependent
tasks to inline functions.

2008-04-26  Bruno Haible  <address@hidden>

        * lib/fflush.c (clear_ungetc_buffer, disable_seek_optimization,
        restore_seek_optimization, update_fpos_cache): New functions, extracted
        from rpl_fflush.
        (rpl_fflush): Use them.
        * m4/fflush.m4 (gl_PREREQ_FFLUSH): New macro.
        (gl_REPLACE_FFLUSH): Use it.

*** lib/fflush.c.orig   2008-04-26 16:41:38.000000000 +0200
--- lib/fflush.c        2008-04-26 16:41:34.000000000 +0200
***************
*** 29,34 ****
--- 29,83 ----
  
  #undef fflush
  
+ static inline void
+ clear_ungetc_buffer (FILE *fp)
+ {
+ #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, 
Cygwin */
+ # if defined __NetBSD__ || defined __OpenBSD__
+   struct __sfileext
+     {
+       struct  __sbuf _ub; /* ungetc buffer */
+       /* More fields, not relevant here.  */
+     };
+ #  define HASUB(fp) (((struct __sfileext *) (fp)->_ext._base)->_ub._base != 
NULL)
+ # else
+ #  define HASUB(fp) ((fp)->_ub._base != NULL)
+ # endif
+   if (HASUB (fp))
+     {
+       fp->_p += stream->_r;
+       fp->_r = 0;
+     }
+ #endif
+ }
+ 
+ #if defined __sferror && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, MacOS X, 
Cygwin */
+ 
+ static inline int
+ disable_seek_optimization (FILE *fp)
+ {
+   int saved_flags = fp->_flags & (__SOPT | __SNPT);
+   fp->_flags = (fp->_flags & ~__SOPT) | __SNPT;
+   return saved_flags;
+ }
+ 
+ static inline void
+ restore_seek_optimization (FILE *fp, int saved_flags)
+ {
+   fp->_flags = (fp->_flags & ~(__SOPT | __SNPT)) | saved_flags;
+ }
+ 
+ #endif
+ 
+ static inline void
+ update_fpos_cache (FILE *fp)
+ {
+ #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, 
Cygwin */
+   fp->_offset = pos;
+   fp->_flags |= __SOFF;
+ #endif
+ }
+ 
  /* Flush all pending data on STREAM according to POSIX rules.  Both
     output and seekable input streams are supported.  */
  int
***************
*** 80,103 ****
       _IOERR, because an ungetc() on this platform prepends the pushed-back
       bytes to the buffer without an indication of the limit between the
       pushed-back bytes and the read-ahead bytes.  */
! #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, 
Cygwin */
!   {
! # if defined __NetBSD__ || defined __OpenBSD__
!     struct __sfileext
!       {
!       struct  __sbuf _ub; /* ungetc buffer */
!       /* More fields, not relevant here.  */
!       };
!     if (((struct __sfileext *) stream->_ext._base)->_ub._base != NULL)
! # else
!     if (stream->_ub._base != NULL)
! # endif
!       {
!       stream->_p += stream->_r;
!       stream->_r = 0;
!       }
!   }
! #endif
  
    /* POSIX does not specify fflush behavior for non-seekable input
       streams.  Some implementations purge unread data, some return
--- 129,135 ----
       _IOERR, because an ungetc() on this platform prepends the pushed-back
       bytes to the buffer without an indication of the limit between the
       pushed-back bytes and the read-ahead bytes.  */
!   clear_ungetc_buffer (stream);
  
    /* POSIX does not specify fflush behavior for non-seekable input
       streams.  Some implementations purge unread data, some return
***************
*** 122,133 ****
      /* Disable seek optimization for the next fseeko call.  This tells the
         following fseeko call to seek to the desired position directly, rather
         than to seek to a block-aligned boundary.  */
!     int saved_flags = stream->_flags & (__SOPT | __SNPT);
!     stream->_flags = (stream->_flags & ~__SOPT) | __SNPT;
  
      result = fseeko (stream, pos, SEEK_SET);
  
!     stream->_flags = (stream->_flags & ~(__SOPT | __SNPT)) | saved_flags;
    }
    return result;
  
--- 154,164 ----
      /* Disable seek optimization for the next fseeko call.  This tells the
         following fseeko call to seek to the desired position directly, rather
         than to seek to a block-aligned boundary.  */
!     int saved_flags = disable_seek_optimization (stream);
  
      result = fseeko (stream, pos, SEEK_SET);
  
!     restore_seek_optimization (stream, saved_flags);
    }
    return result;
  
***************
*** 138,147 ****
      return EOF;
    /* After a successful lseek, update the file descriptor's position cache
       in the stream.  */
! # if defined __sferror           /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin 
*/
!   stream->_offset = pos;
!   stream->_flags |= __SOFF;
! # endif
  
    return 0;
  
--- 169,175 ----
      return EOF;
    /* After a successful lseek, update the file descriptor's position cache
       in the stream.  */
!   update_fpos_cache (stream);
  
    return 0;
  
*** m4/fflush.m4.orig   2008-04-26 16:41:38.000000000 +0200
--- m4/fflush.m4        2008-04-26 16:21:38.000000000 +0200
***************
*** 1,4 ****
! # fflush.m4 serial 5
  
  # Copyright (C) 2007-2008 Free Software Foundation, Inc.
  # This file is free software; the Free Software Foundation
--- 1,4 ----
! # fflush.m4 serial 6
  
  # Copyright (C) 2007-2008 Free Software Foundation, Inc.
  # This file is free software; the Free Software Foundation
***************
*** 56,59 ****
--- 56,67 ----
    AC_REQUIRE([gl_STDIO_H_DEFAULTS])
    REPLACE_FFLUSH=1
    REPLACE_FSEEKO=1
+   gl_PREREQ_FFLUSH
+ ])
+ 
+ # Prerequisites of lib/fflush.c.
+ AC_DEFUN([gl_PREREQ_FFLUSH],
+ [
+   AC_REQUIRE([AC_C_INLINE])
+   :
  ])





reply via email to

[Prev in Thread] Current Thread [Next in Thread]