Prev: msi-wmi: make needlessly global symbols static
Next: [PATCH RFC 3/3] qlist: singly-linked queue implementation
From: Konstantin Khlebnikov on 8 Jul 2010 04:50 Introduce macroses: list_pop_entry list_pop_last_entry list_pop_init_entry list_pop_init_last_entry list_peek_entry list_peek_last_entry as syntax sugar for widely used constructions like this: while (!list_empty(&list_head)) { entry = list_first_entry(&list_head, struct entry, member); list_del(&entry->member); ... } with this macro it become: while (list_pop_entry(&entry, &list_head, member)) { ... } All macros returns true if the list is not empty, otherwise they not change the output variable. list_pop_init_* call list_del_init instead list_del. list_peek_* not remove entries from the list. Signed-off-by: Konstantin Khlebnikov <khlebnikov(a)openvz.org> --- include/linux/list.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 88 insertions(+), 0 deletions(-) diff --git a/include/linux/list.h b/include/linux/list.h index 5d57a3a..6a6e9d3 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -360,6 +360,94 @@ static inline void list_splice_tail_init(struct list_head *list, list_entry((ptr)->next, type, member) /** + * list_peek_entry - check is list non-empty and return list first entry + * @ptr: the pointer to struct * to save first entry + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_peek_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) \ + *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \ + (!__empty); }) + +/** + * list_peek_last_entry - check is list non-empty and return list last entry + * @ptr: the pointer to struct * to save last entry + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_peek_last_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) \ + *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \ + (!__empty); }) + +/** + * list_pop_entry - delete and return list first entry + * @ptr: the pointer to struct * whereto save result + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_pop_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) { \ + *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \ + list_del(&(*(ptr))->member); \ + } (!__empty); }) + +/** + * list_pop_init_entry - delete, reinitialise and return list first entry + * @ptr: the pointer to struct * whereto save result + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_pop_init_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) { \ + *(ptr) = list_first_entry(head, typeof(**(ptr)), member); \ + list_del_init(&(*(ptr))->member); \ + } (!__empty); }) + +/** + * list_pop_last_entry - delete and return list last entry + * @ptr: the pointer to struct * whereto save result + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_pop_last_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) { \ + *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \ + list_del(&(*(ptr))->member); \ + } (!__empty); }) + +/** + * list_pop_init_last_entry - delete, reinitialise and return list last entry + * @ptr: the pointer to struct * whereto save result + * @head: the struct list_head pointer. + * @member: the name of the list_struct within the struct. + * + * Return true if list was not empty and @ptr is changed + */ +#define list_pop_init_last_entry(ptr, head, member) ({ \ + bool __empty = list_empty(head); \ + if (!__empty) { \ + *(ptr) = list_entry((head)->prev, typeof(**(ptr)), member); \ + list_del_init(&(*(ptr))->member); \ + } (!__empty); }) + +/** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo(a)vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ |