473,665 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Could anyone explain the code for me?

The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type )
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}

Questions:
Why the struct does not have a name?
Is the list a two-way list?

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field?

#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;
Thanks in advance.

Jack
Jul 22 '05 #1
13 2457
C++fan wrote:
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type) This doesn't do anything useful.
struct name {
type *lh_first; /* first element */
}
Missing semi-colon after struct definition.
#define LIST_ENTRY(type ) See above.
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */ I suspect le_prev's comment is wrong. Surely le_prev is the previous
node in the list, not the previous le_next (which is /this/ node).
}

Questions:
Why the struct does not have a name?
Looks like an error to me.
Is the list a two-way list? Well there are pointers to both previous and next nodes...

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}
That isn't how macros work. Read a textbook.

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field? They're arguments to a function-style macro. Whoever wrote this doesn't
understand macros, and should be using functions instead.


#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;


How about using functions so we can see what the types of the arguments
are? If possible, post a compileable example.

HTH,
Jacques.

Jul 22 '05 #2
C++fan skrev i meldingen
<15************ **************@ posting.google. com>...
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?


It's an attempt at C++ code written by someone probably learning
basic programming. It's not even syntactically correct. Searching
for "meaning" in such code is useless; evaluate on syntax only.

Jul 22 '05 #3
"C++fan" <jw****@excite. com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type )
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}

Questions:
Why the struct does not have a name?
Is the list a two-way list?

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field?

#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;
Thanks in advance.

Jack


Jack, that's quite a puzzle. As a true "C++fan" I hope you won't actually
use crap like this unless you have to! (Hint: std::list)

Anyway, I'm far from sure of this but maybe it is used as follows:

struct fred_node
{
LIST_ENTRY(stru ct fred_node) node;
Fred fred; // Fred is some arbitrary type -- this is a list of Freds
};

which translates to:

struct fred_node
{
struct {
struct fred_node *le_next; /* next element */
struct fred_node **le_prev; /* address of previous next element */
} node;
Fred fred; // Fred is some arbitrary type -- this is a list of Freds
};

This defines the node type. Then make the list head:

LIST_HEAD(fred_ head, struct fred_node) fredlist;
LIST_INIT(fredl ist);

which is equivalent to:

struct fred_head {
struct fred_node *lh_first; /* first element */
} fredlist;
fredlist->lh_first = NULL;

Now add a node:

struct fred_node *new_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
new_node.fred = Fred(21); // whatever that means
LIST_INSERT_HEA D(mylist, new_node, node);

which expands to

struct fred_node *new_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
new_node.fred = Fred(21); // whatever that means
if ((new_node->node.le_next = (fredlist)->lh_first) != NULL)
fredlist->lh_first->node.le_prev = &new_node->node.le_next ;
fredlist->lh_first = new_node;
new_node->node.le_prev = &new_node;

Now add another node:

struct fred_node *newer_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
newer_node.fred = Fred(-33); // whatever that means
LIST_INSERT_AFT ER(newer_node, new_node, node);

which expands to:

struct fred_node *newer_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
newer_node.fred = Fred(-33); // whatever that means
if ((new_node->node.le_next = newer_node->node.le_next ) != NULL)
newer_node->node.le_next->node.le_prev = &new_node->node.le_next ;
newer_node->node.le_next = new_node;
new_node->node.le_prev = &new_node;

Well, that's what I came up with. I didn't test anything. Good luck.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
C++fan wrote:
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type )
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}


<snip> more of the same </snip>

That's the sort of code people used to write when they wanted to do
generic programming. Awful, isn't it? This is why templates were
introduced to C++.

Jul 22 '05 #5
Hi Jacques:

Thanks a lot.
The code is modified from /sys/queue.h of FreeBSD. Below is the link:
http://fxr.watson.org/fxr/source/sys/queue.h

The original code is from line 328 to 380. But I can not completely
understand it. It is confusing.

Regards,

Jack

Jacques Labuschagne <ja*****@clawsh rimp.com> wrote in message news:<Ls******* *************@n ews02.tsnz.net> ...
C++fan wrote:
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type) This doesn't do anything useful.
struct name {
type *lh_first; /* first element */
}


Missing semi-colon after struct definition.


Why use a struct?
#define LIST_ENTRY(type ) See above.
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */

