Linux內核中雙向鏈表的經典實現


Linux內核中雙向鏈表的經典實現

概要

前面一章"介紹雙向鏈表並給出了C/C++/Java三種實現",本章繼續對雙向鏈表進行探討,介紹的內容是Linux內核中雙向鏈表的經典實現和用法。其中,也會涉及到Linux內核中非常常用的兩個經典宏定義offsetof和container_of。內容包括:
1. Linux中的兩個經典宏定義
2. Linux中雙向鏈表的經典實現

轉載請注明出處:http://www.cnblogs.com/skywang12345/p/3562146.html


更多內容: 數據結構與算法系列 目錄

 

Linux中的兩個經典宏定義

倘若你查看過Linux Kernel的源碼,那么你對 offsetof 和 container_of 這兩個宏應該不陌生。這兩個宏最初是極客寫出的,后來在Linux內核中被推廣使用。

1. offsetof

1.1 offsetof介紹

定義:offsetof在linux內核的include/linux/stddef.h中定義。

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

說明獲得結構體(TYPE)的變量成員(MEMBER)在此結構體中的偏移量。
(01)  ( (TYPE *)0 )   將零轉型為TYPE類型指針,即TYPE類型的指針的地址是0。
(02)  ((TYPE *)0)->MEMBER     訪問結構中的數據成員。
(03)  &( ( (TYPE *)0 )->MEMBER )     取出數據成員的地址。由於TYPE的地址是0,這里獲取到的地址就是相對MEMBER在TYPE中的偏移。
(04)  (size_t)(&(((TYPE*)0)->MEMBER))     結果轉換類型。對於32位系統而言,size_t是unsigned int類型;對於64位系統而言,size_t是unsigned long類型。

 

1.2 offsetof示例
代碼(offset_test.c)

 1 #include <stdio.h>
 2  3 // 獲得結構體(TYPE)的變量成員(MEMBER)在此結構體中的偏移量。  4 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)  5  6 struct student  7 {  8 char gender;  9 int id; 10 int age; 11 char name[20]; 12 }; 13 14 void main() 15 { 16 int gender_offset, id_offset, age_offset, name_offset; 17 18 gender_offset = offsetof(struct student, gender); 19 id_offset = offsetof(struct student, id); 20 age_offset = offsetof(struct student, age); 21 name_offset = offsetof(struct student, name); 22 23 printf("gender_offset = %d\n", gender_offset); 24 printf("id_offset = %d\n", id_offset); 25 printf("age_offset = %d\n", age_offset); 26 printf("name_offset = %d\n", name_offset); 27 }

結果

gender_offset = 0 id_offset = 4 age_offset = 8 name_offset = 12

說明簡單說說"為什么id的偏移值是4,而不是1"。我的運行環境是linux系統,32位的x86架構。這就意味着cpu的數據總線寬度為32,每次能夠讀取4字節數據。gcc對代碼進行處理的時候,是按照4字節對齊的。所以,即使gender是char(一個字節)類型,但是它仍然是4字節對齊的!

 

1.3 offsetof圖解

TYPE是結構體,它代表"整體";而MEMBER是成員,它是整體中的某一部分。
將offsetof看作一個數學問題來看待,問題就相當簡單了:已知'整體'和該整體中'某一個部分',而計算該部分在整體中的偏移。

 

 

2. container_of

2.1 container_of介紹

定義:container_of在linux內核的include/linux/kernel.h中定義。

#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})

說明根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針。
(01) typeof( ( (type *)0)->member )     取出member成員的變量類型。
(02) const typeof( ((type *)0)->member ) *__mptr = (ptr)    定義變量__mptr指針,並將ptr賦值給__mptr。經過這一步,__mptr為member數據類型的常量指針,其指向ptr所指向的地址。
(04) (char *)__mptr    將__mptr轉換為字節型指針。
(05) offsetof(type,member))    就是獲取"member成員"在"結構體type"中的位置偏移。
(06) (char *)__mptr - offsetof(type,member))    就是用來獲取"結構體type"的指針的起始地址(為char *型指針)。
(07) (type *)( (char *)__mptr - offsetof(type,member) )    就是將"char *類型的結構體type的指針"轉換為"type *類型的結構體type的指針"。

 

2.2 container_of示例

代碼(container_test.c)

 1 #include <stdio.h>
 2 #include <string.h>  3  4 // 獲得結構體(TYPE)的變量成員(MEMBER)在此結構體中的偏移量。  5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)  6  7 // 根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針  8 #define container_of(ptr, type, member) ({ \  9 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 10 (type *)( (char *)__mptr - offsetof(type,member) );}) 11 12 struct student 13 { 14 char gender; 15 int id; 16 int age; 17 char name[20]; 18 }; 19 20 void main() 21 { 22 struct student stu; 23 struct student *pstu; 24 25 stu.gender = '1'; 26 stu.id = 9527; 27 stu.age = 24; 28 strcpy(stu.name, "zhouxingxing"); 29 30 // 根據"id地址" 獲取 "結構體的地址"。 31 pstu = container_of(&stu.id, struct student, id); 32 33 // 根據獲取到的結構體student的地址,訪問其它成員 34 printf("gender= %c\n", pstu->gender); 35 printf("age= %d\n", pstu->age); 36 printf("name= %s\n", pstu->name); 37 }

結果

gender= 1 age= 24 name= zhouxingxing

 

2.3 container_of圖解

type是結構體,它代表"整體";而member是成員,它是整體中的某一部分,而且member的地址是已知的。
將offsetof看作一個數學問題來看待,問題就相當簡單了:已知'整體'和該整體中'某一個部分',要根據該部分的地址,計算出整體的地址。

 

Linux中雙向鏈表的經典實現

1. Linux中雙向鏈表介紹

Linux雙向鏈表的定義主要涉及到兩個文件:
include/linux/types.h
include/linux/list.h

 

Linux中雙向鏈表的使用思想
它是將雙向鏈表節點嵌套在其它的結構體中;在遍歷鏈表的時候,根據雙鏈表節點的指針獲取"它所在結構體的指針",從而再獲取數據。

我舉個例子來說明,可能比較容易理解。假設存在一個社區中有很多人,每個人都有姓名和年齡。通過雙向鏈表將人進行關聯的模型圖如下:

person代表人,它有name和age屬性。為了通過雙向鏈表對person進行鏈接,我們在person中添加了list_head屬性。通過list_head,我們就將person關聯起來了。

struct person { int age; char name[20]; struct list_head list; };

 

 

2. Linux中雙向鏈表的源碼分析

(01). 節點定義

struct list_head { struct list_head *next, *prev; };

雖然名稱list_head,但是它既是雙向鏈表的表頭,也代表雙向鏈表的節點。

 

(02). 初始化節點

復制代碼
#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name) static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; list->prev = list; }
復制代碼

LIST_HEAD的作用是定義表頭(節點):新建雙向鏈表表頭name,並設置name的前繼節點和后繼節點都是指向name本身。
LIST_HEAD_INIT的作用是初始化節點:設置name節點的前繼節點和后繼節點都是指向name本身。
INIT_LIST_HEAD和LIST_HEAD_INIT一樣,是初始化節點:將list節點的前繼節點和后繼節點都是指向list本身。

 

(03). 添加節點

復制代碼
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; } static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); } static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); }
復制代碼

__list_add(new, prev, next)的作用是添加節點:將new插入到prev和next之間。在linux中,以"__"開頭的函數意味着是內核的內部接口,外部不應該調用該接口。
list_add(new, head)的作用是添加new節點:將new添加到head之后,是new稱為head的后繼節點。
list_add_tail(new, head)的作用是添加new節點:將new添加到head之前,即將new添加到雙鏈表的末尾。

 

(04). 刪除節點

