找回密码
 注册
关于网站域名变更的通知
查看: 635|回复: 3
打印 上一主题 下一主题

玩转C链表

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-7-17 14:48 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
# m6 Q/ L+ i6 d: Z! o2 r2 h
链表是C语言编程中常用的数据结构,比如我们要建一个整数链表,一般可能这么定义:/ {, f- h" v5 e! A9 U
+ l0 o* C6 n& @/ t7 S/ Z! S- A
  • struct int_node {
  •         int val;
  •         struct int_node *next;
  • };
    / f) L7 j* j) W9 p( I
- {- S2 U$ j% E; h2 B. _# I

& j+ O1 X2 K. U2 z6 [% U) R为了实现链表的插入、删除、遍历等功能,另外要再实现一系列函数,比如:, ]4 d( }, U( s
" K9 H3 @4 F  m/ U4 O, f; o3 o
  • void insert_node(struct int_node **head, int val);
  • void delete_node(struct int_node *head, struct int_node *current);
  • void access_node(struct int_node *head)
  • {
  •         struct int_node *node;
  •         for (node = head; node != NULL; node = node->next) {
  •                 // do something here
  •         }
  • }* e6 a8 z+ c4 D& E  m
7 j( {! a4 I* B% W

8 U+ \4 F4 v/ g3 @  如果我们的代码里只有这么一个数据结构的话,这样做当然没有问题,但是当代码的规模足够大,需要管理很多种链表,难道需要为每一种链表都要实现一套插入、删除、遍历等功能函数吗?
+ Y5 z; b( K% E5 p5 N  A/ q
- i9 w  j' e) _- j+ L熟悉C++的同学可能会说,我们可以用标准模板库啊,但是,我们这里谈的是C,在C语言里有没有比较好的方法呢?7 Y6 k# n, x+ h0 M) }9 P, ~
& A* \  R( {) _8 r% d
Mr.Dave在他的博客里介绍了自己的实现,这个实现是个很好的方案,各位不妨可以参考一下。在本文中,我们把目光投向当今开源界最大的C项目--Linux Kernel,看看Linux内核如何解决这个问题。
. k, N! V; e3 h+ t6 c# W
9 R" p3 L0 i3 b- NLinux内核中一般使用双向链表,声明为struct list_head,这个结构体是在include/linux/types.h中定义的,链表的访问是以宏或者内联函数的形式在include/linux/list.h中定义。" A5 E$ k9 v: e  u3 T) O( `

. ?! [! F0 ^- k: ^, g8 _, }
  • struct list_head {
  •     struct list_head *next, *prev;
  • };( }5 g0 _$ i2 W, [- z* ^) _$ a

4 {5 x' z' _2 P' E3 k( @* R# e% y2 Y% w, P1 P- o  G
Linux内核为链表提供了一致的访问接口。( @( F( K8 X) g; M& y
' F' Y) q: \- J4 h* c9 a1 Z$ C- X% H/ N
  • void INIT_LIST_HEAD(struct list_head *list);
  • void list_add(struct list_head *new, struct list_head *head);
  • void list_add_tail(struct list_head *new, struct list_head *head);
  • void list_del(struct list_head *entry);
  • int list_empty(const struct list_head *head);
    # t' d' e0 j0 {! k
! W% l0 h- Q7 d7 d1 W
  ]( M" L; M6 Z( }' L
以上只是从Linux内核里摘选的几个常用接口,更多的定义请参考Linux内核源代码。
5 y  H0 y0 d. [- z! Z, R! W8 r: ?; \: E' N% f  C* U: h
我们先通过一个简单的实作来对Linux内核如何处理链表建立一个感性的认识。% |8 K! O2 J8 c, ~+ Z2 T

$ e& L: _# P  j
  • #include <stdio.h>
  • #include "list.h"
  • struct int_node {
  •         int val;
  •         struct list_head list;
  • };
  • int main()
  • {
  •         struct list_head head, *plist;
  •         struct int_node a, b;
  •         a.val = 2;
  •         b.val = 3;
  •         INIT_LIST_HEAD(&head);
  •         list_add(&a.list, &head);
  •         list_add(&b.list, &head);
  •         list_for_each(plist, &head) {
  •                 struct int_node *node = list_entry(plist, struct int_node, list);
  •                 printf("val = %d\n", node->val);
  •         }
  •         return 0;
  • }2 ^, `0 r  e6 _8 m4 f
      
" }' ?1 c( C0 T; V9 Y9 s! P  a) g( \) `0 F# z1 ~" D4 s
看完这个实作,是不是觉得在C代码里管理一个链表也很简单呢?5 F* Q5 O- ?' D* M  t! ~; t
6 X8 G- r1 B0 q8 I* j) K/ B9 k
代码中包含的头文件list.h是我从Linux内核里抽取出来并做了一点修改的链表处理代码,现附在这里给大家参考,使用的时候只要把这个头文件包含到自己的工程里即可。
" o. J2 \, p( H  o( B1 x: J+ X8 n0 b/ S1 K# C8 Z, g: B, v% z/ t+ l
  • #ifndef __C_LIST_H
  • #define __C_LIST_H
  • typedef unsigned char     u8;
  • typedef unsigned short    u16;
  • typedef unsigned int      u32;
  • typedef unsigned long     size_t;
  • #define offsetof(TYPE, MEMBER)   ((size_t) &((TYPE *)0)->MEMBER)
  • /**
  • * container_of - cast a member of a structure out to the containing structure
  • * @ptr:    the pointer to the member.
  • * @type:    the type of the container struct this is embedded in.
  • * @member:    the name of the member within the struct.
  • *
  • */
  • #define container_of(ptr, type, member) (type *)((char *)ptr -offsetof(type,member))
  • /*
  • * These are non-NULL pointers that will result in page faults
  • * under normal circumstances, used to verify that nobody uses
  • * non-initialized list entries.
  • */
  • #define LIST_POISON1  ((void *) 0x00100100)
  • #define LIST_POISON2  ((void *) 0x00200200)
  • struct list_head {
  •     struct list_head *next, *prev;
  • };
  • /**
  • * list_entry - get the struct for this entry
  • * @ptr:    the &struct list_head pointer.
  • * @type:    the type of the struct this is embedded in.
  • * @member:    the name of the list_struct within the struct.
  • */
  • #define list_entry(ptr, type, member) \
  •     container_of(ptr, type, member)
  • #define LIST_HEAD_INIT(name) { &(name), &(name) }
  • #define LIST_HEAD(name) \
  •     struct list_head name = LIST_HEAD_INIT(name)
  • static inline void INIT_LIST_HEAD(struct list_head *list)
  • {
  •     list->next = list;
  •     list->prev = list;
  • }
  • /**
  • * list_for_each    -    iterate over a list
  • * @pos:    the &struct list_head to use as a loop counter.
  • * @head:    the head for your list.
  • */
  • #define list_for_each(pos, head) \
  •     for (pos = (head)->next; pos != (head); pos = pos->next)
  • /**
  • * list_for_each_r    -    iterate over a list reversely
  • * @pos:    the &struct list_head to use as a loop counter.
  • * @head:    the head for your list.
  • */
  • #define list_for_each_r(pos, head) \
  •     for (pos = (head)->prev; pos != (head); pos = pos->prev)
  • /*
  • * Insert a new entry between two known consecutive entries.
  • *
  • * This is only for internal list manipulation where we know
  • * the prev/next entries already!
  • */
  • static inline void __list_add(struct list_head *new,
  •                   struct list_head *prev,
  •                   struct list_head *next)
  • {
  •     next->prev = new;
  •     new->next = next;
  •     new->prev = prev;
  •     prev->next = new;
  • }
  • /**
  • * list_add - add a new entry
  • * @new: new entry to be added
  • * @head: list head to add it after
  • *
  • * Insert a new entry after the specified head.
  • * This is good for implementing stacks.
  • */
  • static inline void list_add(struct list_head *new, struct list_head *head)
  • {
  •     __list_add(new, head, head->next);
  • }
  • /**
  • * list_add_tail - add a new entry
  • * @new: new entry to be added
  • * @head: list head to add it before
  • *
  • * Insert a new entry before the specified head.
  • * This is useful for implementing queues.
  • */
  • static inline void list_add_tail(struct list_head *new, struct list_head *head)
  • {
  •     __list_add(new, head->prev, head);
  • }
  • /*
  • * Delete a list entry by making the prev/next entries
  • * point to each other.
  • *
  • * This is only for internal list manipulation where we know
  • * the prev/next entries already!
  • */
  • static inline void __list_del(struct list_head * prev, struct list_head * next)
  • {
  •     next->prev = prev;
  •     prev->next = next;
  • }
  • /**
  • * list_del - deletes entry from list.
  • * @entry: the element to delete from the list.
  • * Note: list_empty on entry does not return true after this, the entry is
  • * in an undefined state.
  • */
  • static inline void list_del(struct list_head *entry)
  • {
  •     __list_del(entry->prev, entry->next);
  •     entry->next = LIST_POISON1;
  •     entry->prev = LIST_POISON2;
  • }
  • /**
  • * list_empty - tests whether a list is empty
  • * @head: the list to test.
  • */
  • static inline int list_empty(const struct list_head *head)
  • {
  •     return head->next == head;
  • }
  • static inline void __list_splice(struct list_head *list,
  •                  struct list_head *head)
  • {
  •     struct list_head *first = list->next;
  •     struct list_head *last = list->prev;
  •     struct list_head *at = head->next;
  •     first->prev = head;
  •     head->next = first;
  •     last->next = at;
  •     at->prev = last;
  • }
  • /**
  • * list_splice - join two lists
  • * @list: the new list to add.
  • * @head: the place to add it in the first list.
  • */
  • static inline void list_splice(struct list_head *list, struct list_head *head)
  • {
  •     if (!list_empty(list))
  •         __list_splice(list, head);
  • }
  • #endif // __C_LIST_H
    4 C3 w3 A8 R/ _  w( M' {$ Y7 c6 i: q

" e2 z. o9 h7 o# N8 ^; m* ~, Q5 t
& j: f( U2 C( Clist_head通常是嵌在数据结构内使用,在上文的实作中我们还是以整数链表为例,int_node的定义如下:6 B% j) W4 t$ c7 q* U& G9 k
$ O- _6 }1 X. x6 j( k
  • struct int_node {
  •         int val;
  •         struct list_head list;
  • };
    / k& }. A: V  x/ j- K6 T

. \4 J  I! B9 @3 H8 T* Q- ~# I5 g% `2 O$ M
使用list_head组织的链表的结构如下图所示:7 f. ~/ r1 m. k
1 B. I  |" j6 D$ e( s2 B; K
) c& c( r: a% n1 P/ ]; z6 c/ p
( Z" v9 F# M/ j+ t( k
9 }+ r. q- s/ ^! r8 D
遍历链表是用宏list_for_each来完成。4 j0 L, I& L/ a2 v

1 C$ l5 F/ s. N  ]
  • #define list_for_each(pos, head) \
  •     for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  •             pos = pos->next)
    / E: }- o% X6 ]8 D" _4 I

5 c, f1 C" C$ E- |& [/ E$ S1 J$ O2 |
. X$ g# Z6 V; C2 m1 d' X; S在这里,pos和head均是struct list_head。在遍历的过程中如果需要访问节点,可以用list_entry来取得这个节点的基址。
. s7 p0 E& a1 Z3 Z7 Z' X! x" ?
( U6 a0 E7 ?9 }' ]4 ^5 t
  • #define list_entry(ptr, type, member) \
  •     container_of(ptr, type, member)" c* {( c4 A1 E( y7 q2 ^% U
; E6 ?% ?/ V! h; R' D4 ?

" g( K# a8 v% m2 U" v  p我们来看看container_of是如何实现的。如下图所示,我们已经知道TYPE结构中MEMBER的地址,如果要得到这个结构体的地址,只需要知道MEMBER在结构体中的偏移量就可以了。如何得到这个偏移量地址呢?这里用到C语言的一个小技巧,我们不妨把结构体投影到地址为0的地方,那么成员的绝对地址就是偏移量。得到偏移量之后,再根据ptr指针指向的地址,就可以很容易的计算出结构体的地址。( M3 h* ]* T+ S) j" G

# D, V! e- l9 c! x* _3 f3 y
) Z3 S4 \! Y  U; T  h) u: C9 v* q' ^$ j7 d
list_entry就是通过上面的方法从ptr指针得到我们需要的type结构体。1 D5 _' ~9 l" e9 R! D- @: l

% [1 Z$ W/ \' u6 N1 j) G* G  NLinux内核代码博大精深,陈莉君老师曾把它形容为“覆压三百余里,隔离天日”(摘自《阿房宫赋》),可见其内容之丰富、结构之庞杂。内核里有着众多重要的数据结构,具有相关性的数据结构之间很多都是用本文介绍的链表组织在一起,看来list_head结构虽小,作用可真不小。
( b8 s7 V( ~5 q9 N& |
5 t) w* v3 b) K% y2 i( o, aLinux内核是个伟大的工程,其源代码里还有很多精妙之处,值得C/C++程序员认真去阅读,即使我们不去做内核相关的工作,阅读精彩的代码对程序员自我修养的提高也是大有裨益的。
1 k+ q  D# r: ?8 y( ], r# X3 _2 x6 t( u1 y: S
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 09:03 , Processed in 0.171875 second(s), 27 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表