I suspect le_prev's comment is wrong. Surely le_prev is the previous
node in the list, not the previous le_next (which is /this/ node).
}

Questions:
Why the struct does not have a name?


Looks like an error to me.


This is the same as the original code.
Is the list a two-way list? Well there are pointers to both previous and next nodes...

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}


That isn't how macros work. Read a textbook.

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field?

They're arguments to a function-style macro. Whoever wrote this doesn't
understand macros, and should be using functions instead.


I agree. I like function. But I have to use it. The three
arguements---listelm, elm and field confuse me.

#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;

It is really confusing. What is (elm)->field?

How about using functions so we can see what the types of the arguments
are? If possible, post a compileable example.

HTH,
Jacques.

Jul 22 '05 #6
C++fan wrote:
Hi Jacques:

Thanks a lot.
The code is modified from /sys/queue.h of FreeBSD. Below is the link:
http://fxr.watson.org/fxr/source/sys/queue.h

The original code is from line 328 to 380. But I can not completely
understand it. It is confusing.


The slashes at the end of each line are not decoration. :-)
They join lines, so for example
#define MY_MACRO(x) \
((x)*2)
means
#define MY_MACRO(x) ((x)*2)

That's pretty ugly code, but they probably felt they had a good reason
at the time. The struct definitions could be used like this:

typedef LIST_ENTRY(foo) foo_entry;

which the preprocessor would turn into

typedef struct{
struct foo *le_next;
struct foo **le_prev
} foo_entry;

which is perfectly valid C (and C++).

Jul 22 '05 #7
By the way, is there any particular reason you're looking at the BSD
source? Or do you just like the deep end?

Jacques.

Jul 22 '05 #8
"C++fan" <jw****@excite. com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type )
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}

Questions:
Why the struct does not have a name?
Is the list a two-way list?

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field?

#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;
Thanks in advance.

Jack


Here is some tested code. I still hate it. :)

struct list_node
{
LIST_ENTRY(list _node) node;
int thing;
};

void list_test()
{
LIST_HEAD(t_hea d, list_node) mylist;
LIST_INIT(&myli st);
list_node *p1 = new list_node;
p1->thing = 24;
LIST_INSERT_HEA D(&mylist, p1, node);
list_node *p2 = new list_node;
p2->thing = -3;
LIST_INSERT_AFT ER(p1, p2, node);

list_node *p3 = mylist.lh_first ;
while (p3 != 0)
{
std::cout << p3->thing << '\n';
p3 = p3->node.le_next ;
}

delete p2;
delete p1;
}

Output is:

24
-3

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #9
Hi Cy:

Thank you very much. Your translation makes me understand more. But I
still have questions about your translation. Please look below.
By the way, I do not like the original code. But I have to use it. I
am studying NS2, which uses that code.

Regards,

Jack

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message news:<3h******* *********@twist er.nyroc.rr.com >...
"C++fan" <jw****@excite. com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?

/*
* List definitions.
*/

#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type )
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}

Questions:
Why the struct does not have a name?
Is the list a two-way list?

/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}

#define LIST_INSERT_AFT ER(listelm, elm, field) {
if (((elm)->field.le_nex t = (listelm)->field.le_nex t) != NULL)
(listelm)->field.le_nex t->field.le_pre v =
&(elm)->field.le_nex t;
(listelm)->field.le_nex t = (elm);
(elm)->field.le_pre v = &(listelm)->field.le_nex t;
}

Question:
What is listelm, elm, field?

#define LIST_INSERT_HEA D(head, elm, field) {
if (((elm)->field.le_nex t = (head)->lh_first) != NULL)
(head)->lh_first->field.le_pre v =
&(elm)->field.le_nex t;
(head)->lh_first = (elm);
(elm)->field.le_pre v = &(head)->lh_first;
}

Question:

If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_pre v = &(head)->lh_first;
Thanks in advance.

Jack
Jack, that's quite a puzzle. As a true "C++fan" I hope you won't actually
use crap like this unless you have to! (Hint: std::list)

Anyway, I'm far from sure of this but maybe it is used as follows:

struct fred_node
{
LIST_ENTRY(stru ct fred_node) node;
Fred fred; // Fred is some arbitrary type -- this is a list of Freds
};

which translates to:

struct fred_node
{
struct {
struct fred_node *le_next; /* next element */
struct fred_node **le_prev; /* address of previous next element */
} node;
Fred fred; // Fred is some arbitrary type -- this is a list of Freds
};

This defines the node type. Then make the list head:

LIST_HEAD(fred_ head, struct fred_node) fredlist;
LIST_INIT(fredl ist);

which is equivalent to:

struct fred_head {
struct fred_node *lh_first; /* first element */
} fredlist;
fredlist->lh_first = NULL;

Now add a node:

struct fred_node *new_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
new_node.fred = Fred(21); // whatever that means
LIST_INSERT_HEA D(mylist, new_node, node);

which expands to

struct fred_node *new_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
new_node.fred = Fred(21); // whatever that means
if ((new_node->node.le_next = (fredlist)->lh_first) != NULL)
fredlist->lh_first->node.le_prev = &new_node->node.le_next ;
fredlist->lh_first = new_node;
new_node->node.le_prev = &new_node;


if ((new_node->node.le_next = (fredlist)->lh_first) != NULL)
It means the head is not NULL, right? So
fredlist->lh_first->node.le_prev should be NULL.
new_node->node.le_next should be NULL, since new_node is a new node.
So what is the meaning of:
fredlist->lh_first->node.le_prev = &new_node->node.le_next ;

If head is NULL, (fredlist)->lh_first is NULL. the new_node becomes
the head.
What is the meaning of new_node->node.le_prev = &new_node; ?
Now add another node:

struct fred_node *newer_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
newer_node.fred = Fred(-33); // whatever that means
LIST_INSERT_AFT ER(newer_node, new_node, node);

which expands to:

struct fred_node *newer_node = (struct fred_node *) malloc(sizeof(s truct
fred_node));
newer_node.fred = Fred(-33); // whatever that means
if ((new_node->node.le_next = newer_node->node.le_next ) != NULL)
newer_node->node.le_next->node.le_prev = &new_node->node.le_next ;
newer_node->node.le_next = new_node;
new_node->node.le_prev = &new_node;


Since newer_node is a new node, newer_node->node.le_next should be
NULL, right?
newer_node->node.le_next->node.le_prev equals to newer_node itself,
right?
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1636
by: John Dean | last post by:
Hi I have been reading quite a lot of Python source code recently and I have come across a particular construct which I don't understand. I would be grateful if somebody could explain the reason for including the following lines of code. I have seen it only at the beginning of a module:- global sys import sys
4
1701
by: Chris | last post by:
Hello Could anyone explain why the following: #footer ul { float : left; margin : 2px 0px 7px 28px; padding : 0px; width : 360px;
3
1557
by: Sid | last post by:
Hi folks, I wrote this code to test if I get the same address for all the variables in a union but for some reason the address I got when I used a char variable in a union seems bizarre- can anyone help explain why I get a bizzarre address for a char variable inside a union ? In other words why is &(x.i) not 0012FED0 in the code below ?
6
2871
by: B. Penn | last post by:
Hi, I was testing pointers and found that I could still dereference a pointer and access the value/variable it pointed to after deleting it, which confused me for "the variable it pointed to is deleted and it now points to nowhere". Did I do anything wrong? I checked a couple of references but couldn't explain it. Could anyone kindly clear it for me please? Here is my code, which compiled with both GNU C++ and Visual Studio ..Net:
21
1630
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
20
5334
by: ctyrrell | last post by:
Does anyone have any idea how to recover from a run-time error 3002 which I get after creating a workspace 242 times? Or better yet, avoid getting it in the first place? I am creating a workspace with a blank password for different Users in order to find out which Users do not have passwords. This works just fine if I do it a few times, but if I want to do it periodically with a loop, it bombs out after 242 times of finding a user with...
1
3593
by: Andrew | last post by:
Hello, friends, I am implementing web app security using asp.net 1.1, and I found the following source code from Yahoo! Mail login page: <form method="post" action="https://login.yahoo.com/config/login?" autocomplete="off" name="login_form"> <input type="hidden" name=".tries" value="1"> <input type="hidden" name=".src" value="ym"> <input type="hidden" name=".md5" value="">
21
55611
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
4
1810
by: Chuthu | last post by:
Could anyone explain me the following concepts in detail? 1. JAXP 2. BGRAPH 3. JBOSS work flow engine
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.