On Thu, Jun 10, 2010 at 9:47 AM, Anton Vorontsov <cbouatmai...@gmail.com> wrote:
> On Thu, Jun 10, 2010 at 09:13:57AM -0600, M. Warner Losh wrote:
> [...]
>> : >> I told you several ways of how to improve the code (based on
>> : >> the ideas from drivers/base/, so the ideas aren't even mine,
>> : >> fwiw).
>> : >
>> : > I tend to agree with Anton here.
>> :
>> : The reason I'm confident doing it that way is that it is *not* a
>> : structure.  There is no structure relationship between the resource
>> : table and the platform_device other than they are allocated with the
>> : same kzalloc() call.  All the code that cares about that is contained
>> : within 4 lines of code.  I'm resistant to using a structure because it
>> : is adds an additional 5-6 lines of code to add a structure that won't
>> : be used anywhere else, and is only 4 lines to begin with.
>>
>> I tend to agree with Grant here.  The idiom he's using is very wide
>> spread in the industry and works extremely well.  It keeps the
>> ugliness confined to a couple of lines and is less ugly than the
>> alternatives for this design pattern.  It is a little surprising when
>> you see the code the first time, granted, but I think its expressive
>> power trumps that small surprise.
>
> Oh, come on. Both constructions are binary equivalent.
>
> So how can people seriously be with *that* code:
>
>        dev->resource = (void *)&dev[1];
>
> which, semantically, is a nonsense and asks for a fix.
>
> While
>        dev_obj->dev.resource = dev_obj->resource;
>
> simply makes sense.

Well, my choices are (without whitespace so as not to bias line count):

A)
struct of_device *alloc_function(int num_res)
{
        struct device *ofdev;
        struct resource *res;
        ofdev = kzalloc(sizeof(*ofdev) + (sizeof(*res) * num_res), GFP_KERNEL);
        if (!ofdev)
                return NULL;
        res = (struct resource *)&ofdev[1];
        ...
        return ofdev;
}
10 lines of code

B)
struct of_device_object {
        struct of_device ofdev;
        struct resource resource[0];
};
struct of_device *alloc_function(int num_res)
{
        struct of_device_object *ofobj;
        struct of_device *ofdev;
        struct resource *res;
        ofobj = kzalloc(sizeof(*ofobj) + (sizeof(*res) * num_res), GFP_KERNEL);
        if (!ofobj)
                return NULL;
        res = ofobj->resource;
        ...
        return &ofobj->ofdev;
}
15 lines of code

C)
struct of_device *alloc_function(int num_res)
{
        struct device *ofdev;
        struct resource *res;
        ofdev = kzalloc(sizeof(*ofdev), GFP_KERNEL)
        if (!ofdev)
                return NULL;
        res = kzalloc((sizeof(*res) * num_res), GFP_KERNEL);
        if (!res) {
                kfree(ofdev);  /* or goto an error unwind label */
                return NULL;
        }
        res = (struct resource *)&ofdev[1];
        ...
        return ofdev;
}
15 lines of code, plus an extra few lines at kfree(ofdev) time.

When I look at the three, option A is more concise and clearer in it's
intent to me.

That being said, I'm looking at refactoring to use
platform_device_alloc() instead, which is effectively option C. (which
I'd normally avoid, but it removes otherwise duplicate code from
drivers/of).

g.

-- 
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
_______________________________________________
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Reply via email to