Archive

Archive for the ‘Linux Kernel’ Category

Linux kernel container_of()

25 September, 2006 Leave a comment

I recently noticed the Linux kernel implementation of container_of() macro. It is as given below:


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

It seem like it can be shortened as follows:


#define container_of(ptr, type, member) \
((type *) ((char *) ptr - offsetof(type, member)))

I believe __mptr is just used for type checking the ptr.

Categories: Linux Kernel
Follow

Get every new post delivered to your Inbox.