Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

2STUPID4C

Name: Anonymous 2016-12-29 5:44

I have smoked a significant amount of 420 but still can't understand how this macro function works:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})


https://github.com/torvalds/linux/blob/master/include/linux/kernel.h

Name: Anonymous 2016-12-29 7:16

>>1
As experienced developer of void.h this macro is a piece of cake.
offsetof gets you offset struct_type<>member distance,
when you apply offset to the ptr, you get to offset 0 which is the address of container of the ptr(all ptr are assumed to be member type).
In pseudocode: container_ptr=ptr - offset_from_containertype_to_member.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List