|
我从include/linux/list.h中提取出了关于双向循环链表的核心部分,并以此为基础加上一个例子展示一下这个非常有用的功能。
list功能的头文件:- /*********************************************************************************************************/
- //Author: Tang Xu
- //File: list_fun.h
- //
- /*********************************************************************************************************/
- #ifndef LIST_FUN_H
- #define LIST_FUN_H
- #define LIST_HEAD_INIT(name) { &(name), &(name) }
- #define LIST_HEAD(name) \
- struct list_head name = LIST_HEAD_INIT(name)
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
- /**
- * container_of - cast a member of a structure out to the containing structure
- * @ptr: the pointer to the member.
- * @type: the type of the container struct this is embedded in.
- * @member: the name of the member within the struct.
- *
- */
- #define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
- /**
- * list_entry - get the struct for this entry
- * @ptr: the &struct list_head pointer.
- * @type: the type of the struct this is embedded in.
- * @member: the name of the list_struct within the struct.
- */
- #define list_entry(ptr, type, member) \
- container_of(ptr, type, member)
- /**
- * list_for_each - iterate over a list
- * @pos: the &struct list_head to use as a loop counter.
- * @head: the head for your list.
- */
- #define list_for_each(pos, head) \
- for (pos = (head)->next; pos != (head); pos = pos->next)
- #define list_for_each_prev(pos, head) \
- for (pos = (head)->prev; pos != (head); pos = pos->prev)
-
- /**
- * list_for_each_entry - iterate over list of given type
- * @pos: the type * to use as a loop counter.
- * @head: the head for your list.
- * @member: the name of the list_struct within the struct.
- */
- #define list_for_each_entry(pos, head, member) \
- for (pos = list_entry((head)->next, typeof(*pos), member); \
- &pos->member != (head); \
- pos = list_entry(pos->member.next, typeof(*pos), member))
- #define list_for_each_entry_reverse(pos, head, member) \
- for (pos = list_entry((head)->prev, typeof(*pos), member); \
- &pos->member != (head); \
- pos = list_entry(pos->member.prev, typeof(*pos), member))
-
- struct list_head {
- struct list_head *next, *prev;
- };
- #ifdef __cplusplus
- extern "C" {
- #endif
- /**
- *
- */
- extern void INIT_LIST_HEAD(struct list_head *list);
- /**
- *
- */
- extern void list_add(struct list_head *new, struct list_head *head);
- /**
- *
- */
- extern void list_add_tail(struct list_head *new, struct list_head *head);
- /**
- *
- */
- extern void list_del(struct list_head *entry);
- /**
- *
- */
- extern int list_empty(const struct list_head *head);
- /**
- *
- */
- extern void list_del_init(struct list_head *entry);
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码
下面是实现该功能的文件。- #include "list_fun.h"
- #define LIST_POISON1 ((void *) 0x00100100)
- #define LIST_POISON2 ((void *) 0x00200200)
-
- void INIT_LIST_HEAD(struct list_head *list)
- {
- list->next = list;
- list->prev = list;
- }
- void __list_add(struct list_head *new,
- struct list_head *prev,
- struct list_head *next)
- {
- next->prev = new;
- new->next = next;
- new->prev = prev;
- prev->next = new;
- }
- void list_add(struct list_head *new, struct list_head *head)
- {
- __list_add(new, head, head->next);
- }
- void list_add_tail(struct list_head *new, struct list_head *head)
- {
- __list_add(new, head->prev, head);
- }
- void __list_del(struct list_head * prev, struct list_head * next)
- {
- next->prev = prev;
- prev->next = next;
- }
- void list_del(struct list_head *entry)
- {
- __list_del(entry->prev, entry->next);
- entry->next = LIST_POISON1;
- entry->prev = LIST_POISON2;
- }
- int list_empty(const struct list_head *head)
- {
- return head->next == head;
- }
- void list_del_init(struct list_head *entry)
- {
- __list_del(entry->prev, entry->next);
- INIT_LIST_HEAD(entry);
- }
复制代码
再下面是如何使用的参考代码。- #include <stdio.h>
- #include "list_fun.h"
- typedef struct _test {
- struct list_head list;
- void * data;
- int len;
- } TESTLIST;
- int main()
- {
- int i;
- TESTLIST head;
- memset( &head, 0, sizeof( TESTLIST));
- INIT_LIST_HEAD( &head.list);
- //创建链表
- TESTLIST * p_testlist = NULL;
- for( i=0; i<100; i++) {
- p_testlist = malloc( sizeof( TESTLIST));
- if( NULL == p_testlist) {
- perror("Memory shortage");
- exit( -1);
- }
- p_testlist->data = NULL;
- p_testlist->len = i;
- list_add( &p_testlist->list, &head.list);
- }
- //遍历链表并打印
- p_testlist = NULL;
- list_for_each_entry( p_testlist, &head.list, list)
- printf("%d\n", p_testlist->len);
- //删除链表
- p_testlist = NULL;
- list_for_each_entry( p_testlist, &head.list, list) {
- list_del( &p_testlist->list);
- if( p_testlist->data) {
- free( p_testlist->data);
- }
- free( p_testlist);
- }
- return 0;
- }
复制代码 |
|