kernel container_of macro
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) \
*__mptr = (ptr);
(type *)( (char *)__mptr - offsetof(type,member) );})
When you use the
cointainer_of macro, you want to retrieve the structure that contains the pointer of a given field. For example:struct numbers {
int one; int two; int three; } n;
int *ptr = &n.two; struct numbers *n_ptr; n_ptr = container_of(ptr, struct numbers, two);
You have a pointer that points in the middle of a structure (and you know that is a pointer to
two [the field name in the structure]), but you want to retrieve the entire structure (numbers). So, you calculate the offset of the filed two in the structure:offsetof(type,member)and subtract this offset from the given pointer. The result is the pointer to the start of the structure. Finally, you cast this pointer to the structure type to have a valid variable.
No comments:
Post a Comment