On Sat, Oct 9, 2010 at 6:16 PM, Bond <jamesbond.2...@gmail.com> wrote:
> On Sat, Oct 9, 2010 at 12:27 PM, Manish Katiyar <mkati...@gmail.com> > wrote: > > On Fri, Oct 8, 2010 at 11:38 PM, Bond <jamesbond.2...@gmail.com> wrote: > >> Hi,in > >> http://lxr.linux.no/#linux+v2.6.18/include/linux/list.h > >> following section of code > >> there is a structure which is defined as > >> > >> struct list_head { > >> struct list_head *next, *prev; > >> }; > >> > >> It is used in another file as > >> > >> #define LIST_HEAD_INIT(name) { &(name), &(name) } > >> > >> #define LIST_HEAD(name) \ > >> struct list_head name = LIST_HEAD_INIT(name) > >> > >> static inline void INIT_LIST_HEAD(struct list_head *list) > >> { > >> list->next = list; > >> list->prev = list; > >> } > >> > >> > >> I came across a book where the code is given as follows in an example > >> > >> include/linux/list.h > >> struct list_head { > >> struct list_head *next,*prev; > >> }; > >> #define LIST_HEAD_INIT(name) {&(name),&(name)} > >> > >> #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name) > >> #define INIT_LIST_HEAD(prt) do {\ > >> (ptr)->next = (ptr);(ptr)->prev= (ptr);\ > >> }while(0) > >> > >> > >> I was not able to understand above code segment. > >> I am aware of what a #define is but still I could not understand above > thing. > >> Can some one help in understanding with some example. > >> Not related to Linux Kernel a normal example where I > >> can make a link list with above defined way. > > > > 0 > > > >> > I read that link but could not understand much out of it. > > -- > 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 > > The list_head structure here is used inside your structure which's used as the nodes for linked list . Whenever you are going to create a node for your list , all the pointers next and prev are initialized to point to itself.This is what INIT_LIST_HEAD does . But the same task can be done at compile time by using the maco LIST_HEAD_INIT() In fact both of them are used for the same task . Now We need a starting point for the list . This variable points to the first element of the list. We use the variable of type list_head to accomplish this . this is often used as a local variable so that you can do it anywhere in your kernel . So to initialize this we use the macro LIST_HEAD() we add elements to this list using the list_add function( api you could call it)