首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

libevent tail queue,该怎么处理

2013-02-24 
libevent tail queue/* * Tail queue definitions. */#define TAILQ_HEAD(name, type)\struct name {\stru

libevent tail queue

/*
 * Tail queue definitions.
 */
#define TAILQ_HEAD(name, type)\
struct name {\
struct type *tqh_first;/* first element */\
struct type **tqh_last;/* addr of last next element */\
}

#define TAILQ_HEAD_INITIALIZER(head)\
{ NULL, &(head).tqh_first }

#define TAILQ_ENTRY(type)\
struct {\
struct type *tqe_next;/* next element */\
struct type **tqe_prev;/* address of previous next element */\
}

/* 
 * tail queue access methods 
 */
#defineTAILQ_FIRST(head)((head)->tqh_first)
#defineTAILQ_END(head)NULL
#defineTAILQ_NEXT(elm, field)((elm)->field.tqe_next)
#define TAILQ_LAST(head, headname)\
(*(((struct headname *)((head)->tqh_last))->tqh_last))
/* XXX */
#define TAILQ_PREV(elm, headname, field)\
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#defineTAILQ_EMPTY(head)\
(TAILQ_FIRST(head) == TAILQ_END(head))

#define TAILQ_FOREACH(var, head, field)\
for((var) = TAILQ_FIRST(head);\
    (var) != TAILQ_END(head);\
    (var) = TAILQ_NEXT(var, field))

#define TAILQ_FOREACH_REVERSE(var, head, field, headname)\
for((var) = TAILQ_LAST(head, headname);\
    (var) != TAILQ_END(head);\
    (var) = TAILQ_PREV(var, headname, field))

/*
 * Tail queue functions.
 */
#defineTAILQ_INIT(head) do {\
(head)->tqh_first = NULL;\
(head)->tqh_last = &(head)->tqh_first;\
} while (0)

#define TAILQ_INSERT_HEAD(head, elm, field) do {\
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)\
(head)->tqh_first->field.tqe_prev =\
    &(elm)->field.tqe_next;\
else\
(head)->tqh_last = &(elm)->field.tqe_next;\
(head)->tqh_first = (elm);\
(elm)->field.tqe_prev = &(head)->tqh_first;\
} while (0)

#define TAILQ_INSERT_TAIL(head, elm, field) do {\
(elm)->field.tqe_next = NULL;\
(elm)->field.tqe_prev = (head)->tqh_last;\
*(head)->tqh_last = (elm);\
(head)->tqh_last = &(elm)->field.tqe_next;\
} while (0)

#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {\
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
(elm)->field.tqe_next->field.tqe_prev =\
    &(elm)->field.tqe_next;\
else\
(head)->tqh_last = &(elm)->field.tqe_next;\
(listelm)->field.tqe_next = (elm);\
(elm)->field.tqe_prev = &(listelm)->field.tqe_next;\
} while (0)

#defineTAILQ_INSERT_BEFORE(listelm, elm, field) do {\
(elm)->field.tqe_prev = (listelm)->field.tqe_prev;\
(elm)->field.tqe_next = (listelm);\


*(listelm)->field.tqe_prev = (elm);\
(listelm)->field.tqe_prev = &(elm)->field.tqe_next;\
} while (0)

#define TAILQ_REMOVE(head, elm, field) do {\
if (((elm)->field.tqe_next) != NULL)\
(elm)->field.tqe_next->field.tqe_prev =\
    (elm)->field.tqe_prev;\
else\
(head)->tqh_last = (elm)->field.tqe_prev;\
*(elm)->field.tqe_prev = (elm)->field.tqe_next;\
} while (0)

#define TAILQ_REPLACE(head, elm, elm2, field) do {\
if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)\
(elm2)->field.tqe_next->field.tqe_prev =\
    &(elm2)->field.tqe_next;\
else\
(head)->tqh_last = &(elm2)->field.tqe_next;\
(elm2)->field.tqe_prev = (elm)->field.tqe_prev;\
*(elm2)->field.tqe_prev = (elm2);\
} while (0)



我从文件中提取出来的,因为libevent在队列中使用的是event,类型有点复杂,搞的有点晕。
那位大虾写个例子,结构体越简单越好,最好就一个整数 libevent
[解决办法]
就是当前的node里存前一个node的next指针的地址吧,没什么特别的。
[解决办法]
这是一个双向链表, struct event_list;

TAILQ_HEAD (event_list, event);  有这样一行代码, 就是定义

#define TAILQ_HEAD(name, type)\
struct name {\
struct type *tqh_first;/* first element */\
struct type **tqh_last;/* addr of last next element */\
}


经过上面的宏定义替换后就变成了
struct event_list
{
struct type *tqh_first;/* first element */\
struct type **tqh_last;/* addr of last next element */\
}

热点排行