-   Programming (
-   -   linked lists in linux kernel how they works? (

cranium2004 03-04-2005 02:04 AM

linked lists in linux kernel how they works?
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 07:16 PM

You have to understand the #define thing first


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


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

Here the compiler will do the following

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

The compiler will replace it with the following

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

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

cranium2004 03-05-2005 02: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 04: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:56 AM.