Skip to contentSkip to conversation

new idl helper, append to Array

DISCUSSION Olaf Hering 3 participants 7 messages Feb 4, 2016 – Feb 5, 2016

lore
Share & tools
lore
#0 lore raw

Ian,

in my pvscsi code I have two copies of a helper function which appends yet another instance of something to an Array, as shown below. This is similar to the _copy variant. Is it worth to let gentypes generate such a helper, like libxl_device_vscsictrl_append_vscsidev()?

While writing this I realize that libxl__realloc will not return, so my helper can be converted from returning int to void, and all the locals can be removed.

static int vscsi_append_dev(libxl__gc *gc, libxl_device_vscsictrl *ctrl,
libxl_device_vscsidev *dev)

{
    int rc;
    libxl_device_vscsidev *devs;

    devs = libxl__realloc(NOGC, ctrl->vscsidevs, sizeof(*dev) * (ctrl->num_vscsidevs + 1));
    if (!devs) {
        rc = ERROR_NOMEM;
        goto out;
    }

    ctrl->vscsidevs = devs;
    libxl_device_vscsidev_init(ctrl->vscsidevs + ctrl->num_vscsidevs);
    libxl_device_vscsidev_copy(CTX, ctrl->vscsidevs + ctrl->num_vscsidevs, dev);
    ctrl->num_vscsidevs++;
    rc = 0;
out:
    return rc;
}

Olaf

#1 lore raw
Quoted 6 lines · Olaf Hering

Ian,

in my pvscsi code I have two copies of a helper function which appends yet another instance of something to an Array, as shown below. This is similar to the _copy variant. Is it worth to let gentypes generate such a helper, like libxl_device_vscsictrl_append_vscsidev()?

If something can be autogenerated without too much trouble then I see no reason not to do so.

I'd go with libxl_<type>_list_append as the naming scheme, which fits in with libxl_<type>_list_free (which probably ought to be autogenerated too, but isn't). So:

libxl_device_vscsidev_list_append(libxl_ctx *ctx, libxl_device_vscsidev *dev, int nr,

				  libxl_device_vscsidev *new);

(if you intend for this to be internal then s/^libxl_/&_/ and s/libxl_ctx \*ctx/libxl__gc *gc/)

Oh, I see you want it to take the type containing the array, that could work to, you'd need to call it libxl_<type>_append_<field>, so

libxl_device_vscsictrl_append_vscsidevs

which looks a bit odd (since the field name is plural and the IDL has no way to find the singular). We could live with that, or s/append/append_to/ or make it varargs and take perhaps multiple new entries and a NULL terminator.

I think the append_to variant is probably least gross.

Looks like various places such as libxl__append_nic_list_of_type could make use of this helper too. As could xl_cmdimpl.c for ARRAY_EXTEND_INIT perhaps (using it everywhere isn't mandatory of course, but if you feel inclined it would be nice)

Quoted 3 lines

While writing this I realize that libxl__realloc will not return, so my helper can be converted from returning int to void, and all the locals can be removed.

Indeed.

Quoted 12 lines

static int vscsi_append_dev(libxl__gc *gc, libxl_device_vscsictrl *ctrl, libxl_device_vscsidev *dev)
{
    int rc;
    libxl_device_vscsidev *devs;

    devs = libxl__realloc(NOGC, ctrl->vscsidevs, sizeof(*dev) * (ctrl->num_vscsidevs + 1)); if (!devs) {
        rc = ERROR_NOMEM;
        goto out;
    }

Quoted 3 lines

    ctrl->vscsidevs = devs; libxl_device_vscsidev_init(ctrl->vscsidevs + ctrl->num_vscsidevs); libxl_device_vscsidev_copy(CTX, ctrl->vscsidevs + ctrl->num_vscsidevs, dev);

Wei, is it necessary to init the dst before copy into it?

Quoted 8 lines

    ctrl->num_vscsidevs++;
    rc = 0;
out:
    return rc;
}

Olaf

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 1 line · Ian Campbell

I think the append_to variant is probably least gross.

libxl_device_vscsidev_append_to_vscsictrl() would work too.

This is what I will use for the time being (modulo introduced runtime bugs):

void libxl_device_vscsictrl_append_vscsidev(libxl_ctx *ctx,
libxl_device_vscsictrl *ctrl, libxl_device_vscsidev *dev)

{
    GC_INIT(ctx);
    ctrl->vscsidevs = libxl__realloc(NOGC, ctrl->vscsidevs, sizeof(*dev) * (ctrl->num_vscsidevs + 1));
    libxl_device_vscsidev_init(ctrl->vscsidevs + ctrl->num_vscsidevs);
    libxl_device_vscsidev_copy(CTX, ctrl->vscsidevs + ctrl->num_vscsidevs, dev);
    ctrl->num_vscsidevs++;
    GC_FREE;
}
Quoted 1 line

Wei, is it necessary to init the dst before copy into it?

It would clear the padding between members, a plain copy will leave them uninitialized.

Olaf

#6 lore raw
In reply to Ian Campbell
Quoted 56 lines · Ian Campbell
Quoted 6 lines · Olaf Hering

Ian,

in my pvscsi code I have two copies of a helper function which appends yet another instance of something to an Array, as shown below. This is similar to the _copy variant. Is it worth to let gentypes generate such a helper, like libxl_device_vscsictrl_append_vscsidev()?

If something can be autogenerated without too much trouble then I see no reason not to do so.

I'd go with libxl_<type>_list_append as the naming scheme, which fits in with libxl_<type>_list_free (which probably ought to be autogenerated too, but isn't). So:

