struct ngx_queue_s{ ngx_queue_t *prev; ngx_queue_t *next; };
#define ngx_queue_init(q) (q)->prev = q; (q)->next = q
#define ngx_queue_empty(h) (h == (h)->prev)
#define ngx_queue_insert_head(h, x) (x)->next = (h)->next; (x)->next->prev = x; (x)->prev = h; (h)->next = x
#define ngx_queue_insert_after ngx_queue_insert_head
#define ngx_queue_insert_tail(h, x) (x)->prev = (h)->prev; (x)->prev->next = x; (x)->next = h; (h)->prev = x
#define ngx_queue_head(h) (h)->next
#define ngx_queue_last(h) (h)->prev
#define ngx_queue_next(q) (q)->next
#define ngx_queue_prev(q) (q)->prev
#define ngx_queue_remove(x) (x)->next->prev = (x)->prev; (x)->prev->next = (x)->next; (x)->prev = NULL; (x)->next = NULL
ngx_queue_split(h, q, n)
函数功能:分割队列。#define ngx_queue_split(h, q, n) (n)->prev = (h)->prev; (n)->prev->next = n; (n)->next = q; (h)->prev = (q)->prev; (h)->prev->next = h; (q)->prev = n;
函数解析:该宏有3个参数,h为队列头(即链表头指针),将该队列从q节点将队列分割为两个队列,q之后的节点组成的新队列的头节点为n,图形演示如下。
分割前:
分割后:
#define ngx_queue_add(h, n) (h)->prev->next = (n)->next; (n)->next->prev = (h)->prev; (h)->prev = (n)->prev; (h)->prev->next = h;
#define ngx_queue_data(q, type, link) (type *) ((u_char *) q - offsetof(type, link))备注:offsetof主要用于求结构体中一个成员在该结构体中的偏移量,第一个参数是结构体的名字,第二个参数是结构体成员的名字。
函数功能:获取队列的中间节点,若队列有奇数个节点(除头节点外),则返回中间的节点,若队列有偶数个节点,则返回后半个队列的第一个节点。
算法思想: middle指针每次后移一个节点,而 next指针每次后移两个节点。ngx_queue_t * ngx_queue_middle(ngx_queue_t *queue) { ngx_queue_t *middle, *next; middle = ngx_queue_head(queue); if (middle == ngx_queue_last(queue)) { return middle; } next = ngx_queue_head(queue); for ( ;; ) { middle = ngx_queue_next(middle); next = ngx_queue_next(next); if (next == ngx_queue_last(queue)) { //奇数个节点 return middle; } next = ngx_queue_next(next); if (next == ngx_queue_last(queue)) { //偶数个节点 return middle; } } }
函数功能:排序队列,使用的是稳定的插入排序。
算法思想:从第一个节点开始遍历,依次将当前节点 q插入到前面已经排好序的队列链表中。Void ngx_queue_sort(ngx_queue_t *queue, ngx_int_t (*cmp)(const ngx_queue_t *, const ngx_queue_t *)) { ngx_queue_t *q, *prev, *next; q = ngx_queue_head(queue); if (q == ngx_queue_last(queue)) { return; } for (q = ngx_queue_next(q); q != ngx_queue_sentinel(queue); q = next) { prev = ngx_queue_prev(q); next = ngx_queue_next(q); ngx_queue_remove(q); do { if (cmp(prev, q) <= 0) { //比较节点 break; } prev = ngx_queue_prev(prev); } while (prev != ngx_queue_sentinel(queue)); ngx_queue_insert_after(prev, q); //依次将节点插入到已经排好序的队列链表中 } }