Re: [PATCH 2/6] Use vector in remote-fileio.c

Simon Marchi
2024-01-01T18:39:41Z

On 2024-01-01 09:20, Lancelot SIX wrote:
> 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)

I agree with Lancelot. And from what I saw after quickly skimming the
code, remote_fileio_resize_fd_map and remote_fileio_next_free_fd could
be removed, since they callers can use simple vector operations.

Simon