Libevent源码分析——队列

在compat/sys/queue.h文件里定义了5种数据结构,分别是单链表、双链表、简单队列、尾队列、环形队列,其中 TAILQ_QUEUE尾队列是libevent里使用最多的, libevent中用到的队列其实也是尾队列 ,所以 这里只分析尾队列 TAILQ_QUEUE 他结构都大同小异, TAILQ_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 */  \
}

TAILQ_ENTRY没有结构体名,所以它一般都是另外一个结构体的成员,tqe_next和tqh_frist是一级指向结构体地址,tqh_last和tqh_prev是二级指针,也就是指向指针的指针,所以指向队列的next指针,构造出来的队列一般如下图所示:

Libevent源码分析——队列_第1张图片

队列操作宏函数如下:

 
//队列中的第一个元素
#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这两个宏可能比较难理解。

  TAILQ_LAST、TAILQ_PREV两个宏操作类似,就只说TAILQ_LAST:
//队列最后一个元素
#define TAILQ_LAST(head, headname)                                      \
        (*(((struct headname *)((head)->tqh_last))->tqh_last))
(head)->tqh_last这里比较好理解,像上面说的队列头TAILQ_HEAD的尾指针是二级指针,它指向队列最后一个的tqe_next指针,最后一个的tqe_next指针指向的是NULL,通过tqe_next指针怎么访问到队列的最后一个节点的值呢, ((struct headname *)((head)->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 */  \
}
(head)->tqh_last是最后一个节点的next指针,(((struct headname *)((head)->tqh_last))前面这一坨强制类型转换的就先不看,同样是二级指针TAILQ_HEAD队列头tqh_last对应队列节点 TAILQ_ENTRY的tqe_prev,假设前面强制类型转换之后就是最后一个节点queue_last的地址, queue_last -> tqh_prev就是倒数第二个节点next指针,倒数第二个节点的next指针指向最后一个节点的地址,取值 *( queue_last -> tqh_prev )就是最后一个节点的值了,全部换回来就是 (* (((struct headname *)((head)->tqh_last)) -> tqh_last ))
 
    这里就解释一下为什么强制类型转换就相当于最后一个节点的地址,其实准确来说应该是最后一个节点的TAILQ_ENTRY的地址,一般队列结构体定义如下:
 
#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的地址,全部通过指针获取元素的值。

Libevent源码分析——队列_第2张图片

 

TAILQ_QUEUE队列是由两部分组成:队列头和队列节点。在Libevent中,队列头一般是event_base结构体的一个成员变量,而队列节点则是event结构体。
其中,结构体struct event_list如下定义:
TAILQ_HEAD (event_list, event);
//所以event_list的定义展开后如下:
struct event_list
{
    struct event *tqh_first;
    struct event **tqh_last;
};
在event结构体中,则有TAILQ_ENTRY(event)类型的成员变量。
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 ;
}
参考: https://blog.csdn.net/luotuo44/article/details/38374009
 
 
 

你可能感兴趣的:(linux学习)