On Thu, Oct 14, 2021 at 3:25 PM Verma, Vishal L
<[email protected]> wrote:
>
> On Thu, 2021-10-14 at 14:27 -0700, Dan Williams wrote:
> > On Thu, Oct 7, 2021 at 1:22 AM Vishal Verma <[email protected]> 
> > wrote:
> >
>
> [..]
>
> > > +
> > > +CXL_EXPORT struct cxl_cmd *cxl_cmd_new_write_label(struct cxl_memdev 
> > > *memdev,
> > > +               void *lsa_buf, unsigned int offset, unsigned int length)
> > > +{
> > > +       struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
> > > +       struct cxl_cmd_set_lsa *set_lsa;
> > > +       struct cxl_cmd *cmd;
> > > +       int rc;
> > > +
> > > +       cmd = cxl_cmd_new_generic(memdev, CXL_MEM_COMMAND_ID_SET_LSA);
> > > +       if (!cmd)
> > > +               return NULL;
> > > +
> > > +       /* this will allocate 'in.payload' */
> > > +       rc = cxl_cmd_set_input_payload(cmd, NULL, sizeof(*set_lsa) + 
> > > length);
> > > +       if (rc) {
> > > +               err(ctx, "%s: cmd setup failed: %s\n",
> > > +                       cxl_memdev_get_devname(memdev), strerror(-rc));
> > > +               goto out_fail;
> > > +       }
> > > +       set_lsa = (void *)cmd->send_cmd->in.payload;
> >
> > ...the cast is still nagging at me especially when this knows what the
> > payload is supposed to be. What about a helper per command type of the
> > form:
> >
> > struct cxl_cmd_$name *to_cxl_cmd_$name(struct cxl_cmd *cmd)
> > {
> >     if (cmd->send_cmd->id != CXL_MEM_COMMAND_ID_$NAME)
> >         return NULL;
> >     return (struct cxl_cmd_$name *) cmd->send_cmd->in.payload;
> > }
> >
> Is the nag just from using a void cast, or having to cast at all? I
> think the void cast was just laziness - it should be cast to
> (struct cxl_cmd_$name *) instead of (void *).

I'd feel better about that to have one explicit cast, then an explicit
'void *' cast just to get default implicit cast behavior.

>
> Having a helper for to_cxl_cmd_$name() does look cleaner, but do we
> need the validation step there? In all these cases, the cmd would've
> been allocated just a few lines above, with cxl_cmd_new_generic(memdev,
> CXL_MEM_COMMAND_ID_$NAME) - so it seems unnecessary?

Yeah, if there's never any other users of it outside of the
'cxl_cmd_new_$name' then the validation and even the helper are
unnecessary if you do the strict type conversion.

Reply via email to