public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent()
@ 2022-03-30  9:57 Sebastian Huber
  2022-03-30  9:57 ` [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static Sebastian Huber
  2022-03-30 19:34 ` [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Jeff Johnston
  0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Huber @ 2022-03-30  9:57 UTC (permalink / raw)
  To: newlib

Remove the _fwalk() implementation to avoid duplicated code with
_fwalk_reent().
---
 newlib/libc/stdio/findfp.c | 16 ++++++++--------
 newlib/libc/stdio/fwalk.c  | 26 --------------------------
 newlib/libc/stdio/local.h  |  1 -
 newlib/libc/stdio/refill.c |  8 ++++----
 winsup/cygwin/syscalls.cc  |  6 +++---
 5 files changed, 15 insertions(+), 42 deletions(-)

diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index 9877c1f2c..7bb48e014 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -323,20 +323,20 @@ __sinit_lock_release (void)
 
 /* Walkable file locking routine.  */
 static int
-__fp_lock (FILE * ptr)
+__fp_lock (struct _reent * ptr __unused, FILE * fp)
 {
-  if (!(ptr->_flags2 & __SNLK))
-    _flockfile (ptr);
+  if (!(fp->_flags2 & __SNLK))
+    _flockfile (fp);
 
   return 0;
 }
 
 /* Walkable file unlocking routine.  */
 static int
-__fp_unlock (FILE * ptr)
+__fp_unlock (struct _reent * ptr __unused, FILE * fp)
 {
-  if (!(ptr->_flags2 & __SNLK))
-    _funlockfile (ptr);
+  if (!(fp->_flags2 & __SNLK))
+    _funlockfile (fp);
 
   return 0;
 }
@@ -346,13 +346,13 @@ __fp_lock_all (void)
 {
   __sfp_lock_acquire ();
 
-  (void) _fwalk (_REENT, __fp_lock);
+  (void) _fwalk_reent (_REENT, __fp_lock);
 }
 
 void
 __fp_unlock_all (void)
 {
-  (void) _fwalk (_REENT, __fp_unlock);
+  (void) _fwalk_reent (_REENT, __fp_unlock);
 
   __sfp_lock_release ();
 }
diff --git a/newlib/libc/stdio/fwalk.c b/newlib/libc/stdio/fwalk.c
index 8040d5718..2cefcc40e 100644
--- a/newlib/libc/stdio/fwalk.c
+++ b/newlib/libc/stdio/fwalk.c
@@ -27,32 +27,6 @@ static char sccsid[] = "%W% (Berkeley) %G%";
 #include <errno.h>
 #include "local.h"
 
-int
-_fwalk (struct _reent *ptr,
-       register int (*function) (FILE *))
-{
-  register FILE *fp;
-  register int n, ret = 0;
-  register struct _glue *g;
-
-  /*
-   * It should be safe to walk the list without locking it;
-   * new nodes are only added to the end and none are ever
-   * removed.
-   *
-   * Avoid locking this list while walking it or else you will
-   * introduce a potential deadlock in [at least] refill.c.
-   */
-  for (g = &ptr->__sglue; g != NULL; g = g->_next)
-    for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
-      if (fp->_flags != 0 && fp->_flags != 1 && fp->_file != -1)
-	ret |= (*function) (fp);
-
-  return ret;
-}
-
-/* Special version of __fwalk where the function pointer is a reentrant
-   I/O function (e.g. _fclose_r).  */
 int
 _fwalk_reent (struct _reent *ptr,
        register int (*reent_function) (struct _reent *, FILE *))
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index f63c5fb10..09ccd6407 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -183,7 +183,6 @@ extern void   __sinit (struct _reent *);
 extern void   _cleanup_r (struct _reent *);
 extern void   __smakebuf_r (struct _reent *, FILE *);
 extern int    __swhatbuf_r (struct _reent *, FILE *, size_t *, int *);
-extern int    _fwalk (struct _reent *, int (*)(FILE *));
 extern int    _fwalk_reent (struct _reent *, int (*)(struct _reent *, FILE *));
 struct _glue * __sfmoreglue (struct _reent *,int n);
 extern int __submore (struct _reent *, FILE *);
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index ccedc7af7..31665bcd9 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -23,10 +23,10 @@
 #include "local.h"
 
 static int
-lflush (FILE *fp)
+lflush (struct _reent * ptr __unused, FILE *fp)
 {
   if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR))
-    return fflush (fp);
+    return _fflush_r (_REENT, fp);
   return 0;
 }
 
