linux中的经典宏定义

offsetof

定义:offsetof在linux内核的include/linux/stddef.h中定义。

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

说明:获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。

(01) ( (TYPE *)0 ) 将零转型为TYPE类型指针,即TYPE类型的指针的地址是0。

(02) ((TYPE *)0)->MEMBER 访问结构中的数据成员。

(03) &( ( (TYPE *)0 )->MEMBER ) 取出数据成员的地址。由于TYPE的地址是0,这里获取到的地址就是相对MEMBER在TYPE中的偏移。

(04) (size_t)(&(((TYPE*)0)->MEMBER)) 结果转换类型。对于32位系统而言,size_t是unsigned int类型;对于64位系统而言,size_t是unsigned long类型。

container_of

定义:container_of在linux内核的include/linux/kernel.h中定义。

#define container_of(ptr, type, member) ({          \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

说明:根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针。

(01) typeof( ( (type *)0)->member ) 取出member成员的变量类型。

(02) const typeof( ((type *)0)->member ) *__mptr = (ptr) 定义变量__mptr指针,并将ptr赋值给__mptr。经过这一步,__mptr为member数据类型的常量指针,其指向ptr所指向的地址。

(03) (char *)__mptr 将__mptr转换为字节型指针。

(04) offsetof(type,member)) 就是获取"member成员"在"结构体type"中的位置偏移。

(05) (char *)__mptr - offsetof(type,member)) 就是用来获取"结构体type"的指针的起始地址(为char *型指针)。

(06) (type *)( (char *)__mptr - offsetof(type,member) ) 就是将"char *类型的结构体type的指针"转换为"type *类型的结构体type的指针"。

 

Linux双向链表实现

Linux双向链表的定义主要涉及到两个文件:

include/linux/types.h

include/linux/list.h

(01). 节点定义

struct list_head {
struct list_head *next, *prev;
};

虽然名称list_head,但是它既是双向链表的表头,也代表双向链表的节点。

(02). 初始化节点

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \

                          struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)

{

    list->next = list;

    list->prev = list;

}

LIST_HEAD的作用是定义表头(节点):新建双向链表表头name,并设置name的前继节点和后继节点都是指向name本身。

LIST_HEAD_INIT的作用是初始化节点:设置name节点的前继节点和后继节点都是指向name本身。

INIT_LIST_HEAD和LIST_HEAD_INIT一样,是初始化节点:将list节点的前继节点和后继节点都是指向list本身。

(03). 添加节点

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;

}

static inline void list_add(struct list_head *new, struct list_head *head)

{

    __list_add(new, head, head->next);

}

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

    __list_add(new, head->prev, head);

}

__list_add(new, prev, next)的作用是添加节点:将new插入到prev和next之间。在linux中,以"__"开头的函数意味着是内核的内部接口,外部不应该调用该接口。

list_add(new, head)的作用是添加new节点:将new添加到head之后,是new称为head的后继节点。

list_add_tail(new, head)的作用是添加new节点:将new添加到head之前,即将new添加到双链表的末尾。

(04). 删除节点

static inline void __list_del(struct list_head * prev, struct list_head * next)

{

    next->prev = prev;

    prev->next = next;

}

static inline void list_del(struct list_head *entry)

{

    __list_del(entry->prev, entry->next);

}

static inline void __list_del_entry(struct list_head *entry)

{

    __list_del(entry->prev, entry->next);

}

static inline void list_del_init(struct list_head *entry)

{

    __list_del_entry(entry);

    INIT_LIST_HEAD(entry);

}

__list_del(prev, next) 和__list_del_entry(entry)都是linux内核的内部接口。

__list_del(prev, next) 的作用是从双链表中删除prev和next之间的节点。

__list_del_entry(entry) 的作用是从双链表中删除entry节点。

list_del(entry) 和 list_del_init(entry)是linux内核的对外接口。

list_del(entry) 的作用是从双链表中删除entry节点。

list_del_init(entry) 的作用是从双链表中删除entry节点,并将entry节点的前继节点和后继节点都指向entry本身。

(05). 替换节点

static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}

list_replace(old, new)的作用是用new节点替换old节点。

(06). 判断双链表是否为空

static inline int list_empty(const struct list_head *head)

{

    return head->next == head;

}