復制代碼
static inline void __list_del(struct list_head * prev, struct list_head * next) { next->prev = prev; prev->next = next; } static inline void list_del(struct list_head *entry) { __list_del(entry->prev, entry->next); } static inline void __list_del_entry(struct list_head *entry) { __list_del(entry->prev, entry->next); } static inline void list_del_init(struct list_head *entry) { __list_del_entry(entry); INIT_LIST_HEAD(entry); }
復制代碼

__list_del(prev, next) 和__list_del_entry(entry)都是linux內核的內部接口。
__list_del(prev, next) 的作用是從雙鏈表中刪除prev和next之間的節點。
__list_del_entry(entry) 的作用是從雙鏈表中刪除entry節點。

list_del(entry) 和 list_del_init(entry)是linux內核的對外接口。
list_del(entry) 的作用是從雙鏈表中刪除entry節點。
list_del_init(entry) 的作用是從雙鏈表中刪除entry節點,並將entry節點的前繼節點和后繼節點都指向entry本身。

 

(05). 替換節點

復制代碼
static inline void list_replace(struct list_head *old, struct list_head *new) { new->next = old->next; new->next->prev = new; new->prev = old->prev; new->prev->next = new; }
復制代碼

list_replace(old, new)的作用是用new節點替換old節點。

 

(06). 判斷雙鏈表是否為空

static inline int list_empty(const struct list_head *head) { return head->next == head; }

list_empty(head)的作用是判斷雙鏈表是否為空。它是通過區分"表頭的后繼節點"是不是"表頭本身"來進行判斷的。

 

(07). 獲取節點

#define list_entry(ptr, type, member) \ container_of(ptr, type, member)

list_entry(ptr, type, member) 實際上是調用的container_of宏。
它的作用是:根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針。

 

(08). 遍歷節點

#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next)

list_for_each(pos, head)和list_for_each_safe(pos, n, head)的作用都是遍歷鏈表。但是它們的用途不一樣!
list_for_each(pos, head)通常用於獲取節點,而不能用到刪除節點的場景。
list_for_each_safe(pos, n, head)通常刪除節點的場景。

 

3. Linux中雙向鏈表的使用示例

雙向鏈表代碼(list.h)

 

  1 #ifndef _LIST_HEAD_H  2 #define _LIST_HEAD_H  3  4 // 雙向鏈表節點  5 struct list_head {  6 struct list_head *next, *prev;  7 };  8  9 // 初始化節點:設置name節點的前繼節點和后繼節點都是指向name本身。  10 #define LIST_HEAD_INIT(name) { &(name), &(name) }  11  12 // 定義表頭(節點):新建雙向鏈表表頭name,並設置name的前繼節點和后繼節點都是指向name本身。  13 #define LIST_HEAD(name) \  14 struct list_head name = LIST_HEAD_INIT(name)  15  16 // 初始化節點:將list節點的前繼節點和后繼節點都是指向list本身。  17 static inline void INIT_LIST_HEAD(struct list_head *list)  18 {  19 list->next = list;  20 list->prev = list;  21 }  22  23 // 添加節點:將new插入到prev和next之間。  24 static inline void __list_add(struct list_head *new,  25 struct list_head *prev,  26 struct list_head *next)  27 {  28 next->prev = new;  29 new->next = next;  30 new->prev = prev;  31 prev->next = new;  32 }  33  34 // 添加new節點:將new添加到head之后,是new稱為head的后繼節點。  35 static inline void list_add(struct list_head *new, struct list_head *head)  36 {  37 __list_add(new, head, head->next);  38 }  39  40 // 添加new節點:將new添加到head之前,即將new添加到雙鏈表的末尾。  41 static inline void list_add_tail(struct list_head *new, struct list_head *head)  42 {  43 __list_add(new, head->prev, head);  44 }  45  46 // 從雙鏈表中刪除entry節點。  47 static inline void __list_del(struct list_head * prev, struct list_head * next)  48 {  49 next->prev = prev;  50 prev->next = next;  51 }  52  53 // 從雙鏈表中刪除entry節點。  54 static inline void list_del(struct list_head *entry)  55 {  56 __list_del(entry->prev, entry->next);  57 }  58  59 // 從雙鏈表中刪除entry節點。  60 static inline void __list_del_entry(struct list_head *entry)  61 {  62 __list_del(entry->prev, entry->next);  63 }  64  65 // 從雙鏈表中刪除entry節點,並將entry節點的前繼節點和后繼節點都指向entry本身。  66 static inline void list_del_init(struct list_head *entry)  67 {  68  __list_del_entry(entry);  69  INIT_LIST_HEAD(entry);  70 }  71  72 // 用new節點取代old節點  73 static inline void list_replace(struct list_head *old,  74 struct list_head *new)  75 {  76 new->next = old->next; 77 new->next->prev = new; 78 new->prev = old->prev; 79 new->prev->next = new; 80 } 81 82 // 雙鏈表是否為空 83 static inline int list_empty(const struct list_head *head) 84 { 85 return head->next == head; 86 } 87 88 // 獲取"MEMBER成員"在"結構體TYPE"中的位置偏移 89 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 90 91 // 根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針 92 #define container_of(ptr, type, member) ({ \ 93 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 94 (type *)( (char *)__mptr - offsetof(type,member) );}) 95 96 // 遍歷雙向鏈表 97 #define list_for_each(pos, head) \ 98 for (pos = (head)->next; pos != (head); pos = pos->next) 99 100 #define list_for_each_safe(pos, n, head) \ 101 for (pos = (head)->next, n = pos->next; pos != (head); \ 102 pos = n, n = pos->next) 103 104 #define list_entry(ptr, type, member) \ 105 container_of(ptr, type, member) 106 107 #endif

 

