From 3b9dfc6aed4c04347ef2f4b4b005e8561e285c6a Mon Sep 17 00:00:00 2001 From: Lynx Zhou Date: Wed, 4 Jul 2018 15:50:31 +0800 Subject: [PATCH 1/2] Add list iterators over list elements Some non-gnu toolchains don't support gnu C extended keyword "typeof", like MDK (if no "gnu" option) and IAR, in this case we can provide list element manipulation macro functions to iterate over the list. --- include/rtservice.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/rtservice.h b/include/rtservice.h index f909f17f86..800d4d175b 100644 --- a/include/rtservice.h +++ b/include/rtservice.h @@ -140,6 +140,24 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l) #define rt_list_entry(node, type, member) \ rt_container_of(node, type, member) +/** + * rt_list_for_each - iterate over a list + * @pos: the rt_list_t * to use as a loop cursor. + * @head: the head for your list. + */ +#define rt_list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +/** + * rt_list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the rt_list_t * to use as a loop cursor. + * @n: another rt_list_t * to use as temporary storage + * @head: the head for your list. + */ +#define rt_list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + /** * rt_list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. @@ -254,6 +272,14 @@ rt_inline int rt_slist_isempty(rt_slist_t *l) #define rt_slist_entry(node, type, member) \ rt_container_of(node, type, member) +/** + * rt_slist_for_each - iterate over a single list + * @pos: the rt_slist_t * to use as a loop cursor. + * @head: the head for your single list. + */ +#define rt_slist_for_each(pos, head) \ + for (pos = (head)->next; &pos->next != (head); pos = pos->next) + /** * rt_slist_for_each_entry - iterate over single list of given type * @pos: the type * to use as a loop cursor. From 754b36383947a191bbc0ada7fb5e22cf56120a13 Mon Sep 17 00:00:00 2001 From: Lynx Zhou Date: Wed, 11 Jul 2018 17:14:44 +0800 Subject: [PATCH 2/2] Add list iterators over list elements Address the comments from armink@rt-thread --- include/rtservice.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/rtservice.h b/include/rtservice.h index 800d4d175b..0bfa424338 100644 --- a/include/rtservice.h +++ b/include/rtservice.h @@ -141,7 +141,7 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l) rt_container_of(node, type, member) /** - * rt_list_for_each - iterate over a list + * rt_list_for_each - iterate over a list * @pos: the rt_list_t * to use as a loop cursor. * @head: the head for your list. */ @@ -273,12 +273,12 @@ rt_inline int rt_slist_isempty(rt_slist_t *l) rt_container_of(node, type, member) /** - * rt_slist_for_each - iterate over a single list + * rt_slist_for_each - iterate over a single list * @pos: the rt_slist_t * to use as a loop cursor. * @head: the head for your single list. */ #define rt_slist_for_each(pos, head) \ - for (pos = (head)->next; &pos->next != (head); pos = pos->next) + for (pos = (head)->next; pos != RT_NULL; pos = pos->next) /** * rt_slist_for_each_entry - iterate over single list of given type