请输入您要查询的百科知识:

 

词条 list_entry
释义

定义

/* 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) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

描述

我们使用list_entry()宏在linux链表中访问链表数据。

原理为指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址。

定义中((size_t) &(type *)0)->member)意为:把0地址转化为type结构的指针,然后获取该结构中member成员的指针,并将其强制转换为size_t类型

例子

如果我们有test_list结构:

struct test_list{

int testdata;

struct list_head list;};

struct test_list a;

a.testdata = 5;

struct test_list *pos = list_entry(&(a.testdata),struct test_list,testdata);

结果:

pos = &a;

可测试:

pos->testdata = 5

解释

&((type *)0)->member:

把“0”强制转化为指针类型,则该指针一定指向“0”(数据段基址)。因为指针是“type *”型的,所以可取到以“0”为基地址的一个type型变量member域的地址。那么这个地址也就等于member域到结构体基地址的偏移字节数。

((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))):

(char *)(ptr)使得指针的加减操作步长为一字节,(unsigned long)(&((type *)0)->member)等于ptr指向的member到该member所在结构体基地址的偏移字节数。二者一减便得出该结构体的地址,转换为 (type *)型的指针。

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/22 8:04:19