码迷,mamicode.com
首页 > 系统相关 > 详细

Linux内核中的常用宏container_of

时间:2017-12-16 22:09:11      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:off   int   har   printf   inux   with   efi   const   fse   

 

/* 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 */
#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(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 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) );})

#include <stdio.h>

struct test_struct {
    int num;
    char ch;
    float fl;
};

int main(void)
{
    struct test_struct init_test_struct = { 99, C, 59.12 };

    char *char_ptr = &init_test_struct.ch;

    struct test_struct *test_struct = container_of(char_ptr, struct test_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;
}

 

Linux内核中的常用宏container_of

标签:off   int   har   printf   inux   with   efi   const   fse   

原文地址:http://www.cnblogs.com/soul-stone/p/8047660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!