温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux内核容器怎么使用

发布时间:2021-11-23 17:11:59 来源:亿速云 阅读:145 作者:iii 栏目:互联网科技

本篇内容主要讲解“Linux内核容器怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux内核容器怎么使用”吧!

文件:include/linux/kernel.h,定义如下:

/** * 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. * */#define container_of(ptr, type, member) ({          \    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \    (type *)( (char *)__mptr - offsetof(type,member) );})    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
一、功能

功能:根据结构体type中成员member的指针ptr获取容器结构体type的地址。

二、参数

1)ptr:指向结构体成员member的指针

2)type:容器结构体类型

3)member:结构体中的成员

三、说明

1)typeof:获取成员member的数据类型

2)(type *)0:定义type结构体类型的0指针

3)typeof( ((type *)0)->member ) *__mptr:即定义member类型的指针__mptr

4)offsetof(type,member):计算成员member的偏移量

5)(type *):定义结构体type指针

图解如下:

Linux内核容器怎么使用

四、示例

文件:drivers/gpu/drm/rockchip/rockchip_drm_vop.c

## 1.定义结构体vopstruct vop {    struct drm_crtc crtc;    ...};## 2.使用container_of(),通过结构体vop中成员crtc的地址x,获取vop地址#define to_vop(x) container_of(x, struct vop, crtc)## 3.初始化结构体vop指针static int vop_hdr_atomic_check(struct drm_crtc *crtc,                struct drm_crtc_state *crtc_state){    struct vop *vop = to_vop(crtc);    ...}

到此,相信大家对“Linux内核容器怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI