LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   linked lists in linux kernel how they works? (http://www.linuxquestions.org/questions/programming-9/linked-lists-in-linux-kernel-how-they-works-297448/)

cranium2004 03-04-2005 01:04 AM

linked lists in linux kernel how they works?
 
hello,
In kernel source in list.h i found following info
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
What i want to know is how LIST_HEAD_INIT(name) evaluted? what will first definition return to second one?

gr33ndata 03-04-2005 06:16 PM

You have to understand the #define thing first

Code:

# define x 12
int y = x;
printf("X:= %d\n",x);

The compiler then will replace the occurence of x every time and replaces it with 12.

Code:

#define MIN(m,n) (m>n)?n:m
int y = MIN(4,3);

Here the compiler will do the following
Code:

int y = (4>3)?3:4;
Ok now to your question
suppose at any part of your code you write the following
Code:

LIST_HEAD(my_struct);
The compiler will replace it with the following
Code:

struct list_head my_struct { &my_struct , &my_struct};
This is some how AFAIK a recursife declaration
I can guess the structure list_head looks as follows
Code:

struct list_head{
  struct list_head* ptr1;
  struct list_head* ptr2;
}


cranium2004 03-05-2005 01:02 AM

So if i called LIST_HEAD like this LIST_HEAD(my_struct);
then it will be replaced as
struct list_head my_struct { &my_struct , &my_struct }
so does that mean it created doubly linked list
struct list_head my_struct {
struct list_head *prev;
struct list_head *next;
}
but how can the &my_struct replaced by pointer next/prev ?

gr33ndata 03-05-2005 03:27 PM

It doesn't. The structure may must have been defined in other place. This will only initialize the structure.


All times are GMT -5. The time now is 01:20 AM.