[Swan-dev] FOR_EACH_LIST_ENTRY_

Andrew Cagney andrew.cagney at gmail.com
Tue Dec 11 13:03:43 UTC 2018


On Sun, 9 Dec 2018 at 12:59, D. Hugh Redelmeier <hugh at mimosa.com> wrote:
>
> I find this macro almost impenetrable.
>
> I think that this is mainly due to it being contorted to make it look like
> the head of a for statement: the code to be iterated over follows the
> macro invocation.

Passing code to a macro is fragile.  We've discussed this before.

> If we passed that code to the macro, I think that the macro could be a lot
> simpler and clearer.

I pushed my next tweak (of course, after testing).

> Here's my stab at it.  Perhaps I've misunderstood something about the
> original and thus may have it wrong.

You might want to read the description.

> Comments?
>
> ==== original ====
>
> #define FOR_EACH_LIST_ENTRY_(HEAD, DATA, NEXT)                          \
>                                                                         \
>         /* set head_ = HEAD (evaluate once) */                          \
>         for (struct list_head *head_ = HEAD;                            \
>              head_ != NULL; head_ = NULL)                               \
>                                                                         \
>                 /* set entry = head.NEXT; skip empty */                 \
>                 for (struct list_entry *entry_ = head_->head.NEXT;      \
>                      entry_ != &head_->head; entry_ = &head_->head)     \
>                                                                         \
>                         /* DATA = ENTRY->data; ENTRY = ENTRY->NEXT */   \
>                         for (DATA = (typeof(DATA))entry_->data,         \
>                                      entry_ = entry_->NEXT;             \
>                              DATA != NULL;                              \
>                              DATA = (typeof(DATA))entry_->data,         \
>                                      entry_ = entry_->NEXT)
>
> ==== replacement ====
>
> /* traverse doubly-linked list
>  *
>  * The HEAD entry is a dummy.
>  * It exists even when the list is empty.
>  * It is distinguished by a NULL data field.
>  */
>
> #define FOR_EACH_LIST_ENTRY_(head, DATA, NEXT, BODY)    \
>         for (struct list_entry *entry_ = (HEAD); ; ) {  \
>                 entry_ = entry_->NEXT;  \
>                 DATA = (typeof(DATA))entry_->data;      \
>                 if (DATA == NULL)       \
>                         break;  \
>                 { BODY ; }      \
>         }
> _______________________________________________
> Swan-dev mailing list
> Swan-dev at lists.libreswan.org
> https://lists.libreswan.org/mailman/listinfo/swan-dev


More information about the Swan-dev mailing list