Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Lancelot SIX <lsix@lancelotsix.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/6] Use vector in remote-fileio.c
Date: Mon, 1 Jan 2024 14:20:41 +0000	[thread overview]
Message-ID: <20240101142041.dndg63bv42hs7lpo@octopus> (raw)
In-Reply-To: <20231231-remote-fileio-v1-2-249cc6c440d9@tromey.com>

Hi Tom,

I have a couple of comments below.

On Sun, Dec 31, 2023 at 01:25:39PM -0700, Tom Tromey wrote:
> This changes remote_fio_data to be an object holding a vector.  This
> simplifies the code somewhat.
> ---
>  gdb/remote-fileio.c | 51 +++++++++++++++++----------------------------------
>  1 file changed, 17 insertions(+), 34 deletions(-)
> 
> diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
> index 367edb54f74..4891b0c5b92 100644
> --- a/gdb/remote-fileio.c
> +++ b/gdb/remote-fileio.c
> @@ -37,9 +37,9 @@
>  #endif
>  #include <signal.h>
>  
> -static struct {
> -  int *fd_map;
> -  int fd_map_size;
> +static struct
> +{
> +  std::vector<int> fd_map;
>  } remote_fio_data;
>  
>  #define FIO_FD_INVALID		-1
> @@ -51,16 +51,13 @@ static int remote_fio_system_call_allowed = 0;
>  static int
>  remote_fileio_init_fd_map (void)
>  {
> -  int i;
> -
> -  if (!remote_fio_data.fd_map)
> +  if (remote_fio_data.fd_map.empty ())
>      {
> -      remote_fio_data.fd_map = XNEWVEC (int, 10);
> -      remote_fio_data.fd_map_size = 10;
> +      remote_fio_data.fd_map.resize (10);
>        remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
>        remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
>        remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
> -      for (i = 3; i < 10; ++i)
> +      for (int i = 3; i < 10; ++i)
>  	remote_fio_data.fd_map[i] = FIO_FD_INVALID;

std::vector<T, Alloc>::resize has an overload which takes a value to use
for new elements of the vector.   This could be simplified as

    remote_fio_data.fd_map.resize (10, FIO_FD_INVALID);
    remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
    remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
    remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;

See below, but overall, I don’t really think we need to keep the "grow
by 10" strategy when using std::vector, this could simply be

    remote_fio_data.fd_map.emplace_back (FIO_FD_CONSOLE_IN)
    remote_fio_data.fd_map.emplace_back (FIO_FD_CONSOLE_OUT)
    remote_fio_data.fd_map.emplace_back (FIO_FD_CONSOLE_OUT)

>      }
>    return 3;
> @@ -69,25 +66,20 @@ remote_fileio_init_fd_map (void)
>  static int
>  remote_fileio_resize_fd_map (void)
>  {
> -  int i = remote_fio_data.fd_map_size;
> -
> -  if (!remote_fio_data.fd_map)
> +  if (remote_fio_data.fd_map.empty ())
>      return remote_fileio_init_fd_map ();
> -  remote_fio_data.fd_map_size += 10;
> -  remote_fio_data.fd_map =
> -    (int *) xrealloc (remote_fio_data.fd_map,
> -		      remote_fio_data.fd_map_size * sizeof (int));
> -  for (; i < remote_fio_data.fd_map_size; i++)
> +
> +  int i = remote_fio_data.fd_map.size ();
> +  remote_fio_data.fd_map.resize (i + 10);
> +  for (; i < remote_fio_data.fd_map.size (); i++)
>      remote_fio_data.fd_map[i] = FIO_FD_INVALID;

Similarly, if you keep the "grow by 10" strategy this could be

     remote_fio_data.fd_map.resize (i + 10, FIO_FD_INVALID);

> -  return remote_fio_data.fd_map_size - 10;
> +  return remote_fio_data.fd_map.size () - 10;
>  }
>  
>  static int
>  remote_fileio_next_free_fd (void)
>  {
> -  int i;
> -
> -  for (i = 0; i < remote_fio_data.fd_map_size; ++i)
> +  for (int i = 0; i < remote_fio_data.fd_map.size (); ++i)
>      if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
>        return i;

I think that’s mostly a personal preference thing, but std::find could
be used instead of an explicit loop

    auto it = std::find (remote_fio_data.fd_map.begin (),
                         remote_fio_data.fd_map.end (),
    		         FIO_FD_INVALID);
    if (it != remote_fio_data.fd_map.end ())
      return std::distance (remote_fio_data.fd_map.begin (), it);
    return remote_fileio_resize_fd_map ();

I think it could be possible to go one step further and remove
remote_fileio_resize_fd_map entirely.  The "grow by 10" strategy is not
necessary anymore if you use a std::vector.  A similar allocation
strategies comes for free.

    static int
    remote_fileio_next_free_fd ()
    {
        auto it = std::find (remote_fio_data.fd_map.begin (),
                             remote_fio_data.fd_map.end (),
        		         FIO_FD_INVALID);
        if (it != remote_fio_data.fd_map.end ())
          return std::ditance (remote_fio_data.fd_map.begin (), it);

        remote_fio_data.fd_map.emplace_back (FIO_FD_INVALID);
	return remote_fio_data.fd_map.size () - 1;
    }

One could also inline this inside remote_fileio_map_fd, and directly set
the appropriate FD value instead of using FIO_FD_INVALID temporarily.
But I would be happy keeping things this way for readability.

Best,
Lancelot.

>    return remote_fileio_resize_fd_map ();
> @@ -106,7 +98,7 @@ static int
>  remote_fileio_map_fd (int target_fd)
>  {
>    remote_fileio_init_fd_map ();
> -  if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
> +  if (target_fd < 0 || target_fd >= remote_fio_data.fd_map.size ())
>      return FIO_FD_INVALID;
>    return remote_fio_data.fd_map[target_fd];
>  }
> @@ -115,7 +107,7 @@ static void
>  remote_fileio_close_target_fd (int target_fd)
>  {
>    remote_fileio_init_fd_map ();
> -  if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
> +  if (target_fd >= 0 && target_fd < remote_fio_data.fd_map.size ())
>      remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
>  }
>  
> @@ -1147,21 +1139,12 @@ do_remote_fileio_request (remote_target *remote, char *buf)
>  void
>  remote_fileio_reset (void)
>  {
> -  int ix;
> -
> -  for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
> +  for (int fd : remote_fio_data.fd_map)
>      {
> -      int fd = remote_fio_data.fd_map[ix];
> -
>        if (fd >= 0)
>  	close (fd);
>      }
> -  if (remote_fio_data.fd_map)
> -    {
> -      xfree (remote_fio_data.fd_map);
> -      remote_fio_data.fd_map = NULL;
> -      remote_fio_data.fd_map_size = 0;
> -    }
> +  remote_fio_data.fd_map.clear ();
>  }
>  
>  /* Handle a file I/O request.  BUF points to the packet containing the
> 
> -- 
> 2.43.0
> 

  reply	other threads:[~2024-01-01 14:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-31 20:25 [PATCH 0/6] Make remote-fileio per-target Tom Tromey
2023-12-31 20:25 ` [PATCH 1/6] Make remote_fio_func_map 'const' Tom Tromey
2023-12-31 20:25 ` [PATCH 2/6] Use vector in remote-fileio.c Tom Tromey
2024-01-01 14:20   ` Lancelot SIX [this message]
2024-01-01 18:39     ` Simon Marchi
2023-12-31 20:25 ` [PATCH 3/6] Use methods for remote fileio Tom Tromey
2023-12-31 20:25 ` [PATCH 4/6] Remove sentinel from remote_fio_func_map Tom Tromey
2023-12-31 20:25 ` [PATCH 5/6] Move remote_fileio_data to header file Tom Tromey
2023-12-31 20:25 ` [PATCH 6/6] Store remote fileio state in remote_state Tom Tromey
2024-01-01 14:28 ` [PATCH 0/6] Make remote-fileio per-target Lancelot SIX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240101142041.dndg63bv42hs7lpo@octopus \
    --to=lsix@lancelotsix.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox