Linux list_head
在linux kernel里面链表应用非常广泛。
我们在应用程序中,定义一个链表结构通常要包含数据域,如下:
typedef struct _listNode{
int data;
struct _listNode *prev, *next;
}ListNode;
但在内核代码中,定义的链表都没有数据域, 如下:
struct list_head {
struct list_head *next, *prev;
};
如果需要定义双链表结构,则在数据结构中包含一个list_head的成员变量,
struct book{
int data;
struct list_head list;
}
假设我们有另外一个结构体person,里面也有一个链表,这个链表是book的双链表。(代表一个人有多本书)
struct person{
char *name;
int booknum;
struct list_head booklist;
}
我们在初始化时,创建book的结构,并且将book加入到person的book list中。
struct person *myperson;
book= kmalloc(sizeof(struct book), GFP_KERNEL);
list_add_tail(&book->list, &myperson->booklist);
person->booknum++
那么后续我们知道booklist结构的地址(address A),如何知道booklist中的book结构(address B)呢?

struct book mybook;
list_for_each_entry(mybook, &myperson->booklist, list) //遍历myperson->booklist,每个元素放在mybook.
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
最终展开的myperson->booklist的第一个元素的book结构为list_entry((&myperson->booklist)->next, struct book, list)
可以看出通过list_entry来获取myperson->booklist中的book结构。
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define container_of(ptr, type, member) ({ \//ptr为(&myperson->booklist)的元素的地址(address A),type为实际的结构struct book,list是book中的list成员名字
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
将list_entry展开
((struct book*)((char *)((&myperson->booklist)->next)-(size_t)(&((struct book *)0)->list)))
(unsigned long)(&((struct book *)0)->list))即list相对于book 首地址的偏移(上图的offset)。
1. ( (type*)0 ) 将零转型为type类型指针;
2. ((tpye*)0)->member访问结构中的数据成员;
3. &( ( (type*)0 )->member)取出数据成员的地址;
4.(size_t)(&(((type*)0)->member))转换成size_t类型,即member相对type结构首地址的偏移。
myperson的booklist 的地址(address A) 减去 偏移(offset)即为book结构首地址(Address B)。
Linux list_head的更多相关文章
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- linux 内核分析之list_head
转自:http://www.cnblogs.com/riky/archive/2006/12/28/606242.html 一.链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指针将一 ...
- linux内核的双链表list_head、散列表hlist_head
一.双链表list_head 1.基本概念 linux内核提供的标准链表可用于将任何类型的数据结构彼此链接起来. 不是数据内嵌到链表中,而是把链表内嵌到数据对象中. 即:加入链表的数据结构必须包含一个 ...
- Linux 内核list_head 学习
Linux 内核list_head 学习(一) http://www.cnblogs.com/zhuyp1015/archive/2012/06/02/2532240.html 在Linux内核中,提 ...
- Linux利用list_head结构实现双向链表
原文地址:http://www.cnblogs.com/bastard/archive/2012/10/19/2731107.html 通常实现双向链表的数据结构: struct list_node1 ...
- [转载]Linux内核list_head学习(二)
前一篇文章讨论了list_head 结构的基本结构和实现原理,本文主要介绍一下实例代码. 自己如果想在应用程序中使用list_head 的相应操作(当然应该没人使用了,C++ STL提供了list 用 ...
- [转载]Linux 内核list_head 学习(一)
在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head.虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作li ...
- Linux内核中的双向链表struct list_head
一.双向链表list_head Linux内核驱动开发会经常用到Linux内核中经典的双向链表list_head,以及它的拓展接口和宏定义:list_add.list_add_tail.list_de ...
- go例子(一) 使用go语言实现linux内核中的list_head
package list 代码 package list import ( "fmt" ) // 数据接口 type ElemType interface{} // 节点 type ...
随机推荐
- dubbox生产者与消费者案例
一.首先要将dubbox添加到本地maven仓库 参考: https://blog.csdn.net/try_and_do/article/details/83383861 二.目录结 ...
- 三行代码实现垂直居中和cube
三行代码实现上下居中 position: relative;top: 50%;transform: translateY(-50%); 效果如下: 代码: <!DOCTYPE html> ...
- Docker最全教程——从理论到实战(十二)
前言 Ubuntu是一个以桌面应用为主的开源GNU/Linux操作系统,应用很广.本篇主要讲述Ubuntu下使用SSH远程登录并安装Docker,并且提供了Docker安装的两种方式,希望对大家有所帮 ...
- 洛谷P1086 花生采摘
https://www.luogu.org/problem/P1086 #include <bits/stdc++.h> using namespace std; typedef long ...
- MySQL数据库重点监控指标
MySQL数据库重点监控指标 QPS queries per seconds 每秒中查询数量 show global status like 'Question%'; Queries/seconds ...
- Allegro 反射仿真--仿真设置
一.打开BRD文件 打开PCB SI,启动Cadence Product Choices界面,如图1-1所示,一般我们选择Allegro PCB SI 630(SPECCTRAQuest),具体如下图 ...
- MATLAB一些常用的function
在MATLAB中一些常用的算数符号与我们平时所用的不同,比如:根号,平方,e,以及对数函数等. (1)平方:a^2 意思为a的平方,亦可以写成a*a: (2)根号:sqrt(x)意思为对x开根号,x既 ...
- codeforces 1288D. Minimax Problem(二分)
链接:https://codeforces.com/contest/1288/problem/D D. Minimax Problem 题意:给定n个数组,长度为m,从n中数组挑选两个数组,两个数组中 ...
- 什么是类的hashcode值
1.要知道什么是类的hashcode值,首要要了解什么是hash(哈希).Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换 ...
- Java-POJ1010-STAMP
说良心话,题目不难,但是题目真的很不好懂,解读一下吧 题意: 读入分两行,第一行为邮票面额(面额相同也视为种类不同)以0结束,第二行为顾客要求的面额,以0结束 要求:每个顾客最多拿4张邮票,并求最优解 ...