list_empty(head)的作用是判断双链表是否为空。它是通过区分"表头的后继节点"是不是"表头本身"来进行判断的。

(07). 获取节点

#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

list_entry(ptr, type, member) 实际上是调用的container_of宏。

它的作用是:根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针。

(08). 遍历节点

#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)

list_for_each(pos, head)和list_for_each_safe(pos, n, head)的作用都是遍历链表。但是它们的用途不一样!

list_for_each(pos, head)通常用于获取节点,而不能用到删除节点的场景。

list_for_each_safe(pos, n, head)通常删除节点的场景。

linux内核-双向链表的更多相关文章

  1. 例说Linux内核链表(三)

    经常使用的linux内核双向链表API介绍 linux link list结构图例如以下: 内核双向链表的在linux内核中的位置:/include/linux/list.h 使用双向链表的过程,主要 ...

  2. Linux 内核数据结构:Linux 双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  3. Linux 内核数据结构:双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  4. Linux内核中双向链表的经典实现

    概要 前面一章"介绍双向链表并给出了C/C++/Java三种实现",本章继续对双向链表进行探讨,介绍的内容是Linux内核中双向链表的经典实现和用法.其中,也会涉及到Linux内核 ...

  5. 深度剖析linux内核万能--双向链表,Hash链表模版

    我们都知道,链表是数据结构中用得最广泛的一种数据结构,对于数据结构,有顺序存储,数组就是一种.有链式存储,链表算一种.当然还有索引式的,散列式的,各种风格的说法,叫法层出不穷,但是万变不离其中,只要知 ...

  6. Linux 内核里的数据结构:双向链表

    原文:https://blog.csdn.net/qq_33487044/article/details/78827260 双向链表 Linux 内核自己实现了双向链表,可以在 include/lin ...

  7. Linux内核分析--内核中的数据结构双向链表【转】

    本文转自:http://blog.csdn.net/yusiguyuan/article/details/19840065 一.首先介绍内核中链表 内核中定义的链表是双向链表,在上篇文章--libev ...

  8. Linux内核中的双向链表struct list_head

    一.双向链表list_head Linux内核驱动开发会经常用到Linux内核中经典的双向链表list_head,以及它的拓展接口和宏定义:list_add.list_add_tail.list_de ...

  9. Linux内核分析--内核中的数据结构双向链表续【转】

    在解释完内核中的链表基本知识以后,下面解释链表的重要接口操作: 1. 声明和初始化 实际上Linux只定义了链表节点,并没有专门定义链表头,那么一个链表结构是如何建立起来的呢?让我们来看看LIST_H ...

随机推荐

  1. poj 3249 Test for Job (记忆化深搜)

    http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  2. tar 解压缩命令

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  3. centos7安装chrome及加载poatman开发插件

    为什么要安装chrome?因为centos7的默认浏览器firefox的实在是不习惯,上面占了太多,本来显示器就不大... 好了,首先下载chome的rpm安装包(如果需要的可以留言,我有备份) 然后 ...

  4. EasyUI Datagrid 取编辑修改后的内容

    <script type="text/javascript"> $(function () { $('#tt').datagrid({ iconCls: 'icon-e ...

  5. jquery取消事件冒泡和取消默认行为

    $('button').click(functon(e){ /*code*/ e.stopPropagation();//取消事件冒泡 e.preventDefault();//取消默认行为 })

  6. tomcat配置301重定向(urlRewrite URL重写)

    tomcat默认情况下不带www的域名是不会跳转到带www的域名的,而且也无法像apache那样通过配置.htaccess来实现.如果想要把不带“www'的域名重定向到带”www"域名下,又 ...

  7. linux的定时任务crontab

    每隔一分钟执行以下语句: #打印当前时间: date "+%Y-%m-%d %T" 保存为/usr/test/test.sh 查看系统中当前用户有多少个定时任务: crontab ...

  8. Vue.js vui 饿了么Vue2.0的组件库

    http://www.oschina.net/news/78038/vue-js-2-0-3 http://git.oschina.net/durcframework/vui http://eleme ...

  9. CVE爬虫抓取漏洞URL

    String url1="http://www.cnnvd.org.cn/vulnerability/index/vulcode2/tomcat/vulcode/tomcat/cnnvdid ...

  10. Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

    最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...