linux內核容器數據結構鏈錶list.h

dhy9974 2022-01-07 20:08:51 阅读数:965

linux 容器 list.h list

最近想看看linux設備驅動開發類的書籍。記錄一下,方便以後查閱。

一個驅動程序管理很多個設備,為了在驅動程序中跟踪每個設備,一般來說都是用到鏈錶。鏈錶有兩種類型:單鏈錶和雙鏈錶。

內核開發者要保持最少的代碼,只實現了循環雙向鏈錶,因為這個結構能够實現FIFO和LIFO。為了使用內核實現的鏈錶,需要加入頭文件<linux/list.h>,其中最核心的數據結構是struct list_head,其定義如下:

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

這裏主要說明一下平時我們看到的循環雙向鏈錶和內核實現的循環雙向鏈錶的區別。

我們在上學時學到的循環雙向鏈錶一般都是這樣實現:

struct car {
int number;
char *color;
struct car *prev;
struct car *next;
};

如下圖所示:

這樣的實現方式是結構體的成員加入指向自己本身的指針。

具體的建立鏈錶,添加節點,删除節點,遍曆鏈錶的實現這裏就不寫了。 

但是這樣的實現方式也有一個缺點,就是當我們有各種不同類別的對象時,需要為每一種對象的鏈錶寫一個鏈錶的增删改查的操作。那麼有沒有一種統一個方式呢?當然有:

既然傳統實現鏈錶的方法不能包含所有類別的對象,那麼我們可以讓不同類別的對象包含同一種“鏈錶節點”!

解釋一下剛才說的那句話,也就是內核鏈錶的實現方法,我們這樣定義car結構體:

struct car {
int number;
char *color;
struct list_head list;
};

其中 struct list_head就是本文最開始提到的結構。

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

這樣實現的方式跟傳統方式的區別主要在於結構體裏面的指針類型。傳統方法裏面的指針是指向car這個結構本身:struct car *prev, *next;。而現在方法裏面包含一個list_head結構,list_head結構裏面的指針是指向list_head這個結構的,而不是指向整個car結構。

如下圖所示:

看到這裏相信有人會有點疑惑:這樣實現循環鏈錶的方式只是list_head互相指來指去。我們訪問不了整個car結構裏面的數據部分啊,比如car裏面的number和color,即使獲得了當前節點的指針,但是這個指針不是指向car結構體的,訪問不了結構體的數據域啊。別著急,請往下看。

linux內核函數定義了一個宏container_of,具體定義在include/linux/kernel.h中。這個宏的主要作用:根據一個結構體的某個成員地址,名稱和結構體類型,可以得到指向這個結構體的指針。這一點很容易想通,整個結構體所有成員的內存大小都知道了,根據某一個成員的地址,計算一下偏移量,就可以獲得整個結構體的首地址。

定義如下:

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

解釋一下: 

container_of(ptr, type, member)
包含的參數如下:
ptr:指向結構體某成員的指針(結構體某成員的地址)
type:整個結構體的類型
member: ptr所指向結構體成員的名字

看到這裏,剛才的那個疑惑就可以解釋了。雖然循環鏈錶的實現方式是結構體裏面的list_head在指來指去,當我們需要訪問結構體裏面的數據域時就可以根據car這個結構體成員list_head的地址找到整個結構體的地址,進而對結構體內的數據域進行訪問。

看到這裏應該就豁然開朗了:接下來看一下鏈錶的建立,初始化,增加節點,删除節點,和遍曆鏈錶吧。

1.初始化和建立鏈錶:

有兩種方法創建和初始化鏈錶:

/* 方法1:*/
struct list_head carlist;
INIT_LIST_HEAD(&carlist);
//宏定義如下:
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/* 方法2:*/
LIST_HEAD(carlist);
//宏定義如下:
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define LIST_HEAD_INIT(name) { &(name), &(name) }

 這兩種方法都是都是建立了struct list_head 類型的變量,也就是循環鏈錶的頭節點,這個節點是不包含數據域部分的。跟car這個結構體裏面的數據部分無關。

2.創建和添加鏈錶節點

 主要有兩個接口:

         頭插法list_add(),這種方法可以實現堆棧。

         尾插法lsit_add_tail(),這種方法可以實現隊列。

尾插法和頭插法都調用同一個內部函數_list_add,頭插法好理解,直接就在head和head->next之間插入節點即可。尾插法其實也一樣,因為是循環雙向鏈錶,頭節點的前一個節點就是尾節點,所以就在head->prev,head之間插入節點即可。

