Re: [RFC PATCH v1 1/2] list: add type-safer list_head wrapper

From: Barnabás Pőcze
Date: Thu Mar 10 2022 - 21:49:51 EST


Hi


2022. március 11., péntek 3:01 keltezéssel, Linus Torvalds írta:
> On Thu, Mar 10, 2022 at 5:42 PM Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > That one didn't do the automatic offset thing, but see
> >
> > https://lore.kernel.org/all/CAADWXX-Pr-D3wSr5wsqTEOBSJzB9k7bSH+7hnCAj0AeL0=U4mg@xxxxxxxxxxxxxx/
> >
> > on the problems that has.
>
> Note: I think the problems are serious enough that it almost certainly
> isn't worth doing - it makes the code uglier for very little upside.
>
> So I tried to explain how it _could_ be done, but that doesn't mean
> that it _should_ be done.
>
> Having the member name as part of the list traversal macro isn't
> actually generally a real problem.
>
> I added it to the list_traversal_head() macro in that original patch
> because I think we can easily use the member head name to _verify_
> that the declaration and the use match.
>
> Yes, squirrelling off the offset and not needing the member head name
> at all at when traversing the list is obviously simpler syntax, but
> that part has never been the real problem with list traversal. And
> verifying that the member name that is passed in is the same as in the
> list_traversal_head() would be trivial.
>
> To verify it, we could simply change that type name from:
>
> type *name##_traversal_type;
>
> to be
>
> type *name##_traversal_type_##member;
>
> instead, and suddenly the member name in 'list_traverse()' has to
> match that thing that list_traversal_head() created.
>
> So yes, you'd have that third argument in list_traverse(), but it
> would be trivially checked at compile-time.

That is indeed a simpler thing to do, and doesn't need `offsetof()` at the
declaration, but there are places - not many - where the `list_head` member
is inside a subobject, for example, so `member` now contains a period.


>
> And you'd avoid all the ugly complexities (described above) with lists
> that are embedded inside data structures that refer to each other)


Regards,
Barnabás Pőcze