libxl_device_vscsidev_list_append(libxl_ctx *ctx, libxl_device_vscsidev *dev, int nr,

				  libxl_device_vscsidev *new);

(if you intend for this to be internal then s/^libxl_/&_/ and s/libxl_ctx \*ctx/libxl__gc *gc/)

Oh, I see you want it to take the type containing the array, that could work to, you'd need to call it libxl_<type>_append_<field>, so

libxl_device_vscsictrl_append_vscsidevs

which looks a bit odd (since the field name is plural and the IDL has no way to find the singular). We could live with that, or s/append/append_to/ or make it varargs and take perhaps multiple new entries and a NULL terminator.

I think the append_to variant is probably least gross.

Looks like various places such as libxl__append_nic_list_of_type could make use of this helper too. As could xl_cmdimpl.c for ARRAY_EXTEND_INIT perhaps (using it everywhere isn't mandatory of course, but if you feel inclined it would be nice)

Quoted 3 lines

While writing this I realize that libxl__realloc will not return, so my helper can be converted from returning int to void, and all the locals can be removed.

Indeed.

Quoted 12 lines

static int vscsi_append_dev(libxl__gc *gc, libxl_device_vscsictrl *ctrl, libxl_device_vscsidev *dev)
{
    int rc;
    libxl_device_vscsidev *devs;

    devs = libxl__realloc(NOGC, ctrl->vscsidevs, sizeof(*dev) * (ctrl->num_vscsidevs + 1)); if (!devs) {
        rc = ERROR_NOMEM;
        goto out;
    }

Quoted 6 lines
Quoted 3 lines

    ctrl->vscsidevs = devs; libxl_device_vscsidev_init(ctrl->vscsidevs + ctrl->num_vscsidevs); libxl_device_vscsidev_copy(CTX, ctrl->vscsidevs + ctrl->num_vscsidevs, dev);

Wei, is it necessary to init the dst before copy into it?

I don't think it is absolutely necessary because every field inside dst will be overwritten, but better safe than sorry.

Wei.

Quoted 8 lines
Quoted 8 lines

    ctrl->num_vscsidevs++;
    rc = 0;
out:
    return rc;
}

Olaf

#3 lore raw
In reply to Olaf Hering
Quoted 21 lines · Olaf Hering
Quoted 1 line · Ian Campbell

I think the append_to variant is probably least gross.

libxl_device_vscsidev_append_to_vscsictrl() would work too.

This is what I will use for the time being (modulo introduced runtime bugs):

void libxl_device_vscsictrl_append_vscsidev(libxl_ctx *ctx,
libxl_device_vscsictrl *ctrl, libxl_device_vscsidev *dev)

{
    GC_INIT(ctx);
    ctrl->vscsidevs = libxl__realloc(NOGC, ctrl->vscsidevs, sizeof(*dev) * (ctrl->num_vscsidevs + 1));
    libxl_device_vscsidev_init(ctrl->vscsidevs + ctrl->num_vscsidevs);
    libxl_device_vscsidev_copy(CTX, ctrl->vscsidevs + ctrl->num_vscsidevs, dev);
    ctrl->num_vscsidevs++;
    GC_FREE;
}
Quoted 5 lines
Quoted 1 line

Wei, is it necessary to init the dst before copy into it?

It would clear the padding between members, a plain copy will leave them uninitialized.

There is that. Yes, _init memset the whole structure to zeros.

Wei.

Quoted 1 line

Olaf

#4 lore raw
In reply to Olaf Hering
Quoted 5 lines · Olaf Hering
Quoted 1 line · Ian Campbell

I think the append_to variant is probably least gross.

libxl_device_vscsidev_append_to_vscsictrl() would work too.

While looking at the MERGE macro in libxl.c, a _remove_from could be added as well. I have not checked if there are other users beside the the MERGE function.

Olaf

#5 lore raw
Quoted 11 lines · Olaf Hering
Quoted 5 lines · Olaf Hering
Quoted 1 line · Ian Campbell

I think the append_to variant is probably least gross.

libxl_device_vscsidev_append_to_vscsictrl() would work too.

While looking at the MERGE macro in libxl.c, a _remove_from could be added as well. I have not checked if there are other users beside the the MERGE function.

If you have the tuits please feel free to arrange to autogenerate as much as you like ;-)

(If you are unsure about the general utility you could always make them internal only for now)

Ian.