【7】Linux-内核链表

<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. 宏定义中的如需赋值,则传入应为左值,因为宏定义中参数只是起到替代作用,并不进行参数赋值,故无法将右值转换为左值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值