Re: inline asm question(s)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hello there ;)

> 211 #define container_of(ptr, type, member) ({                      \
> 212         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
> 213         (type *)( (char *)__mptr - offsetof(type,member) );})
> 
> I can't understand the ((type *)0) part - type is passed as an argument and it 
> is some struct pointer but the trailing 0 ... what does it actually do?

you need the type of the member, so you can have a proper pointer to it.
you could achieve this by supplying member_type directly, but you dont 
need to. by using typeof ((type *)0)->member we get the type of the member
('type' is the type of the container).


> offsetof is similar:
> 
> <from include/linux/stddef.h>
>  12 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

well, lets say you have a structure defined like this:
struct foo {
	int a;
	int b;
	int c;
	char d;
};

to get the offset of member c in this struct, we need to size of all 
members which come b4 c: offset_of_c = sizeof(a)+sizeof(b) .
however, a more generic and _much_ better way:
lets say you had a pointer to a struct foo (foo_ptr), so you can get the 
offset like this: 

offset_of_c = &foo_ptr->c - foo_ptr

the macro actually saves the subtraction, by letting the compiler think 
that foo_ptr is 0 ((TYPE *)0) (located at start of memory). 

-- 
========================================================================
nir.

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux