2015-04-02 02:13:16

by Florian Grandel

[permalink] [raw]
Subject: How to get the hci_dev from an adv instance's work struct in a linked list?

Hi!

I got quite far today refactoring from

struct hci_dev { ... struct adv_info adv_instance; ... };

to

struct hci_dev { ... struct list_head adv_instances; ... };

But in the end I got stuck on refactoring the adv timeout callback.
Originally it looked like this:

static void adv_timeout_expired(struct work_struct *work)
{
struct hci_dev *hdev = container_of(work, struct hci_dev,
adv_instance.timeout_exp.work);
...
}

But now I would have to construct a pointer to the hci_dev struct from a
list entry:

static void adv_timeout_expired(struct work_struct *work)
{
struct adv_info *adv_instance = container_of(work, struct adv_info,
timeout_exp.work);
...
}

I couldn't find a way to get the list head of the adv info list entry so
that I could retrieve its containing hci_dev structure, though.

How would I construct the required hci_dev struct from the work pointer
in this case? Any ideas?

Florian


2015-04-02 09:58:37

by Florian Grandel

[permalink] [raw]
Subject: Re: How to get the hci_dev from an adv instance's work struct in a linked list?

Hi Arman,

it's impressive how responsive you are on this list here. Really cool. :-)

>> I got quite far today refactoring from
>>
>> struct hci_dev { ... struct adv_info adv_instance; ... };
>>
>> to
>>
>> struct hci_dev { ... struct list_head adv_instances; ... };
>>
>> But in the end I got stuck on refactoring the adv timeout callback.
>> Originally it looked like this:
>>
>> static void adv_timeout_expired(struct work_struct *work)
>> {
>> struct hci_dev *hdev = container_of(work, struct hci_dev,
>> adv_instance.timeout_exp.work);
>> ...
>> }
>>
>> But now I would have to construct a pointer to the hci_dev struct from a
>> list entry:
>>
>> static void adv_timeout_expired(struct work_struct *work)
>> {
>> struct adv_info *adv_instance = container_of(work, struct adv_info,
>> timeout_exp.work);
>> ...
>> }
>>
>> I couldn't find a way to get the list head of the adv info list entry so
>> that I could retrieve its containing hci_dev structure, though.
>>
>
> Once simple solution is to add a struct hci_dev pointer to struct
> adv_info. Once you get the pointer to the instance then you can obtain
> the hci_dev pointer that way.

Yes, that's an obvious solution albeit it's a bit ugly as it introduces
extra redundancy. I thought of it, too, but I hoped I could avoid it.
Maybe I can solve it like that for now and then we see whether another
solution emerges once we got the refactored code in front of us.

Florian

2015-04-02 02:49:03

by Arman Uguray

[permalink] [raw]
Subject: Re: How to get the hci_dev from an adv instance's work struct in a linked list?

Hi Florian,

> On Wed, Apr 1, 2015 at 7:13 PM, Florian Grandel <[email protected]> wrote:
> Hi!
>
> I got quite far today refactoring from
>
> struct hci_dev { ... struct adv_info adv_instance; ... };
>
> to
>
> struct hci_dev { ... struct list_head adv_instances; ... };
>
> But in the end I got stuck on refactoring the adv timeout callback.
> Originally it looked like this:
>
> static void adv_timeout_expired(struct work_struct *work)
> {
> struct hci_dev *hdev = container_of(work, struct hci_dev,
> adv_instance.timeout_exp.work);
> ...
> }
>
> But now I would have to construct a pointer to the hci_dev struct from a
> list entry:
>
> static void adv_timeout_expired(struct work_struct *work)
> {
> struct adv_info *adv_instance = container_of(work, struct adv_info,
> timeout_exp.work);
> ...
> }
>
> I couldn't find a way to get the list head of the adv info list entry so
> that I could retrieve its containing hci_dev structure, though.
>

Once simple solution is to add a struct hci_dev pointer to struct
adv_info. Once you get the pointer to the instance then you can obtain
the hci_dev pointer that way.

> How would I construct the required hci_dev struct from the work pointer in
> this case? Any ideas?
>
> Florian
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth"
> in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Arman