Re: question about linked list implementation in kernel.h
On Tue, Sep 14, 2010 at 1:27 AM, Jan Ceuleers jan.ceule...@computer.orgwrote: On 11/09/10 17:49, Bond wrote: (type *)( (char *)__mptr - offsetof(type,member) );}) there a subtraction has been done why is this subtraction done? Not having read any of the documentation, what comes to mind is this: if __mptr is a pointer to the member of a structure, the above expression returns a pointer to the first byte of the containing structure, typecast to the type of the structure. Example: typedef struct { int a; char b; } test_t; test_t test; Now assume that __mptr is a pointer to test.b, then the above expression returns a pointer to test, i.e. a pointer to the structure that b is a member of. Jan Hi, Jan no problem I am clear with the original question that I had asked with Manohar.
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 10:09 PM, Manish Katiyar mkati...@gmail.com wrote: If you want some more details about this macro. http://www.spinics.net/lists/linux-usb-devel/msg11766.html Thanks but Manohars explanation was perfect for newbies like me.
Re: question about linked list implementation in kernel.h
On 11/09/2010 14:33, Bond wrote: I read I was going through include/linux/kernel.h encountered following code #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)-member ) *__mptr = (ptr);\ in above code I am not clear with (type *)0 how is it working any link? First link on google: http://www.kroah.com/log/linux/container_of.html -- Carlo -- To unsubscribe from this list: send an email with unsubscribe kernelnewbies to ecar...@nl.linux.org Please read the FAQ at http://kernelnewbies.org/FAQ
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 6:22 PM, Carlo Caione carlo.cai...@gmail.comwrote: First link on google: Its not that I had not searched it did search but I had searched typedef and type and I kept reading links related to that only. What to be searched is not that obvious when some one is not clear. http://www.kroah.com/log/linux/container_of.html Any how thanks for link.
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 6:22 PM, Carlo Caione carlo.cai...@gmail.comwrote: http://www.kroah.com/log/linux/container_of.html I am not able to understand a single bit of the explanaition on the above link. They have assumed I programmed PCI buses which I have not.
Re: question about linked list implementation in kernel.h
On Sat, 11 Sep 2010, Bond wrote: On Sat, Sep 11, 2010 at 6:22 PM, Carlo Caione carlo.cai...@gmail.com wrote: http://www.kroah.com/log/linux/container_of.html I am not able to understand a single bit of the explanaition on the above link. They have assumed I programmed PCI buses which I have not. you're making this too difficult -- all container_of() does is, given: * a structure definition, * the name of a member field within that structure, and * the address of that internal member field in an instance what you get back is the address of the enclosing structure. this allows you to work with member fields of a structure but, when you need to, back up to the structure they're contained in. kernel linked lists are built on this idea. http://crashcourse.ca/introduction-linux-kernel-programming/intermission-lets-talk-about-linked-lists-and-containerof-free rday -- Robert P. J. Day Waterloo, Ontario, CANADA Top-notch, inexpensive online Linux/OSS/kernel courses http://crashcourse.ca Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday
Re: question about linked list implementation in kernel.h
I just made a small example (with comments) to play around with :-) Let me know if you feel something is wrong in my explanations (I'm still learning as well!) Hope it helps! On Sat, Sep 11, 2010 at 6:03 PM, Bond jamesbond.2...@gmail.com wrote: I read I was going through include/linux/kernel.h encountered following code #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)-member ) *__mptr = (ptr);\ in above code I am not clear with (type *)0 how is it working any link? -- /manohar #include stdio.h #include stdlib.h #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER) /* * The first line gets the type of the member inside the structure. It does * this by casting a NULL pointer to the structure type and using the GCC * typeof() extension. * * The second line evaluates to a value of the address of the containing * structure. This is done using the value of the member pointer and * subtracting the offset of the member from its own address. * * eg. if a member is at address 10 and is at an offset of 4 bytes into * the struture, the containing structure's pointer is at address (10 - 4) = 6 */ #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)-member ) *__mptr = (ptr);\ (type *)( (char *)__mptr - offsetof(type,member) );}) struct test { int a; }; /* * Given the pointer to a member inside a structure, retreive its containing * structure pointer */ void test_func(int *ptr) { /* You can see the output of this macro using: gcc -E cont.c */ struct test *container = container_of(ptr, struct test, a); printf(Retreived pointer: %x\n, (unsigned int)container); printf(Value: %d\n, container-a); } int main() { struct test *t = malloc(sizeof(struct test)); t-a = 5; printf(Structure pointer: %x\n, (unsigned int)t); printf(Value: %d\n, t-a); test_func(t-a); free(t); /* * This is how a block is evaluated to a value (explains how the macro * works. See output from gcc -E cont.c. */ int val = ({5 + 5;}); printf(Block value: %d\n, val); }
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 9:12 PM, Bond jamesbond.2...@gmail.com wrote: http://crashcourse.ca/introduction-linux-kernel-programming/intermission-lets-talk-about-linked-lists-and-containerof-free Robert (type *)( (char *)__mptr - offsetof(type,member) );}) there a subtraction has been done why is this subtraction done? Manohar I am going through your doc.
Re: question about linked list implementation in kernel.h
On 11/09/2010 17:45, Manohar Vanga wrote: I just made a small example (with comments) to play around with :-) Let me know if you feel something is wrong in my explanations (I'm still learning as well!) Hope it helps! It is correct. Good explanation. -- Carlo -- To unsubscribe from this list: send an email with unsubscribe kernelnewbies to ecar...@nl.linux.org Please read the FAQ at http://kernelnewbies.org/FAQ
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 9:15 PM, Manohar Vanga manohar.va...@gmail.comwrote: I just made a small example (with comments) to play around with :-) Let me know if you feel something is wrong in my explanations (I'm still learning as well!) Hope it helps! Manohar I just happen to understand great great work man.It is clear completely. Awesome explanation I am copy pasting from the file you gave I hope you don't mind. #include stdio.h #include stdlib.h #define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-MEMBER) /* * The first line gets the type of the member inside the structure. It does * this by casting a NULL pointer to the structure type and using the GCC * typeof() extension. * * The second line evaluates to a value of the address of the containing * structure. This is done using the value of the member pointer and * subtracting the offset of the member from its own address. * * eg. if a member is at address 10 and is at an offset of 4 bytes into * the struture, the containing structure's pointer is at address (10 - 4) = 6 */ #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)-member ) *__mptr = (ptr);\ (type *)( (char *)__mptr - offsetof(type,member) );}) struct test { int a; }; /* * Given the pointer to a member inside a structure, retreive its containing * structure pointer */ void test_func(int *ptr) { /* You can see the output of this macro using: gcc -E cont.c */ struct test *container = container_of(ptr, struct test, a); printf(Retreived pointer: %x\n, (unsigned int)container); printf(Value: %d\n, container-a); } int main() { struct test *t = malloc(sizeof(struct test)); t-a = 5; printf(Structure pointer: %x\n, (unsigned int)t); printf(Value: %d\n, t-a); test_func(t-a); free(t); /* * This is how a block is evaluated to a value (explains how the macro * works. See output from gcc -E cont.c. */ int val = ({5 + 5;}); printf(Block value: %d\n, val); }
Re: question about linked list implementation in kernel.h
On Sat, Sep 11, 2010 at 5:33 AM, Bond jamesbond.2...@gmail.com wrote: I read I was going through include/linux/kernel.h encountered following code #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)-member ) *__mptr = (ptr); \ in above code I am not clear with (type *)0 If you want some more details about this macro. http://www.spinics.net/lists/linux-usb-devel/msg11766.html how is it working any link? -- Thanks - Manish == [$\*.^ -- I miss being one of them == -- To unsubscribe from this list: send an email with unsubscribe kernelnewbies to ecar...@nl.linux.org Please read the FAQ at http://kernelnewbies.org/FAQ