利用linux內核代碼玩轉c鏈表

鏈表是C語言編程中經常使用的數據結構,好比咱們要建一個整數鏈表,通常可能這麼定義:html

struct int_node {
        int val;
        struct int_node *next;
};

爲了實現鏈表的插入、刪除、遍歷等功能,另外要再實現一系列函數,好比:node

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
        }
}

若是咱們的代碼裏只有這麼一個數據結構的話,這樣作固然沒有問題,可是當代碼的規模足夠大,須要管理不少種鏈表,難道須要爲每一種鏈表都要實現一套插入、刪除、遍歷等功能函數嗎?linux

熟悉C++的同窗可能會說,咱們能夠用標準模板庫啊,可是,咱們這裏談的是C,在C語言裏有沒有比較好的方法呢?程序員

Mr.Dave在他的博客裏介紹了本身的實現,這個實現是個很好的方案,各位不妨能夠參考一下。在本文中,咱們把目光投向當今開源界最大的C項目--Linux Kernel,看看Linux內核如何解決這個問題。編程

Linux內核中通常使用雙向鏈表,聲明爲struct list_head,這個結構體是在include/linux/types.h中定義的,鏈表的訪問是以宏或者內聯函數的形式在include/linux/list.h中定義。數據結構

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

Linux內核爲鏈表提供了一致的訪問接口:函數

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內核源代碼oop

咱們先經過一個簡單的實做來對Linux內核如何處理鏈表創建一個感性的認識。測試

代碼中包含的頭文件list.h是從Linux內核裏抽取出來並作了一點修改的鏈表處理代碼,使用的時候只要把這個頭文件包含進來便可。fetch

list.h:

#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

測試代碼main.c

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

編譯運行:

$ ls
list.h  main.c
$ gcc main.c
$ ./a.out 
val = 3
val = 2

list_head一般是嵌在數據結構內使用,在上文的實做中咱們仍是以整數鏈表爲例,int_node的定義以下:

struct int_node {
        int val;
        struct list_head list;
};

使用list_head組織的鏈表的結構以下圖所示:

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

#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來取得這個節點的基址。

#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++程序員認真去閱讀,即便咱們不去作內核相關的工做,閱讀精彩的代碼對程序員自我修養的提升也是大有裨益的。

相關文章
相關標籤/搜索