[ns] What's ifhead_.lh_first mean?
Hi,
> When I read ns source codes, in file channel.cc, I cannot understand
> the variable ifhead_.lh_first, what is it defined as?
> I know ifhead_ is a structor in c++, but can you tell me what's the
> detail of the structor? and what's ifhead_.lh_first ?
The if_head structure is defined in mac/phy.h (which is included by mac/channel.h)
as below.
--------------
58 LIST_HEAD(if_head, Phy);
LIST_HEAD macro is defined in lib/bsd-list.h as below.
--------------
92 /*
93 * List definitions.
94 */
95 #define LIST_HEAD(name, type) \
96 struct name { \
97 type *lh_first; /* first element */ \
98 }
So, if_head structure is defined as below.
--------------
struct if_head {
Phy *lh_first; /* first element */
}
So, ifhead.lh_first means "the firstly-added PHY element on this Channel."
A phy is added onto a channel by executing "addif" instance procedure of
the channel at TCL level. Below is the C++ code which is executed when
"addif" instance procedure is executed.
mac/channel.cc
--------------
98 int Channel::command(int argc, const char*const* argv)
99 {
100
101 if (argc == 3) {
102 TclObject *obj;
103
104 if( (obj = TclObject::lookup(argv[2])) == 0) {
105 fprintf(stderr, "%s lookup failed\n", argv[1]);
106 return TCL_ERROR;
107 }
108 if (strcmp(argv[1], "trace-target") == 0) {
109 trace_ = (Trace*) obj;
110 return (TCL_OK);
111 }
112 else if(strcmp(argv[1], "addif") == 0) {
113 ((Phy*) obj)->insertchnl(&ifhead_);
114 ((Phy*) obj)->setchnl(this);
115 return TCL_OK;
116 }
mac/phy.h
------------
87 // list of all network interfaces on a channel
88 Phy* nextchnl(void) const { return chnl_link_.le_next; }
89 inline void insertchnl(struct if_head *head) {
90 LIST_INSERT_HEAD(head, this, chnl_link_);
91 //channel_ = chnl;
92 }
lib/bsd-list.h
--------------
128 #define LIST_INSERT_HEAD(head, elm, field) { \
129 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
130 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
131 (head)->lh_first = (elm); \
132 (elm)->field.le_prev = &(head)->lh_first; \
133 }
Could this be answer for your question?
Regards,
masuto