內核 Linux中雙向鏈表的經典實現

1. Linux中雙向鏈表介紹

Linux雙向鏈表的定義主要涉及到兩個文件:
include/linux/types.h
include/linux/list.h

Linux中雙向鏈表的使用思想
它是將雙向鏈表節點嵌套在其它的結構體中;在遍歷鏈表的時候,根據雙鏈表節點的指針獲取"它所在結構體的指針",從而再獲取數據。

我舉個例子來說明,可能比較容易理解。假設存在一個社區中有很多人,每個人都有姓名和年齡。通過雙向鏈表將人進行關聯的模型圖如下:

person代表人,它有name和age屬性。爲了通過雙向鏈表對person進行鏈接,我們在person中添加了list_head屬性。通過list_head,我們就將person關聯起來了。

struct person 
{ 
    int age; 
    char name[20];
    struct list_head list; 
};

2. Linux中雙向鏈表的源碼分析

(01). 節點定義

struct list_head {
    struct list_head *next, *prev;
};
//雖然名稱list_head,但是它既是雙向鏈表的表頭,也代表雙向鏈表的節點。

(02). 初始化節點


#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_HEAD的作用是定義表頭(節點):新建雙向鏈表表頭name,並設置name的前繼節點和後繼節點都是指向name本身。
//LIST_HEAD_INIT的作用是初始化節點:設置name節點的前繼節點和後繼節點都是指向name本身。
//INIT_LIST_HEAD和LIST_HEAD_INIT一樣,是初始化節點:將list節點的前繼節點和後繼節點都是指向list本身。

(03). 添加節點

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

static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}
/*
__list_add(new, prev, next)的作用是添加節點:將new插入到prev和next之間。在linux中,以"__"開頭的函數意味着是內核的內部接口,外部不應該調用該接口。
list_add(new, head)的作用是添加new節點:將new添加到head之後,是new稱爲head的後繼節點。
list_add_tail(new, head)的作用是添加new節點:將new添加到head之前,即將new添加到雙鏈表的末尾。
*/

(04). 刪除節點

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

static inline void __list_del_entry(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

static inline void list_del_init(struct list_head *entry)
{
    __list_del_entry(entry);
    INIT_LIST_HEAD(entry);
}
/*
__list_del(prev, next) 和__list_del_entry(entry)都是linux內核的內部接口。
__list_del(prev, next) 的作用是從雙鏈表中刪除prev和next之間的節點。
__list_del_entry(entry) 的作用是從雙鏈表中刪除entry節點。

list_del(entry) 和 list_del_init(entry)是linux內核的對外接口。
list_del(entry) 的作用是從雙鏈表中刪除entry節點。
list_del_init(entry) 的作用是從雙鏈表中刪除entry節點,並將entry節點的前繼節點和後繼節點都指向entry本身。
*/

(05). 替換節點


static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}

//list_replace(old, new)的作用是用new節點替換old節點。

(06). 判斷雙鏈表是否爲空

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}
//list_empty(head)的作用是判斷雙鏈表是否爲空。它是通過區分"表頭的後繼節點"是不是"表頭本身"來進行判斷的。

(07). 獲取節點

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)
/*
list_entry(ptr, type, member) 實際上是調用的container_of宏。
它的作用是:根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針。
*/

(08). 遍歷節點

#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)
/*
list_for_each(pos, head)和list_for_each_safe(pos, n, head)的作用都是遍歷鏈表。但是它們的用途不一樣!
list_for_each(pos, head)通常用於獲取節點,而不能用到刪除節點的場景。
list_for_each_safe(pos, n, head)通常刪除節點的場景。
*/

3. Linux中雙向鏈表的使用示例

雙向鏈表代碼(list.h)

#ifndef _LIST_HEAD_H
#define _LIST_HEAD_H

// 雙向鏈表節點
struct list_head {
    struct list_head *next, *prev;
};

// 初始化節點:設置name節點的前繼節點和後繼節點都是指向name本身。
#define LIST_HEAD_INIT(name) { &(name), &(name) }

// 定義表頭(節點):新建雙向鏈表表頭name,並設置name的前繼節點和後繼節點都是指向name本身。
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

// 初始化節點:將list節點的前繼節點和後繼節點都是指向list本身。
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

// 添加節點:將new插入到prev和next之間。
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;
}

// 添加new節點:將new添加到head之後,是new稱爲head的後繼節點。
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

// 添加new節點:將new添加到head之前,即將new添加到雙鏈表的末尾。
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

// 從雙鏈表中刪除entry節點。
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

// 從雙鏈表中刪除entry節點。
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

// 從雙鏈表中刪除entry節點。
static inline void __list_del_entry(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

// 從雙鏈表中刪除entry節點,並將entry節點的前繼節點和後繼節點都指向entry本身。
static inline void list_del_init(struct list_head *entry)
{
    __list_del_entry(entry);
    INIT_LIST_HEAD(entry);
}

// 用new節點取代old節點
static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}

// 雙鏈表是否爲空
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

// 獲取"MEMBER成員"在"結構體TYPE"中的位置偏移
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

// 根據"結構體(type)變量"中的"域成員變量(member)的指針(ptr)"來獲取指向整個結構體變量的指針
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

// 遍歷雙向鏈表
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

#endif

雙向鏈表測試代碼(test.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

struct person
{
    int age;
    char name[20];
    struct list_head list;
};

void main(int argc, char* argv[])
{
    struct person *pperson;
    struct person person_head;
    struct list_head *pos, *next;
    int i;

    // 初始化雙鏈表的表頭
    INIT_LIST_HEAD(&person_head.list);

    // 添加節點
    for (i=0; i<5; i++)
    {
        pperson = (struct person*)malloc(sizeof(struct person));
        pperson->age = (i+1)*10;
        sprintf(pperson->name, "%d", i+1);
        // 將節點鏈接到鏈表的末尾
        // 如果想把節點鏈接到鏈表的表頭後面,則使用 list_add
        list_add_tail(&(pperson->list), &(person_head.list));
    }

    // 遍歷鏈表
    printf("==== 1st iterator d-link ====\n");
    list_for_each(pos, &person_head.list)
    {
        pperson = list_entry(pos, struct person, list);
        printf("name:%-2s, age:%d\n", pperson->name, pperson->age);
    }

    // 刪除節點age爲20的節點
    printf("==== delete node(age:20) ====\n");
    list_for_each_safe(pos, next, &person_head.list)
    {
        pperson = list_entry(pos, struct person, list);
        if(pperson->age == 20)
        {
            list_del_init(pos);
            free(pperson);
        }
    }

    // 再次遍歷鏈表
    printf("==== 2nd iterator d-link ====\n");
    list_for_each(pos, &person_head.list)
    {
        pperson = list_entry(pos, struct person, list);
        printf("name:%-2s, age:%d\n", pperson->name, pperson->age);
    }

    // 釋放資源
    list_for_each_safe(pos, next, &person_head.list)
    {
        pperson = list_entry(pos, struct person, list);
        list_del_init(pos);
        free(pperson);
    }

}

運行結果

巨人的肩膀:https://www.cnblogs.com/skywang12345/p/3562146.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章