LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-04-2005, 01:04 AM   #1
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Rep: Reputation: 30
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?
 
Old 03-04-2005, 06:16 PM   #2
gr33ndata
Member
 
Registered: Aug 2003
Location: DMZ
Distribution: Ubuntu
Posts: 144

Rep: Reputation: 15
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;
}
 
Old 03-05-2005, 01:02 AM   #3
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
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 ?
 
Old 03-05-2005, 03:27 PM   #4
gr33ndata
Member
 
Registered: Aug 2003
Location: DMZ
Distribution: Ubuntu
Posts: 144

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists: java vs c kpachopoulos Programming 4 10-30-2005 04:41 PM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 05:56 PM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM


All times are GMT -5. The time now is 07:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration