你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

Linux内核中链表的结构 list_head

[复制链接]
gaosmile 发布时间:2020-9-2 22:42
  在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作list_head 的通用接口很容易实现代码的重用,有点类似于C++的继承机制(希望有机会写篇文章研究一下C语言的面向对象机制)。
首先找到list_head结构体定义,kernel/inclue/linux/types.h  如下:

  1. ' ]' q( n4 [" m% y
  2. struct list_head {
    % f. t4 L' l/ z$ `
  3.   struct list_head *next, *prev;
    ! Z( [9 }5 N% o, p* d3 |: t7 D7 H
  4. };; u) [1 O! m; J% o5 w' l
  5. #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
微信图片_20200902223445.jpg

9 A1 g! q- i2 w, g, f) t
      然后就开始围绕这个结构开始构建链表,然后插入、删除节点 ,遍历整个链表等等,其实内核已经提供好了现成的接口,接下来就让我们进入 kernel/include/linux/list.h中:
一. 创建链表
    内核提供了下面的这些接口来初始化链表:
  1. " ]5 {, q/ E4 H1 T" E
  2. #define LIST_HEAD_INIT(name) { &(name), &(name) }
      S+ Y' [9 p$ ?& [+ J. N" t( [

  3.   b5 _" l& y; d! s
  4. #define LIST_HEAD(name) \1 O) B3 p6 F1 c! T$ Q( W0 N4 H
  5.   struct list_head name = LIST_HEAD_INIT(name)5 q7 I# x7 B4 C) q* A0 J5 W2 Y* a* t. q7 k

  6. " E5 T: {" H& C1 i. o3 A4 R- `
  7. static inline void INIT_LIST_HEAD(struct list_head *list)
    ; \+ l9 @% _* M  r: ]! j( M
  8. {
    , E$ ^4 S  V5 i8 l
  9.   WRITE_ONCE(list->next, list);& Y( ~" [. a' ]3 F
  10.   list->prev = list;( C8 a, a9 X7 ]0 F, E/ T
  11. }
复制代码
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结构,等就是采用这种方法创建链表的)。为简便理解,定义如下:

  1. 3 K$ j0 I3 k. q$ _, e
  2. struct  mylist{
    " f) d8 `( F$ B. H
  3.     int type;% ]; z( |' l3 K% I. i9 D3 i
  4.     char name[MAX_NAME_LEN];& N" Z- L+ \8 _1 Q/ K( p% a
  5.     struct list_head list;8 z# j2 z% F) ~) E
  6. }
复制代码

9 ~/ A$ F7 t% w
创建链表,并初始化
  1. / H% b; [$ J( L' `( L
  2. structlist_head myhead;
    8 D' w$ [  k* q; P. b
  3. INIT_LIST_HEAD(&myhead);
复制代码
5 ~. B4 \9 [9 c. }
这样我们的链表就初始化完毕,链表头的myhead就prev 和 next指针分别指向myhead自己了,如下图:
微信图片_20200902223452.jpg
二. 添加节点
" x6 M3 x) \4 [  C! e# \$ M. }
内核已经提供了添加节点的接口了
0 k3 f' k9 ^' l
1.  list_add
    如下所示。根据注释可知,是在链表头head后方插入一个新节点new。

  1. ! {; B: ?# o# e' }9 H
  2. /**
    " Y* x# T9 F6 X1 v) A, @
  3. * list_add - add a new entry
    ( b. }0 o! ^( x5 G* \+ I0 J& \
  4. * @new: new entry to be added) B# A- ?' b* p% x  O5 N) `
  5. * @head: list head to add it after
    9 C/ F* C3 Z! S/ r
  6. *
    9 H( w7 B+ ^1 T5 T# \
  7. * Insert a new entry after the specified head.
      W5 [$ C2 a9 Z* B/ U  m( E
  8. * This is good for implementing stacks.' K' j- \- X5 b& D+ b
  9. */
    7 i  D/ @5 t* ^: Q( _
  10. static inline void list_add(struct list_head *new, struct list_head *head)9 a  u  s& n, ^
  11. {: m3 o' G0 c$ q7 n  I9 C
  12.   __list_add(new, head, head->next);
    ! D; e/ j1 @9 `6 p' Q$ {+ I
  13. }
复制代码

, X4 v! Y5 r" l5 h9 M+ E  T
list_add再调用__list_add接口

  1. 5 ~' d& x8 x1 Q4 C( x4 }0 d. h
  2. /*
    # b6 s2 r+ ?% U( O7 I+ E
  3. * Insert a new entry between two known consecutive entries.- e1 V: A* p' h- n6 J
  4. *
    ; R( M% E* R3 ~# M
  5. * This is only for internal list manipulation where we know
    8 M* x) F4 K& x; h( b# V& N
  6. * the prev/next entries already!( A0 l4 a6 w2 ?0 R+ P/ J
  7. */
    / I6 S) D, G. e# L
  8. static inline void __list_add(struct list_head *new,( ?4 q3 i9 \7 @; w3 j
  9.             struct list_head *prev,+ w, h5 \. ]9 e) n$ u
  10.             struct list_head *next)
    9 F  T: p8 T& i% V/ r, g% K! j
  11. {
    5 ]& L0 o! n% p( R4 Z
  12.   if (!__list_add_valid(new, prev, next))! z& [. a1 Q- t' ^% Q
  13.     return;  w$ a/ c) D2 u+ J9 Y
  14. ! C# P, p/ \- }  e$ Z/ V
  15.   next->prev = new;, ?1 g# }: Y+ ]
  16.   new->next = next;
    7 {, ~2 O, h7 p9 A8 k
  17.   new->prev = prev;
    9 v* R1 W4 U2 k
  18.   WRITE_ONCE(prev->next, new);
    3 v9 E6 ~" v6 t/ E
  19. }
复制代码
: A' }* B( \8 O: X1 X( g
其实就是在myhead链表头后和链表头后第一个节点之间插入一个新节点。然后这个新的节点就变成了链表头后的第一个节点了。
接着上面步骤创建1个节点然后插入到myhead之后
  1. ( k. }3 B: S) ~) o( \0 i2 o
  2. struct mylist node1;, m1 `1 R9 q9 Z
  3. node1.type = I2C_TYPE;$ z; i) ]$ {$ h! y" ]8 J
  4. strcpy(node1.name,"yikoulinux");( o$ ]$ w8 t) _3 j
  5. list_add(&node1.list,&myhead);
复制代码

, _5 j/ _: y/ w! l+ f. F
微信图片_20200902223457.png

: G/ n: n6 ~$ v' q* m
然后在创建第二个节点,同样把它插入到header_task之后
  1. 3 c- {' r& m7 \: }
  2. struct mylist node2;
    ) q! u2 {5 M& v
  3. node2.type = I2C_TYPE;
    5 _: V0 h3 |! A# ~/ a7 i8 o
  4. strcpy(node2.name,"yikoupeng");
    ) V1 ]' V/ L/ p- I) ?0 }9 d0 k
  5. list_add(&node2.list,&myhead);
复制代码

% G* x% q7 [2 Z0 c
微信图片_20200902223500.png
list_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.让我们来看一下它的具体实现。
  1.   p' z8 T& B4 ]: o0 z1 T% f- Z
  2. /**
    4 p, `0 s) c1 y6 @  q
  3. * list_add_tail - add a new entry
    " U( G' b0 b( ^8 X9 K
  4. * @new: new entry to be added
    , p9 n! ?9 L+ {7 M5 ?. J; J
  5. * @head: list head to add it before
    + t! t4 i& I( ?0 K) G, l, c
  6. *
    + j  b4 q9 ?6 t: J2 k7 S2 b
  7. * Insert a new entry before the specified head.5 H1 C/ e3 H$ R! @3 o2 ^
  8. * This is useful for implementing queues.
    ' K  v) [; O. G$ O' {
  9. */
    4 W1 ~8 b$ Q+ V( P/ F9 K3 a
  10. static inline void list_add_tail(struct list_head *new, struct list_head *head)* \4 ?3 u% f7 s/ L# b5 n0 b- f
  11. {. k$ y! N: L6 M8 p% [
  12.   __list_add(new, head->prev, head);0 F+ Y1 S: D1 K3 A8 m9 }
  13. }
复制代码

# D0 t: x" S- ]/ {6 l: q/ ^: B
从注释可得出:(1)在一个特定的链表头前面插入一个节点
(2)这个方法很适用于队列的实现(why?)
进一步把__list_add()展开如下:
  1. ) [- D- d7 r6 N9 l3 b- o, l( Y$ l
  2. /*
    3 z0 d6 r3 x( D# O2 ^1 [
  3. * Insert a new entry between two known consecutive entries.
    9 {% r* M- h' F0 ?
  4. *2 |- k- |# K: c$ M( ^1 n5 K0 n4 J
  5. * This is only for internal list manipulation where we know
    # @* t  b; }" P$ B, e' d
  6. * the prev/next entries already!8 \' N9 Y/ B2 `! q8 P
  7. */
    ) ]. F% g' P$ o% ?) X  \/ I: ~- s& T
  8. static inline void __list_add(struct list_head *new,
    * }2 n( s( _2 b3 n0 u- M3 y7 m
  9.             struct list_head *prev,
    8 {  M/ d4 `% W
  10.             struct list_head *next)) L# C9 Q4 p5 N( ?# l$ s
  11. {
    / ]8 x* @7 C. v: g
  12.   if (!__list_add_valid(new, prev, next))2 z  h) w$ Q7 C. W! b( E- q
  13.     return;
      e) o& P/ K: E# i. s% ~9 q

  14. 9 ^' V8 X, y' }7 A: G) V$ I
  15.   next->prev = new;
    - W6 c( J! w9 q; ^$ X
  16.   new->next = next;9 g+ I# e" `7 s' ^8 ?' s( }7 t
  17.   new->prev = prev;3 Y/ \9 B% D/ f4 O  z3 V9 p, ~
  18.   WRITE_ONCE(prev->next, new);5 E9 @) a- i# u( m* o
  19. }
复制代码
$ n) t& u; e# T$ W" h) e+ ~( K
    所以,很清楚明了, list_add_tail就相当于在链表头前方依次插入新的节点(也可理解为在链表尾部开始插入节点,此时,header节点既是为节点,保持不变)
    利用上面分析list_add接口的方法可画出数据结构图形如下。
(1)创建一个链表头(实际上应该是表尾)代码参考第一节;   
微信图片_20200902223505.jpg
  
(2)插入第一个节点 node1.list , 调用
  1. 6 Z6 _8 Y& i+ V3 f8 N4 c1 S# Y0 }: B
  2.   struct mylist node1;+ Z4 v2 `( f. M( G2 r
  3. node1.type = I2C_TYPE;. E/ n* ]( v' }; z
  4. strcpy(node1.name,"yikoulinux");
    7 [% J" K. B9 M
  5. list_add_tail(&node1.list,&myhead);
复制代码
* H! z; t2 y6 R- ?; ~. p- [! O
微信图片_20200902223510.png
   
(3) 插入第二个节点node2.list,调用
  1. " {, Y* L( w1 ?: j! ?* h7 A' m: e
  2. struct mylist node2;
    & B; }/ j9 _- b% a+ S$ ~. E. b% `. \
  3. node2.type = I2C_TYPE;) A) u5 R1 _9 h. Q  L% v
  4. strcpy(node2.name,"yikoupeng");) F  S8 L+ X1 T/ ?: k! \1 i
  5. list_add_tail(&node2.list,&myhead);
复制代码
; @1 y% _  I3 D7 z* v
微信图片_20200902223518.png
list_add_tail
依此类推,每次插入的新节点都是紧挨着 header_task表尾,而插入的第一个节点my_first_task排在了第一位,my_second_task排在了第二位,可得出:先插入的节点排在前面,后插入的节点排在后面,“先进先出,后进后出”,这不正是队列的特点吗(First in First out)!
三. 删除节点
     内核同样在list.h文件中提供了删除节点的接口 list_del(), 让我们看一下它的实现流程

  1. 7 S: U% X: ]- I8 ^, d
  2. static inline void list_del(struct list_head *entry)
    1 a4 T. k, V4 |& ~+ _( I- _
  3. {$ @0 ?. w0 h6 q, M1 b8 n! _
  4.   __list_del_entry(entry);
    3 h" D' {2 @. s1 J, l
  5.   entry->next = LIST_POISON1;
    2 U: C* Z- D9 [7 q
  6.   entry->prev = LIST_POISON2;
    2 j3 H* v' K' i' b
  7. }) P8 v2 k3 U0 V! k- R
  8. /*7 J* u8 T1 o1 a- g
  9. * Delete a list entry by making the prev/next entries
    5 V+ l  j# l) U- W. m
  10. * point to each other.; J% `# q, ^! }
  11. *; h6 ]- u; y) C8 v8 h
  12. * This is only for internal list manipulation where we know2 P$ o( \: ~- |$ M: u2 W6 B- b
  13. * the prev/next entries already!0 A4 n. b' n. N& \8 ?& a$ X8 u9 Y1 \& x
  14. */; [. p4 c: H* c0 T' d' k) Q- |9 n. _
  15. static inline void __list_del(struct list_head * prev, struct list_head * next)
    & p, Z5 `* t' ?2 f$ `+ y
  16. {
    - _  \0 u8 U) Z2 K1 k
  17.   next->prev = prev;
      Q1 F+ d, A# {0 A) V9 l
  18.   WRITE_ONCE(prev->next, next);
    : `/ s$ Y$ a5 }8 @; p  J
  19. }
    , C0 D9 z9 }; v# P( W! ?- V
  20. " L1 e3 \' H* [6 u6 B: \. W
  21. /**
    % N3 r! H5 k# g7 w2 I
  22. * list_del - deletes entry from list.
    6 g9 j$ ?1 j3 |' U# P+ y
  23. * @entry: the element to delete from the list.
    8 |6 c: V; ^" z4 O# ^
  24. * Note: list_empty() on entry does not return true after this, the entry is
    + ?0 y4 Y8 @; n8 b3 P
  25. * in an undefined state.
    ! E% H8 D7 F1 y/ a* ?5 }: W
  26. */
    , C, _5 R, z9 U3 h
  27. static inline void __list_del_entry(struct list_head *entry)
    0 L9 z3 S2 v, ?. {
  28. {" j0 k2 u. r* g. g5 w
  29.   if (!__list_del_entry_valid(entry))  y4 d7 }4 m0 A# E! A8 s
  30.     return;& ?7 d8 R- D, b3 @- L" w" h( s  l

  31. 3 m- w: H6 a. z
  32.   __list_del(entry->prev, entry->next);% A+ O  ]: Y% R- W0 B* s
  33. }
复制代码

& e8 }  @( z$ b: O7 @
    利用list_del(struct list_head *entry) 接口就可以删除链表中的任意节点了,但需注意,前提条件是这个节点是已知的,既在链表中真实存在,切prev,next指针都不为NULL。
   
四. 链表遍历
    内核是同过下面这个宏定义来完成对list_head链表进行遍历的,如下 :
  1. ' H: h# T5 ?2 i  q6 I2 \$ f
  2. /**" t7 M  F  H- [
  3. * list_for_each  -  iterate over a list
    5 ^. h7 i2 r& I4 i+ i6 N( ]0 z/ Z
  4. * @pos:  the &struct list_head to use as a loop cursor.. |' A/ \$ @( n% v4 r
  5. * @head:  the head for your list.
      P: ]8 N! \2 L, ^! `
  6. */
    9 O2 J; h# m, _
  7. #define list_for_each(pos, head) \
    1 r. C, n# b7 E) C) @& l
  8.   for (pos = (head)->next; pos != (head); pos = pos->next)
复制代码
' M, I% ]7 I" A
   上面这种方式是从前向后遍历的,同样也可以使用下面的宏反向遍历:

  1. . |: \/ [0 ~( A7 h* O7 V& K& j
  2. /**
      X7 ^' A" G9 Y% M1 n
  3. * list_for_each_prev  -  iterate over a list backwards
    - A0 ]1 G" n4 `1 w
  4. * @pos:  the &struct list_head to use as a loop cursor.
    " I4 U; P, \) B  C( g2 ~& |2 b
  5. * @head:  the head for your list.
    4 x) D, G! F; v( V4 X! d0 X
  6. */7 X) F# b' i7 V  a3 @. x
  7. #define list_for_each_prev(pos, head) \
    3 |8 ?2 v2 x' a1 p3 K- e$ {! P
  8.   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这个链表进行的,涉及的结构体也都是:
  1. # C5 v  i4 O  T% z; W" S
  2. struct list_head {
    3 L; x4 Y4 }2 d9 n
  3.   struct list_head *next, *prev;
    + e" s5 j) ]! e* d% ]# G' D
  4. };
复制代码

2 n1 n  u' \* r8 c5 W, d* g; ]. A
        其实,正如文章一开始所说,我们真正更关心的是包含list_head这个结构体字段的宿主结构体,因为只有定位到了宿主结构体的起始地址,我们才能对对宿主结构体中的其它有意义的字段进行操作。

  1. # C9 L- |1 q, f- j4 n
  2. struct mylist  x  E# m& f; N6 x, @
  3. { + O, F* m, H* A2 a
  4.     int type;
    ' ~; Q2 z, X9 z+ u8 G/ g* R( k
  5.     char name[MAX_NAME_LEN];
    , q- M) Y3 x' F! G  }$ s* Y" |& f& n; p
  6.   struct list_head list;  " w1 T* {: x' K7 S. G' G4 j. e
  7. };
复制代码
" m8 j% Q% Z5 ]! G$ A
   那我们如何根据list这个字段的地址而找到宿主结构node1的位置呢?
list.h中定义如下:
  1. , P. y, s6 s/ a. S8 m5 @* m' x
  2. /**0 u+ v  T3 [+ b5 [* V
  3. * list_entry - get the struct for this entry
    5 ^/ w- e! e; X6 @+ h
  4. * @ptr:  the &struct list_head pointer.
    ' ?( g' c" }- [8 u6 B) y5 o  `# T0 q
  5. * @type:  the type of the struct this is embedded in.& ^: @2 n1 y! @% k
  6. * @member:  the name of the list_head within the struct.9 c5 T2 j2 C; G  O( g5 z
  7. */0 W$ T8 e& S" u
  8. #define list_entry(ptr, type, member) \4 C6 a0 r7 c4 ?
  9.   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之后此宏定义改变,但实现思想保持一致)
