Linux散列表(二)——宏

散列表宏承接了双向链表宏的风范,好使好用!务必区分“结点”和“元素”!双链表宏博文中已经提及,这里不赘述!

1、获取元素(结构体)基址

 

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_entry_safe(ptr, type, member) \

	({ typeof(ptr) ____ptr = (ptr); \

	   ____ptr ? hlist_entry(____ptr, type, member) : NULL; \

	})

第一个不带安全机制,第二个带安全机制,即ptr为NULL时,返回NULL,否则,返回基址!原理 点击这里查看container_of

 

2、操作散列表结点

2.1、遍历散列表结点

 

#define hlist_for_each(pos, head) \

	for (pos = (head)->first; pos ; pos = pos->next)

 

2.2、删除散列表结点

 

#define hlist_for_each_safe(pos, n, head) \

	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \

	     pos = n)

 

 

3、操作散列表元素

3.1、从head的first开始遍历

 

/**

 * hlist_for_each_entry	- iterate over list of given type

 * @pos:	the type * to use as a loop cursor.

 * @head:	the head for your list.

 * @member:	the name of the hlist_node within the struct.

 */

#define hlist_for_each_entry(pos, head, member)				\

	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\

	     pos;							\

	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

 

3.2、从pos的next开始遍历

 

/**

 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point

 * @pos:	the type * to use as a loop cursor.

 * @member:	the name of the hlist_node within the struct.

 */

#define hlist_for_each_entry_continue(pos, member)			\

	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\

	     pos;							\

	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

 

3.3、从pos开始遍历

 

/**

 * hlist_for_each_entry_from - iterate over a hlist continuing from current point

 * @pos:	the type * to use as a loop cursor.

 * @member:	the name of the hlist_node within the struct.

 */

#define hlist_for_each_entry_from(pos, member)				\

	for (; pos;							\

	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))

 

3.4、从head的first开始删除

 

/**

 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry

 * @pos:	the type * to use as a loop cursor.

 * @n:		another &struct hlist_node to use as temporary storage

 * @head:	the head for your list.

 * @member:	the name of the hlist_node within the struct.

 */

#define hlist_for_each_entry_safe(pos, n, head, member) 		\

	for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\

	     pos && ({ n = pos->member.next; 1; });			\

	     pos = hlist_entry_safe(n, typeof(*pos), member))

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>声明<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

>>     知识要传播,劳动要尊重! 受益于开源,回馈于社会! 大家共参与,服务全人类!     

>>     本博文由my_live_123原创(http://blog.csdn.net/cwcmcw),转载请注明出处!   

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>^_^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 


 

你可能感兴趣的:(linux)