在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作list_head 的通用接口很容易实现代码的重用,有点类似于C++的继承机制(希望有机会写篇文章研究一下C语言的面向对象机制)。 首先找到list_head结构体定义,kernel/inclue/linux/types.h 如下:
' ]' q( n4 [" m% y- struct list_head {
% f. t4 L' l/ z$ ` - struct list_head *next, *prev;
! Z( [9 }5 N% o, p* d3 |: t7 D7 H - };; u) [1 O! m; J% o5 w' l
- #define LIST_HEAD_INIT(name) { &(name), &(name) }
复制代码 4 x D" M0 S+ E' k- t+ m: K7 j
需要注意的一点是,头结点head是不使用的,这点需要注意。 使用list_head组织的链表的结构如下图所示:
- ?5 s/ p& K m+ U
9 A1 g! q- i2 w, g, f) t 然后就开始围绕这个结构开始构建链表,然后插入、删除节点 ,遍历整个链表等等,其实内核已经提供好了现成的接口,接下来就让我们进入 kernel/include/linux/list.h中: 一. 创建链表 内核提供了下面的这些接口来初始化链表: - " ]5 {, q/ E4 H1 T" E
- #define LIST_HEAD_INIT(name) { &(name), &(name) }
S+ Y' [9 p$ ?& [+ J. N" t( [
b5 _" l& y; d! s- #define LIST_HEAD(name) \1 O) B3 p6 F1 c! T$ Q( W0 N4 H
- struct list_head name = LIST_HEAD_INIT(name)5 q7 I# x7 B4 C) q* A0 J5 W2 Y* a* t. q7 k
" E5 T: {" H& C1 i. o3 A4 R- `- static inline void INIT_LIST_HEAD(struct list_head *list)
; \+ l9 @% _* M r: ]! j( M - {
, E$ ^4 S V5 i8 l - WRITE_ONCE(list->next, list);& Y( ~" [. a' ]3 F
- list->prev = list;( C8 a, a9 X7 ]0 F, E/ T
- }
复制代码2 {9 v- q0 y& V. h4 g# s* w, {
如: 可以通过 LIST_HEAD(mylist) 进行初始化一个链表,mylist的prev 和 next 指针都是指向自己。 - structlist_head mylist = {&mylist, &mylist} ;
' q- |$ J+ G/ m5 p0 T
但是如果只是利用mylist这样的结构体实现链表就没有什么实际意义了,因为正常的链表都是为了遍历结构体中的其它有意义的字段而创建的,而我们mylist中只有 prev和next指针,却没有实际有意义的字段数据,所以毫无意义。 综上,我们可以创建一个宿主结构,然后在此结构中再嵌套mylist字段,宿主结构又有其它的字段(进程描述符 task_struct,页面管理的page结构,等就是采用这种方法创建链表的)。为简便理解,定义如下:
3 K$ j0 I3 k. q$ _, e- struct mylist{
" f) d8 `( F$ B. H - int type;% ]; z( |' l3 K% I. i9 D3 i
- char name[MAX_NAME_LEN];& N" Z- L+ \8 _1 Q/ K( p% a
- struct list_head list;8 z# j2 z% F) ~) E
- }
复制代码
9 ~/ A$ F7 t% w创建链表,并初始化 - / H% b; [$ J( L' `( L
- structlist_head myhead;
8 D' w$ [ k* q; P. b - INIT_LIST_HEAD(&myhead);
复制代码 5 ~. B4 \9 [9 c. }
这样我们的链表就初始化完毕,链表头的myhead就prev 和 next指针分别指向myhead自己了,如下图: 二. 添加节点
" x6 M3 x) \4 [ C! e# \$ M. } 内核已经提供了添加节点的接口了 0 k3 f' k9 ^' l
1. list_add 如下所示。根据注释可知,是在链表头head后方插入一个新节点new。
! {; B: ?# o# e' }9 H- /**
" Y* x# T9 F6 X1 v) A, @ - * list_add - add a new entry
( b. }0 o! ^( x5 G* \+ I0 J& \ - * @new: new entry to be added) B# A- ?' b* p% x O5 N) `
- * @head: list head to add it after
9 C/ F* C3 Z! S/ r - *
9 H( w7 B+ ^1 T5 T# \ - * Insert a new entry after the specified head.
W5 [$ C2 a9 Z* B/ U m( E - * This is good for implementing stacks.' K' j- \- X5 b& D+ b
- */
7 i D/ @5 t* ^: Q( _ - static inline void list_add(struct list_head *new, struct list_head *head)9 a u s& n, ^
- {: m3 o' G0 c$ q7 n I9 C
- __list_add(new, head, head->next);
! D; e/ j1 @9 `6 p' Q$ {+ I - }
复制代码
, X4 v! Y5 r" l5 h9 M+ E T
list_add再调用__list_add接口
5 ~' d& x8 x1 Q4 C( x4 }0 d. h- /*
# b6 s2 r+ ?% U( O7 I+ E - * Insert a new entry between two known consecutive entries.- e1 V: A* p' h- n6 J
- *
; R( M% E* R3 ~# M - * This is only for internal list manipulation where we know
8 M* x) F4 K& x; h( b# V& N - * the prev/next entries already!( A0 l4 a6 w2 ?0 R+ P/ J
- */
/ I6 S) D, G. e# L - static inline void __list_add(struct list_head *new,( ?4 q3 i9 \7 @; w3 j
- struct list_head *prev,+ w, h5 \. ]9 e) n$ u
- struct list_head *next)
9 F T: p8 T& i% V/ r, g% K! j - {
5 ]& L0 o! n% p( R4 Z - if (!__list_add_valid(new, prev, next))! z& [. a1 Q- t' ^% Q
- return; w$ a/ c) D2 u+ J9 Y
- ! C# P, p/ \- } e$ Z/ V
- next->prev = new;, ?1 g# }: Y+ ]
- new->next = next;
7 {, ~2 O, h7 p9 A8 k - new->prev = prev;
9 v* R1 W4 U2 k - WRITE_ONCE(prev->next, new);
3 v9 E6 ~" v6 t/ E - }
复制代码: A' }* B( \8 O: X1 X( g
其实就是在myhead链表头后和链表头后第一个节点之间插入一个新节点。然后这个新的节点就变成了链表头后的第一个节点了。 接着上面步骤创建1个节点然后插入到myhead之后 - ( k. }3 B: S) ~) o( \0 i2 o
- struct mylist node1;, m1 `1 R9 q9 Z
- node1.type = I2C_TYPE;$ z; i) ]$ {$ h! y" ]8 J
- strcpy(node1.name,"yikoulinux");( o$ ]$ w8 t) _3 j
- list_add(&node1.list,&myhead);
复制代码
, _5 j/ _: y/ w! l+ f. F
: G/ n: n6 ~$ v' q* m
然后在创建第二个节点,同样把它插入到header_task之后 - 3 c- {' r& m7 \: }
- struct mylist node2;
) q! u2 {5 M& v - node2.type = I2C_TYPE;
5 _: V0 h3 |! A# ~/ a7 i8 o - strcpy(node2.name,"yikoupeng");
) V1 ]' V/ L/ p- I) ?0 }9 d0 k - list_add(&node2.list,&myhead);
复制代码
% G* x% q7 [2 Z0 clist_add 以此类推,每次插入一个新节点,都是紧靠着header节点,而之前插入的节点依次排序靠后,那最后一个节点则是第一次插入header后的那个节点。最终可得出:先来的节点靠后,而后来的节点靠前,“先进后出,后进先出”。所以此种结构类似于 stack“堆栈”, 而header_task就类似于内核stack中的栈顶指针esp,它都是紧靠着最后push到栈的元素。 3 ~: y! }5 @0 c: b% ^
2. list_add_tail 接口 上面所讲的list_add接口是从链表头header后添加的节点。同样,内核也提供了从链表尾处向前添加节点的接口list_add_tail.让我们来看一下它的具体实现。 - p' z8 T& B4 ]: o0 z1 T% f- Z
- /**
4 p, `0 s) c1 y6 @ q - * list_add_tail - add a new entry
" U( G' b0 b( ^8 X9 K - * @new: new entry to be added
, p9 n! ?9 L+ {7 M5 ?. J; J - * @head: list head to add it before
+ t! t4 i& I( ?0 K) G, l, c - *
+ j b4 q9 ?6 t: J2 k7 S2 b - * Insert a new entry before the specified head.5 H1 C/ e3 H$ R! @3 o2 ^
- * This is useful for implementing queues.
' K v) [; O. G$ O' { - */
4 W1 ~8 b$ Q+ V( P/ F9 K3 a - static inline void list_add_tail(struct list_head *new, struct list_head *head)* \4 ?3 u% f7 s/ L# b5 n0 b- f
- {. k$ y! N: L6 M8 p% [
- __list_add(new, head->prev, head);0 F+ Y1 S: D1 K3 A8 m9 }
- }
复制代码
# D0 t: x" S- ]/ {6 l: q/ ^: B
从注释可得出:(1)在一个特定的链表头前面插入一个节点 (2)这个方法很适用于队列的实现(why?) 进一步把__list_add()展开如下: - ) [- D- d7 r6 N9 l3 b- o, l( Y$ l
- /*
3 z0 d6 r3 x( D# O2 ^1 [ - * Insert a new entry between two known consecutive entries.
9 {% r* M- h' F0 ? - *2 |- k- |# K: c$ M( ^1 n5 K0 n4 J
- * This is only for internal list manipulation where we know
# @* t b; }" P$ B, e' d - * the prev/next entries already!8 \' N9 Y/ B2 `! q8 P
- */
) ]. F% g' P$ o% ?) X \/ I: ~- s& T - static inline void __list_add(struct list_head *new,
* }2 n( s( _2 b3 n0 u- M3 y7 m - struct list_head *prev,
8 { M/ d4 `% W - struct list_head *next)) L# C9 Q4 p5 N( ?# l$ s
- {
/ ]8 x* @7 C. v: g - if (!__list_add_valid(new, prev, next))2 z h) w$ Q7 C. W! b( E- q
- return;
e) o& P/ K: E# i. s% ~9 q
9 ^' V8 X, y' }7 A: G) V$ I- next->prev = new;
- W6 c( J! w9 q; ^$ X - new->next = next;9 g+ I# e" `7 s' ^8 ?' s( }7 t
- new->prev = prev;3 Y/ \9 B% D/ f4 O z3 V9 p, ~
- WRITE_ONCE(prev->next, new);5 E9 @) a- i# u( m* o
- }
复制代码 $ n) t& u; e# T$ W" h) e+ ~( K
所以,很清楚明了, list_add_tail就相当于在链表头前方依次插入新的节点(也可理解为在链表尾部开始插入节点,此时,header节点既是为节点,保持不变) 利用上面分析list_add接口的方法可画出数据结构图形如下。 (1)创建一个链表头(实际上应该是表尾)代码参考第一节; (2)插入第一个节点 node1.list , 调用 - 6 Z6 _8 Y& i+ V3 f8 N4 c1 S# Y0 }: B
- struct mylist node1;+ Z4 v2 `( f. M( G2 r
- node1.type = I2C_TYPE;. E/ n* ]( v' }; z
- strcpy(node1.name,"yikoulinux");
7 [% J" K. B9 M - list_add_tail(&node1.list,&myhead);
复制代码 * H! z; t2 y6 R- ?; ~. p- [! O
(3) 插入第二个节点node2.list,调用 - " {, Y* L( w1 ?: j! ?* h7 A' m: e
- struct mylist node2;
& B; }/ j9 _- b% a+ S$ ~. E. b% `. \ - node2.type = I2C_TYPE;) A) u5 R1 _9 h. Q L% v
- strcpy(node2.name,"yikoupeng");) F S8 L+ X1 T/ ?: k! \1 i
- list_add_tail(&node2.list,&myhead);
复制代码 ; @1 y% _ I3 D7 z* v
list_add_tail 依此类推,每次插入的新节点都是紧挨着 header_task表尾,而插入的第一个节点my_first_task排在了第一位,my_second_task排在了第二位,可得出:先插入的节点排在前面,后插入的节点排在后面,“先进先出,后进后出”,这不正是队列的特点吗(First in First out)! 三. 删除节点 内核同样在list.h文件中提供了删除节点的接口 list_del(), 让我们看一下它的实现流程
7 S: U% X: ]- I8 ^, d- static inline void list_del(struct list_head *entry)
1 a4 T. k, V4 |& ~+ _( I- _ - {$ @0 ?. w0 h6 q, M1 b8 n! _
- __list_del_entry(entry);
3 h" D' {2 @. s1 J, l - entry->next = LIST_POISON1;
2 U: C* Z- D9 [7 q - entry->prev = LIST_POISON2;
2 j3 H* v' K' i' b - }) P8 v2 k3 U0 V! k- R
- /*7 J* u8 T1 o1 a- g
- * Delete a list entry by making the prev/next entries
5 V+ l j# l) U- W. m - * point to each other.; J% `# q, ^! }
- *; h6 ]- u; y) C8 v8 h
- * This is only for internal list manipulation where we know2 P$ o( \: ~- |$ M: u2 W6 B- b
- * the prev/next entries already!0 A4 n. b' n. N& \8 ?& a$ X8 u9 Y1 \& x
- */; [. p4 c: H* c0 T' d' k) Q- |9 n. _
- static inline void __list_del(struct list_head * prev, struct list_head * next)
& p, Z5 `* t' ?2 f$ `+ y - {
- _ \0 u8 U) Z2 K1 k - next->prev = prev;
Q1 F+ d, A# {0 A) V9 l - WRITE_ONCE(prev->next, next);
: `/ s$ Y$ a5 }8 @; p J - }
, C0 D9 z9 }; v# P( W! ?- V - " L1 e3 \' H* [6 u6 B: \. W
- /**
% N3 r! H5 k# g7 w2 I - * list_del - deletes entry from list.
6 g9 j$ ?1 j3 |' U# P+ y - * @entry: the element to delete from the list.
8 |6 c: V; ^" z4 O# ^ - * Note: list_empty() on entry does not return true after this, the entry is
+ ?0 y4 Y8 @; n8 b3 P - * in an undefined state.
! E% H8 D7 F1 y/ a* ?5 }: W - */
, C, _5 R, z9 U3 h - static inline void __list_del_entry(struct list_head *entry)
0 L9 z3 S2 v, ?. { - {" j0 k2 u. r* g. g5 w
- if (!__list_del_entry_valid(entry)) y4 d7 }4 m0 A# E! A8 s
- return;& ?7 d8 R- D, b3 @- L" w" h( s l
3 m- w: H6 a. z- __list_del(entry->prev, entry->next);% A+ O ]: Y% R- W0 B* s
- }
复制代码
& e8 } @( z$ b: O7 @ 利用list_del(struct list_head *entry) 接口就可以删除链表中的任意节点了,但需注意,前提条件是这个节点是已知的,既在链表中真实存在,切prev,next指针都不为NULL。 四. 链表遍历 内核是同过下面这个宏定义来完成对list_head链表进行遍历的,如下 : - ' H: h# T5 ?2 i q6 I2 \$ f
- /**" t7 M F H- [
- * list_for_each - iterate over a list
5 ^. h7 i2 r& I4 i+ i6 N( ]0 z/ Z - * @pos: the &struct list_head to use as a loop cursor.. |' A/ \$ @( n% v4 r
- * @head: the head for your list.
P: ]8 N! \2 L, ^! ` - */
9 O2 J; h# m, _ - #define list_for_each(pos, head) \
1 r. C, n# b7 E) C) @& l - for (pos = (head)->next; pos != (head); pos = pos->next)
复制代码 ' M, I% ]7 I" A
上面这种方式是从前向后遍历的,同样也可以使用下面的宏反向遍历:
. |: \/ [0 ~( A7 h* O7 V& K& j- /**
X7 ^' A" G9 Y% M1 n - * list_for_each_prev - iterate over a list backwards
- A0 ]1 G" n4 `1 w - * @pos: the &struct list_head to use as a loop cursor.
" I4 U; P, \) B C( g2 ~& |2 b - * @head: the head for your list.
4 x) D, G! F; v( V4 X! d0 X - */7 X) F# b' i7 V a3 @. x
- #define list_for_each_prev(pos, head) \
3 |8 ?2 v2 x' a1 p3 K- e$ {! P - for (pos = (head)->prev; pos != (head); pos = pos->prev)
复制代码
( L, g: Q9 N+ A 而且,list.h 中也提供了list_replace(节点替换) list_move(节点移位) ,翻转,查找等接口,这里就不在一一分析了。 五. 宿主结构1.找出宿主结构 list_entry(ptr, type, member) 上面的所有操作都是基于list_head这个链表进行的,涉及的结构体也都是: - # C5 v i4 O T% z; W" S
- struct list_head {
3 L; x4 Y4 }2 d9 n - struct list_head *next, *prev;
+ e" s5 j) ]! e* d% ]# G' D - };
复制代码
2 n1 n u' \* r8 c5 W, d* g; ]. A 其实,正如文章一开始所说,我们真正更关心的是包含list_head这个结构体字段的宿主结构体,因为只有定位到了宿主结构体的起始地址,我们才能对对宿主结构体中的其它有意义的字段进行操作。
# C9 L- |1 q, f- j4 n- struct mylist x E# m& f; N6 x, @
- { + O, F* m, H* A2 a
- int type;
' ~; Q2 z, X9 z+ u8 G/ g* R( k - char name[MAX_NAME_LEN];
, q- M) Y3 x' F! G }$ s* Y" |& f& n; p - struct list_head list; " w1 T* {: x' K7 S. G' G4 j. e
- };
复制代码 " m8 j% Q% Z5 ]! G$ A
那我们如何根据list这个字段的地址而找到宿主结构node1的位置呢? list.h中定义如下: - , P. y, s6 s/ a. S8 m5 @* m' x
- /**0 u+ v T3 [+ b5 [* V
- * list_entry - get the struct for this entry
5 ^/ w- e! e; X6 @+ h - * @ptr: the &struct list_head pointer.
' ?( g' c" }- [8 u6 B) y5 o `# T0 q - * @type: the type of the struct this is embedded in.& ^: @2 n1 y! @% k
- * @member: the name of the list_head within the struct.9 c5 T2 j2 C; G O( g5 z
- */0 W$ T8 e& S" u
- #define list_entry(ptr, type, member) \4 C6 a0 r7 c4 ?
- container_of(ptr, type, member)
复制代码5 a+ x7 u0 W$ L
list.h中提供了list_entry宏来实现对应地址的转换,但最终还是调用了container_of宏,所以container_of宏的伟大之处不言而喻。 2 container_of 做linux驱动开发的同学是不是想到了LDD3这本书中经常使用的一个非常经典的宏定义! - container_of(ptr, type, member)
, i% l0 A8 P8 d& H
在LDD3这本书中的第三章字符设备驱动,以及第十四章驱动设备模型中多次提到,我觉得这个宏应该是内核最经典的宏之一。那接下来让我们揭开她的面纱: 此宏在内核代码 kernel/include/linux/kernel.h中定义(此处kernel版本为3.10;新版本4.13之后此宏定义改变,但实现思想保持一致) 而offsetof定义在 kernel/include/linux/stddef.h ,如下: 举个例子,来简单分析一下container_of内部实现机制。 例如: - c4 \) \- \) ^9 F+ q7 j
- struct test' L J/ p- G0 E/ s, c$ N4 J
- {3 }0 n5 u* \4 X# d, s
- int a;
/ a9 u$ b8 C8 N1 l- ?; ]7 N - short b;
8 h: [' C5 O( A - char c;
U$ {- ?/ Z: l0 ? - };
I$ d8 I' d% Z+ Q4 j: x9 ` - struct test *p = (struct test *)malloc(sizeof(struct test));) l: V2 c, q8 P' i$ y
- test_function(&(p->b));
( F/ T* v; z0 K0 x - * l/ i' t# B- v0 J: A, i0 i. i
- int test_function(short *addr_b)
% j. G8 ~; A: ]6 K - {
5 u/ {! |) W9 Y9 n5 s - //获取struct test结构体空间的首地址; D u/ R" Z7 z" ~
- struct test *addr;+ {' C* [8 v* m: ?% l$ G
- addr = container_of(addr_b,struct test,b);
7 N0 X+ O/ {1 c; _8 b; M9 u/ { - }
复制代码 % U2 t' h. e8 ]0 @, [
展开container_of宏,探究内部的实现:
% I+ X- `+ A6 [! T
- f1 e/ `: j5 B8 k4 N6 ~- typeof ( ( (struct test *)0 )->b ) ; (1)
% H% s8 D, Z& [+ I7 }: f6 c/ U - typeof ( ( (struct test *)0 )->b ) *__mptr = addr_b ; (2)3 @8 _# p1 }; i/ y
- (struct test *)( (char *)__mptr - offsetof(struct test,b)) (3)
复制代码
$ b+ ]2 |+ S) l) }. H2 v) o
(1) 获取成员变量b的类型 ,这里获取的就是short 类型。这是GNU_C的扩展语法。 (2) 用获取的变量类型,定义了一个指针变量 __mptr ,并且将成员变量 b的首地址赋值给它 (3) 这里的offsetof(struct test,b)是用来计算成员b在这个struct test 结构体的偏移。__mptr 是成员b的首地址, 现在 减去成员b在结构体里面的偏移值,算出来的是不是这个结构体的 首地址呀 。 3. 宿主结构的遍历 我们可以根据结构体中成员变量的地址找到宿主结构的地址,并且我们可以对成员变量所建立的链表进行遍历,那我们是不是也可以通过某种方法对宿主结构进行遍历呢? 答案肯定是可以的,内核在list.h中提供了下面的宏: - 0 E* V+ Q8 ^9 ~. w
- /**1 c9 ]4 V/ Y$ f: u; i& \
- * list_for_each_entry - iterate over list of given type1 K7 B O6 W' i0 D: i) C( }3 n
- * @pos: the type * to use as a loop cursor.) G, G# @9 W6 }1 s9 v
- * @head: the head for your list.; H' i) {7 W5 ~( u6 h
- * @member: the name of the list_head within the struct.' }/ i% F; F7 H2 ~
- */. \* c* [1 H1 n ?0 I
- #define list_for_each_entry(pos, head, member) \0 |, k( e' z' {/ L7 K; C! p- c
- for (pos = list_first_entry(head, typeof(*pos), member); \
: ^3 ~! }( m( s - &pos->member != (head); \7 S) Y. f' P4 {9 ~
- pos = list_next_entry(pos, member))
复制代码
5 X, _9 J P; q 其中,list_first_entry 和 list_next_entry宏都定义在list.h中,分别代表:获取第一个真正的宿主结构的地址;获取下一个宿主结构的地址。它们的实现都是利用list_entry宏。 - ! P; m6 S0 R8 e) p; t" s# I Q. v
- /*** G- W0 L1 Z6 @7 S
- * list_first_entry - get the first element from a list# j# K: c0 q. j- v3 ?* z% n
- * @ptr: the list head to take the element from.
. D7 M) r( e. N6 G, t! D - * @type: the type of the struct this is embedded in.! h- `$ i4 V3 Q& m5 F, r. o
- * @member: the name of the list_head within the struct.
& r( o! |9 w: v- P! R/ s9 ` - *
7 B: D2 U1 L6 e% i; ^) Q# U: D - * Note, that list is expected to be not empty.
; m+ R( \" D1 t+ S# s. K - */: @/ {& a P% [5 L
- #define list_first_entry(ptr, type, member) \* A6 U5 i0 y% @7 j3 f
- list_entry((ptr)->next, type, member)1 D8 W/ ]. V2 B
2 i# I) i$ @6 J, X( I1 _- /**$ W! H5 u' v7 Y
- * list_next_entry - get the next element in list) h, T$ h4 a( N$ i/ ^
- * @pos: the type * to cursor% ^/ C1 u+ V9 k6 B, k9 @
- * @member: the name of the list_head within the struct.
" @9 a, M/ J, @7 q1 }, C. O - */$ u2 `5 X% {" }8 x
- #define list_next_entry(pos, member) \& ?/ l( G$ ] h- ^4 h
- list_entry((pos)->member.next, typeof(*(pos)), member)
复制代码
5 G8 {# m5 D* Q: P5 p6 Z
最终实现了宿主结构的遍历 - 7 c3 h7 t) g& ?: T+ ]8 ]# p
- #define list_for_each_entry(pos, head, member) \
. c1 a/ p' F3 `9 ^& v; P - for (pos = list_first_entry(head, typeof(*pos), member); \5 R, D2 @2 e$ J R6 w$ R
- &pos->member != (head); \
7 A# ?8 ?2 L: S6 g; L* f - pos = list_next_entry(pos, member))
复制代码 W) \5 w) V9 S& \7 B2 {: C& A
首先pos定位到第一个宿主结构地址,然后循环获取下一个宿主结构地址,如果查到宿主结构中的member成员变量(宿主结构中struct list_head定义的字段)地址为head,则退出,从而实现了宿主结构的遍历。如果要循环对宿主结构中的其它成员变量进行操作,这个遍历操作就显得特别有意义了。 我们用上面的 nod结构举个例子:
+ }! a' l2 ?- k# m1 ^- struct my_list *pos_ptr = NULL ;
. L4 s. X- K( _( c- _1 D6 h0 B - list_for_each_entry (pos_ptr, &myhead, list )
" B' S$ D% ^" C! S' Q6 _3 f - { ) t1 m/ u; T, M: f8 E0 p B/ K+ ?+ ~
- printk ("val = %d\n" , pos_ptr->val); , A' R- S5 S" y" y1 E
- }
复制代码
: u; z/ |# ]- V9 G! S) n. b
}! b4 E# [8 `* c } 实例1 一个简单的链表的实现为方便起见,本例把内核的list.h文件单独拷贝出来,这样就可以独立于内核来编译测试。 功能描述:本例比较简单,仅仅实现了单链表节点的创建、删除、遍历。 - - B1 {( Q8 F, ~
- #include "list.h" 2 O$ `4 k" x8 T7 Y( v f
- #include <stdio.h>
3 \% c. p, k2 p* t: P - #include <string.h>5 \; @1 l6 P ]6 V0 ^
- 3 y" f* B2 z2 ~4 A
- #define MAX_NAME_LEN 32$ v8 Q+ O5 ^4 \6 x8 s& |' N
- #define MAX_ID_LEN 10
- _* H- C' ?" n" D; [. j9 B( F - . V6 A5 A, C* P5 E9 D2 L7 n7 H
- struct list_head myhead;
% O9 G% G" m5 U - : {# G) p# T% M% e: u
- #define I2C_TYPE 1; _2 P; @7 Y6 E6 A$ u: z- b
- #define SPI_TYPE 25 Q+ F1 [. q1 q: `& _9 s
- ) `. ?' p) U# S. I) P
- char *dev_name[]={
. H# b* J5 r, ~4 f: L+ R. Z d/ Y - "none",+ ~& C( k4 }/ R7 G" V" Y% q7 u6 X' d
- "I2C",
2 q4 A7 p% p4 p3 v3 I. q4 q* C - "SPI"
O2 v, d7 G8 D - };, S5 t; {& E9 b# ^( |# K8 |
- 0 S T- e, E4 _) n& A, q" E6 {
- struct mylist3 ^9 P( `9 h: F& p1 d/ H: t9 w
- {
/ C9 g' J7 L. f% l( v - int type;9 Y" b2 L! z( p+ W" r
- char name[MAX_NAME_LEN];! X$ P$ v! A( n( ]8 V6 ^
- struct list_head list;
1 d3 n' x# X1 c9 v1 y* I - };
r9 G+ { f; R7 M g X
! b6 T* ^3 z' S# j- void display_list(struct list_head *list_head): T1 _" A% [+ p
- {
+ ?' C7 C/ q8 L0 K - int i=0;
a0 q% c5 q: a. P; [7 ~ - struct list_head *p;+ N5 P9 ^; {8 }( t) q
- struct mylist *entry;
* @0 u! m+ D7 G4 `+ ?8 | - printf("-------list---------\n");
4 M6 q. k' K5 n1 j N - list_for_each(p,list_head)
$ p' V$ A9 ], ^# G( {: t9 E3 E- |4 N/ F - {
/ Z( k q% y+ V+ a; u - printf("node[%d]\n",i++);( ?: U$ n2 u, {& y3 _0 k
- entry=list_entry(p,struct mylist,list);$ G4 i/ \; k( K' }) X) S/ q8 i/ A
- printf("\ttype: %s\n",dev_name[entry->type]);5 K1 U6 W/ P3 r1 E
- printf("\tname: %s\n",entry->name);, e# n' x5 x3 t2 ?7 \" B
- }& w/ P3 ? G* O' S) |
- printf("-------end----------\n");
. ]& p1 `$ ^( k. r; P0 t7 | - }
: d8 A R6 W- X& N
f% F7 i5 f8 w" R8 d- int main(void)
a- r" {$ `' e' y - {
0 @# a9 ?4 @- e
" b5 y! a8 x% r* Q6 N% r' H1 h- struct mylist node1;
+ |) v" M) z( {5 L9 B1 L, b5 h - struct mylist node2;
0 h0 q* c6 F% X2 U Z; W
2 M2 X; F7 x, J5 n- INIT_LIST_HEAD(&myhead);
4 f- E8 L& g& }- o$ ` - . c4 k) @1 G1 L9 R; n
- node1.type = I2C_TYPE;
4 s1 |' V/ {4 ~ - strcpy(node1.name,"yikoulinux");. Z$ a0 l X, Y5 b7 ]: c7 S
- a. ^' Z4 D: F) X" k/ B
- node2.type = I2C_TYPE;
3 H, S" C+ O# E) J) {& b' r9 Y* q - strcpy(node2.name,"yikoupeng");( J* _$ c# e( b
$ A9 {, A0 K' \6 b- ~! B- list_add(&node1.list,&myhead);
" D6 Z7 a5 z1 S- l% u( w - list_add(&node2.list,&myhead);& n, m6 ~5 _' ^" D/ {
8 s. g! t9 V2 ?/ H; D; u1 w* E- display_list(&myhead);
/ A, t& Z4 Q |. r; v - X& e2 y* a1 r4 q3 [
- list_del(&node1.list);9 o) _$ ?* r2 J9 S, ?. V! _
- 4 f( y: u* Y# V/ x. N
- display_list(&myhead);. C' X- L. C1 N! M
- return 0;. t: [2 u$ V& q) z* ]
- }
复制代码
3 Z, h% u7 B8 h& b# Z* |0 L# S) s- z3 L8 h$ a: |- B2 G' h
运行结果 实例2 如何在一个链表上管理不同类型的节点 功能描述:本实例主要实现在同一个链表上管理两个不同类型的节点,实现增删改查的操作。# L6 Y. B- l4 | V# a, p
结构体定义一个链表要想区分节点的不同类型,那么节点中必须要有信息能够区分该节点类型,为了方便节点扩展,我们参考Linux内核,定义一个统一类型的结构体: - 2 z# Y2 B6 x" {$ y$ r
- struct device! j( ?9 V' {, O5 H
- {
: Y% I3 x( J0 _ k - int type;
# m) N, k& v9 A) M' U6 l* } - char name[MAX_NAME_LEN];7 }3 I0 Q, B4 W
- struct list_head list;5 A7 C( X2 _% w- T+ Q2 C3 e7 H& I
- };
复制代码
+ j" p! a! a& ]; R+ T" J
其中成员type表示该节点的类型:
: |$ v' d! d4 y" [- #defineI2C_TYPE 1 / x8 [& S" b6 _, E2 ~
- #define SPI_TYPE 2
复制代码 ( [8 G" d/ O7 F9 I" I* P
有了该结构体,我们要定义其他类型的结构体只需要包含该结构体即可,这个思想有点像面向对象语言的基类,后续派生出新的属性叫子类,说到这,一口君又忍不住想挖个坑,写一篇如何用C语言实现面向对象思想的继承、多态、interface。1 T- G" W, X. j1 c: A
下面我们定义2种类型的结构体: i2c这种类型设备的专用结构体:
* t8 A5 z; `, ~) w5 _! r- struct i2c_node5 t# ~1 d6 r6 N3 |" I
- {, U7 \7 n7 @- x2 i0 w ?
- int data;. G/ {: M4 B) N8 } V8 a3 r5 x
- unsigned int reg;
0 k- M" w' [/ C1 `! ^. }. E - struct device dev;9 Z5 ~: n. i m& e' t" {" i8 S
- };
复制代码 4 [5 {2 l0 i3 M" X) J/ _) H2 `
spi这种类型设备的专用结构体:
; L9 g& W% \& }: o- F, p/ L) G! a- struct spi_node
+ W' s0 A4 I# h! j - {
' I [+ c% v/ b - unsigned int reg;/ H3 \) [0 {0 `5 X) u
- struct device dev;- E! N& v6 L3 b
- };
复制代码
0 [5 t: K* w! L% N7 ^) r" Z6 B我特意让两个结构体大小类型不一致。 结构类型6 L0 B6 X& d1 |' [! Q
链表头结点定义5 ]9 I; j( D( |9 v' Y: J
- structlist_head device_list;- ~0 T; Z! O1 g7 U3 C
根据之前我们讲解的思想,这个链表链接起来后,应该是以下这种结构:2 h$ v3 d+ a: D# l3 X
# y, {% v0 W0 E. R8 i( q% p
节点的插入我们定义的节点要插入链表仍然是要依赖list_add(),既然我们定义了struct device这个结构体,那么我们完全可以参考linux内核,针对不同的节点封装函数,要注册到这个链表只需要调用该函数即可。 实现如下: 设备i2c的注册函数如下:
& {, j0 M+ E8 A- T- Y+ a' @# i- void i2c_register_device(struct device*dev)
4 c2 g2 Q2 [/ ?! U. L9 n0 p) P l- |# S - {
8 p" E0 @9 T8 n x& ` - dev.type = I2C_TYPE;
* Q2 D1 _$ _: T" O* X- I \ - strcpy(dev.name,"yikoulinux");5 {3 S+ ]/ H* y/ Q) ~3 A# H$ i* b
- list_add(&dev->list,&device_list); Z4 V5 J" I* F3 A2 a9 a5 G' M
- }
复制代码 5 o+ J: b _& R6 V: n1 \, D
设备spi的注册函数如下: - 7 p% _/ B7 V$ G1 |) G j5 O1 |
- void spi_register_device(struct device*dev)6 R$ c0 i! J5 V0 \# D* o, f' r$ N
- {
8 M* Q6 c5 d, H0 y9 ` - dev.type = SPI_TYPE;7 `8 X# q8 ^( [7 @/ a
- strcpy(dev.name,"yikoupeng");
* D9 C) ~( f' r7 f2 z: n - list_add(&dev->list,&device_list);
8 i! n6 F. I4 k! V - }
复制代码
1 W, _" o" R' e# Q. T/ C0 J2 C* ~% [4 h% g
我们可以看到注册函数功能是填充了struct device 的type和name成员,然后再调用list_add()注册到链表中。这个思想很重要,因为Linux内核中许许多多的设备节点也是这样添加到其他的链表中的。要想让自己的C语言编程能力得到质的提升,一定要多读内核代码,即使看不懂也要坚持看,古人有云:代码读百遍其义自见。 节点的删除同理,节点的删除,我们也统一封装成函数,同样只传递参数device即可: - : m: x# `; Q9 i# ?2 U6 _3 X; j7 W$ _) U& R
- void i2c_unregister_device(struct device *device)! I! e% d. e6 b$ b
- {
) e: ^+ v/ [- v7 x1 o7 A - // struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);# U8 O/ d- W5 E5 I# {* w: M; a4 r
- list_del(&device->list);, D& _: C5 W1 A! P
- }
; r! O# _0 d4 [ - void spi_unregister_device(struct device *device)! R- b3 U- L% s' X7 B
- {2 r- I9 B$ x3 u7 d3 A/ \
- // struct spi_node *spi_device=container_of(dev, struct spi_node, dev);
: \9 U V# p$ \3 G - list_del(&device->list);" p C, k2 e, Z5 [. r% W' I
- }
复制代码1 |8 r9 C4 X/ M/ H' o
在函数中,可以用container_of提取出了设备节点的首地址,实际使用中可以根据设备的不同释放不同的资源。 宿主结构的遍历节点的遍历,在这里我们通过设备链表device_list开始遍历,假设该节点名是node,通过list_for_each()可以得到node->dev->list的地址,然后利用container_of 可以得到node->dev、node的地址。
) i( Z* V" c, k- q; C- void display_list(struct list_head *list_head)
, j, w! ]# U; R$ E - {
; Q9 F1 @2 v }' P3 W9 v - int i=0;; b+ E5 u- ]- v4 u" O9 x
- struct list_head *p;
# l% j& d( Y0 Q4 l6 E5 ]4 K - struct device *entry;
( h* t7 ]" }! ?$ n - " { K4 c7 d, A/ o( G
- printf("-------list---------\n");
* S5 b( o: e V* n& ^: ^ - list_for_each(p,list_head)5 ]+ n j! e4 w+ z8 q3 o* T' Z
- {
& G; {: U: x$ |8 `; x' T: h. K - printf("node[%d]\n",i++);5 ^: O7 a- V8 P1 D4 o
- entry=list_entry(p,struct device,list); L- |4 k) b W. J$ ~. ]" w" v
8 `7 Y$ w: J O( ?9 C- switch(entry->type)
5 \/ I# i( L) Z; A5 R1 n' R - {
2 q" A! U+ P4 k9 u. ]; X - case I2C_TYPE:) O7 u7 W* m% u9 [$ D9 n
- display_i2c_device(entry);
& p* ~. ]- Q) O( X+ i - break;4 P. b# r$ G: d$ R5 V( E3 \
- case SPI_TYPE:8 P" r: k) d& M
- display_spi_device(entry);
- l; d8 c7 f9 n4 _# T1 d" { - break;
+ V& | Q4 Z1 D- \0 o1 [ - default:' y8 S% b0 s7 u
- printf("unknown device type!\n");/ M4 M3 {! F# |3 w9 U9 \
- break;- O, H4 q/ `5 x) `( `& z! ?0 F
- }
# N/ M" b8 f* f) T3 U - display_device(entry);% J& _( R# j" | A0 K, E* [
- }
7 }/ l: B$ C, B/ m$ ` - printf("-------end----------\n");, ]% `0 E2 k, B' M3 V9 c6 ]& C" N
- }
复制代码 + g. _: R4 a# Z4 S
由以上代码可知,利用内核链表的统一接口,找个每一个节点的list成员,然后再利用container_of 得到我们定义的标准结构体struct device,进而解析出节点的类型,调用对应节点显示函数,这个地方其实还可以优化,就是我们可以在struct device中添加一个函数指针,在xxx_unregister_device()函数中可以将该函数指针直接注册进来,那么此处代码会更精简高效一些。如果在做项目的过程中,写出这种面向对象思想的代码,那么你的地址是肯定不一样的。读者有兴趣可以自己尝试一下。 - ( \$ x! z4 @) N2 E7 [9 j6 I3 f3 _* e; C
- void display_i2c_device(struct device *device)3 h# H: E) x4 l6 l* W$ g7 A
- {8 n* Q6 G/ S1 h- L! ?5 A/ r$ J
- struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);
7 ]3 A$ m5 ~; w. N! S# O, t, [ - printf("\t i2c_device->data: %d\n",i2c_device->data);. D" H) K2 ?4 T
- printf("\t i2c_device->reg: %#x\n",i2c_device->reg);
# h+ ~5 Y% s2 c2 J2 ~/ g* ?+ w - }
复制代码
# U- a. T% v' |5 V* \( V, T- void display_spi_device(struct device *device)
. j9 x' p, g5 D - {( g1 X6 V, @9 a, j7 f/ k
- struct spi_node *spi_device=container_of(device, struct spi_node, dev);8 x* q4 f9 q# d& |$ H5 M0 [
- printf("\t spi_device->reg: %#x\n",spi_device->reg);
( z/ G7 j5 I; x2 u$ b4 q - }
复制代码
9 h7 Q6 n! Q- S- g% f上述代码提取出来宿主节点的信息。 实例代码- 1 c2 H& N8 y5 K2 L! W* L' P8 @
- #include "list.h"
- V' a/ P" s! H6 x+ w3 ?& x% g$ A - #include <stdio.h>
5 G* l: B2 g4 E2 \; A$ c5 J - #include <string.h>
6 Y6 {; w [1 M; G$ I; G/ B! ? - 8 H7 Q/ G9 n) U
- #define MAX_NAME_LEN 326 D) c5 H! T' o6 O) f L
- #define MAX_ID_LEN 10( x6 j% f/ W) X, u3 @, A
- ( ]$ z5 N1 f8 E
- struct list_head device_list;3 F6 |$ B7 { X# i* [4 T$ D
1 G5 `1 ?2 q6 m5 w' Q8 m- #define I2C_TYPE 1$ B, z8 d6 _0 r/ T
- #define SPI_TYPE 2
p0 J. ^: R, s- n. o3 T6 r9 Z - 4 E8 E9 C2 G( N& T: P
- char *dev_name[]={; n" F2 D l* H& X
- "none",5 x. y" c1 f2 Z B& @
- "I2C",+ P& ?. R6 l2 C, w( O; U2 p4 c2 |
- "SPI"
5 I) t1 J) @5 E - };5 I% N6 Z* ^; }2 a: g6 ~) l
3 B9 w( [5 V, o$ K; Z$ y- struct device0 }& f) X4 d- A7 W9 Z
- {! Z$ A+ c; P- {1 N- T
- int type;
, Z9 g8 C8 }( g - char name[MAX_NAME_LEN];
& N* y L2 u, u8 K8 g) _: s3 F - struct list_head list;
$ Z4 A1 U; u1 L6 N; E* b0 W, n - };) y \) d! v- F# _! d: U6 X
$ g0 Q; T- M* T+ ^% L M3 }- struct i2c_node6 n7 m8 l2 A, x: c8 z; i) {
- {
& E/ C9 U( N1 A4 T4 A1 V - int data;
5 {+ U5 q. ^! D: V' P6 }" l - unsigned int reg;
1 m0 b1 k. D' I1 v$ g- I - struct device dev;8 ^8 i7 c$ l- q: _
- };1 j) f6 I [, Y9 t
- struct spi_node
( a0 v) W% b: H, U, E - { # ^: A% l. m G+ g6 C2 }
- unsigned int reg;" [5 N4 P+ ^$ L5 E* U
- struct device dev;
9 ?3 v8 f6 e# C - };2 a0 ~7 o3 f% {! t5 R, f( y: P$ C* ~9 J
- void display_i2c_device(struct device *device)8 M2 S, ?* k m3 T' d2 @
- {! A4 f% Z* b, ~% g4 H* h) a5 m
- struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);
3 n# r" h8 j8 m2 r - 5 B. Y7 q% D w6 B
- printf("\t i2c_device->data: %d\n",i2c_device->data);
- D9 p! m2 E0 D0 T- \3 K$ g - printf("\t i2c_device->reg: %#x\n",i2c_device->reg);
& K7 [: H' Y( x" m! E - }5 ?9 D( K/ C- [6 _
- void display_spi_device(struct device *device)
5 t+ \6 y! g3 ^" y7 ~ - {
" y4 M& W1 K( O. | - struct spi_node *spi_device=container_of(device, struct spi_node, dev);
* b/ y( j6 X5 Q: [& V$ `/ c - 5 f. {6 p, H, F5 p
- printf("\t spi_device->reg: %#x\n",spi_device->reg);4 J1 o% d# T! j9 e/ x
- }
& s8 b. k) N3 h5 e- p% E+ ^/ r* P - void display_device(struct device *device)
5 ]( ~* C6 N" k" K% E - {
* d# G4 w* D6 c# H' g9 Y) B - printf("\t dev.type: %d\n",device->type);
: N# f" g2 |. }( t' @) G - printf("\t dev.type: %s\n",dev_name[device->type]);
- ~& J* T' w7 d+ d( ~ - printf("\t dev.name: %s\n",device->name); q3 S) D& w! Y V O: T
- }
: k9 b* r/ G* k* j - void display_list(struct list_head *list_head)- y% `' M# n4 ]
- {- R7 |' M# n6 { |/ w y3 g) n
- int i=0;, m7 k( {7 q+ R2 o: w& \
- struct list_head *p;
) E" U/ ]# y" j$ [0 _/ K9 B8 l! W - struct device *entry;
' M/ O1 B4 I( ?% v$ I4 M: o - 0 c# A* E( f5 t: X) ~
- printf("-------list---------\n");
, N- X8 a0 A3 U3 Y6 U - list_for_each(p,list_head)
- W1 K( Z* |% q, E2 W - {* @4 N" h' ]% Q& K9 H
- printf("node[%d]\n",i++);4 K, u& S& h7 v2 e ]
- entry=list_entry(p,struct device,list);
7 @7 _; m0 k7 k, W( ]* R7 R/ D9 ]3 u - % P$ k4 e8 @$ C7 M
- switch(entry->type)+ L& T" L6 g2 d6 K. ^8 k- J/ k- M2 B
- {
" m' ~7 N- p& E6 d9 w! j/ D O) U - case I2C_TYPE:6 V2 m7 M0 ^ I% s0 U% v' o1 B
- display_i2c_device(entry);
# D1 I M( E% u& g - break;
0 A. z) [+ p5 l4 [ - case SPI_TYPE:
2 G% t& b1 V2 t2 l6 \( \ - display_spi_device(entry);2 X2 o, w! J8 m o
- break;% x9 @' F. c- W& }" l" D4 E4 d
- default:& J& B9 f% k5 E
- printf("unknown device type!\n");2 }6 A. S H4 e6 N; {
- break;
2 E9 ^, L" S$ X- h C - }
9 f! v3 p7 [6 \3 x/ i - display_device(entry);
0 @7 T. n' b+ R+ U - }$ s$ y1 I! L' V }5 f
- printf("-------end----------\n");
2 z- G" n7 K. H/ M3 m4 q- W+ x - }
2 A- z2 w `9 W: j) l - void i2c_register_device(struct device*dev)
" f% d; Z0 o0 n$ a% ~/ ^ - {! O( ?$ W, m8 w2 J5 u& x$ N
- struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);
5 y6 g$ J) y: ^
9 X7 V0 W2 g4 P5 A6 X3 e% O L- i2c_device->dev.type = I2C_TYPE;
! _4 [2 D" a9 h% ^ - strcpy(i2c_device->dev.name,"yikoulinux");
7 p9 v, p" B( i/ t+ W
+ j% `" L) C" R$ h; @; |/ c- list_add(&dev->list,&device_list);
& g- |7 \2 [( C# W - }
1 T) D) _% L1 O& X - void spi_register_device(struct device*dev)
B- M6 C7 q; l* t. y) f7 C - {5 T% R0 `$ k0 P& j
- struct spi_node *spi_device=container_of(dev, struct spi_node, dev);9 J1 x. d# Q, j, T
- " j5 t) I2 ]$ T K- z/ P+ T; j
- spi_device->dev.type = SPI_TYPE;2 b2 Z% X3 ]2 T
- strcpy(spi_device->dev.name,"yikoupeng");
3 [# G/ e: e m( v6 q - : m! F F d4 _" E
- list_add(&dev->list,&device_list);
' Y: F2 [1 l. k( _9 B d+ C# g7 J8 ^, {4 U - }
$ _- b* Z, e1 c: t, w - void i2c_unregister_device(struct device *device)
* k; h6 d5 Y8 |" o - {
/ I. u/ d5 w0 p- j" J' E+ H - struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);& I8 s0 b5 i5 P: D! ^
- ' z' b3 h/ {" n# @
- list_del(&device->list);# ~" k1 M- I- X# K, T
- }
7 X# u3 l. d) a - void spi_unregister_device(struct device *device). t9 X+ q3 [' a
- {
8 Q1 n H0 T% F) K; m# \7 u; I - struct spi_node *spi_device=container_of(dev, struct spi_node, dev);
- M$ Y2 m. {( t4 w Q - ( X( \- `- ?4 L( o' w2 L7 C' e
- list_del(&device->list);% r9 v* X. v, o4 V
- }9 u, E' M5 ]! C; j- S3 m6 S
- int main(void)
: o5 }3 u9 Q- @1 z% { - {
& s: C$ s- E# z% x - 1 t1 _2 B3 n, D( i; X I
- struct i2c_node dev1;& T0 d: O2 b% t+ Y- J2 c2 h J
- struct spi_node dev2;4 H5 k# R/ w+ q8 d7 z! P& }+ O
- ' `7 w+ D6 ~4 _ Q' m, d" J
- INIT_LIST_HEAD(&device_list);* n7 G& A0 L! R) Y: }
- dev1.data = 1;
+ S c Z) G% O/ ?! t - dev1.reg = 0x40009000;# B! i* I' `6 A6 _
- i2c_register_device(&dev1.dev);; v9 L6 w2 u; q* c6 [
- dev2.reg = 0x40008000;" O6 g- ^, J7 p8 p c; r
- spi_register_device(&dev2.dev);
5 f% o$ @. u$ Q x- Z - display_list(&device_list);
3 M1 j' f, C7 w2 ] - unregister_device(&dev1.dev);5 G6 x! K4 N7 W% v; f2 v8 A
- display_list(&device_list);
( O" k& z* n1 z3 d: \ - return 0;2 b& l- p- z2 }' S5 O
- }
复制代码代码主要功能: 117-118 :定义两个不同类型的节点dev1,dev2;
?1 ?8 b7 X2 e 120 :初始化设备链表; 121-122、124:初始化节点数据; 123/125 :向链表device_list注册这两个节点; 126 :显示该链表; 127 :删除节点dev1; 128 :显示该链表。
! |# i' I- t7 b+ l( F+ V ( R/ k0 P4 h Z5 ?
程序运行截图 , b0 \- n# y! P( }/ k
读者可以试试如何管理更多类型的节点。 2 K$ _3 S% K- ?* C9 t* a( q: i
实例3 实现节点在两个链表上自由移动 功能描述:初始化两个链表,实现两个链表上节点的插入和移动。每个节点维护大量的临时内存数据。 节点创建节点结构体创建如下:
) l8 O* e" w0 V/ I4 S1 E, D4 |- struct mylist{
# Z" T/ ?* A2 X7 Q' i, Y2 c& e7 Y - int number;
, ?3 |; i: v% h0 | - char type;
0 u9 ]; G6 ~ W) r - char *pmem; //内存存放地址,需要malloc! K& O8 E- p) {# n" B2 n7 S% Y+ D
- struct list_head list;
8 G; p- H$ b( ~/ h - };
复制代码 , J* f% d6 G! R* g1 z8 ^# e1 D
需要注意成员pmem,因为要维护大量的内存,我们最好不要直定义个很大的数组,因为定义的变量位于栈中,而一般的系统给栈的空间是有限的,如果定义的变量占用空间太大,会导致栈溢出,一口君曾经就遇到过这个bug。 链表定义和初始化链表定义如下:
u( G* c+ p9 f. n# [+ G7 Y6 N- structlist_head active_head;
. b r. B% I+ m$ E8 e9 k - struct list_head free_head;
复制代码 初始化
. w/ F k$ k! L" o2 R1 x5 h- INIT_LIST_HEAD(&free_head);2 h q5 L8 r, i! [ X' O
- INIT_LIST_HEAD(&active_head);
复制代码 " Y4 H# s- {6 d* W, ~
这两个链表如下:
8 a; m0 K. [3 @$ Q3 g
关于节点,因为该实例是从实际项目中剥离出来,节点启示是起到一个缓冲去的作用,数量不是无限的,所以在此我们默认最多10个节点。 我们不再动态创建节点,而是先全局创建指针数组,存放这10个节点的地址,然后将这10个节点插入到对应的队列中。 数组定义: - structmylist*list_array[BUFFER_NUM];, ^1 s- O" ^. o& ]2 d E
这个数组只用于存放指针,所以定义之后实际情况如下: 3 O# s' r) `7 I9 ~8 U, g1 T* @
2 l7 b) s \' E6 S- Q9 k
" A% ], V/ k# r% e! K- ~
初始化这个数组对应的节点: - 7 W k3 L+ |2 c9 h5 M% i
- static ssize_t buffer_ring_init()
) D d. W2 E' _* q9 F7 b - {
! u, @8 x3 E, V2 F O: x; | - int i=0;' t/ Y' @4 s+ `$ {" a- w
- for(i=0;i<BUFFER_NUM;i++){- z) Q- U0 R$ q5 @# p+ Z
- list_array[i]=malloc(sizeof(struct mylist));, f5 A, k6 q' V8 t! W
- INIT_LIST_HEAD(&list_array[i]->list);8 m1 h' T& Y& }) c
- list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);
6 u( X& O" B9 N! A - }
4 b; e4 X! T ]# L6 s' Q - return 0;
# i# S8 _" b5 x w - }
复制代码
* v' B4 j0 y* c5:为下标为i的节点分配实际大小为sizeof(structmylist)的内存 6:初始化该节点的链表 7:为pmem成员从堆中分配一块内存 初始化完毕,链表实际情况如下: 节点插入- $ Q; D" `8 ]; P7 M/ t: S J
- static ssize_t insert_free_list_all()
* E' V1 K$ D! h; M - {* T) Z: g7 Q6 B# Q
- int i=0;
( E+ ?' I9 v+ J5 A
0 O4 J* w4 {$ u, u$ U! K- for(i=0;i<BUFFER_NUM;i++){
& i. i- G8 `# C7 t5 ^) o - list_add(&list_array[i]->list,&free_head);3 D3 q; f2 s# F
- }. _$ D$ A5 V' Q/ H9 S9 u* ~
- return 0;& u, o$ m+ z+ W) y
- }
复制代码 9 }. `3 U W; M4 P' z+ }
& Y9 [. T! ?9 q6 D. k8 ^% ?
8:用头插法将所有节点插入到free_head链表中 所有节点全部插入free链表后,结构图如下: - T7 C% g- T6 i
9 c3 s+ ` j$ C, Q: T" _) f
遍历链表虽然可以通过数组遍历链表,但是实际在操作过程中,在链表中各个节点的位置是错乱的。所以最好从借助list节点来查找各个节点。 - how_list(&free_head);3 C+ W8 g6 L6 [) h A! {5 O8 y
- show_list(&active_head);
复制代码 8 w) u2 S, N8 c- n7 P
代码实现如下: - " Z a M. e; F1 |% \5 @) U. c
- void show_list(struct list_head *list_head)
6 n% q( s6 h0 B" N4 l6 } - {
& n+ O v9 e, w" K; F1 W - int i=0;
v* W4 ?0 B5 c1 q - struct mylist*entry,*tmp;! j+ s. d) e, I
- //判断节点是否为空* s1 n2 {& m9 ~9 d
- if(list_empty(list_head)==true)
+ G6 q0 R8 Q# `2 \. @2 b x - {
. Y. E2 o; I' s7 U* P9 w - return;
% ]7 p) E; o; C% c) P - }
% M, Z3 x2 D2 X- N2 r: s - list_for_each_entry_safe(entry,tmp,list_head,list)
, I) S% j+ _- N - {. d" M/ T" ~) R0 l2 S" A' j
- printf("[%d]=%d\t",i++,entry->number);
1 U1 Y1 f" A4 C5 x8 d - if(i%4==0)( B" \; |' f9 d6 ?7 T% q( }( a
- {
, @, U0 q, S8 n9 c" f/ p+ P - printf("\n");; Y5 R* A1 ]( O+ f! D; y. M
- }
1 I5 g/ |% n& n - } 7 e2 h$ D+ r- w; F- E. P
- }
复制代码
) c' Y V; j7 j: R4 B) L: n
P8 |4 Y( P. D0 x) h 节点移动 将节点从active_head链表移动到free_head链表,有点像生产者消费者模型中的消费者,吃掉资源后,就要把这个节点放置到空闲链表,让生产者能够继续生产数据,所以这两个函数我起名eat、spit,意为吃掉和吐,希望你们不要觉得很怪异。
" N" d$ r' M/ S- int eat_node()
5 m5 p2 B; e; ^0 `- v7 f8 a - {
7 ~( A: Y! U- x& H) e7 R - struct mylist*entry=NULL;
( E5 G& A6 D& I* K1 [& U - 0 O$ ?' }" I) h7 \
- if(list_empty(&active_head)==true)
! n1 X7 S# |5 r% X - {
& C/ T6 @5 B$ L2 J# P" O$ r- [ - printf("list active_head is empty!-----------\n");. a0 \# P6 `6 M6 C
- }
: ?& p8 }' g l$ x" e" D - entry=list_first_entry(&active_head,struct mylist,list);
+ \! e2 M1 @- s/ Z! ~6 F W) ` - printf("\t eat node=%d\n",entry->number);7 }7 I+ N7 L* a* d' t9 [" _
- list_move_tail(&entry->list,&free_head);
7 B/ I `) c* T! c6 l. o8 v - }
复制代码 7 M1 M1 A- h# R ~6 W! L! D
6 Z# a% I5 a$ Z, p* P( }2 N: V# M
节点移动的思路是: 1. 利用list_empty判断该链表是否为空 2. 利用list_first_entry从active_head链表中查找到一个节点,并用指针entry指向该节点 3. 利用list_move_tail将该节点移入到free_head链表,注意此处不能用list_add,因为这个节点我要从原链表把他删除掉,然后插入到新链表。 将节点从free_head链表移动到active_head链表。 - 0 n3 _/ \( a% V
- spit_node()
. q8 r/ Q) o( e4 X2 Q. C% o# D - {
8 c; n2 m; P5 q8 C, A8 a - struct mylist*entry=NULL;
6 l2 m- y0 a( R: A3 V - ; l" x% @( I# {$ b. ]
- if(list_empty(&free_head)==true)
, q5 m5 l ]$ |$ w - {0 p' }- ^' r& o2 k& _
- printf("list free_head is empty!-----------\n"); j" Y) v/ E; N' i! l1 t
- }3 i% t. u* u# u" c" _, H
- entry=list_first_entry(&free_head,struct mylist,list);
; U3 x5 [+ A2 |# u% P9 H - printf("\t spit node=%d\n",entry->number);* E3 a! M0 m7 E& c
- list_move_tail(&entry->list,&active_head);1 u- T% O( o# a# A" g
- }
复制代码 W" Q% l! Y/ x6 S7 M3 I) S8 \, [
大部分功能讲解完了,下面我们贴下完整代码。 代码实例# k P% T* O, ^6 @9 Z, h! p! P8 c
/ _6 E$ R7 `- J2 P9 i- #include <stdint.h>
$ h! n: e# g# ]. \1 H1 y - #include <stdio.h>; i3 f. }' e) Z) }/ z, e
- #include <stdlib.h>7 `6 N8 p! |- |- h0 w3 z8 h
- #include <unistd.h>
# \. E/ I& P/ {; h- g& S - #include <byteswap.h>8 }; D+ M. M7 \. l
- #include <string.h>
) T8 Q9 M4 I# [, m - #include <errno.h>0 `, g1 ^" H7 Z( u" S
- #include <signal.h>7 [4 |4 ]' y+ h2 M4 w* T( p2 \
- #include <fcntl.h>
6 M3 s8 i; f- ]# K/ t$ W4 }0 T - #include <ctype.h>, J1 w5 n' o! b8 @8 E r, c; t
- #include <termios.h>5 L' c" ?% `" f# F! D, L9 d! w P$ W
- #include <sys/types.h>- a" T8 F8 M L( e+ A2 c
- #include <sys/mman.h>
+ C' n! I: y+ @9 |$ r - #include <sys/ioctl.h>
; V5 W$ f! R/ f6 w - #include "list.h" // linux-3.14/scripts/kconfig/list.h
/ X6 ? w0 V- @: i. J- M) B - $ T ?4 ?& q" A
- #undef NULL
6 N+ I( e8 }9 i - #define NULL ((void *)0)- S/ {; H8 {& |- R3 Q& c
- , a- z" C' @, q' `1 D
- enum {+ q! E( E& S; o% d5 \
- false = 0,! m, l3 K4 u5 ~$ k. g
- true = 1: j0 ^, w0 \& M# P, V5 j2 @5 O
- };+ _& V, {1 e- l, [7 @" H0 X
6 R6 q1 W* s. d- #define DATA_TYPE 0x14
- E& w1 ^9 b$ w* h( Q" J) j% o$ ~ - #define SIG_TYPE 0x15# H6 ]) \" v( I/ \ L6 m
- ' v* `. h1 v+ t4 p
- struct mylist{
/ g6 K: G# A, B0 }- @ - int number;' B- e3 j# A! Z% m* x" G1 n* C
- char type;
9 z, r% I7 C% W8 `& U - char *pmem;
6 m4 e2 a U4 G9 s: X# l - struct list_head list;+ F0 {: {3 K, V2 U$ B
- };
/ G, @- h4 S2 O; e; S1 G6 H" r - #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n",__LINE_,__FILE__,errno,strerror(errno));exit(1);}while(0)
* B) J- N9 j/ m: C4 ]) [ - struct list_head active_head;$ m: j0 [7 {1 H/ `1 l9 S
- struct list_head free_head;* d. @$ J; q% x4 Z6 `
- #define BUFFER_NUM 10
% y" z$ Q: }0 U9 m4 m5 h - #define DATA_BUFFER_SIZE 512
4 v/ b1 J, v- r9 ^+ w$ L$ M5 N - struct mylist*list_array[BUFFER_NUM];
1 H% t0 R, Y/ |7 ] - 9 u' b1 n* w8 s
- 8 ]: c" K! j- C* s( u
- int born_number(int number)
k/ D4 W: Y) N# @3 W3 o8 r6 m4 `& E - {- z; [7 W' E+ A7 M
- struct mylist *entry=NULL;
Q5 _- I' k* p
' m! X z* O0 o# V- if(list_empty(&free_head)==true)
1 Z; z% a& c0 h# t. y) r - {0 c3 t0 A5 j4 j5 V8 M
- printf("list free_head is empty!----------------\n");
7 Y$ l4 z" ?9 e& A$ K3 r% e* i - }
+ _ h1 j8 q$ k% h* U1 ? - entry = list_first_entry(&free_head,struct mylist,list);
1 g" g9 A. h% I: { - entry->type = DATA_TYPE;: d N: ^. ]$ z% _1 B7 }4 T
- entry->number=number;
- h/ \" C0 E0 S - list_move_tail(&entry->list,&active_head);
4 I$ {- A2 A* b - }& ~1 L3 M/ F- @6 E2 i2 W
7 x+ v l5 Q& o" @- int eat_node()' G. G4 X% J% ?: A; W w) |- c
- {/ {; O. |# F& S
- struct mylist*entry=NULL;- b% `/ U0 s+ ~/ b! ^
- : Y6 s4 T+ r1 u0 B
- if(list_empty(&active_head)==true)
0 ]) O @& x% C2 A. Z - {7 }6 f1 E" P, `' Z$ I0 L7 Q5 j# x- {
- printf("list active_head is empty!-----------\n");# Q+ {0 I9 i! I
- }
- t( ^( A. I; z2 \3 t) v% C - entry=list_first_entry(&active_head,struct mylist,list);
% [& M; E' B6 g8 `: H3 M: v0 U - printf("\t eat node=%d\n",entry->number);
. q: Z' @* \5 t/ [/ ^! E - list_move_tail(&entry->list,&free_head);. g) ?0 u* l8 S) e3 k) K: ^1 g
- }
Y6 ^) R3 G4 l S% D2 O7 H - spit_node()
. D' R5 x7 S( n1 O2 K' t/ ? Z7 L& H! S - {9 E" G0 p; E( T
- struct mylist*entry=NULL;
4 n5 {( C# C! w
. W% i6 _) `/ H/ V: S- if(list_empty(&free_head)==true)' f+ P* z/ W' O9 R m: n
- {$ N$ y" P% {! G1 y. e) u! M
- printf("list free_head is empty!-----------\n");
) H0 K/ k1 `( z9 k1 Q) Y - }9 ?- V$ T! C, j3 A" P3 E9 u! W
- entry=list_first_entry(&free_head,struct mylist,list);
4 K1 M# \( l$ Y - printf("\t spit node=%d\n",entry->number);7 R. [; X: F0 F* G+ {9 Y
- list_move_tail(&entry->list,&active_head);
& D( R6 \7 N7 n8 X - }) A, D+ W. q" x: x8 t# H$ s
) y" h4 g! e$ d# Y* V( H: ] w- void show_list(struct list_head *list_head)
. Y' F- ]. B, P9 X4 u2 L - {& x8 l+ C: C8 r7 Y
- int i=0;- p- z& V7 g* i# o$ f
- struct mylist*entry,*tmp;9 o5 i4 A6 U5 O
) M, a I8 V5 ]- if(list_empty(list_head)==true)
% v; ~2 r/ l( p$ D: g- G! k( V - {- M- I5 b! Q6 H" \- ]
- return;
' u/ ?2 y7 n' u5 T, A3 }, u - }
+ p$ M* I( }* f% s' B* b' b# ~. m - list_for_each_entry_safe(entry,tmp,list_head,list)
; n5 L0 J6 a+ o7 q. T7 }% A - {! Y7 K1 P. D" |; }$ K0 e& x2 D
- printf("[%d]=%d\t",i++,entry->number);
, h+ T- y# [ K& @4 L - if(i%4==0)' F3 m, c8 i6 L
- {9 z! a1 f3 H% J* S: b
- printf("\n");& X0 E, q2 G1 m! B) X5 T
- }
+ j5 W! z! J; y4 J9 m* `7 T - }
$ T5 @1 ^& ~! H- {# U& _ - }
: l9 _* [' ]' U( V" D8 h( g - int list_num(struct list_head *list_head)8 j) I4 _7 X& l9 Z! q' e' R
- {% V1 w9 a' m9 h' _
- int i=0;# E' W2 F2 R$ u; `& y/ g0 B
- struct mylist *entry,*tmp;' v3 Z% q- T- i% w; z0 P) p; l- e
- // printf("----------show free list-------------\n");/ {' H( Z2 W4 [% Q3 A8 g
- list_for_each_entry_safe(entry,tmp,list_head,list)% g8 U. z K G. `3 L
- {5 h4 t- O1 |; Q6 [' G! t7 k
- i++;
8 z1 N2 u* y$ }. G9 R - }
* y0 f% O2 }, X5 B! `. k$ J3 N2 Z# K - return i;) w% f+ i/ u6 L: O; w- J, U
- }! e. O/ I1 F9 p0 B# B) a n8 L
- static ssize_t buffer_ring_init()# }- P( S/ C9 W
- {! y3 D S$ K7 L8 D! `
- int i=0;
$ r$ i7 A! s# o$ g. G, e8 e3 h
2 H$ H0 A8 a( d- for(i=0;i<BUFFER_NUM;i++){: i, Q2 e, Y8 S1 S; ]$ u
- list_array[i]=malloc(sizeof(struct mylist));) N- p9 n. F7 v( Q. Z: r, W
- INIT_LIST_HEAD(&list_array[i]->list);$ p, {/ `9 ? j- Y* r9 t
- list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);% M7 [: o7 k6 ^$ f
- list_add_tail(&list_array[i]->list,&free_head);" K/ n) Z7 V+ p' }) X2 @' d: j
- }
$ X' s5 g3 U: |; n( j; p* y - return 0;* _; }: Z+ t4 }# C+ }* r
- }
+ `! e) b1 h1 k7 }& T7 W" a - static ssize_t insert_free_list_all()! W% g. o) ~' n+ W
- {8 s5 R: T! [$ L+ e) n, y; F
- int i=0;- ?# M/ S' K5 [8 ^; ?1 W
, v, S% d: k, u% Y. W; Z4 d- for(i=0;i<BUFFER_NUM;i++){% J9 h2 C, Z! v5 `
- list_add_tail(&list_array[i]->list,&free_head);) y5 b( v* q. w& y+ b
- }+ y& }8 }) D! q
- return 0;
" X' C1 y( @$ w* h) V0 _ - }& C9 W& C# B( y9 s
- static ssize_t buffer_ring_free()( X- m: Q, b z; {) |; S
- {: e. S B" Y2 A. b( |" b2 H/ k" s) r
- int buffer_count=0;
/ _; Z3 g( b( Y. ]% i- H) z8 F - struct mylist*entry=NULL;
9 P; _: l6 d! l6 U y - for(;buffer_count<BUFFER_NUM;buffer_count++)% ?' O: a, X# a
- {
2 j B. I2 Q& v - free(list_array[buffer_count]->pmem);
Z5 ^( v( R* p0 j - free(list_array[buffer_count]);
. H, c1 z1 ^7 h2 {& d; p2 L3 I - }
, l& U' w7 g- A8 t - return 0;" Q$ c' I" H9 L2 X _# [! X: w
- }7 G! ^2 O5 l& y; K. ]; U1 C
; `: C6 M+ }) v4 G. I( D# h1 V- int main(int argc,char**argv)
; c& [7 g K6 l - {& J# f7 d! X8 w! l& ]. p' I$ V8 o
- INIT_LIST_HEAD(&free_head);
3 p- I0 L7 k7 X - INIT_LIST_HEAD(&active_head);6 Q, F2 ?' q( @/ X/ ]4 X9 q0 U; _
- buffer_ring_init();
1 ?7 w; u. }. [2 c - insert_free_list_all();
1 z& ~$ f/ o! ]; b' \! R - born_number(1);
( C! j: u7 c. e9 H, N& N- ~. } - born_number(2);
! W* r0 s& D( ^; M$ j# f - born_number(3);) V6 x' Z* I; T* p
- born_number(4);! N" V9 C2 ~% \7 t1 e7 C1 S
- born_number(5);/ \. p5 I/ ?4 R a
- born_number(6);! W& U/ [5 C# f
- born_number(7);2 B; i. q, L8 o( _3 c
- born_number(8);
2 H3 [9 P; ~* `" U7 l, x0 r- z( E - born_number(9);/ ^/ z4 m0 G: a5 T4 w
- born_number(10);
6 ]! {* b% S% k) `6 `$ f
8 i5 E8 Z4 U1 Q/ p' o9 x- printf("\n----------active list[%d]------------\n",list_num(&active_head));
, D9 y7 o* W( I Z/ n" T - show_list(&active_head);% d8 _2 F8 @6 I) c: _$ {
- printf("\n--------------end-----------------\n");
2 B4 m& h# r4 Q7 P% r9 J& A
/ C, T+ B$ P, s% Z- w5 X7 A- printf("\n----------free list[%d]------------\n",list_num(&free_head));
8 m$ [% O o' X. }; a a! {" F - show_list(&free_head);2 h0 o2 P: q P- h q) h- Q3 H. N* V: Z) z
- printf("\n--------------end-----------------\n");
* W. A4 |0 p) P3 V
+ R6 ~9 C9 t3 ?( O4 z1 _- printf("\n\n active list----------> free list \n");5 ` |4 y+ Z; ~9 Z9 }0 Y, D& _# }
% I0 Q4 G) s8 [- eat_node();
7 {; l6 }) O# C4 R7 _$ K - eat_node();9 g- {% K# H! x) W6 y
- eat_node();
" Y# H1 S/ N. g S. L: Y+ N+ _, K - , {1 W! Y7 D$ V2 S$ x3 e# |1 [# t
- printf("\n----------active list[%d]------------\n",list_num(&active_head));" X, A+ R6 a2 z, P
- show_list(&active_head);
3 B$ b3 x8 f j1 k - printf("\n--------------end-----------------\n");+ K3 U7 Q! T: o" e
- 2 ~. V: {3 Z, H9 _+ L
- printf("\n----------free list[%d]------------\n",list_num(&free_head)); + b. X! t0 R. q, Y, L: m r
- show_list(&free_head);
& E( D) K" R8 D D* h% \ - printf("\n--------------end-----------------\n");0 d* z7 ]7 m/ h. z
- 0 J* i; s% {, u1 C3 |
s+ K% ~. J( t3 v7 |- printf("\n\n free list----------> active list \n");/ g e; T i* h6 u1 c9 k
. E& n+ ?& N) o7 D6 k- spit_node();6 o4 n5 C0 z6 K& G) N f( s* G: c W0 \
- spit_node();
* W+ C! }& }/ S# P' e
: j# i, k1 r g m. o: s" |( C$ l- printf("\n----------active list[%d]------------\n",list_num(&active_head));
; h s. |" v0 n( @. B - show_list(&active_head);
6 w/ A- d( K" @4 Q; E - printf("\n--------------end-----------------\n");$ J7 E# y& h: T! w& m
" k5 Z) \) s" O$ F7 |( k: ?- printf("\n----------free list[%d]------------\n",list_num(&free_head));
9 R) k1 f5 p, H- A: G3 F - show_list(&free_head);) v( K" p) I) K8 U
- printf("\n--------------end-----------------\n");0 v- z5 Q3 ]) d& `8 z2 x p6 v
- 1 X0 B* j1 P5 p3 k# z( E% f
- }
复制代码
7 A% \; d; S4 s运行结果如下: list_head短小精悍,读者可以借鉴此文实现其他功能。 0 S0 N Z& [% x5 ^+ T
8 i( V' Q$ `! A: U" O
|