雙向鏈表測試代碼(test.c)

 1 #include <stdio.h> 
 2 #include <stdlib.h>  3 #include <string.h>  4 #include "list.h"  5  6 struct person  7 {  8 int age;  9 char name[20]; 10 struct list_head list; 11 }; 12 13 void main(int argc, char* argv[]) 14 { 15 struct person *pperson; 16 struct person person_head; 17 struct list_head *pos, *next; 18 int i; 19 20 // 初始化雙鏈表的表頭 21 INIT_LIST_HEAD(&person_head.list); 22 23 // 添加節點 24 for (i=0; i<5; i++) 25  { 26 pperson = (struct person*)malloc(sizeof(struct person)); 27 pperson->age = (i+1)*10; 28 sprintf(pperson->name, "%d", i+1); 29 // 將節點鏈接到鏈表的末尾 30 // 如果想把節點鏈接到鏈表的表頭后面,則使用 list_add 31 list_add_tail(&(pperson->list), &(person_head.list)); 32  } 33 34 // 遍歷鏈表 35 printf("==== 1st iterator d-link ====\n"); 36 list_for_each(pos, &person_head.list) 37  { 38 pperson = list_entry(pos, struct person, list); 39 printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 40  } 41 42 // 刪除節點age為20的節點 43 printf("==== delete node(age:20) ====\n"); 44 list_for_each_safe(pos, next, &person_head.list) 45  { 46 pperson = list_entry(pos, struct person, list); 47 if(pperson->age == 20) 48  { 49  list_del_init(pos); 50  free(pperson); 51  } 52  } 53 54 // 再次遍歷鏈表 55 printf("==== 2nd iterator d-link ====\n"); 56 list_for_each(pos, &person_head.list) 57  { 58 pperson = list_entry(pos, struct person, list); 59 printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 60  } 61 62 // 釋放資源 63 list_for_each_safe(pos, next, &person_head.list) 64  { 65 pperson = list_entry(pos, struct person, list); 66  list_del_init(pos); 67  free(pperson); 68  } 69 70 }

 

運行結果

復制代碼
==== 1st iterator d-link ====
name:1 , age:10 name:2 , age:20 name:3 , age:30 name:4 , age:40 name:5 , age:50 ==== delete node(age:20) ==== ==== 2nd iterator d-link ==== name:1 , age:10 name:3 , age:30 name:4 , age:40 name:5 , age:50
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM