Loading ...
Sorry, an error occurred while loading the content.
 

list_entry macro explanation?

Expand Messages
  • Chidanand
    Hello Friends, I am finding difficulty in understanding the below macro can any body help me to understamd it or give me any help link on net . #define
    Message 1 of 3 , Sep 3, 2003
      Hello Friends,

      I am finding difficulty in understanding the below macro can any body help me to understamd it or give me any help link on net .

      #define list_entry(ptr, type, member)
      ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

      this macro is from list.h file which returns only single pointer of user type struct. please help me to understand why subtraction in the macro and the type cast char * and unsigned long .

      thanks in advance

      wings.




      ---------------------------------
      Do you Yahoo!?
      Yahoo! SiteBuilder - Free, easy-to-use web site design software

      [Non-text portions of this message have been removed]
    • Aneesh Kumar K.V
      ... Simple this macro help to find the address of the structure given th address of the element of the structure , type of the structure and the element name.
      Message 2 of 3 , Sep 3, 2003
        On Thu, 2003-09-04 at 11:28, Chidanand wrote:
        > Hello Friends,
        >
        > I am finding difficulty in understanding the below macro can any body
        > help me to understamd it or give me any help link on net .
        >
        > #define list_entry(ptr, type, member)
        > ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
        >


        Simple this macro help to find the address of the structure given th
        address of the element of the structure , type of the structure and the
        element name.

        As below.

        struct a{

        int b ;
        int c;
        int d;

        }

        now given the address of c address of a is nothing but

        address of c - offset of c from a . Offset of c from from a is
        calculated by a cc trick saying &(( struct a *)0)->c). that is assume
        that the struct is at 0 then address of c is nothing but the offset of c
        from a.

        -aneesh
      • Karthick Ramnarayanan
        ... ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)- member))) ... Okay, Lets say you have a structure definition like this: struct sample { struct
        Message 3 of 3 , Sep 3, 2003
          >>>--Original Message-----<<<
          >>From: Chidanand [mailto:wingscng@...] <<<
          >>Sent: Thursday, September 04, 2003 11:29 AM <<
          >>To: linux-bangalore-programming@yahoogroups.com<<
          >>Subject: [blug-prog] list_entry macro explanation?

          >I am finding difficulty in understanding the below macro can any body help me to understamd it or give me any help link on net .
          >#define list_entry(ptr, type, member)
          ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

          >this macro is from list.h file which returns only single pointer of user type struct. please help me to understand why subtraction in the macro and the type cast char * and unsigned long .

          Okay,
          Lets say you have a structure definition like this:
          struct sample {
          struct list_head list_head ;
          /*The fields for your samples*/
          }sample;

          Now the above definition in linux/list.h is a generic definition of a list_head that
          helps you in extracting objects of any type through a call to the macro:
          list_entry(list_head_headpointer,your structure type,field);
          In the above case:
          it would be : list_entry(&sample.list_head,struct sample,list_head);

          What does it do:
          The macro first extracts the offset of the field "list_head" in your structure.
          To get the offset of your field "list_head" in the "struct sample",
          you will have a pointer to "0" or (&(( struct sample *) 0)->list_head)
          Typecasting a pointer to "0" is a trick to get the field offset in your structure.
          Once you get the field offset of the field passed to "list_entry" macro: (
          in the above case ,its "list_head" (which is 0),
          you have to subtract the pointer to the "list_head" or &sample.list_head (in the above case)
          to get the pointer to the "struct sample" as thats how you would have stored
          objects in the list_head in the first place,through a call to
          list_add{_tail} ( &sample.list_head,head_of_the_list);
          Hope thats clear.
          Regards,
          -Karthick




          ---------------------------------
          Do you Yahoo!?
          Yahoo! SiteBuilder - Free, easy-to-use web site design software

          [Non-text portions of this message have been removed]




          Before posting to this list, read the list rules FIRST!

          http://linux-bangalore.org/discussions/rules.php


          Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        Your message has been successfully submitted and would be delivered to recipients shortly.