Re: [PATCH] linux/container_of.h: Add memberof()

From: Alejandro Colomar
Date: Mon Aug 21 2023 - 07:23:48 EST


Hi Andy,

On 2023-08-21 13:18, Andy Shevchenko wrote:
> On Sun, Aug 20, 2023 at 09:52:22PM +0200, Alejandro Colomar wrote:
>>
>> -#define typeof_member(T, m) typeof(((T*)0)->m)
>> +
>> +#define memberof(T, member) ((T){}.member)
>
> I'm not sure. This seems to me utilization of compound literal, while above
> uses direct struct member pointer calculations.

Both can be used in most cases. The only exception is offsetof(3), where
you need the pointer calculation. The good thing about the compound
literal is that it's farther away from causing UB, but if that's not a
concern --using sizeof() or typeof() will usually make things safe from
UB, as there's really no dereference, but just to be a little paranoic--,
I could change the definition of memberof() to use the pointer thing.

Should I send a v2 with the pointer thing?

[I'll take some time, as I need to restore my USB with keys, which just
died yesterday. I didn't sign this email due to that.]

Cheers,
Alex

>
>> +#define typeof_member(T, m) typeof(memberof(T, m))
>

--
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5