提交 e9b846d1 编写于 作者: J JasonJiaJie

[list]1.Modify rt_slist_foreach to rt_slist_for_each_entry. 2.Add...

[list]1.Modify rt_slist_foreach to rt_slist_for_each_entry. 2.Add rt_sllist_isempty. 3.Make code cleanup

1.Modify rt_slist_foreach to rt_slist_for_each_entry. 2.Add rt_sllist_isempty. 3.Make code cleanup
上级 a2a8e2ee
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
* 2006-09-07 Bernard move the kservice APIs to rtthread.h * 2006-09-07 Bernard move the kservice APIs to rtthread.h
* 2007-06-27 Bernard fix the rt_list_remove bug * 2007-06-27 Bernard fix the rt_list_remove bug
* 2012-03-22 Bernard rename kservice.h to rtservice.h * 2012-03-22 Bernard rename kservice.h to rtservice.h
* 2017-11-15 JasonJia Modify rt_slist_foreach to rt_slist_for_each_entry.
* Make code cleanup.
*/ */
#ifndef __RT_SERVICE_H__ #ifndef __RT_SERVICE_H__
...@@ -38,6 +40,14 @@ extern "C" { ...@@ -38,6 +40,14 @@ extern "C" {
/*@{*/ /*@{*/
/**
* rt_container_of - return the member address of ptr, if the type of ptr is the
* struct type.
*/
#define rt_container_of(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
/** /**
* @brief initialize a list object * @brief initialize a list object
*/ */
...@@ -128,7 +138,7 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l) ...@@ -128,7 +138,7 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l)
* @param member the name of list in structure * @param member the name of list in structure
*/ */
#define rt_list_entry(node, type, member) \ #define rt_list_entry(node, type, member) \
((type *)((char *)(node) - (unsigned long)(&((type *)0)->member))) rt_container_of(node, type, member)
/** /**
* rt_list_for_each_entry - iterate over list of given type * rt_list_for_each_entry - iterate over list of given type
...@@ -136,9 +146,9 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l) ...@@ -136,9 +146,9 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l)
* @head: the head for your list. * @head: the head for your list.
* @member: the name of the list_struct within the struct. * @member: the name of the list_struct within the struct.
*/ */
#define rt_list_for_each_entry(pos, head, member) \ #define rt_list_for_each_entry(pos, head, member) \
for (pos = rt_list_entry((head)->next, typeof(*pos), member); \ for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = rt_list_entry(pos->member.next, typeof(*pos), member)) pos = rt_list_entry(pos->member.next, typeof(*pos), member))
/** /**
...@@ -148,10 +158,10 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l) ...@@ -148,10 +158,10 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l)
* @head: the head for your list. * @head: the head for your list.
* @member: the name of the list_struct within the struct. * @member: the name of the list_struct within the struct.
*/ */
#define rt_list_for_each_entry_safe(pos, n, head, member) \ #define rt_list_for_each_entry_safe(pos, n, head, member) \
for (pos = rt_list_entry((head)->next, typeof(*pos), member), \ for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
n = rt_list_entry(pos->member.next, typeof(*pos), member); \ n = rt_list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = n, n = rt_list_entry(n->member.next, typeof(*n), member)) pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
/** /**
...@@ -195,6 +205,19 @@ rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n) ...@@ -195,6 +205,19 @@ rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
l->next = n; l->next = n;
} }
rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
{
unsigned int len = 0;
const rt_slist_t *list = l->next;
while (list != RT_NULL)
{
list = list->next;
len ++;
}
return len;
}
rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n) rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
{ {
/* remove slist head */ /* remove slist head */
...@@ -207,17 +230,9 @@ rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n) ...@@ -207,17 +230,9 @@ rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
return l; return l;
} }
rt_inline unsigned int rt_slist_len(const rt_slist_t *l) rt_inline int rt_slist_isempty(rt_slist_t *l)
{ {
unsigned int len = 0; return l->next == RT_NULL;
const rt_slist_t *list = l->next;
while (list != RT_NULL)
{
list = list->next;
len ++;
}
return len;
} }
/** /**
...@@ -226,23 +241,30 @@ rt_inline unsigned int rt_slist_len(const rt_slist_t *l) ...@@ -226,23 +241,30 @@ rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
* @param type the type of structure * @param type the type of structure
* @param member the name of list in structure * @param member the name of list in structure
*/ */
#define rt_slist_entry(node, type, member) \ #define rt_slist_entry(node, type, member) \
((type *)((char*)(node)-(unsigned long)(&((type *)0)->member))) rt_container_of(node, type, member)
/** /**
* rt_slist_for_each_entry - iterate over single list of given type * rt_slist_for_each_entry - iterate over single list of given type
* @node: the type * to use as a loop cursor. * @pos: the type * to use as a loop cursor.
* @list: the head for your single list. * @head: the head for your single list.
* @member: the name of the list_struct within the struct.
*/ */
#define rt_slist_foreach(node, list) \ #define rt_slist_fore_each_entry(pos, head, member) \
for ((node) = (list)->next; (node) != RT_NULL; (node) = (node)->next) for (pos = rt_slist_entry((head)->next, typeof(*pos), member); \
&pos->member != (RT_NULL); \
pos = rt_slist_entry(pos->member.next, typeof(*pos), member))
/** /**
* rt_container_of - return the member address of ptr, if the type of ptr is the * rt_slist_first_entry - get the first element from a slist
* struct type. * @ptr: the slist head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the slist_struct within the struct.
*
* Note, that slist is expected to be not empty.
*/ */
#define rt_container_of(ptr, type, member) \ #define rt_slist_first_entry(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) rt_slist_entry((ptr)->next, type, member)
/*@}*/ /*@}*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册