/*
* 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 */ \
}
TAILQ_ENTRY没有结构体名,所以它一般都是另外一个结构体的成员,tqe_next和tqh_frist是一级指向结构体地址,tqh_last和tqh_prev是二级指针,也就是指向指针的指针,所以指向队列的next指针,构造出来的队列一般如下图所示:
//队列中的第一个元素
#define TAILQ_FIRST(head) ((head)->tqh_first)
//队列结束
#define TAILQ_END(head) NULL
//队列中当前元素的下一个节点
#define TAILQ_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))
//队列是否为空
#define TAILQ_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, headname, field) \
for((var) = TAILQ_LAST(head, headname); \
(var) != TAILQ_END(head); \
(var) = TAILQ_PREV(var, headname, field))
/*
* Tail queue functions.
*/
//队列头初始化
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
//队列初始化
#define TAILQ_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)
//队列节点前插入
#define TAILQ_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)
其他的宏都比较简单,比较好理解,TAILQ_LAST,TAILQ_PREV这两个宏可能比较难理解。
//队列最后一个元素
#define TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
一级指针tqh_first指向队列第一个节点的地址,二级指针tqh_last指向队列最后一个节点的next指针,队列节点的TAILQ_ENTRY同样也是一级指针tqe_next指向下一个节点的地址,tqe_prev指向前一个节点的next指针。
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
struct type **tqe_prev; /* address of previous next element */ \
}
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
下面这张图更能反映文中结构体的内存布局,TAILQ_HEAD和TAILQ_ENTRY都是一级指针在前二级指针在后,TAILQ_HEAD和TAILQ__ENTRY内存分布相同,强制类型转换获取TAILQ_ENTRY的地址,因为**tqh_last指向*tqh_next, 无论一级还是二级指针,其都是指向另外一个地址,二级指针指向一级指针的地址,TAILQ_ENTRY结构中tqh_next在前它的地址强制类型转换TAILQ_HEAD,前面说的TAILQ_HEAD和TAIL_ENTRY内存结构相同,就可以表示TAILQ_ENTRY的地址,全部通过指针获取元素的值。
TAILQ_HEAD (event_list, event);
//所以event_list的定义展开后如下:
struct event_list
{
struct event *tqh_first;
struct event **tqh_last;
};
struct event_base;
struct event {
struct event_callback ev_evcallback;
/* for managing timeouts */
union {
TAILQ_ENTRY(event) ev_next_with_common_timeout;
size_t min_heap_idx;
} ev_timeout_pos;
evutil_socket_t ev_fd;
struct event_base *ev_base;
union {
/* used for io events */
struct {
LIST_ENTRY (event) ev_io_next;
struct timeval ev_timeout;
} ev_io;
/* used by signal events */
struct {
LIST_ENTRY (event) ev_signal_next;
short ev_ncalls;
/* Allows deletes in callback */
short *ev_pncalls;
} ev_signal;
} ev_;
short ev_events;
short ev_res; /* result passed to event callback */
struct timeval ev_timeout;
};
TAILQ_HEAD (event_list, event);
#include
#include
#include "queue.h"
#define ENTRY_NAME entry
struct queue_entry_t
{
int value;
//从TAILQ_ENTRY的定义可知,它只能是结构体或者共用体的成员变量
TAILQ_ENTRY(queue_entry_t)ENTRY_NAME;
};
TAILQ_HEAD(queue_head_t, queue_entry_t);
int main(int argc, char **argv)
{
struct queue_head_t queue_head;
struct queue_entry_t *queue, *new_item, *tmp,*next;
int i;
int var ;
TAILQ_INIT(&queue_head);
for(i = 0; i < 6; ++i)
{
new_item = NULL ;
new_item = (struct queue_entry_t*)malloc(sizeof(struct queue_entry_t));
new_item->value = i;
TAILQ_INSERT_TAIL(&queue_head, new_item, ENTRY_NAME);//在队尾插入数据
}
queue = (struct queue_entry_t*)malloc(sizeof(struct queue_entry_t));
queue->value = 10;
TAILQ_INSERT_HEAD(&queue_head, queue, ENTRY_NAME);//在队头插入数据
tmp = (struct queue_entry_t*)malloc(sizeof(struct queue_entry_t));
tmp->value = 20;
//在队头元素q的后面插入元素
TAILQ_INSERT_AFTER(&queue_head, queue, tmp, ENTRY_NAME);
i = 0 ;
TAILQ_FOREACH(tmp, &queue_head, ENTRY_NAME)
{
i++ ;
printf("the %dth entry is %d\n", i,tmp->value) ;
}
new_item = (struct queue_entry_t*)malloc(sizeof(struct queue_entry_t));
new_item->value = 100;
tmp = TAILQ_FIRST(&queue_head);
//用new_iten替换第一个元素
TAILQ_REPLACE(&queue_head, tmp, new_item, ENTRY_NAME);
free(tmp) ;
i = 0 ;
TAILQ_FOREACH(tmp, &queue_head, ENTRY_NAME)
{
i++ ;
printf("the %dth entry is %d\n", i,tmp->value) ;
}
while ( (tmp) = TAILQ_FIRST(&queue_head) ) {
TAILQ_REMOVE(&queue_head, tmp, ENTRY_NAME);
free(tmp);
}
if (!TAILQ_EMPTY(&queue_head)) {
printf("tail queue is NOT empty!\n");
}
return 0 ;
}