@@ -102,10 +102,10 @@ __srefill_r (struct _reent * ptr,
    */
   if (fp->_flags & (__SLBF | __SNBF))
     {
-      /* Ignore this file in _fwalk to avoid potential deadlock. */
+      /* Ignore this file in _fwalk_reent to avoid potential deadlock. */
       short orig_flags = fp->_flags;
       fp->_flags = 1;
-      (void) _fwalk (_GLOBAL_REENT, lflush);
+      (void) _fwalk_reent (_GLOBAL_REENT, lflush);
       fp->_flags = orig_flags;
 
       /* Now flush this file without locking it. */
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 24ec01da8..1cecaa017 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -3058,10 +3058,10 @@ _cygwin_istext_for_stdio (int fd)
 }
 
 /* internal newlib function */
-extern "C" int _fwalk (struct _reent *ptr, int (*function) (FILE *));
+extern "C" int _fwalk_reent (struct _reent *ptr, int (*function) (struct _reent *, FILE *));
 
 static int
-setmode_helper (FILE *f)
+setmode_helper (struct _reent *ptr __unused, FILE *f)
 {
   if (fileno (f) != _my_tls.locals.setmode_file)
     {
@@ -3137,7 +3137,7 @@ cygwin_setmode (int fd, int mode)
 	_my_tls.locals.setmode_mode = O_TEXT;
       else
 	_my_tls.locals.setmode_mode = O_BINARY;
-      _fwalk (_GLOBAL_REENT, setmode_helper);
+      _fwalk_reent (_GLOBAL_REENT, setmode_helper);
     }
   return res;
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static
  2022-03-30  9:57 [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Sebastian Huber
@ 2022-03-30  9:57 ` Sebastian Huber
  2022-03-30 19:34   ` Jeff Johnston
  2022-03-30 19:34 ` [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Jeff Johnston
  1 sibling, 1 reply; 4+ messages in thread
From: Sebastian Huber @ 2022-03-30  9:57 UTC (permalink / raw)
  To: newlib

Rename __sfmoreglue() in sfmoreglue() and make it static.  This function is
only used by __sfp() in the same translation unit.

Remove use of register keyword.
---
 newlib/libc/stdio/findfp.c | 7 +++----
 newlib/libc/stdio/local.h  | 1 -
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index 7bb48e014..eca47bd92 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -122,9 +122,8 @@ struct glue_with_file {
   FILE file;
 };
 
-struct _glue *
-__sfmoreglue (struct _reent *d,
-       register int n)
+static struct _glue *
+sfmoreglue (struct _reent *d, int n)
 {
   struct glue_with_file *g;
 
@@ -160,7 +159,7 @@ __sfp (struct _reent *d)
 	if (fp->_flags == 0)
 	  goto found;
       if (g->_next == NULL &&
-	  (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
+	  (g->_next = sfmoreglue (d, NDYNAMIC)) == NULL)
 	break;
     }
   _newlib_sfp_lock_exit ();
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index 09ccd6407..86422eb11 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -184,7 +184,6 @@ extern void   _cleanup_r (struct _reent *);
 extern void   __smakebuf_r (struct _reent *, FILE *);
 extern int    __swhatbuf_r (struct _reent *, FILE *, size_t *, int *);
 extern int    _fwalk_reent (struct _reent *, int (*)(struct _reent *, FILE *));
-struct _glue * __sfmoreglue (struct _reent *,int n);
 extern int __submore (struct _reent *, FILE *);
 
 #ifdef __LARGE64_FILES
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent()
  2022-03-30  9:57 [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Sebastian Huber
  2022-03-30  9:57 ` [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static Sebastian Huber
@ 2022-03-30 19:34 ` Jeff Johnston
  1 sibling, 0 replies; 4+ messages in thread
From: Jeff Johnston @ 2022-03-30 19:34 UTC (permalink / raw)
  To: Sebastian Huber; +Cc: Newlib

Looks fine.

-- Jeff J.

On Wed, Mar 30, 2022 at 5:58 AM Sebastian Huber <
sebastian.huber@embedded-brains.de> wrote:

> Remove the _fwalk() implementation to avoid duplicated code with
> _fwalk_reent().
> ---
>  newlib/libc/stdio/findfp.c | 16 ++++++++--------
>  newlib/libc/stdio/fwalk.c  | 26 --------------------------
>  newlib/libc/stdio/local.h  |  1 -
>  newlib/libc/stdio/refill.c |  8 ++++----
>  winsup/cygwin/syscalls.cc  |  6 +++---
>  5 files changed, 15 insertions(+), 42 deletions(-)
>
> diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
> index 9877c1f2c..7bb48e014 100644
> --- a/newlib/libc/stdio/findfp.c
> +++ b/newlib/libc/stdio/findfp.c
> @@ -323,20 +323,20 @@ __sinit_lock_release (void)
>
>  /* Walkable file locking routine.  */
>  static int
> -__fp_lock (FILE * ptr)
> +__fp_lock (struct _reent * ptr __unused, FILE * fp)
>  {
> -  if (!(ptr->_flags2 & __SNLK))
> -    _flockfile (ptr);
> +  if (!(fp->_flags2 & __SNLK))
> +    _flockfile (fp);
>
>    return 0;
>  }
>
>  /* Walkable file unlocking routine.  */
>  static int
> -__fp_unlock (FILE * ptr)
> +__fp_unlock (struct _reent * ptr __unused, FILE * fp)
>  {
> -  if (!(ptr->_flags2 & __SNLK))
> -    _funlockfile (ptr);
> +  if (!(fp->_flags2 & __SNLK))
> +    _funlockfile (fp);
>
>    return 0;
>  }
> @@ -346,13 +346,13 @@ __fp_lock_all (void)
>  {
>    __sfp_lock_acquire ();
>
> -  (void) _fwalk (_REENT, __fp_lock);
> +  (void) _fwalk_reent (_REENT, __fp_lock);
>  }
>
>  void
>  __fp_unlock_all (void)
>  {
> -  (void) _fwalk (_REENT, __fp_unlock);
> +  (void) _fwalk_reent (_REENT, __fp_unlock);
>
>    __sfp_lock_release ();
>  }
> diff --git a/newlib/libc/stdio/fwalk.c b/newlib/libc/stdio/fwalk.c
> index 8040d5718..2cefcc40e 100644
> --- a/newlib/libc/stdio/fwalk.c
> +++ b/newlib/libc/stdio/fwalk.c
> @@ -27,32 +27,6 @@ static char sccsid[] = "%W% (Berkeley) %G%";
>  #include <errno.h>
>  #include "local.h"
>
> -int
> -_fwalk (struct _reent *ptr,
> -       register int (*function) (FILE *))
> -{
> -  register FILE *fp;
> -  register int n, ret = 0;
> -  register struct _glue *g;
> -
> -  /*
> -   * It should be safe to walk the list without locking it;
> -   * new nodes are only added to the end and none are ever
> -   * removed.
> -   *
> -   * Avoid locking this list while walking it or else you will
> -   * introduce a potential deadlock in [at least] refill.c.
> -   */
> -  for (g = &ptr->__sglue; g != NULL; g = g->_next)
> -    for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
> -      if (fp->_flags != 0 && fp->_flags != 1 && fp->_file != -1)
> -       ret |= (*function) (fp);
> -
> -  return ret;
> -}
> -
> -/* Special version of __fwalk where the function pointer is a reentrant
> -   I/O function (e.g. _fclose_r).  */
>  int
>  _fwalk_reent (struct _reent *ptr,
>         register int (*reent_function) (struct _reent *, FILE *))
> diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
> index f63c5fb10..09ccd6407 100644
> --- a/newlib/libc/stdio/local.h
> +++ b/newlib/libc/stdio/local.h
> @@ -183,7 +183,6 @@ extern void   __sinit (struct _reent *);
>  extern void   _cleanup_r (struct _reent *);
>  extern void   __smakebuf_r (struct _reent *, FILE *);
>  extern int    __swhatbuf_r (struct _reent *, FILE *, size_t *, int *);
> -extern int    _fwalk (struct _reent *, int (*)(FILE *));
>  extern int    _fwalk_reent (struct _reent *, int (*)(struct _reent *,
> FILE *));
>  struct _glue * __sfmoreglue (struct _reent *,int n);
>  extern int __submore (struct _reent *, FILE *);
> diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
> index ccedc7af7..31665bcd9 100644
> --- a/newlib/libc/stdio/refill.c
> +++ b/newlib/libc/stdio/refill.c
> @@ -23,10 +23,10 @@
>  #include "local.h"
>
>  static int
> -lflush (FILE *fp)
> +lflush (struct _reent * ptr __unused, FILE *fp)
>  {
>    if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR))
> -    return fflush (fp);
> +    return _fflush_r (_REENT, fp);
>    return 0;
>  }
>
> @@ -102,10 +102,10 @@ __srefill_r (struct _reent * ptr,
>     */
>    if (fp->_flags & (__SLBF | __SNBF))
>      {
> -      /* Ignore this file in _fwalk to avoid potential deadlock. */
> +      /* Ignore this file in _fwalk_reent to avoid potential deadlock. */
>        short orig_flags = fp->_flags;
>        fp->_flags = 1;
> -      (void) _fwalk (_GLOBAL_REENT, lflush);
> +      (void) _fwalk_reent (_GLOBAL_REENT, lflush);
>        fp->_flags = orig_flags;
>
>        /* Now flush this file without locking it. */
> diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
> index 24ec01da8..1cecaa017 100644
> --- a/winsup/cygwin/syscalls.cc
> +++ b/winsup/cygwin/syscalls.cc
> @@ -3058,10 +3058,10 @@ _cygwin_istext_for_stdio (int fd)
>  }
>
>  /* internal newlib function */
> -extern "C" int _fwalk (struct _reent *ptr, int (*function) (FILE *));
> +extern "C" int _fwalk_reent (struct _reent *ptr, int (*function) (struct
> _reent *, FILE *));
>
>  static int
> -setmode_helper (FILE *f)
> +setmode_helper (struct _reent *ptr __unused, FILE *f)
>  {
>    if (fileno (f) != _my_tls.locals.setmode_file)
>      {
> @@ -3137,7 +3137,7 @@ cygwin_setmode (int fd, int mode)
>         _my_tls.locals.setmode_mode = O_TEXT;
>        else
>         _my_tls.locals.setmode_mode = O_BINARY;
> -      _fwalk (_GLOBAL_REENT, setmode_helper);
> +      _fwalk_reent (_GLOBAL_REENT, setmode_helper);
>      }
>    return res;
>  }
> --
> 2.34.1
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static
  2022-03-30  9:57 ` [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static Sebastian Huber
@ 2022-03-30 19:34   ` Jeff Johnston
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Johnston @ 2022-03-30 19:34 UTC (permalink / raw)
  To: Sebastian Huber; +Cc: Newlib

Looks fine.

-- Jeff J.

On Wed, Mar 30, 2022 at 5:58 AM Sebastian Huber <
sebastian.huber@embedded-brains.de> wrote:

> Rename __sfmoreglue() in sfmoreglue() and make it static.  This function is
> only used by __sfp() in the same translation unit.
>
> Remove use of register keyword.
> ---
>  newlib/libc/stdio/findfp.c | 7 +++----
>  newlib/libc/stdio/local.h  | 1 -
>  2 files changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
> index 7bb48e014..eca47bd92 100644
> --- a/newlib/libc/stdio/findfp.c
> +++ b/newlib/libc/stdio/findfp.c
> @@ -122,9 +122,8 @@ struct glue_with_file {
>    FILE file;
>  };
>
> -struct _glue *
> -__sfmoreglue (struct _reent *d,
> -       register int n)
> +static struct _glue *
> +sfmoreglue (struct _reent *d, int n)
>  {
>    struct glue_with_file *g;
>
> @@ -160,7 +159,7 @@ __sfp (struct _reent *d)
>         if (fp->_flags == 0)
>           goto found;
>        if (g->_next == NULL &&
> -         (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
> +         (g->_next = sfmoreglue (d, NDYNAMIC)) == NULL)
>         break;
>      }
>    _newlib_sfp_lock_exit ();
> diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
> index 09ccd6407..86422eb11 100644
> --- a/newlib/libc/stdio/local.h
> +++ b/newlib/libc/stdio/local.h
> @@ -184,7 +184,6 @@ extern void   _cleanup_r (struct _reent *);
>  extern void   __smakebuf_r (struct _reent *, FILE *);
>  extern int    __swhatbuf_r (struct _reent *, FILE *, size_t *, int *);
>  extern int    _fwalk_reent (struct _reent *, int (*)(struct _reent *,
> FILE *));
> -struct _glue * __sfmoreglue (struct _reent *,int n);
>  extern int __submore (struct _reent *, FILE *);
>
>  #ifdef __LARGE64_FILES
> --
> 2.34.1
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-30 19:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-30  9:57 [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Sebastian Huber
2022-03-30  9:57 ` [PATCH 2/2] newlib: Rename __sfmoreglue() and make it static Sebastian Huber
2022-03-30 19:34   ` Jeff Johnston
2022-03-30 19:34 ` [PATCH 1/2] Replace _fwalk() calls with _fwalk_reent() Jeff Johnston

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).