Linux--內核鏈表
阿新 • • 發佈:2017-11-04
undefined pty ember 信息 方便 nbsp offset 表函數 specified
1、內核鏈表的定義在include/linux/list.h
struct list_head { struct list_head *next, *prev; };
容易看出,Linux內核鏈表為雙向鏈表。
2、Linux鏈表與普通鏈表區別
我們通常定義的鏈表是在鏈表節點中嵌入元素,比如
struct MyList { int StudentID; /* 被嵌入的元素 */ struct MyList *prev; struct MyList *next; }
而Linux為了移植方便性和通用性,在元素結構體中嵌入鏈表節點
strcut MyList { int StudentID;struct list_head head; /* 鏈表節點作為結構體元素 */ }
3、Linux內核鏈表中提供的操作鏈表函數
(1)初始化
static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; /* 下一個節點指向自己 */ list->prev = list; /* 前一個節點指向自己 */ }
(2)添加鏈表節點
list_add(struct list_head *new, struct list_head *head)
// new 的節點被插入到 head(head為任意鏈表頭) 的後面
/** * list_add - add a new entry * @new: new entry to be added * @head: list head to add it after * Insert a new entry after the specified head. * This is good for implementing stacks. */ static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); /*節點插入到head和head->next之間 */ } 而__list_add函數如下 static inline 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; }
list_add_tail(struct list_head *new, struct list_head *head)
new 的節點插入到 head 的前面
/** * list_add_tail - add a new entry * @new: new entry to be added * @head: list head to add it before * * Insert a new entry before the specified head. * This is useful for implementing queues. */ static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); }
(3)刪除節點
方法一:
/** * list_del - deletes entry from list. * @entry: the element to delete from the list. * Note: list_empty() on entry does not return true after this, the entry is * in an undefined state. */ static inline void list_del(struct list_head *entry) { __list_del(entry->prev, entry->next); entry->next = (void *)0xDEADBEEF; /* 將指針指向2個不可訪問的位置 */ entry->prev = (void *)0xBEEFDEAD; } 其中調用的__list_del函數如下, static inline void __list_del(struct list_head *prev, struct list_head *next) { next->prev = prev; /* */ prev->next = next; }
註意list_del函數中的最後兩條語句,類似於free()的作用。
當用戶打算訪問地址0xDEADBEEF或0xBEEFDEAD時,將產生頁中斷。
方法二:
為了更安全的刪除節點,可使用list_del_init
/** * list_del_init - deletes entry from list and reinitialize it. * @entry: the element to delete from the list. */ static inline void list_del_init(struct list_head *entry) { __list_del(entry->prev, entry->next); INIT_LIST_HEAD(entry); }
(4)提取結構的數據信息
按通常的方式使用鏈表很容易獲取數據信息,但使用Linux內核鏈表要訪問數據則比較困難,關鍵是如何求取鏈表節點地址和數據地址的偏移量。
註意list_entry傳遞的參數!type指傳遞的是類型,不是變量。
list_entry(ptr, type, member) // list_head的指針 , 外部結構的數據類型(struct statu),成員名
//返回指向 data 的指針
/** * 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) container_of定義在include/linux/kernel.h中, /** * 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)); })
(5)鏈表的遍歷
/** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)
舉一個栗子:
struct list_head *entry; struct list_head dev; //鏈表頭 list_for_each(entry, dev) { card = list_entry(entry, struct list_head, card); if(card->dev_midi == minor) break; }
Linux--內核鏈表