[NS2] LIST usage
怎樣使用ns自己的串列(List)
(LIST_ENTRY -> LIST_HEAD -> LIST_INIT)
1、#include "lib/bsd-list.h"
2、初始化:
1) 在節點中添加,包括了指向前驅和後繼的節點。
#define LIST_ENTRY(type)
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}
2) 定義一個串列,串列的類型為type,表頭為lh_first。
#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
3) 初始化串列。
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}
4) 插入節點。
#define LIST_INSERT_AFTER(listelm, elm, field) {
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)
(listelm)->field.le_next->field.le_prev = &(elm)->field.le_next;
(listelm)->field.le_next = (elm);
(elm)->field.le_prev = &(listelm)->field.le_next;
}
#define LIST_INSERT_BEFORE(listelm, elm, field) {
(elm)->field.le_prev = (listelm)->field.le_prev;
(elm)->field.le_next = (listelm);
*(listelm)->field.le_prev = (elm);
(listelm)->field.le_prev = &(elm)->field.le_next;
}
#define LIST_INSERT_HEAD(head, elm, field) {
if (((elm)->field.le_next = (head)->lh_first) != NULL)
(head)->lh_first->field.le_prev = &(elm)->field.le_next;
(head)->lh_first = (elm);
(elm)->field.le_prev = &(head)->lh_first;
}
5) 刪除節點
#define LIST_REMOVE(elm, field) {
if ((elm)->field.le_next != NULL)
(elm)->field.le_next->field.le_prev = (elm)->field.le_prev;
*(elm)->field.le_prev = (elm)->field.le_next;
}
6)一個例子
1. 定義節點類型
class MFlood_RTEntry {
friend class MFlood_RTable;
friend class MFlood;
public:
MFlood_RTEntry();
MFlood_RTEntry(nsaddr_t src,u_int32_t seq);
bool isNewSeq(u_int32_t seq); // old -> false, new->true
void addSeq(u_int32_t seq); // add a seqno to seqno array(rt_seqnos)
protected:
LIST_ENTRY(MFlood_RTEntry) rt_link;
nsaddr_t src_;
u_int32_t seq_;
u_int32_t rt_seqnos[REM_SEQ_COUNT]; //seqno array
u_int32_t max_seqno; //max seqno
u_int32_t min_seqno; //max seqno
u_int16_t seq_it; // seqno's iterator
};
2. 建立一個串列節點類型為MFlood_RTEntry的串列 rthead
LIST_HEAD(, MFlood_RTEntry) rthead;
3. 初始化鏈表rhead為NULL
LIST_INIT(&rthead)
4. 怎樣使用
MFlood_RTEntry*
MFlood_Rtable::rt_lookup(nsaddr_t id) {
Mflood_RTEntry *rt = rthead.lh_first; //獲取鏈表表頭
for(; rt; rt = rt->rt_link.le_next) {
if(rt->src_ == id)
break;
}
return rt;
}
5. 刪除節點
void
MFlood_RTable::rt_delete(nsaddr_t id) {
MFlood_RTEntry *rt = rt_lookup(id);
if(rt) {
LIST_REMOVE(rt, rt_link);
delete rt;
}
}
6. 插入節點
rt = new MFlood_RTEntry(ih->saddr(), fh->seq_);
LIST_INSERT_HEAD(&rtable_.rthead,rt,rt_link);
