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 ...
随机推荐
- PP: Triple-shapelet networks for time series classification
Problem: time series classification shapelet-based method: two issues 1. for multi-class imbalanced ...
- Python标准库之时间模块time与datatime模块详解
时间模块time与datatime 时间表示方式: 时间戳 格式化时间字符串 元组 时间戳格式: time.time()#输出1581664531.749063 元组格式: time.localtim ...
- jQuery的12种选择器
jQuery的12种选择器 1.#id : 根据给定的ID匹配一个元素 显示(用加粗的代替颜色): 这是第一个p标签 2.* : 匹配所有元素,多用于结合上下文来搜索 显示 : 这是p标签 这是di ...
- C#通过属性名字符串获取、设置对象属性值
之前理工项目从这个博客找到了相对应的方法:C#通过属性名字符串获取.设置对象属性值 https://www.cnblogs.com/willingtolove/p/12198871.html
- [HEOI2015] 小Z的房间 - 矩阵树定理
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 105; const i ...
- Echat 地图样式格式
1.白色 mapStyle: { styleJson: [{ 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': ' ...
- COMMUNITY DETECTION_python-louvain
Python-louvain Package pip install python-louvain import community #first compute the best partition ...
- 操作系统-多用户如何理解(Linux)
单用户.多用户.单任务.多任务,这么多种操作系统容易让人迷糊.其实这种初看你会觉得理解了一点,但其实你仔细研究会发现,多用户到底讲的是什么鬼? 多任务比较简单,就是应用程序都要放置到内存上去给CPU调 ...
- UES
Spring-core-4.3.16 ObjectUtils.java public static boolean isEmpty(Object obj) { if (obj == null) { r ...
- 创建本地yum源仓库
更新本地yum源 yum仓库服务端配置如下 : 1. 创建yum仓库目录 mkdir -p /data/yum_data/ cd /data/yum_data/ #可以上传rpm包到此目录,此目录下面 ...