Re: [RFA v2 1/4] C++-ify break-catch-sig

Simon Marchi
2017-07-21T19:53:39Z

On 2017-07-20 20:13, Sergio Durigan Junior wrote:
>>
>> while (*arg != '\0')
>> {
>> int num;
>> - gdb_signal_type signal_number;
>> - char *one_arg, *endptr;
>> - struct cleanup *inner_cleanup;
>> + gdb_signal signal_number;
>> + char *endptr;
>>
>> - one_arg = extract_arg (&arg);
>> + gdb::unique_xmalloc_ptr<char> one_arg (extract_arg (&arg));
>> if (one_arg == NULL)
>> break;
>> - inner_cleanup = make_cleanup (xfree, one_arg);
>>
>> /* Check for the special flag "all". */
>> - if (strcmp (one_arg, "all") == 0)
>> + if (strcmp (one_arg.get (), "all") == 0)
>> {
>> arg = skip_spaces (arg);
>> if (*arg != '\0' || !first)
>> error (_("'all' cannot be caught with other signals"));
>> - *catch_all = 1;
>> - gdb_assert (result == NULL);
>> - do_cleanups (inner_cleanup);
>> - discard_cleanups (cleanup);
>> - return NULL;
>> + *catch_all = true;
>> + gdb_assert (result.empty ());
>> + return result;
>> }
>>
>> first = 0;
>>
>> /* Check if the user provided a signal name or a number. */
>> - num = (int) strtol (one_arg, &endptr, 0);
>> + num = (int) strtol (one_arg.get (), &endptr, 0);
>> if (*endptr == '\0')
>> signal_number = gdb_signal_from_command (num);
>> else
>> {
>> - signal_number = gdb_signal_from_name (one_arg);
>> + signal_number = gdb_signal_from_name (one_arg.get ());
>> if (signal_number == GDB_SIGNAL_UNKNOWN)
>> - error (_("Unknown signal name '%s'."), one_arg);
>> + error (_("Unknown signal name '%s'."), one_arg.get ());
>> }
>>
>> - VEC_safe_push (gdb_signal_type, result, signal_number);
>> - do_cleanups (inner_cleanup);
>> + result.push_back (signal_number);
>> }
>>
>> - discard_cleanups (cleanup);
>> + result.shrink_to_fit ();
>
> Do you really need this call here? You're using safe_push above, so I
> don't think the vector will end up with a bigger capacity than its
> size.

safe_push is the old implementations, std::vector grows the capacity
exponentially, so you're likely to be left with unused capacity.

Simon