Re: [patch 4/4] Intel IOMMU Suspend/Resume Support - Code Clean Up

From: David Woodhouse
Date: Fri Apr 03 2009 - 08:37:56 EST


On Fri, 2009-03-27 at 14:22 -0700, Fenghua Yu wrote:
> plain text document attachment (iommu_sr_4.patch)
> A new macro list_for_each_entry_selected_do() is defined for linked list.
> iommu uses this macro to have concise and more readable code.
>
> It's defined as generic code. Hopefully it could be used in other places.
>
> Signed-off-by: Fenghua Yu <fenghua.yu@xxxxxxxxx>

This is overkill, surely? It would be easier to do...

#define for_each_active_iommu(i, drhd) \
list_for_each_entry(drhd, &dmar_drhd_units, list) \
if (i=drhd->iommu, !drhd->ignored)

#define for_each_iommu(i, drhd) \
list_for_each_entry(drhd, &dmar_drhd_units, list) \
if (i=drhd->iommu, 1)

Or if you really want to avoid it eating a stray 'else' then

#define for_each_active_iommu(i, drhd) \
list_for_each_entry(drhd, &dmar_drhd_units, list) \
if (i=drhd->iommu, drhd->ignored) {} else

#define for_each_iommu(i, drhd) \
list_for_each_entry(drhd, &dmar_drhd_units, list) \
if (i=drhd->iommu, 0) {} else


> ---
>
> include/linux/dmar.h | 8 ++++++++
> include/linux/list.h | 42 ++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 50 insertions(+)
>
> diff --git a/include/linux/dmar.h b/include/linux/dmar.h
> index 2f34274..146d54b 100644
> --- a/include/linux/dmar.h
> +++ b/include/linux/dmar.h
> @@ -44,6 +44,14 @@ extern struct list_head dmar_drhd_units;
> #define for_each_drhd_unit(drhd) \
> list_for_each_entry(drhd, &dmar_drhd_units, list)
>
> +#define for_each_active_iommu(i, drhd) \
> + list_for_each_entry_selected_do(drhd, &dmar_drhd_units, list, \
> + !drhd->ignored, {i = drhd->iommu; })
> +
> +#define for_each_iommu(i, drhd) \
> + list_for_each_entry_selected_do(drhd, &dmar_drhd_units, list, \
> + 1, {i = drhd->iommu; })
> +
> extern int dmar_table_init(void);
> extern int dmar_dev_scope_init(void);
>
> diff --git a/include/linux/list.h b/include/linux/list.h
> index 969f6e9..6580f43 100644
> --- a/include/linux/list.h
> +++ b/include/linux/list.h
> @@ -530,6 +530,48 @@ static inline void list_splice_tail_init(struct list_head *list,
> &pos->member != (head); \
> pos = n, n = list_entry(n->member.prev, typeof(*n), member))
>
> +/**
> + * list_first_entry_selected - get the first selected element from a list
> + * @pos: the type * to use as a loop cursor.
> + * @head: the head for your list.
> + * @member: the name of the list_struct within the struct.
> + * @selector: the selector to select the first entry.
> + */
> +#define list_first_entry_selected(pos, head, member, selector) \
> + pos = list_entry((head)->next, typeof(*pos), member), \
> + pos = ({while (!(selector) && (&pos->member != (head))) \
> + pos = list_entry(pos->member.next, typeof(*pos), member); \
> + pos; })
> +
> +/**
> + * list_next_entry_selected - get the next selected element from a list
> + * @pos: the type * to use as a loop cursor.
> + * @head: the head for your list.
> + * @member: the name of the list_struct within the struct.
> + * @selector: the selector to select the next entry.
> + */
> +#define list_next_entry_selected(pos, head, member, selector) \
> + pos = list_entry(pos->member.next, typeof(*pos), member), \
> + pos = ({while (!(selector) && (&pos->member != (head))) \
> + pos = list_entry(pos->member.next, typeof(*pos), member); \
> + pos; })
> +
> +/**
> + * list_for_each_entry_selected_do - iterate over list of given type and
> + * selected entries. Do something on each selected entry.
> + * @pos: the type * to use as a loop cursor.
> + * @head: the head for your list.
> + * @member: the name of the list_struct within the struct.
> + * @selector: the selector to select entries.
> + * @to_do: things to do on each selected entry.
> + */
> +#define list_for_each_entry_selected_do(pos, head, member, selector, to_do) \
> + for (list_first_entry_selected(pos, head, member, selector), \
> + ({if (&pos->member != (head)) to_do}); \
> + prefetch(pos->member.next), &pos->member != (head); \
> + list_next_entry_selected(pos, head, member, selector), \
> + ({if (&pos->member != (head)) to_do}))
> +
> /*
> * Double linked lists with a single pointer list head.
> * Mostly useful for hash tables where the two pointer list head is
>
--
dwmw2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/