微信图片_20200902223825.png
而offsetof定义在 kernel/include/linux/stddef.h ,如下:
微信图片_20200902223830.png
举个例子,来简单分析一下container_of内部实现机制。
例如:
  1.   c4 \) \- \) ^9 F+ q7 j
  2. struct   test' L  J/ p- G0 E/ s, c$ N4 J
  3. {3 }0 n5 u* \4 X# d, s
  4.     int       a;
    / a9 u$ b8 C8 N1 l- ?; ]7 N
  5.     short   b;
    8 h: [' C5 O( A
  6.     char    c;
      U$ {- ?/ Z: l0 ?
  7. };
      I$ d8 I' d% Z+ Q4 j: x9 `
  8. struct  test  *p = (struct test *)malloc(sizeof(struct  test));) l: V2 c, q8 P' i$ y
  9. test_function(&(p->b));
    ( F/ T* v; z0 K0 x
  10. * l/ i' t# B- v0 J: A, i0 i. i
  11. int  test_function(short *addr_b)
    % j. G8 ~; A: ]6 K
  12. {
    5 u/ {! |) W9 Y9 n5 s
  13.       //获取struct test结构体空间的首地址; D  u/ R" Z7 z" ~
  14.      struct  test *addr;+ {' C* [8 v* m: ?% l$ G
  15.      addr =   container_of(addr_b,struct test,b);
    7 N0 X+ O/ {1 c; _8 b; M9 u/ {
  16. }
复制代码
% U2 t' h. e8 ]0 @, [
展开container_of宏,探究内部的实现:
% I+ X- `+ A6 [! T

  1. - f1 e/ `: j5 B8 k4 N6 ~
  2. typeof (  ( (struct test   *)0 )->b ) ;                        (1)
    % H% s8 D, Z& [+ I7 }: f6 c/ U
  3. typeof (  ( (struct test   *)0 )->b )   *__mptr =  addr_b ;    (2)3 @8 _# p1 }; i/ y
  4. (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中提供了下面的宏:
  1. 0 E* V+ Q8 ^9 ~. w
  2. /**1 c9 ]4 V/ Y$ f: u; i& \
  3. * list_for_each_entry  -  iterate over list of given type1 K7 B  O6 W' i0 D: i) C( }3 n
  4. * @pos:  the type * to use as a loop cursor.) G, G# @9 W6 }1 s9 v
  5. * @head:  the head for your list.; H' i) {7 W5 ~( u6 h
  6. * @member:  the name of the list_head within the struct.' }/ i% F; F7 H2 ~
  7. */. \* c* [1 H1 n  ?0 I
  8. #define list_for_each_entry(pos, head, member)        \0 |, k( e' z' {/ L7 K; C! p- c
  9.   for (pos = list_first_entry(head, typeof(*pos), member);  \
    : ^3 ~! }( m( s
  10.        &pos->member != (head);          \7 S) Y. f' P4 {9 ~
  11.        pos = list_next_entry(pos, member))
复制代码

5 X, _9 J  P; q
其中,list_first_entry 和  list_next_entry宏都定义在list.h中,分别代表:获取第一个真正的宿主结构的地址;获取下一个宿主结构的地址。它们的实现都是利用list_entry宏。
  1. ! P; m6 S0 R8 e) p; t" s# I  Q. v
  2. /*** G- W0 L1 Z6 @7 S
  3. * list_first_entry - get the first element from a list# j# K: c0 q. j- v3 ?* z% n
  4. * @ptr:  the list head to take the element from.
    . D7 M) r( e. N6 G, t! D
  5. * @type:  the type of the struct this is embedded in.! h- `$ i4 V3 Q& m5 F, r. o
  6. * @member:  the name of the list_head within the struct.
    & r( o! |9 w: v- P! R/ s9 `
  7. *
    7 B: D2 U1 L6 e% i; ^) Q# U: D
  8. * Note, that list is expected to be not empty.
    ; m+ R( \" D1 t+ S# s. K
  9. */: @/ {& a  P% [5 L
  10. #define list_first_entry(ptr, type, member) \* A6 U5 i0 y% @7 j3 f
  11.   list_entry((ptr)->next, type, member)1 D8 W/ ]. V2 B

  12. 2 i# I) i$ @6 J, X( I1 _
  13. /**$ W! H5 u' v7 Y
  14. * list_next_entry - get the next element in list) h, T$ h4 a( N$ i/ ^
  15. * @pos:  the type * to cursor% ^/ C1 u+ V9 k6 B, k9 @
  16. * @member:  the name of the list_head within the struct.
    " @9 a, M/ J, @7 q1 }, C. O
  17. */$ u2 `5 X% {" }8 x
  18. #define list_next_entry(pos, member) \& ?/ l( G$ ]  h- ^4 h
  19.   list_entry((pos)->member.next, typeof(*(pos)), member)
复制代码

5 G8 {# m5 D* Q: P5 p6 Z
最终实现了宿主结构的遍历
  1. 7 c3 h7 t) g& ?: T+ ]8 ]# p
  2. #define list_for_each_entry(pos, head, member)      \
    . c1 a/ p' F3 `9 ^& v; P
  3.   for (pos = list_first_entry(head, typeof(*pos), member);  \5 R, D2 @2 e$ J  R6 w$ R
  4.        &pos->member != (head);          \
    7 A# ?8 ?2 L: S6 g; L* f
  5.        pos = list_next_entry(pos, member))
复制代码
  W) \5 w) V9 S& \7 B2 {: C& A
首先pos定位到第一个宿主结构地址,然后循环获取下一个宿主结构地址,如果查到宿主结构中的member成员变量(宿主结构中struct list_head定义的字段)地址为head,则退出,从而实现了宿主结构的遍历。如果要循环对宿主结构中的其它成员变量进行操作,这个遍历操作就显得特别有意义了。
       我们用上面的 nod结构举个例子:

  1. + }! a' l2 ?- k# m1 ^
  2. struct my_list *pos_ptr = NULL ;
    . L4 s. X- K( _( c- _1 D6 h0 B
  3. list_for_each_entry (pos_ptr, &myhead, list )
    " B' S$ D% ^" C! S' Q6 _3 f
  4. { ) t1 m/ u; T, M: f8 E0 p  B/ K+ ?+ ~
  5.          printk ("val =  %d\n" , pos_ptr->val); , A' R- S5 S" y" y1 E
  6. }
复制代码

: u; z/ |# ]- V9 G! S) n. b

  }! b4 E# [8 `* c  }
实例1 一个简单的链表的实现
为方便起见,本例把内核的list.h文件单独拷贝出来,这样就可以独立于内核来编译测试。
功能描述:
本例比较简单,仅仅实现了单链表节点的创建、删除、遍历。
  1. - B1 {( Q8 F, ~
  2. #include "list.h" 2 O$ `4 k" x8 T7 Y( v  f
  3. #include <stdio.h>
    3 \% c. p, k2 p* t: P
  4. #include <string.h>5 \; @1 l6 P  ]6 V0 ^
  5. 3 y" f* B2 z2 ~4 A
  6. #define MAX_NAME_LEN 32$ v8 Q+ O5 ^4 \6 x8 s& |' N
  7. #define MAX_ID_LEN 10
    - _* H- C' ?" n" D; [. j9 B( F
  8. . V6 A5 A, C* P5 E9 D2 L7 n7 H
  9. struct list_head myhead;
    % O9 G% G" m5 U
  10. : {# G) p# T% M% e: u
  11. #define I2C_TYPE 1; _2 P; @7 Y6 E6 A$ u: z- b
  12. #define SPI_TYPE 25 Q+ F1 [. q1 q: `& _9 s
  13. ) `. ?' p) U# S. I) P
  14. char *dev_name[]={
    . H# b* J5 r, ~4 f: L+ R. Z  d/ Y
  15.   "none",+ ~& C( k4 }/ R7 G" V" Y% q7 u6 X' d
  16.   "I2C",
    2 q4 A7 p% p4 p3 v3 I. q4 q* C
  17.   "SPI"
      O2 v, d7 G8 D
  18. };, S5 t; {& E9 b# ^( |# K8 |
  19. 0 S  T- e, E4 _) n& A, q" E6 {
  20. struct mylist3 ^9 P( `9 h: F& p1 d/ H: t9 w
  21. {
    / C9 g' J7 L. f% l( v
  22.     int type;9 Y" b2 L! z( p+ W" r
  23.     char name[MAX_NAME_LEN];! X$ P$ v! A( n( ]8 V6 ^
  24.   struct list_head list;  
    1 d3 n' x# X1 c9 v1 y* I
  25. };
      r9 G+ {  f; R7 M  g  X

  26. ! b6 T* ^3 z' S# j
  27. void display_list(struct list_head *list_head): T1 _" A% [+ p
  28. {
    + ?' C7 C/ q8 L0 K
  29.   int i=0;
      a0 q% c5 q: a. P; [7 ~
  30.   struct list_head *p;+ N5 P9 ^; {8 }( t) q
  31.   struct mylist *entry;
    * @0 u! m+ D7 G4 `+ ?8 |
  32.   printf("-------list---------\n");
    4 M6 q. k' K5 n1 j  N
  33.   list_for_each(p,list_head)
    $ p' V$ A9 ], ^# G( {: t9 E3 E- |4 N/ F
  34.   {
    / Z( k  q% y+ V+ a; u
  35.     printf("node[%d]\n",i++);( ?: U$ n2 u, {& y3 _0 k
  36.     entry=list_entry(p,struct mylist,list);$ G4 i/ \; k( K' }) X) S/ q8 i/ A
  37.     printf("\ttype: %s\n",dev_name[entry->type]);5 K1 U6 W/ P3 r1 E
  38.     printf("\tname: %s\n",entry->name);, e# n' x5 x3 t2 ?7 \" B
  39.   }& w/ P3 ?  G* O' S) |
  40.   printf("-------end----------\n");
    . ]& p1 `$ ^( k. r; P0 t7 |
  41. }
    : d8 A  R6 W- X& N

  42.   f% F7 i5 f8 w" R8 d
  43. int main(void)
      a- r" {$ `' e' y
  44. {
    0 @# a9 ?4 @- e

  45. " b5 y! a8 x% r* Q6 N% r' H1 h
  46.   struct mylist node1;
    + |) v" M) z( {5 L9 B1 L, b5 h
  47.   struct mylist node2;
    0 h0 q* c6 F% X2 U  Z; W

  48. 2 M2 X; F7 x, J5 n
  49.   INIT_LIST_HEAD(&myhead);
    4 f- E8 L& g& }- o$ `
  50. . c4 k) @1 G1 L9 R; n
  51.   node1.type = I2C_TYPE;
    4 s1 |' V/ {4 ~
  52.   strcpy(node1.name,"yikoulinux");. Z$ a0 l  X, Y5 b7 ]: c7 S
  53.   a. ^' Z4 D: F) X" k/ B
  54.   node2.type = I2C_TYPE;
    3 H, S" C+ O# E) J) {& b' r9 Y* q
  55.   strcpy(node2.name,"yikoupeng");( J* _$ c# e( b

  56. $ A9 {, A0 K' \6 b- ~! B
  57.   list_add(&node1.list,&myhead);
    " D6 Z7 a5 z1 S- l% u( w
  58.   list_add(&node2.list,&myhead);& n, m6 ~5 _' ^" D/ {

  59. 8 s. g! t9 V2 ?/ H; D; u1 w* E
  60.   display_list(&myhead);
    / A, t& Z4 Q  |. r; v
  61.   X& e2 y* a1 r4 q3 [
  62.   list_del(&node1.list);9 o) _$ ?* r2 J9 S, ?. V! _
  63. 4 f( y: u* Y# V/ x. N
  64.   display_list(&myhead);. C' X- L. C1 N! M
  65.   return 0;. t: [2 u$ V& q) z* ]
  66. }
复制代码

3 Z, h% u7 B8 h& b# Z* |0 L# S) s
- z3 L8 h$ a: |- B2 G' h
    运行结果
微信图片_20200902223529.jpg
实例2  如何在一个链表上管理不同类型的节点
功能描述:
本实例主要实现在同一个链表上管理两个不同类型的节点,实现增删改查的操作。# L6 Y. B- l4 |  V# a, p
结构体定义
一个链表要想区分节点的不同类型,那么节点中必须要有信息能够区分该节点类型,为了方便节点扩展,我们参考Linux内核,定义一个统一类型的结构体:
  1. 2 z# Y2 B6 x" {$ y$ r
  2. struct device! j( ?9 V' {, O5 H
  3. {
    : Y% I3 x( J0 _  k
  4.     int type;
    # m) N, k& v9 A) M' U6 l* }
  5.     char name[MAX_NAME_LEN];7 }3 I0 Q, B4 W
  6.     struct list_head list;5 A7 C( X2 _% w- T+ Q2 C3 e7 H& I
  7. };
复制代码

+ j" p! a! a& ]; R+ T" J
其中成员type表示该节点的类型:

  1. : |$ v' d! d4 y" [
  2. #defineI2C_TYPE 1 / x8 [& S" b6 _, E2 ~
  3. #define SPI_TYPE 2
复制代码
( [8 G" d/ O7 F9 I" I* P
有了该结构体,我们要定义其他类型的结构体只需要包含该结构体即可,这个思想有点像面向对象语言的基类,后续派生出新的属性叫子类,说到这,一口君又忍不住想挖个坑,写一篇如何用C语言实现面向对象思想的继承、多态、interface。1 T- G" W, X. j1 c: A
下面我们定义2种类型的结构体:
i2c这种类型设备的专用结构体:

  1. * t8 A5 z; `, ~) w5 _! r
  2. struct i2c_node5 t# ~1 d6 r6 N3 |" I
  3. {, U7 \7 n7 @- x2 i0 w  ?
  4.   int data;. G/ {: M4 B) N8 }  V8 a3 r5 x
  5.   unsigned int reg;
    0 k- M" w' [/ C1 `! ^. }. E
  6.   struct device dev;9 Z5 ~: n. i  m& e' t" {" i8 S
  7. };
复制代码
4 [5 {2 l0 i3 M" X) J/ _) H2 `
spi这种类型设备的专用结构体:

  1. ; L9 g& W% \& }: o- F, p/ L) G! a
  2. struct spi_node
    + W' s0 A4 I# h! j
  3. {  
    ' I  [+ c% v/ b
  4.   unsigned int reg;/ H3 \) [0 {0 `5 X) u
  5.   struct device dev;- E! N& v6 L3 b
  6. };
复制代码

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
微信图片_20200902223538.jpg
节点的插入
我们定义的节点要插入链表仍然是要依赖list_add(),既然我们定义了struct device这个结构体,那么我们完全可以参考linux内核,针对不同的节点封装函数,要注册到这个链表只需要调用该函数即可。
实现如下:
设备i2c的注册函数如下:

  1. & {, j0 M+ E8 A- T- Y+ a' @# i
  2. void i2c_register_device(struct device*dev)
    4 c2 g2 Q2 [/ ?! U. L9 n0 p) P  l- |# S
  3. {
    8 p" E0 @9 T8 n  x& `
  4.   dev.type = I2C_TYPE;
    * Q2 D1 _$ _: T" O* X- I  \
  5.   strcpy(dev.name,"yikoulinux");5 {3 S+ ]/ H* y/ Q) ~3 A# H$ i* b
  6.   list_add(&dev->list,&device_list);    Z4 V5 J" I* F3 A2 a9 a5 G' M
  7. }
复制代码
5 o+ J: b  _& R6 V: n1 \, D
设备spi的注册函数如下:
  1. 7 p% _/ B7 V$ G1 |) G  j5 O1 |
  2. void spi_register_device(struct device*dev)6 R$ c0 i! J5 V0 \# D* o, f' r$ N
  3. {
    8 M* Q6 c5 d, H0 y9 `
  4.   dev.type = SPI_TYPE;7 `8 X# q8 ^( [7 @/ a
  5.   strcpy(dev.name,"yikoupeng");
    * D9 C) ~( f' r7 f2 z: n
  6.   list_add(&dev->list,&device_list);  
    8 i! n6 F. I4 k! V
  7. }
复制代码

1 W, _" o" R' e
# Q. T/ C0 J2 C* ~% [4 h% g
  我们可以看到注册函数功能是填充了struct device 的type和name成员,然后再调用list_add()注册到链表中。这个思想很重要,因为Linux内核中许许多多的设备节点也是这样添加到其他的链表中的。要想让自己的C语言编程能力得到质的提升,一定要多读内核代码,即使看不懂也要坚持看,古人有云:代码读百遍其义自见。
节点的删除
同理,节点的删除,我们也统一封装成函数,同样只传递参数device即可:
  1. : m: x# `; Q9 i# ?2 U6 _3 X; j7 W$ _) U& R
  2. void i2c_unregister_device(struct device *device)! I! e% d. e6 b$ b
  3. {
    ) e: ^+ v/ [- v7 x1 o7 A
  4. //  struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);# U8 O/ d- W5 E5 I# {* w: M; a4 r
  5.   list_del(&device->list);, D& _: C5 W1 A! P
  6. }
    ; r! O# _0 d4 [
  7. void spi_unregister_device(struct device *device)! R- b3 U- L% s' X7 B
  8. {2 r- I9 B$ x3 u7 d3 A/ \
  9. //  struct spi_node *spi_device=container_of(dev, struct spi_node, dev);
    : \9 U  V# p$ \3 G
  10.   list_del(&device->list);" p  C, k2 e, Z5 [. r% W' I
  11. }
复制代码
1 |8 r9 C4 X/ M/ H' o
在函数中,可以用container_of提取出了设备节点的首地址,实际使用中可以根据设备的不同释放不同的资源。
宿主结构的遍历
节点的遍历,在这里我们通过设备链表device_list开始遍历,假设该节点名是node,通过list_for_each()可以得到node->dev->list的地址,然后利用container_of 可以得到node->dev、node的地址。

  1. ) i( Z* V" c, k- q; C
  2. void display_list(struct list_head *list_head)
    , j, w! ]# U; R$ E
  3. {
    ; Q9 F1 @2 v  }' P3 W9 v
  4.   int i=0;; b+ E5 u- ]- v4 u" O9 x
  5.   struct list_head *p;
    # l% j& d( Y0 Q4 l6 E5 ]4 K
  6.   struct device *entry;
    ( h* t7 ]" }! ?$ n
  7. " {  K4 c7 d, A/ o( G
  8.   printf("-------list---------\n");
    * S5 b( o: e  V* n& ^: ^
  9.   list_for_each(p,list_head)5 ]+ n  j! e4 w+ z8 q3 o* T' Z
  10.   {
    & G; {: U: x$ |8 `; x' T: h. K
  11.     printf("node[%d]\n",i++);5 ^: O7 a- V8 P1 D4 o
  12.     entry=list_entry(p,struct device,list);  L- |4 k) b  W. J$ ~. ]" w" v

  13. 8 `7 Y$ w: J  O( ?9 C
  14.     switch(entry->type)
    5 \/ I# i( L) Z; A5 R1 n' R
  15.     {
    2 q" A! U+ P4 k9 u. ]; X
  16.       case I2C_TYPE:) O7 u7 W* m% u9 [$ D9 n
  17.                            display_i2c_device(entry);
    & p* ~. ]- Q) O( X+ i
  18.         break;4 P. b# r$ G: d$ R5 V( E3 \
  19.       case SPI_TYPE:8 P" r: k) d& M
  20.         display_spi_device(entry);
    - l; d8 c7 f9 n4 _# T1 d" {
  21.         break;
    + V& |  Q4 Z1 D- \0 o1 [
  22.       default:' y8 S% b0 s7 u
  23.         printf("unknown device type!\n");/ M4 M3 {! F# |3 w9 U9 \
  24.         break;- O, H4 q/ `5 x) `( `& z! ?0 F
  25.     }
    # N/ M" b8 f* f) T3 U
  26.     display_device(entry);% J& _( R# j" |  A0 K, E* [
  27.   }
    7 }/ l: B$ C, B/ m$ `
  28.   printf("-------end----------\n");, ]% `0 E2 k, B' M3 V9 c6 ]& C" N
  29. }
复制代码
+ g. _: R4 a# Z4 S
由以上代码可知,利用内核链表的统一接口,找个每一个节点的list成员,然后再利用container_of 得到我们定义的标准结构体struct device,进而解析出节点的类型,调用对应节点显示函数,这个地方其实还可以优化,就是我们可以在struct device中添加一个函数指针,在xxx_unregister_device()函数中可以将该函数指针直接注册进来,那么此处代码会更精简高效一些。如果在做项目的过程中,写出这种面向对象思想的代码,那么你的地址是肯定不一样的。读者有兴趣可以自己尝试一下。
  1. ( \$ x! z4 @) N2 E7 [9 j6 I3 f3 _* e; C
  2. void display_i2c_device(struct device *device)3 h# H: E) x4 l6 l* W$ g7 A
  3. {8 n* Q6 G/ S1 h- L! ?5 A/ r$ J
  4.   struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);
    7 ]3 A$ m5 ~; w. N! S# O, t, [
  5.   printf("\t  i2c_device->data: %d\n",i2c_device->data);. D" H) K2 ?4 T
  6.   printf("\t  i2c_device->reg: %#x\n",i2c_device->reg);
    # h+ ~5 Y% s2 c2 J2 ~/ g* ?+ w
  7. }
复制代码

  1. # U- a. T% v' |5 V* \( V, T
  2. void display_spi_device(struct device *device)
    . j9 x' p, g5 D
  3. {( g1 X6 V, @9 a, j7 f/ k
  4.   struct spi_node *spi_device=container_of(device, struct spi_node, dev);8 x* q4 f9 q# d& |$ H5 M0 [
  5.   printf("\t  spi_device->reg: %#x\n",spi_device->reg);
    ( z/ G7 j5 I; x2 u$ b4 q
  6. }
复制代码

9 h7 Q6 n! Q- S- g% f
上述代码提取出来宿主节点的信息。
实例代码
  1. 1 c2 H& N8 y5 K2 L! W* L' P8 @
  2. #include "list.h"
    - V' a/ P" s! H6 x+ w3 ?& x% g$ A
  3. #include <stdio.h>
    5 G* l: B2 g4 E2 \; A$ c5 J
  4. #include <string.h>
    6 Y6 {; w  [1 M; G$ I; G/ B! ?
  5. 8 H7 Q/ G9 n) U
  6. #define MAX_NAME_LEN 326 D) c5 H! T' o6 O) f  L
  7. #define MAX_ID_LEN 10( x6 j% f/ W) X, u3 @, A
  8. ( ]$ z5 N1 f8 E
  9. struct list_head device_list;3 F6 |$ B7 {  X# i* [4 T$ D

  10. 1 G5 `1 ?2 q6 m5 w' Q8 m
  11. #define I2C_TYPE 1$ B, z8 d6 _0 r/ T
  12. #define SPI_TYPE 2
      p0 J. ^: R, s- n. o3 T6 r9 Z
  13. 4 E8 E9 C2 G( N& T: P
  14. char *dev_name[]={; n" F2 D  l* H& X
  15.   "none",5 x. y" c1 f2 Z  B& @
  16.   "I2C",+ P& ?. R6 l2 C, w( O; U2 p4 c2 |
  17.   "SPI"
    5 I) t1 J) @5 E
  18. };5 I% N6 Z* ^; }2 a: g6 ~) l

  19. 3 B9 w( [5 V, o$ K; Z$ y
  20. struct device0 }& f) X4 d- A7 W9 Z
  21. {! Z$ A+ c; P- {1 N- T
  22.   int type;
    , Z9 g8 C8 }( g
  23.   char name[MAX_NAME_LEN];
    & N* y  L2 u, u8 K8 g) _: s3 F
  24.   struct list_head list;
    $ Z4 A1 U; u1 L6 N; E* b0 W, n
  25. };) y  \) d! v- F# _! d: U6 X

  26. $ g0 Q; T- M* T+ ^% L  M3 }
  27. struct i2c_node6 n7 m8 l2 A, x: c8 z; i) {
  28. {
    & E/ C9 U( N1 A4 T4 A1 V
  29.   int data;
    5 {+ U5 q. ^! D: V' P6 }" l
  30.   unsigned int reg;
    1 m0 b1 k. D' I1 v$ g- I
  31.   struct device dev;8 ^8 i7 c$ l- q: _
  32. };1 j) f6 I  [, Y9 t
  33. struct spi_node
    ( a0 v) W% b: H, U, E
  34. {  # ^: A% l. m  G+ g6 C2 }
  35.   unsigned int reg;" [5 N4 P+ ^$ L5 E* U
  36.   struct device dev;
    9 ?3 v8 f6 e# C
  37. };2 a0 ~7 o3 f% {! t5 R, f( y: P$ C* ~9 J
  38. void display_i2c_device(struct device *device)8 M2 S, ?* k  m3 T' d2 @
  39. {! A4 f% Z* b, ~% g4 H* h) a5 m
  40.   struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);
    3 n# r" h8 j8 m2 r
  41. 5 B. Y7 q% D  w6 B
  42.   printf("\t  i2c_device->data: %d\n",i2c_device->data);
    - D9 p! m2 E0 D0 T- \3 K$ g
  43.   printf("\t  i2c_device->reg: %#x\n",i2c_device->reg);
    & K7 [: H' Y( x" m! E
  44. }5 ?9 D( K/ C- [6 _
  45. void display_spi_device(struct device *device)
    5 t+ \6 y! g3 ^" y7 ~
  46. {
    " y4 M& W1 K( O. |
  47.   struct spi_node *spi_device=container_of(device, struct spi_node, dev);
    * b/ y( j6 X5 Q: [& V$ `/ c
  48. 5 f. {6 p, H, F5 p
  49.   printf("\t  spi_device->reg: %#x\n",spi_device->reg);4 J1 o% d# T! j9 e/ x
  50. }
    & s8 b. k) N3 h5 e- p% E+ ^/ r* P
  51. void display_device(struct device *device)
    5 ]( ~* C6 N" k" K% E
  52. {
    * d# G4 w* D6 c# H' g9 Y) B
  53.     printf("\t  dev.type: %d\n",device->type);
    : N# f" g2 |. }( t' @) G
  54.     printf("\t  dev.type: %s\n",dev_name[device->type]);
    - ~& J* T' w7 d+ d( ~
  55.     printf("\t  dev.name: %s\n",device->name);  q3 S) D& w! Y  V  O: T
  56. }
    : k9 b* r/ G* k* j
  57. void display_list(struct list_head *list_head)- y% `' M# n4 ]
  58. {- R7 |' M# n6 {  |/ w  y3 g) n
  59.   int i=0;, m7 k( {7 q+ R2 o: w& \
  60.   struct list_head *p;
    ) E" U/ ]# y" j$ [0 _/ K9 B8 l! W
  61.   struct device *entry;
    ' M/ O1 B4 I( ?% v$ I4 M: o
  62. 0 c# A* E( f5 t: X) ~
  63.   printf("-------list---------\n");
    , N- X8 a0 A3 U3 Y6 U
  64.   list_for_each(p,list_head)
    - W1 K( Z* |% q, E2 W
  65.   {* @4 N" h' ]% Q& K9 H
  66.     printf("node[%d]\n",i++);4 K, u& S& h7 v2 e  ]
  67.     entry=list_entry(p,struct device,list);
    7 @7 _; m0 k7 k, W( ]* R7 R/ D9 ]3 u
  68. % P$ k4 e8 @$ C7 M
  69.     switch(entry->type)+ L& T" L6 g2 d6 K. ^8 k- J/ k- M2 B
  70.     {
    " m' ~7 N- p& E6 d9 w! j/ D  O) U
  71.       case I2C_TYPE:6 V2 m7 M0 ^  I% s0 U% v' o1 B
  72.         display_i2c_device(entry);
    # D1 I  M( E% u& g
  73.         break;
    0 A. z) [+ p5 l4 [
  74.       case SPI_TYPE:
    2 G% t& b1 V2 t2 l6 \( \
  75.         display_spi_device(entry);2 X2 o, w! J8 m  o
  76.         break;% x9 @' F. c- W& }" l" D4 E4 d
  77.       default:& J& B9 f% k5 E
  78.         printf("unknown device type!\n");2 }6 A. S  H4 e6 N; {
  79.         break;
    2 E9 ^, L" S$ X- h  C
  80.     }
    9 f! v3 p7 [6 \3 x/ i
  81.     display_device(entry);
    0 @7 T. n' b+ R+ U
  82.   }$ s$ y1 I! L' V  }5 f
  83.   printf("-------end----------\n");
    2 z- G" n7 K. H/ M3 m4 q- W+ x
  84. }
    2 A- z2 w  `9 W: j) l
  85. void i2c_register_device(struct device*dev)
    " f% d; Z0 o0 n$ a% ~/ ^
  86. {! O( ?$ W, m8 w2 J5 u& x$ N
  87.   struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);
    5 y6 g$ J) y: ^

  88. 9 X7 V0 W2 g4 P5 A6 X3 e% O  L
  89.   i2c_device->dev.type = I2C_TYPE;
    ! _4 [2 D" a9 h% ^
  90.   strcpy(i2c_device->dev.name,"yikoulinux");
    7 p9 v, p" B( i/ t+ W

  91. + j% `" L) C" R$ h; @; |/ c
  92.   list_add(&dev->list,&device_list);  
    & g- |7 \2 [( C# W
  93. }
    1 T) D) _% L1 O& X
  94. void spi_register_device(struct device*dev)
      B- M6 C7 q; l* t. y) f7 C
  95. {5 T% R0 `$ k0 P& j
  96.   struct spi_node *spi_device=container_of(dev, struct spi_node, dev);9 J1 x. d# Q, j, T
  97. " j5 t) I2 ]$ T  K- z/ P+ T; j
  98.   spi_device->dev.type = SPI_TYPE;2 b2 Z% X3 ]2 T
  99.   strcpy(spi_device->dev.name,"yikoupeng");
    3 [# G/ e: e  m( v6 q
  100. : m! F  F  d4 _" E
  101.   list_add(&dev->list,&device_list);  
    ' Y: F2 [1 l. k( _9 B  d+ C# g7 J8 ^, {4 U
  102. }
    $ _- b* Z, e1 c: t, w
  103. void i2c_unregister_device(struct device *device)
    * k; h6 d5 Y8 |" o
  104. {
    / I. u/ d5 w0 p- j" J' E+ H
  105.   struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);& I8 s0 b5 i5 P: D! ^
  106. ' z' b3 h/ {" n# @
  107.   list_del(&device->list);# ~" k1 M- I- X# K, T
  108. }
    7 X# u3 l. d) a
  109. void spi_unregister_device(struct device *device). t9 X+ q3 [' a
  110. {
    8 Q1 n  H0 T% F) K; m# \7 u; I
  111.   struct spi_node *spi_device=container_of(dev, struct spi_node, dev);
    - M$ Y2 m. {( t4 w  Q
  112. ( X( \- `- ?4 L( o' w2 L7 C' e
  113.   list_del(&device->list);% r9 v* X. v, o4 V
  114. }9 u, E' M5 ]! C; j- S3 m6 S
  115. int main(void)
    : o5 }3 u9 Q- @1 z% {
  116. {
    & s: C$ s- E# z% x
  117. 1 t1 _2 B3 n, D( i; X  I
  118.   struct i2c_node dev1;& T0 d: O2 b% t+ Y- J2 c2 h  J
  119.   struct spi_node dev2;4 H5 k# R/ w+ q8 d7 z! P& }+ O
  120. ' `7 w+ D6 ~4 _  Q' m, d" J
  121.   INIT_LIST_HEAD(&device_list);* n7 G& A0 L! R) Y: }
  122.   dev1.data = 1;
    + S  c  Z) G% O/ ?! t
  123.   dev1.reg = 0x40009000;# B! i* I' `6 A6 _
  124.   i2c_register_device(&dev1.dev);; v9 L6 w2 u; q* c6 [
  125.   dev2.reg  = 0x40008000;" O6 g- ^, J7 p8 p  c; r
  126.   spi_register_device(&dev2.dev);  
    5 f% o$ @. u$ Q  x- Z
  127.   display_list(&device_list);
    3 M1 j' f, C7 w2 ]
  128.   unregister_device(&dev1.dev);5 G6 x! K4 N7 W% v; f2 v8 A
  129.   display_list(&device_list);  
    ( O" k& z* n1 z3 d: \
  130.   return 0;2 b& l- p- z2 }' S5 O
  131. }
复制代码
代码主要功能:
  • 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
微信图片_20200902223549.jpg
读者可以试试如何管理更多类型的节点。
2 K$ _3 S% K- ?* C9 t* a( q: i
实例3  实现节点在两个链表上自由移动
功能描述:
初始化两个链表,实现两个链表上节点的插入和移动。每个节点维护大量的临时内存数据。
节点创建
节点结构体创建如下:

  1. ) l8 O* e" w0 V/ I4 S1 E, D4 |
  2. struct mylist{
    # Z" T/ ?* A2 X7 Q' i, Y2 c& e7 Y
  3.   int number;
    , ?3 |; i: v% h0 |
  4.   char type;
    0 u9 ]; G6 ~  W) r
  5.     char *pmem;  //内存存放地址,需要malloc! K& O8 E- p) {# n" B2 n7 S% Y+ D
  6.   struct list_head list;
    8 G; p- H$ b( ~/ h
  7. };
复制代码
, J* f% d6 G! R* g1 z8 ^# e1 D
需要注意成员pmem,因为要维护大量的内存,我们最好不要直定义个很大的数组,因为定义的变量位于栈中,而一般的系统给栈的空间是有限的,如果定义的变量占用空间太大,会导致栈溢出,一口君曾经就遇到过这个bug。
链表定义和初始化
链表定义如下:

  1.   u( G* c+ p9 f. n# [+ G7 Y6 N
  2. structlist_head active_head;
    . b  r. B% I+ m$ E8 e9 k
  3. struct list_head free_head;
复制代码
初始化

  1. . w/ F  k$ k! L" o2 R1 x5 h
  2. INIT_LIST_HEAD(&free_head);2 h  q5 L8 r, i! [  X' O
  3. INIT_LIST_HEAD(&active_head);
复制代码
" Y4 H# s- {6 d* W, ~
这两个链表如下:

8 a; m0 K. [3 @$ Q3 g
微信图片_20200902223557.jpg
关于节点,因为该实例是从实际项目中剥离出来,节点启示是起到一个缓冲去的作用,数量不是无限的,所以在此我们默认最多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* @
微信图片_20200902223608.jpg
2 l7 b) s  \' E6 S- Q9 k
" A% ], V/ k# r% e! K- ~
初始化这个数组对应的节点:
  1. 7 W  k3 L+ |2 c9 h5 M% i
  2. static ssize_t buffer_ring_init()
    ) D  d. W2 E' _* q9 F7 b
  3. {
    ! u, @8 x3 E, V2 F  O: x; |
  4.   int i=0;' t/ Y' @4 s+ `$ {" a- w
  5.   for(i=0;i<BUFFER_NUM;i++){- z) Q- U0 R$ q5 @# p+ Z
  6.     list_array[i]=malloc(sizeof(struct mylist));, f5 A, k6 q' V8 t! W
  7.     INIT_LIST_HEAD(&list_array[i]->list);8 m1 h' T& Y& }) c
  8.     list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);
    6 u( X& O" B9 N! A
  9.   }
    4 b; e4 X! T  ]# L6 s' Q
  10.   return 0;
    # i# S8 _" b5 x  w
  11. }
复制代码

* v' B4 j0 y* c
5:为下标为i的节点分配实际大小为sizeof(structmylist)的内存
6:初始化该节点的链表
7:为pmem成员从堆中分配一块内存
初始化完毕,链表实际情况如下:
微信图片_20200902223613.jpg
节点插入
  1. $ Q; D" `8 ]; P7 M/ t: S  J
  2. static ssize_t insert_free_list_all()
    * E' V1 K$ D! h; M
  3. {* T) Z: g7 Q6 B# Q
  4.   int i=0;
    ( E+ ?' I9 v+ J5 A

  5. 0 O4 J* w4 {$ u, u$ U! K
  6.   for(i=0;i<BUFFER_NUM;i++){
    & i. i- G8 `# C7 t5 ^) o
  7.     list_add(&list_array[i]->list,&free_head);3 D3 q; f2 s# F
  8.   }. _$ D$ A5 V' Q/ H9 S9 u* ~
  9.   return 0;& u, o$ m+ z+ W) y
  10. }
复制代码
9 }. `3 U  W; M4 P' z+ }
& Y9 [. T! ?9 q6 D. k8 ^% ?
8:用头插法将所有节点插入到free_head链表中
所有节点全部插入free链表后,结构图如下:
- T7 C% g- T6 i
微信图片_20200902223617.jpg
9 c3 s+ `  j$ C, Q: T" _) f
遍历链表
虽然可以通过数组遍历链表,但是实际在操作过程中,在链表中各个节点的位置是错乱的。所以最好从借助list节点来查找各个节点。
  1. how_list(&free_head);3 C+ W8 g6 L6 [) h  A! {5 O8 y
  2. show_list(&active_head);
复制代码
8 w) u2 S, N8 c- n7 P
代码实现如下:
  1. " Z  a  M. e; F1 |% \5 @) U. c
  2. void show_list(struct list_head *list_head)
    6 n% q( s6 h0 B" N4 l6 }
  3. {
    & n+ O  v9 e, w" K; F1 W
  4.   int i=0;
      v* W4 ?0 B5 c1 q
  5.   struct mylist*entry,*tmp;! j+ s. d) e, I
  6.   //判断节点是否为空* s1 n2 {& m9 ~9 d
  7.   if(list_empty(list_head)==true)
    + G6 q0 R8 Q# `2 \. @2 b  x
  8.   {
    . Y. E2 o; I' s7 U* P9 w
  9.     return;
    % ]7 p) E; o; C% c) P
  10.   }
    % M, Z3 x2 D2 X- N2 r: s
  11.   list_for_each_entry_safe(entry,tmp,list_head,list)
    , I) S% j+ _- N
  12.   {. d" M/ T" ~) R0 l2 S" A' j
  13.     printf("[%d]=%d\t",i++,entry->number);
    1 U1 Y1 f" A4 C5 x8 d
  14.     if(i%4==0)( B" \; |' f9 d6 ?7 T% q( }( a
  15.     {
    , @, U0 q, S8 n9 c" f/ p+ P
  16.       printf("\n");; Y5 R* A1 ]( O+ f! D; y. M
  17.     }
    1 I5 g/ |% n& n
  18.   }  7 e2 h$ D+ r- w; F- E. P
  19. }
复制代码

) c' Y  V; j7 j: R4 B) L: n

  P8 |4 Y( P. D0 x) h
节点移动
将节点从active_head链表移动到free_head链表,有点像生产者消费者模型中的消费者,吃掉资源后,就要把这个节点放置到空闲链表,让生产者能够继续生产数据,所以这两个函数我起名eat、spit,意为吃掉和吐,希望你们不要觉得很怪异。

  1. " N" d$ r' M/ S
  2. int eat_node()
    5 m5 p2 B; e; ^0 `- v7 f8 a
  3. {
    7 ~( A: Y! U- x& H) e7 R
  4.   struct mylist*entry=NULL;
    ( E5 G& A6 D& I* K1 [& U
  5. 0 O$ ?' }" I) h7 \
  6.   if(list_empty(&active_head)==true)
    ! n1 X7 S# |5 r% X
  7.   {
    & C/ T6 @5 B$ L2 J# P" O$ r- [
  8.     printf("list active_head is empty!-----------\n");. a0 \# P6 `6 M6 C
  9.   }
    : ?& p8 }' g  l$ x" e" D
  10.   entry=list_first_entry(&active_head,struct mylist,list);
    + \! e2 M1 @- s/ Z! ~6 F  W) `
  11.   printf("\t eat node=%d\n",entry->number);7 }7 I+ N7 L* a* d' t9 [" _
  12.   list_move_tail(&entry->list,&free_head);
    7 B/ I  `) c* T! c6 l. o8 v
  13. }
复制代码
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链表。
  1. 0 n3 _/ \( a% V
  2. spit_node()
    . q8 r/ Q) o( e4 X2 Q. C% o# D
  3. {
    8 c; n2 m; P5 q8 C, A8 a
  4.   struct mylist*entry=NULL;
    6 l2 m- y0 a( R: A3 V
  5. ; l" x% @( I# {$ b. ]
  6.   if(list_empty(&free_head)==true)
    , q5 m5 l  ]$ |$ w
  7.   {0 p' }- ^' r& o2 k& _
  8.     printf("list free_head is empty!-----------\n");  j" Y) v/ E; N' i! l1 t
  9.   }3 i% t. u* u# u" c" _, H
  10.   entry=list_first_entry(&free_head,struct mylist,list);
    ; U3 x5 [+ A2 |# u% P9 H
  11.   printf("\t spit node=%d\n",entry->number);* E3 a! M0 m7 E& c
  12.   list_move_tail(&entry->list,&active_head);1 u- T% O( o# a# A" g
  13. }
复制代码
  W" Q% l! Y/ x6 S7 M3 I) S8 \, [
大部分功能讲解完了,下面我们贴下完整代码。
代码实例
# k  P% T* O, ^6 @9 Z, h! p! P8 c

  1. / _6 E$ R7 `- J2 P9 i
  2. #include <stdint.h>
    $ h! n: e# g# ]. \1 H1 y
  3. #include <stdio.h>; i3 f. }' e) Z) }/ z, e
  4. #include <stdlib.h>7 `6 N8 p! |- |- h0 w3 z8 h
  5. #include <unistd.h>
    # \. E/ I& P/ {; h- g& S
  6. #include <byteswap.h>8 }; D+ M. M7 \. l
  7. #include <string.h>
    ) T8 Q9 M4 I# [, m
  8. #include <errno.h>0 `, g1 ^" H7 Z( u" S
  9. #include <signal.h>7 [4 |4 ]' y+ h2 M4 w* T( p2 \
  10. #include <fcntl.h>
    6 M3 s8 i; f- ]# K/ t$ W4 }0 T
  11. #include <ctype.h>, J1 w5 n' o! b8 @8 E  r, c; t
  12. #include <termios.h>5 L' c" ?% `" f# F! D, L9 d! w  P$ W
  13. #include <sys/types.h>- a" T8 F8 M  L( e+ A2 c
  14. #include <sys/mman.h>
    + C' n! I: y+ @9 |$ r
  15. #include <sys/ioctl.h>
    ; V5 W$ f! R/ f6 w
  16. #include "list.h"     //  linux-3.14/scripts/kconfig/list.h
    / X6 ?  w0 V- @: i. J- M) B
  17. $ T  ?4 ?& q" A
  18. #undef NULL
    6 N+ I( e8 }9 i
  19. #define NULL ((void *)0)- S/ {; H8 {& |- R3 Q& c
  20. , a- z" C' @, q' `1 D
  21. enum {+ q! E( E& S; o% d5 \
  22.   false  = 0,! m, l3 K4 u5 ~$ k. g
  23.   true  = 1: j0 ^, w0 \& M# P, V5 j2 @5 O
  24. };+ _& V, {1 e- l, [7 @" H0 X

  25. 6 R6 q1 W* s. d
  26. #define DATA_TYPE 0x14
    - E& w1 ^9 b$ w* h( Q" J) j% o$ ~
  27. #define SIG_TYPE 0x15# H6 ]) \" v( I/ \  L6 m
  28. ' v* `. h1 v+ t4 p
  29. struct mylist{
    / g6 K: G# A, B0 }- @
  30.   int number;' B- e3 j# A! Z% m* x" G1 n* C
  31.   char type;
    9 z, r% I7 C% W8 `& U
  32.   char *pmem;
    6 m4 e2 a  U4 G9 s: X# l
  33.   struct list_head list;+ F0 {: {3 K, V2 U$ B
  34. };
    / G, @- h4 S2 O; e; S1 G6 H" r
  35. #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 ]) [
  36. struct list_head active_head;$ m: j0 [7 {1 H/ `1 l9 S
  37. struct list_head free_head;* d. @$ J; q% x4 Z6 `
  38. #define BUFFER_NUM 10
    % y" z$ Q: }0 U9 m4 m5 h
  39. #define DATA_BUFFER_SIZE 512
    4 v/ b1 J, v- r9 ^+ w$ L$ M5 N
  40. struct mylist*list_array[BUFFER_NUM];
    1 H% t0 R, Y/ |7 ]
  41. 9 u' b1 n* w8 s
  42. 8 ]: c" K! j- C* s( u
  43. int born_number(int number)
      k/ D4 W: Y) N# @3 W3 o8 r6 m4 `& E
  44. {- z; [7 W' E+ A7 M
  45.   struct mylist *entry=NULL;
      Q5 _- I' k* p

  46. ' m! X  z* O0 o# V
  47.   if(list_empty(&free_head)==true)
    1 Z; z% a& c0 h# t. y) r
  48.   {0 c3 t0 A5 j4 j5 V8 M
  49.     printf("list free_head is empty!----------------\n");
    7 Y$ l4 z" ?9 e& A$ K3 r% e* i
  50.   }
    + _  h1 j8 q$ k% h* U1 ?
  51.   entry = list_first_entry(&free_head,struct mylist,list);
    1 g" g9 A. h% I: {
  52.   entry->type = DATA_TYPE;: d  N: ^. ]$ z% _1 B7 }4 T
  53.   entry->number=number;
    - h/ \" C0 E0 S
  54.   list_move_tail(&entry->list,&active_head);  
    4 I$ {- A2 A* b
  55. }& ~1 L3 M/ F- @6 E2 i2 W

  56. 7 x+ v  l5 Q& o" @
  57. int eat_node()' G. G4 X% J% ?: A; W  w) |- c
  58. {/ {; O. |# F& S
  59.   struct mylist*entry=NULL;- b% `/ U0 s+ ~/ b! ^
  60. : Y6 s4 T+ r1 u0 B
  61.   if(list_empty(&active_head)==true)
    0 ]) O  @& x% C2 A. Z
  62.   {7 }6 f1 E" P, `' Z$ I0 L7 Q5 j# x- {
  63.     printf("list active_head is empty!-----------\n");# Q+ {0 I9 i! I
  64.   }
    - t( ^( A. I; z2 \3 t) v% C
  65.   entry=list_first_entry(&active_head,struct mylist,list);
    % [& M; E' B6 g8 `: H3 M: v0 U
  66.   printf("\t eat node=%d\n",entry->number);
    . q: Z' @* \5 t/ [/ ^! E
  67.   list_move_tail(&entry->list,&free_head);. g) ?0 u* l8 S) e3 k) K: ^1 g
  68. }
      Y6 ^) R3 G4 l  S% D2 O7 H
  69. spit_node()
    . D' R5 x7 S( n1 O2 K' t/ ?  Z7 L& H! S
  70. {9 E" G0 p; E( T
  71.   struct mylist*entry=NULL;
    4 n5 {( C# C! w

  72. . W% i6 _) `/ H/ V: S
  73.   if(list_empty(&free_head)==true)' f+ P* z/ W' O9 R  m: n
  74.   {$ N$ y" P% {! G1 y. e) u! M
  75.     printf("list free_head is empty!-----------\n");
    ) H0 K/ k1 `( z9 k1 Q) Y
  76.   }9 ?- V$ T! C, j3 A" P3 E9 u! W
  77.   entry=list_first_entry(&free_head,struct mylist,list);
    4 K1 M# \( l$ Y
  78.   printf("\t spit node=%d\n",entry->number);7 R. [; X: F0 F* G+ {9 Y
  79.   list_move_tail(&entry->list,&active_head);
    & D( R6 \7 N7 n8 X
  80. }) A, D+ W. q" x: x8 t# H$ s

  81. ) y" h4 g! e$ d# Y* V( H: ]  w
  82. void show_list(struct list_head *list_head)
    . Y' F- ]. B, P9 X4 u2 L
  83. {& x8 l+ C: C8 r7 Y
  84.   int i=0;- p- z& V7 g* i# o$ f
  85.   struct mylist*entry,*tmp;9 o5 i4 A6 U5 O

  86. ) M, a  I8 V5 ]
  87.   if(list_empty(list_head)==true)
    % v; ~2 r/ l( p$ D: g- G! k( V
  88.   {- M- I5 b! Q6 H" \- ]
  89.     return;
    ' u/ ?2 y7 n' u5 T, A3 }, u
  90.   }
    + p$ M* I( }* f% s' B* b' b# ~. m
  91.   list_for_each_entry_safe(entry,tmp,list_head,list)
    ; n5 L0 J6 a+ o7 q. T7 }% A
  92.   {! Y7 K1 P. D" |; }$ K0 e& x2 D
  93.     printf("[%d]=%d\t",i++,entry->number);
    , h+ T- y# [  K& @4 L
  94.     if(i%4==0)' F3 m, c8 i6 L
  95.     {9 z! a1 f3 H% J* S: b
  96.       printf("\n");& X0 E, q2 G1 m! B) X5 T
  97.     }
    + j5 W! z! J; y4 J9 m* `7 T
  98.   }  
    $ T5 @1 ^& ~! H- {# U& _
  99. }
    : l9 _* [' ]' U( V" D8 h( g
  100. int list_num(struct list_head *list_head)8 j) I4 _7 X& l9 Z! q' e' R
  101. {% V1 w9 a' m9 h' _
  102.   int i=0;# E' W2 F2 R$ u; `& y/ g0 B
  103.   struct mylist *entry,*tmp;' v3 Z% q- T- i% w; z0 P) p; l- e
  104. //  printf("----------show free list-------------\n");/ {' H( Z2 W4 [% Q3 A8 g
  105.   list_for_each_entry_safe(entry,tmp,list_head,list)% g8 U. z  K  G. `3 L
  106.   {5 h4 t- O1 |; Q6 [' G! t7 k
  107.     i++;
    8 z1 N2 u* y$ }. G9 R
  108.   }
    * y0 f% O2 }, X5 B! `. k$ J3 N2 Z# K
  109.   return i;) w% f+ i/ u6 L: O; w- J, U
  110. }! e. O/ I1 F9 p0 B# B) a  n8 L
  111. static ssize_t buffer_ring_init()# }- P( S/ C9 W
  112. {! y3 D  S$ K7 L8 D! `
  113.   int i=0;
    $ r$ i7 A! s# o$ g. G, e8 e3 h

  114. 2 H$ H0 A8 a( d
  115.   for(i=0;i<BUFFER_NUM;i++){: i, Q2 e, Y8 S1 S; ]$ u
  116.     list_array[i]=malloc(sizeof(struct mylist));) N- p9 n. F7 v( Q. Z: r, W
  117.     INIT_LIST_HEAD(&list_array[i]->list);$ p, {/ `9 ?  j- Y* r9 t
  118.     list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);% M7 [: o7 k6 ^$ f
  119.     list_add_tail(&list_array[i]->list,&free_head);" K/ n) Z7 V+ p' }) X2 @' d: j
  120.   }
    $ X' s5 g3 U: |; n( j; p* y
  121.   return 0;* _; }: Z+ t4 }# C+ }* r
  122. }
    + `! e) b1 h1 k7 }& T7 W" a
  123. static ssize_t insert_free_list_all()! W% g. o) ~' n+ W
  124. {8 s5 R: T! [$ L+ e) n, y; F
  125.   int i=0;- ?# M/ S' K5 [8 ^; ?1 W

  126. , v, S% d: k, u% Y. W; Z4 d
  127.   for(i=0;i<BUFFER_NUM;i++){% J9 h2 C, Z! v5 `
  128.     list_add_tail(&list_array[i]->list,&free_head);) y5 b( v* q. w& y+ b
  129.   }+ y& }8 }) D! q
  130.   return 0;
    " X' C1 y( @$ w* h) V0 _
  131. }& C9 W& C# B( y9 s
  132. static ssize_t buffer_ring_free()( X- m: Q, b  z; {) |; S
  133. {: e. S  B" Y2 A. b( |" b2 H/ k" s) r
  134.   int buffer_count=0;
    / _; Z3 g( b( Y. ]% i- H) z8 F
  135.   struct mylist*entry=NULL;
    9 P; _: l6 d! l6 U  y
  136.   for(;buffer_count<BUFFER_NUM;buffer_count++)% ?' O: a, X# a
  137.   {
    2 j  B. I2 Q& v
  138.     free(list_array[buffer_count]->pmem);
      Z5 ^( v( R* p0 j
  139.     free(list_array[buffer_count]);
    . H, c1 z1 ^7 h2 {& d; p2 L3 I
  140.   }
    , l& U' w7 g- A8 t
  141.   return 0;" Q$ c' I" H9 L2 X  _# [! X: w
  142. }7 G! ^2 O5 l& y; K. ]; U1 C

  143. ; `: C6 M+ }) v4 G. I( D# h1 V
  144. int main(int argc,char**argv)
    ; c& [7 g  K6 l
  145. {& J# f7 d! X8 w! l& ]. p' I$ V8 o
  146.   INIT_LIST_HEAD(&free_head);
    3 p- I0 L7 k7 X
  147.   INIT_LIST_HEAD(&active_head);6 Q, F2 ?' q( @/ X/ ]4 X9 q0 U; _
  148.   buffer_ring_init();
    1 ?7 w; u. }. [2 c
  149.   insert_free_list_all();
    1 z& ~$ f/ o! ]; b' \! R
  150.   born_number(1);
    ( C! j: u7 c. e9 H, N& N- ~. }
  151.   born_number(2);
    ! W* r0 s& D( ^; M$ j# f
  152.   born_number(3);) V6 x' Z* I; T* p
  153.   born_number(4);! N" V9 C2 ~% \7 t1 e7 C1 S
  154.   born_number(5);/ \. p5 I/ ?4 R  a
  155.   born_number(6);! W& U/ [5 C# f
  156.   born_number(7);2 B; i. q, L8 o( _3 c
  157.   born_number(8);
    2 H3 [9 P; ~* `" U7 l, x0 r- z( E
  158.   born_number(9);/ ^/ z4 m0 G: a5 T4 w
  159.   born_number(10);
    6 ]! {* b% S% k) `6 `$ f

  160. 8 i5 E8 Z4 U1 Q/ p' o9 x
  161.   printf("\n----------active list[%d]------------\n",list_num(&active_head));
    , D9 y7 o* W( I  Z/ n" T
  162.   show_list(&active_head);% d8 _2 F8 @6 I) c: _$ {
  163.   printf("\n--------------end-----------------\n");
    2 B4 m& h# r4 Q7 P% r9 J& A

  164. / C, T+ B$ P, s% Z- w5 X7 A
  165.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  
    8 m$ [% O  o' X. }; a  a! {" F
  166.   show_list(&free_head);2 h0 o2 P: q  P- h  q) h- Q3 H. N* V: Z) z
  167.   printf("\n--------------end-----------------\n");
    * W. A4 |0 p) P3 V

  168. + R6 ~9 C9 t3 ?( O4 z1 _
  169.   printf("\n\n    active list----------> free list \n");5 `  |4 y+ Z; ~9 Z9 }0 Y, D& _# }

  170. % I0 Q4 G) s8 [
  171.   eat_node();
    7 {; l6 }) O# C4 R7 _$ K
  172.   eat_node();9 g- {% K# H! x) W6 y
  173.   eat_node();
    " Y# H1 S/ N. g  S. L: Y+ N+ _, K
  174. , {1 W! Y7 D$ V2 S$ x3 e# |1 [# t
  175.   printf("\n----------active list[%d]------------\n",list_num(&active_head));" X, A+ R6 a2 z, P
  176.   show_list(&active_head);
    3 B$ b3 x8 f  j1 k
  177.   printf("\n--------------end-----------------\n");+ K3 U7 Q! T: o" e
  178. 2 ~. V: {3 Z, H9 _+ L
  179.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  + b. X! t0 R. q, Y, L: m  r
  180.   show_list(&free_head);
    & E( D) K" R8 D  D* h% \
  181.   printf("\n--------------end-----------------\n");0 d* z7 ]7 m/ h. z
  182. 0 J* i; s% {, u1 C3 |

  183.   s+ K% ~. J( t3 v7 |
  184.   printf("\n\n    free list----------> active list \n");/ g  e; T  i* h6 u1 c9 k

  185. . E& n+ ?& N) o7 D6 k
  186.   spit_node();6 o4 n5 C0 z6 K& G) N  f( s* G: c  W0 \
  187.   spit_node();
    * W+ C! }& }/ S# P' e

  188. : j# i, k1 r  g  m. o: s" |( C$ l
  189.   printf("\n----------active list[%d]------------\n",list_num(&active_head));
    ; h  s. |" v0 n( @. B
  190.   show_list(&active_head);
    6 w/ A- d( K" @4 Q; E
  191.   printf("\n--------------end-----------------\n");$ J7 E# y& h: T! w& m

  192. " k5 Z) \) s" O$ F7 |( k: ?
  193.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  
    9 R) k1 f5 p, H- A: G3 F
  194.   show_list(&free_head);) v( K" p) I) K8 U
  195.   printf("\n--------------end-----------------\n");0 v- z5 Q3 ]) d& `8 z2 x  p6 v
  196. 1 X0 B* j1 P5 p3 k# z( E% f
  197. }
复制代码

7 A% \; d; S4 s
运行结果如下:
微信图片_20200902223626.jpg
微信图片_20200902223630.jpg
list_head短小精悍,读者可以借鉴此文实现其他功能。
0 S0 N  Z& [% x5 ^+ T
8 i( V' Q$ `! A: U" O
收藏 评论1 发布时间:2020-9-2 22:42

举报

1个回答
sincomaster 回答时间:2020-9-3 11:46:23
头大

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版