/* 頭插法 */
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 將頭節點,頭節點的下一個節點,待插入的節點作為參數,
跟普通的鏈錶實現一樣 */
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;
}

以carlist為例:

/* 頭插法 */
/* 創建鏈錶節點 */
struct car *redcar = (struct car *)malloc(sizeof(struct car));
struct car *bluecar = (struct car *)malloc(sizeof(struct car));
/* 初始化節點 */
INIT_LIST_HEAD(&bluecar->list);
INIT_LIST_HEAD(&redcar->list);
/* 填充字段 */
redcar->color = "red";
redcar->number = 1;
bluecar->color = "blue";
bluecar->door_number = 2;
/* 加入鏈錶中 */
list_add(&redcar->list, &carlist);
list_add(&bluecar->list, &carlist);

3.删除鏈錶節點

 接口:list_del()

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

删除後,為什麼還要設置删除掉的節點指針呢?因為删除後,該選節點已不在鏈錶當中,因此不會再使用。LIST_POISON1/2是兩個不會存在於內核空間的地址,如果使用,就會報錯 。

#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)

注意,list_del只是斷開了節點的prev和next的指針,分配給當前節點的內存還需要手動釋放。 

以carlist為例,删除紅色的車:

list_del(&redcar->list);
free(redcar);

4.鏈錶的遍曆

接口:list_for_each_entry

我們可以看到使用了container_of宏,以car結構為例,即根據car結構體的成員struct list_head list的地址獲取整個結構體的地址。

/**
* list_for_each_entry - 遍曆鏈錶
* @pos: 用於迭代,就像是for(i=0,i<foo,i++)一樣
* @head: 鏈錶的頭節點指針
* @member: list_struct這個結構的名稱,我們的例子中是list
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

以carlist為例:

struct car *acar;
list_for_each_entry(acar, &carlist, list)
{
printf("the car's color:%s\n",acar->color);
}

5.整合一下整個過程:

#include "list.h"
#include <stdio.h>
#include <stdlib.h>
struct car
{
int number;
char *color;
struct list_head list;
};
int main()
{
/* 建立鏈錶,頭節點 */
static LIST_HEAD(carlist);
/* 創建小車實例1,並插入鏈錶 */
struct car *redcar = (struct car *)malloc(sizeof(struct car));
INIT_LIST_HEAD(&redcar->list); /* 初始化所創建的節點 */
redcar->color = "red";
redcar->number = 1;
list_add(&redcar->list, &carlist);
/* 創建小車實例2,並插入鏈錶 */
struct car *bluecar = (struct car *)malloc(sizeof(struct car));
INIT_LIST_HEAD(&bluecar->list);
bluecar->color = "blue";
bluecar->number = 2;
list_add(&bluecar->list, &carlist);
/* 創建小車實例3,並插入鏈錶 */
struct car *yellowcar = (struct car *)malloc(sizeof(struct car));
INIT_LIST_HEAD(&yellowcar->list);
yellowcar->color = "yellow";
yellowcar->number = 3;
list_add(&yellowcar->list, &carlist);
/* 遍曆鏈錶 */
struct car *acar;
list_for_each_entry(acar, &carlist, list)
{
printf("the car color:%s,number:%d\n",acar->color,acar->number);
}
/* 删除一個元素 */
list_del(&redcar->list);
free(redcar);
printf("\nwe delete red car\n");
/* 再次遍曆鏈錶 */
list_for_each_entry(acar, &carlist, list)
{
printf("the car color:%s,number:%d\n",acar->color,acar->number);
}
}

附上list.h的代碼,這個list.h是整合了一下linux裏面list.h的鏈錶函數。在使用時,只需要加入list.h即可。

/*
* Copyright (C) 2012 Fusion-io. All rights reserved.
*
* This header was taken from the Linux kernel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next, *prev;
};
#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;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
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;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* 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.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
#else
extern void list_add(struct list_head *new, struct list_head *head);
#endif
/**
* 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.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
#else
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
* Note: if 'old' was empty, it will be overwritten.
*/
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 void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* 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;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
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);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* 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)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* __list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This variant differs from list_for_each() in that it's the
* simplest possible list iteration code, no prefetching is done.
* Use this for code that knows the list to be very short (empty
* or 1 entry) most of the time.
*/
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); \
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#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_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_struct within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue.
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_continue
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_from
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_for_each_entry_safe_reverse
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member), \
n = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.prev, typeof(*n), member))
#endif

版权声明:本文为[dhy9974]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201072008505162.html