RE: inline asm question(s)

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

 



NULL is defined within the kernel code in linux\stdfed.h, so the result
should be platform independent.

Thanks & regards,
Suvidh Mathur

-----Original Message-----
From: Anupam Kapoor [mailto:anupam.kapoor@gmail.com] 
Sent: Wednesday, June 30, 2004 2:22 PM
To: Nir Tzachar
Cc: Borislav Petkov; kernelnewbies@nl.linux.org
Subject: Re: inline asm question(s)

On Wed, 30 Jun 2004 11:38:47 +0300 (IDT), Nir Tzachar
<tzachar@cs.bgu.ac.il> wrote:
> 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) .
no, this is not correct. the compilers are free to add padding to meet
alignment requirements of a given platform.

> 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).
the assumption here is that null is repersented as a bunch of zeros on
a platform. if that is  not the case, you will get strange results.

kind regards
anupam
> 
> --
>
========================================================================
> nir.

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


--
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