linux kernel中的鏈表

來源:互聯網
上載者:User

  鏈表是C語言編程中常用的資料結構,比如我們要建一個整數鏈表,一般可能這麼定義:

?

1

2

3

4

struct int_node {

int val;

struct int_node *next;

};

  為了實現鏈表的插入、刪除、遍曆等功能,另外要再實現一系列函數,比如:

?

1

2

3

4

5

6

7

8

9

void insert_node(struct int_node *head, struct int_node *current);

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

}

}

  如果我們的代碼裡只有這麼一個資料結構的話,這樣做當然沒有問題,但是當代碼的規模足夠大,需要管理很多種鏈表,難道需要為每一種鏈表都要實現一套插入、刪除、遍曆等功能函數嗎?熟悉C++的同學可能會說,我們可以用標準模板庫啊,但是,我們這裡談的是C,在C語言裡有沒有比較好的方法呢?

Mr.Dave在他的部落格裡介紹了自己的實現,這個實現是個很好的方案,各位不妨可以參考一下。在本文中,我們把目光投向當今開源界最大的C項目--Linux Kernel,看看Linux核心如何解決這個問題。

  Linux核心中一般使用雙向鏈表,聲明為struct list_head,這個結構體是在include/linux/types.h中定義的,鏈表的訪問是以宏或者內嵌函式的形式在include/linux/list.h中定義。

?

1

2

3

struct list_head {

struct list_head *next, *prev;

};

  Linux核心為鏈表提供了一致的提供者。

?

1

2

3

4

5

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);

   以上只是從Linux核心裡摘選的幾個常用介面,更多的定義請參考Linux核心原始碼。我們先通過一個簡單的實作來對Linux核心如何處理鏈表建立一個感性的認識。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#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;

}

  看完這個實作,是不是覺得在C代碼裡管理一個鏈表也很簡單呢?代碼中包含的標頭檔list.h是我從Linux核心裡抽取出來並做了一點修改的鏈表處理代碼,現附在這裡給大家參考,使用的時候只要把這個標頭檔包含到自己的工程裡即可。


#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

  list_head通常是嵌在資料結構內使用,在上文的實作中我們還是以整數鏈表為例,int_node的定義如下:

?

1

2

3

4

struct int_node {

int val;

struct list_head list;

};

  使用list_head組織的鏈表的結構如所示:

  遍曆鏈表是用宏list_for_each來完成。

?

1

2

3

#define list_for_each(pos, head) \

for (pos = (head)->next; prefetch(pos->next), pos != (head); \

pos = pos->next)

  在這裡,pos和head均是struct list_head。在遍曆的過程中如果需要訪問節點,可以用list_entry來取得這個節點的基址。

?

1

2

#define list_entry(ptr, type, member) \

container_of(ptr, type, member)

  我們來看看container_of是如何?的。如所示,我們已經知道TYPE結構中MEMBER的地址,如果要得到這個結構體的地址,只需要知道MEMBER在結構體中的位移量就可以了。如何得到這個位移量地址呢?這裡用到C語言的一個小技巧,我們不妨把結構體投影到地址為0的地方,那麼成員的絕對位址就是位移量。得到位移量之後,再根據ptr指標指向的地址,就可以很容易的計算出結構體的地址。

  list_entry就是通過上面的方法從ptr指標得到我們需要的type結構體。

  Linux核心代碼博大精深,陳莉君老師曾把它形容為“覆壓三百餘裡,隔離天日”(摘自《阿房宮賦》),可見其內容之豐富、結構之龐雜。核心裡有著眾多重要的資料結構,具有相關性的資料結構之間很多都是用本文介紹的鏈表組織在一起,看來list_head結構雖小,作用可真不小。

  Linux核心是個偉大的工程,其原始碼裡還有很多精妙之處,值得C/C++程式員認真去閱讀,即使我們不去做核心相關的工作,閱讀精彩的代碼對程式員自我修養的提高也是大有裨益的。

kernel中list的定義:http://lxr.oss.org.cn/source//include/linux/list.h?v=2.6.30

原文:http://www.cnblogs.com/wwang/archive/2010/11/28/1889281.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.