Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Linux kernel container

Shulou Source: shulou.com Published: 2022-06-01 20:59:00 09月11日 Update

This article focuses on "how to use the Linux kernel container". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the Linux kernel container.

File: include/linux/kernel.h, defined as follows:

/ * * 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) 1. Function

Function: obtain the address of the container structure member according to the pointer ptr of the member type in the structure type.

Second, parameters

1) ptr: pointer to structure member member

2) type: container structure type

3) member: a member in a structure

III. Explanation

1) typeof: gets the data type of the member member

2) (type *) 0: 0 pointer that defines the type of type structure

3) typeof (type *) 0)-> member) * _ _ mptr: that is, the pointer _ _ mptr that defines the member type

4) offsetof (type,member): calculates the offset of the member member

5) (type *): defines the structure type pointer

The illustration is as follows:

IV. Examples

File: drivers/gpu/drm/rockchip/rockchip_drm_vop.c

# # 1. Define the structure vopstruct vop {struct drm_crtc crtc;...}; # # 2. Using container_of (), obtain the vop address # define to_vop (x) container_of (x, struct vop, crtc) # # 3 through the address x of the member crtc in the structure vop. Initialize the structure vop pointer static int vop_hdr_atomic_check (struct drm_crtc * crtc, struct drm_crtc_state * crtc_state) {struct vop * vop = to_vop (crtc);.} so far, I believe you have a better understanding of "how to use the Linux kernel container". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Structure member pointer container type kernel address content function file learning utility depth interest parameter practicality practicality pointing simple operation data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux vpn Microsoft macOS