Linux内核-链表
linux内核链表的定义(定义了双向链表,不含数据域)
定义在 /linux-source-3.13.0/include/linux/types.h 头文件中.
struct list_head {
struct list_head *next, *prev;
};
我们可以利用这个数据结构定义含有数据域的链表,如:
struct my_list
{
void * mydata; //void * 可以指向任何类型的数据
struct list_head list; //隐藏了链表指针
}
链表的声明和初始化宏(list.h)
定义在 /linux-source-3.13.0/include/linux/list.h 中.
#define LIST_HEAD_INIT(name) { &(name), &(name) }
初始化一个名为name的双向链表的头节点.(name.pre = &name , name.next = &name)
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
通过调用LIST_HEAD,来声明和初始化一个自己的链表头,产生一个空链表.如:LIST_HEAD(mylist_head)
在链表添加一个节点
定义在 /linux-source-3.13.0/include/linux/list.h 中
具体实现:
/*
* 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.
*/
static inline void list_add(struct list_head *new, struct list_head *head) //在head节点后插入new节点,循环链表没有首尾,head可以是任意节点
{
__list_add(new, head, head->next);
} /**
* 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) //在head节点前插入new节点
{
__list_add(new, head->prev, head);
}
遍历链表
定义在 /linux-source-3.13.0/include/linux/list.h 中
/**
* 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)
它实际上是一个for循环,利用传入的pos作为循环变量,从表头开始逐顶向后(next方向)移动pos,直至回到head.
/**
* 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_entry宏:从结构体(type)某成员变量(menber)指针(prt)来求出该结构体(type)的首指针.
Linux内核-链表的更多相关文章
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- Linux 内核链表
一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...
- 深入分析 Linux 内核链表
转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/ 一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- linux内核链表的移植与使用
一. Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...
- [国嵌攻略][108][Linux内核链表]
链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...
- linux内核链表的使用
linux内核链表:链表通常包括两个域:数据域和指针域.struct list_head{struct list_head *next,*prev;};include/linux/list.h中实现了 ...
随机推荐
- C语言编译器 cc 编译原理
生成一个可执行的文件通常需要经过以下几个步骤: 预处理你的源代码,去掉注释,以及其他技巧性的工作就像在 C 中展开宏. 检查代码的语法看你是否遵守了这个语言的规则.如果没有,编译器会给出 警告. 把源 ...
- c time_t 和 oc NSDate 的转换
c time_t 和 oc NSDate 的转换 1:time_t 转 oc NSDate time_t some_time_t=NULL; NSDate *someDate = [NSDate da ...
- IOS开发—UIDatePicker 日期/时间选取器(滚轮)
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- linux笔记:linux常用命令-网络命令
网络命令:ping(测试网络连通性) 网络命令:ifconfig(查看和设置网卡信息) 注意:在查看网卡信息时,直接输入ifconfig命令即可. 网络命令:last(列出目前和过去登入系统的用户信息 ...
- mysql通过binlog日志来恢复数据
简介 在生产的过程中有这么一个业务场景:比如我在2016-11-19 09:30:00 通过mysqldump的方式备份了数据库,但是在2016-11-19 10:30:00的时候数据库崩溃了,如果通 ...
- 能源项目xml文件标签释义--<mvc:annotation-driven>
<mvc:annotation-driven />的可选配置 <mvc:annotation-driven message-codes-resolver ="bean re ...
- web前端页面性能优化小结
影响用户访问的最大部分是前端的页面.网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢?其实应该是属于功能的表现. 而我 ...
- robotframework笔记15
资源和变量文件 用户关键字和变量 测试用例文件 和 测试套件 初始化文件只能用于文件在哪里 了,但 资源文件 提供一种机制来分享它们. 自 资源文件结构非常接近测试用例文件,它是 容易创建它们. 变量 ...
- http协议分析工具【转】
转自:http://www.cnblogs.com/klguang/p/4624333.html
- JavaScript设计模式的简单理解
设计模式可以理解为一系列的代码框架,我觉得主要涉及封装的概念.把实现某一功能的代码段封装在函数中,可以方便调用,同时利于代码的复用,提高了代码的可维护性.下面简单介绍一下几种设计模式的个人感受. 1. ...