0

I am trying to understand some proprietary code in which container_of is called with single parameter which is a member struct of the return struct.

struct want{
.
.
struct member_used;
};
member_struct *ptr_member_used;
struct want *ret_struct container_of(ptr_member_used);

i checked following links

http://codinghighway.com/2013/08/10/the-magical-container-of-macro-and-its-use-in-the-linux-kernel/

Doubts regarding container_of macro in linux

Understanding container_of macro in the Linux kernel

When do you use container_of macro?

but all these links are using three parameters but none of them is using struct member to retrieve corresponding struct.

My worry here is how container_of macro returns corresponding struct just by passing struct member?

program is running correctly on 64 bit ubuntu 14.0.4 with kernel 3.13.0 and gcc 4.8.4

Community
  • 1
  • 1
incompetent
  • 1,715
  • 18
  • 29
  • In your proprietary code most probably the name of the container struct as long with it's type are always the same, that's why the macro has one parameter only, but you have to provide the `container_of()` implementation to actually shed a light on this. – 0andriy Aug 31 '15 at 12:16
  • interestingly my code does not have any implementation of container_of. or it is save to say that doxygen is not catching this implementation so i concluded it must be in kernel headers – incompetent Aug 31 '15 at 12:39
  • does kvm has different implementation of this macro? code is using extensively kvm code – incompetent Aug 31 '15 at 12:42
  • doxygen might skip some code. It's not nice tool to use for browsing the actual code. I recommend to run cscope and use its data base instead. – 0andriy Aug 31 '15 at 13:33
  • Kernel build system generate cmd file during build stage. There is a list of header files inside cmd file your module depends on. Extract the list and run grep on it to find definition. Make sure that doxygen see all include files. One of files from list might be in non standard locations. – alexander Aug 31 '15 at 13:58

2 Answers2

6

container_of macro basically used to obtain the parent structure address from any of it's member.

In definition of the macro 1st line is determining type of the member and second line calculating offset between structure start to the member from which we are determining the struct start point.

Refer to below code for clear understanding, it's simple:

#include <stdio.h>
#include <stddef.h>

// Copied from linux/kernel.h
#define container_of(ptr, type, member) ({                      \
                const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
                (type *)( (char *)__mptr - offsetof(type,member) );})

struct myStruct { int a, b; };

int main()
{
    struct myStruct var = {.a = 0, .b = 0}; 

    int *memberPointer = &var.b;

    printf("Struct addr=%p\n", &var);

    struct myStruct *newSp = container_of(memberPointer, struct myStruct, b); 

    printf("Struct addr new=%p\n", newSp);

    if(newSp == &var)
    {   
        printf("It's equal.\n");
    }   

    return 0;
}

Output:

Struct addr=0x7ffddcfd2b10
Struct addr new=0x7ffddcfd2b10
It's equal.
Pushpendra
  • 459
  • 1
  • 4
  • 18
0

There is another definition of container_of somewhere in the sources used to build proprietary code. That another definition can be macro or function and take one argument. There is nothing more I can say about it. You should find it in the headers or sources.

alexander
  • 2,703
  • 18
  • 16