Monday, April 19, 2010

Learning from Linux Kernel

1. container_of: Being passed a pointer, it returns the pointer to the structure that contains this.


#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
where
#define offsetof(type, member) \
    ((size_t) ( (char *)&((type *)(0))->member - (char *)0 ))

No comments: