1. 程式人生 > >資料結構 筆記:Linux核心連結串列剖析

資料結構 筆記:Linux核心連結串列剖析

Linux核心連結串列的位置及依賴

-位置

·{linux-2.6.39}\\include\linux\list.h

-依賴

#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/poison.h>
#include <linux/prefetch.h>

移植時注意事項

-清楚檔案間的依賴

·剝離依賴檔案中與連結串列實現相關的程式碼

-清楚平臺相關程式碼(GNU C)

·({})

·typeof

·__builtin_prefetch

·static inline

Linux核心連結串列的實現

-帶頭結點的雙向迴圈連結串列,且頭結點為表中成員

-頭結點的next指標指向首結點

-頭結點的prev指標指向尾結點

Linux 核心連結串列的結點定義

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

Linux核心連結串列的建立及初始化

strcut Node
{
    struct list_head head;
    int value;
};

int main(void)
{
    struct Node 1 = {0};
    struct list_head* list = (struct list_head*)&1;

    INIT_LIST_HEAD(list);
    
    //...
}

Linux核心連結串列的插入插座

-在連結串列頭部插入:list_add(new,head)

-在連結串列尾部插入:list_add_tail(new,head)

Linux核心連結串列的遍歷

-正想遍歷:list_for_each(pos,head)

-逆向遍歷:list_for_each_prev(pos,head)

總結:

-Linux核心連結串列移植時需要提出依賴以及平臺相關程式碼

-Linux核心連結串列是帶頭結點的雙向迴圈連結串列

-使用Linux核心連結串列時需要自定義連結串列結點

-將struct list_head 作為結構體的第一個成員或者最後一個成員

-strcut list_head作為最後一個成員時,需要使用list_entry巨集

-list_entry的定義中使用了container_of巨集