<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">ARM-內核鏈表</span>
鏈表:
結構:
優點:建立鏈表時無需預先知道數據總量,可以隨機分配空間,可以高效地在鏈表中的任意位置實時插入或刪除數據。
開銷:主要是訪問的順序性和組織鏈的空間損失
傳統鏈表:
分類:單向鏈表,雙向鏈表,雙向循環鏈表
特點:傳統鏈表的指針域是指向下一個節點的數據處,這也就導致了當數據類型改變時,需要改變指針的類型,不具有通用性。
單向鏈表:
雙向鏈表
雙向循環鏈表
內核鏈表:
特點:節點的指針域指向下一個節點/上一個節點的指針域,由此可以統一指針的類型,具有一般通用性,使用指針域跟數據入口地址【設爲0地址】的offset來尋找數據入口,從而輸出節點數據;內核鏈表一般爲雙向循環鏈表。
內核鏈表指針結構【參照linux內核源碼】:
struct list_head{
struct list_head *next;
struct list_head *prev;
};
內核源碼中的內核鏈表函數:
INIT_LIST_HEAD:創建鏈表
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
list_add:在鏈表頭插入節點
/*
* 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:在鏈表尾插入節點
/*
* 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_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);
}
list_del:刪除節點
/*
* 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 = (void *)0xDEADBEEF;
entry->prev = (void *)0xBEEFDEAD;
}
list_entry:取出節點
#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) ({ \
const typeof(((type *)0)->member)*__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
/**
* 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:遍歷節點
/**
* 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; prefetch(pos->next), pos != (head); \
pos = pos->next)
內核鏈表使用例子:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/list.h>
/*定義節點結構*/
struct score{
int num;
int english;
int math;
struct list_head list;
}stu1, stu2, stu3;
struct list_head head_list;
struct list_head *pos;
struct score *tmp;
static int mylist_init(void)
{
/*1.初始化鏈表*/
INIT_LIST_HEAD(&head_list);
/*2.定義節點並在鏈尾插入節點*/
stu1.num = 1;
stu1.english = 91;
stu1.math = 81;
list_add_tail(&(stu1.list), &head_list);
stu2.num = 2;
stu2.english = 92;
stu2.math = 82;
list_add_tail(&(stu2.list), &head_list);
stu3.num = 3;
stu3.english = 93;
stu3.math = 83;
list_add_tail(&(stu3.list), &head_list);
/*3.遍歷節點以及取出節點*/
list_for_each(pos, &head_list)
{
tmp = list_entry(&pos, struct score, list);
printk("No.%d, english is %d, math is %d \n", tmp->num, tmp->english, tmp->math);
}
return 0;
}
static void mylist_exit(void)
{
/*1.刪除節點*/
list_del(&(stu1.list));
list_del(&(stu2.list));
list_del(&(stu3.list));
}
module_init(mylist_init);
module_exit(mylist_exit);
Tips:
1. 宏定義中的如需賦值,則傳入應爲左值,因爲宏定義中參數只是起到替代作用,並不進行參數賦值,故無法將右值轉換爲左值。