On 26/06/25 08:03AM, BALATON Zoltan wrote:
> On Thu, 25 Jun 2026, Harsh Prateek Bora wrote:
> > On 24/06/26 7:14 pm, Aditya Gupta wrote:
> > > On 26/06/23 09:24PM, Shivang Upadhyay wrote:
> > > > On Tue, 2026-06-23 at 19:49 +0530, Aditya Gupta wrote:
> > > > > +static const char *pnv_get_machine_type(enum PnvChipType chip_type)
> > > > > +{
> > > > > +    if (chip_type == PNV_CHIP_POWER8) {
> > > > > +        return "powernv8";
> > > > > +    } else if (chip_type == PNV_CHIP_POWER9) {
> > > > > +        return "powernv9";
> > > > > +    } else if (chip_type == PNV_CHIP_POWER10) {
> > > > > +        return "powernv10";
> > > > > +    } else if (chip_type == PNV_CHIP_POWER11) {
> > > > > +        return "powernv11";
> > > > > +    } else {
> > > > > +        g_assert_not_reached();
> > > > > +    }
> > > > > +}
> > > > 
> > > > How about refactoring to the following?
> > > > 
> > > >      static const char *const machine_types[] = {
> > > >          [PNV_CHIP_POWER8]  = "powernv8",
> > > >          [PNV_CHIP_POWER9]  = "powernv9",
> > > >          [PNV_CHIP_POWER10] = "powernv10",
> > > >          [PNV_CHIP_POWER11] = "powernv11",
> > > >      };
> > > >      return machine_types[x];
> > > 
> > > Has a subtle difference, that it may not assert/segfault on unknown
> > > value for x, I want the test to crash/fail if this is called with an
> > > unknown/new processor chip. I believe the if-else/switch is better as it
> > > handles (by 'crashing') unknown values too.
> > 
> > We could just do:
> > 
> > if (chip_type >= PNV_CHIP_MAX) {
> >    g_assert_not_reached();
> > } else {
> >    return machine_types[chip_type];
> > }
> 
> Or even g_assert(chip_type < PNV_CHIP_MAX); without the if?
> 
> Regards,
> BALATON Zoltan
> 
> > That way just adding new array entry would work for future chips.

will do in v3. thanks !

- Aditya G

Reply via email to