浅析container_of(ptr, type, member)

本文介绍了一种在C语言中通过指针操作结构体的方法,利用宏container_of实现从结构体成员指针到结构体本身的定位。通过具体实例展示了如何在内核或嵌入式系统中遍历链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址
          type
      |----------|
      |          |
      |          |
      |----------|
ptr-->| member --|
      |----------|
      |          |
      |          |
      |----------|
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr:    the pointer to the member.
* @type:   the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
* */

 


1.ptr为物理地址,其类型和member类型一致,最终使用typeof( ((type *)0)->member )
  由编译器自动返回member的类型
2.type为包含member成员的结构体
3.offsetof(type,member)为member成员在type结构体中的偏移值,大小范围0~sizeof(type)字节
 (因为以0地址为type类型数据结构的起始地址)
4.ptr- offsetof()就等于包含该ptr的type结构体父变量的物理起始地址,强制转换为(type*).

应用举例:


//-------------------------------------------------------------
list_entry((head)->next, typeof(*pos), member)返回(head)->next物理指针所处位置向前减去offsetof()个字节数据之后, 其父变量pos的物理地址,父变量的类型在编译时由typeof(*pos)自动返回(gliethttp).
所以list_for_each_entry遍历head下面挂接的类型为typeof(*pos)的childs结构体们,当然每个child结构体包含struct list_head node之类相似的双向链表list_head类型项,就这样通过循环pos将依次指向双向链表上的各个child.(member就是child类型中被定义的变量名)
//-------------------------------------------------------------
下面一段程序摘自: drivers/usb/driver.c

 

### container_of 宏的定义与作用 `container_of` 是 Linux 内核中常用的宏之一,用于从结构体成员指针获取整个结构体实例的地址。它的核心功能在于解决嵌套数据结构中的逆向寻址问题。 #### 实现细节 以下是 `container_of` 的标准实现方式: ```c #define container_of(ptr, type, member) ({ \ const typeof(((type *)0)->member) *__mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); }) ``` 此宏的核心逻辑基于偏移量计算[^1]。具体来说: - `(typeof(((type *)0)->member))` 获取指定类型的成员变量类型。 - `offsetof(type, member)` 计算成员相对于结构体起始位置的字节偏移量。 - 使用 `(char *)__mptr - offsetof(type, member)` 将成员指针转换回完整的结构体指针。 这种操作依赖于 C 语言的标准行为以及编译器的支持。 #### 示例代码 下面是一个简单的例子来展示如何使用 `container_of`: ```c #include <stdio.h> #include <stddef.h> struct example { int value; char data[20]; }; int main() { struct example ex = { .value = 42 }; char *data_ptr = ex.data; // 使用 container_of 获取原始结构体 struct example *ex_ptr = container_of(data_ptr, struct example, data); printf("Value: %d\n", ex_ptr->value); return 0; } ``` 在此示例中,`container_of` 成功地将指向 `data` 字段的指针还原为包含该字段的完整结构体实例。 #### 应用场景 `container_of` 常见的应用场景包括但不限于链表节点管理、设备驱动程序开发等。它允许开发者通过任意成员访问其所属的整体对象,从而简化复杂的数据处理流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值