Common macro container_of in the Linux kernel

Source: Internet
Author: User

/*linux-2.6.38.8/include/linux/compiler-gcc4.h*/#define__compiler_offsetof (A, B) __builtin_offsetof (A, B)/*linux-2.6.38.8/include/linux/stddef.h*/#undefOffsetof#ifdef __compiler_offsetof#defineOffsetof (Type,member) __compiler_offsetof (Type,member)#else#defineOffsetof (Type, MEMBER) ((size_t) & ((type *) 0)->member)#endif/*linux-2.6.38.8/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 was embedded in. * @member: The name of the member within the struct. * */#defineContainer_of (PTR, type, member) ({Const typeof((Type *)0)->member) *__mptr =(PTR); (Type*)( (Char*) __mptr-offsetof (Type,member));}) #include<stdio.h>structtest_struct {intnum; Charch; floatFL;};intMainvoid){    structTest_struct init_test_struct = { About,'C',59.12 }; Char*char_ptr = &init_test_struct.ch; structTest_struct *test_struct = container_of (Char_ptr,structtest_struct, ch); printf ("Test_struct->num =%d\n Test_struct->ch =%c\n TEST_STRUCT->FL =%f\n", Test_struct->num, Test_struct->ch, test_struct->FL); return 0;}

Common macro container_of in the Linux kernel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.