libevent源码分析-- queue h中TAILQ QUEUE的理解

                libevent中的例子中使用的是FreeBSD下的queue.h,在linux的/usr/include/sys/queue.h也有该头文件,下tail queue的定义
#define TAILQ_HEAD(name, type)    \struct name {      \ struct type *tqh_first; /* first element */ \ struct type **tqh_last; /* addr of last next element */\}#define TAILQ_ENTRY(type)     \struct {       \ struct type *tqe_next; /* next element */  \ struct type **tqe_prev;/* addr of previous next element*/ \}                                                                #define TAILQ_INIT(head) do {    \ (head)->tqh_first = NULL;    \ (head)->tqh_last = &(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_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_FIRST(head)  ((head)->tqh_first)#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)) 

我们就先分析上面的这些定义,先看个应用的例子

#include <stdio.h>#include <stdlib.h>#include "queue.h"struct QUEUE_ITEM{
     int value; TAILQ_ENTRY(QUEUE_ITEM) entries;};TAILQ_HEAD(,QUEUE_ITEM) queue_head;int main(int argc,char **argv)struct QUEUE_ITEM *item; struct QUEUE_ITEM *tmp_item; TAILQ_INIT(&queue_head); int i=0for(i=5;i<10;i+=2){  item=malloc(sizeof(item));  item->value=i;  TAILQ_INSERT_TAIL(&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值