Understanding container_of macro in the Linux kernel

container_of macro linux example
offsetof macro
container_of c
container_of pointer member
container_of windows
typeof in linux kernel
gdb container_of
macro in struct

When I was browsing the Linux kernel, I found a container_of macro which is defined as follows:

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

I understand what does container_of do, but what I do not understand is the last sentence, which is

(type *)( (char *)__mptr - offsetof(type,member) );})

If we use the macro as follows:

container_of(dev, struct wifi_device, dev);

The corresponding part of the last sentence would be:

(struct wifi_device *)( (char *)__mptr - offset(struct wifi_device, dev);

which looks like doing nothing. Could anybody please fill the void here?

Your usage example container_of(dev, struct wifi_device, dev); might be a bit misleading as you are mixing two namespaces there.

While the first dev in your example refers to the name of pointer the second dev refers to the name of a structure member.

Most probably this mix up is provoking all that headache. In fact the member parameter in your quote refers to the name given to that member in the container structure.

Taking this container for example:

struct container {
  int some_other_data;
  int this_data;
}

And a pointer int *my_ptr to the this_data member you'd use the macro to get a pointer to struct container *my_container by using:

struct container *my_container;
my_container = container_of(my_ptr, struct container, this_data);

Taking the offset of this_data to the beginning of the struct into account is essential to getting the correct pointer location.

Effectively you just have to subtract the offset of the member this_data from your pointer my_ptr to get the correct location.

That's exactly what the last line of the macro does.

The Magical container_of() Macro (by @radekpazdera), References. container_of() explained by Greg KH · container_of() definition in Linux Kernel · Statements and Declarations in Expressions  container_of () is a macro defined in <linux/kernel.h>. The syntax of the Macro is given below. container_of (ptr, type, member) Where, 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.

The last sentence cast:

(type *)(...)

a pointer to a given type. The pointer is calculated as offset from a given pointer dev:

( (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 the filed 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.

Understanding container_of macro in the Linux kernel, Your usage example container_of(dev, struct wifi_device, dev); might be a bit misleading as you are mixing two namespaces there. While the  Definition: container_of macro is defined in linux/kernel.h. ( { void *__mptr = (void *) (ptr); ( (type *) (__mptr - offsetof (type, member))); }) To understand these two lines of the macro, we need to get familiar with the following concepts. typeof () offsetof () Compound Statements.

It is an utilisation of a gcc extension, the statements expressions. If you see the macro as something returning a value, then the last line would be :

return (struct wifi_device *)( (char *)__mptr - offset(struct wifi_device, dev);

See the linked page for an explanation of compound statements. Here is an example :

int main(int argc, char**argv)
{
    int b;
    b = 5;
    b = ({int a; 
            a = b*b; 
            a;});
    printf("b %d\n", b); 
}

The output is

b 25

Understanding container_of macro of Linux Kernel, container_of macro is the macro you see almost in most of driver code. Search of "container_of" macro in drivers folder of Linux Source code gave  Understanding container_of macro in the Linux kernel When you begin with the kernel, and you start to look around and read the code, you will eventually come across this magical preprocessor construct. What does it do? Well, precisely what its name indicates.

Understanding of container_of macro in Linux kernel, If you don't, you can understand that by using example. Container_of(). container_of() is a macro defined in <linux/kernel.h>. The syntax of the  As the name says, the container_of macro is used to find the container of the given field of a structure. The macro is defined in include/linux/kernel.h and looks like the following: #define container_of(ptr, type, member) ({ \ const typeof(((type *)0)->member) * __mptr

container_of(), What exactly does the weird container_of() macro do? This macro is defined as: #​define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) This also prevents other parts of the kernel from modifying the unique fields of the​  Well, precisely what its name indicates. It takes three arguments – a pointer, type of the container, and the name of the member the pointer refers to. The macro will then expand to a new address pointing to the container which accommodates the respective member.

Linux Kernel Programming: How "container_of" macro works, & an , h" header. Please interpret this code and try some trick to understand "​container_of". container_of macro is defined in linux/kernel.h. Question: Tag: linux,linux-kernel,linux-device-driver I want to implement multiple hrtimers, but I'm not sure how to use all of them with same callback function. For example I have array of type my_struct where one of the field is a struct hrtimer.

container_of() – Stupid Projects, I'll give my explanation hoping that it won't waste more internet space. The container_of macro is defined in several places in the Linux kernel. I am trying to use a hashtable in my kernel module. However, I don't really understand the API.The hash_add(hashtable, node, key) macro lets me insert a struct hlist_node element, where the struct is defined as

Device Driver Design Patterns , State Container 2. container_of() 1. pointer to a struct member in the callback. container_of() is a macro defined in <linux/kernel.h> What container_of() does is​  container_of macro is defined in linux /kernel.h syntax: container_of (pointer, container_type, container_field); This macro takes a pointer to a filed name container_field, within a structure of type container_type, and returns a pointer to the containing structure.

Comments
  • this answer has a real and intuitive example using red-black tree rb_node.
  • For those of you who need more detailed explanation: Radek Pazdera explained container_of macro (include/linux/kernel.h) really clearly on his blog. BTW: list_entry macro (include/linux/list.h) used to be defined very similarly, but now is defined as container_of.
  • Also this blogpost by Greg Kroah-Hartman may be useful: kroah.com/log/linux/container_of.html
  • In principle, that's a nice answer, except that it does not really answer considerable piece of the question? E.g., what does the last line of code achieve and how?