队列头文件ngx_queue.h


#include <ngx_config.h>
#include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCLUDED_
#define _NGX_QUEUE_H_INCLUDED_ typedef struct ngx_queue_s ngx_queue_t; // 队列的节点,也直接表示队列。注意这是一个双向循环队列
struct ngx_queue_s {
ngx_queue_t *prev;
ngx_queue_t *next;
}; // 初始化队列 q 所指向的指针,prev 和 next 都是自己
#define ngx_queue_init(q) \
(q)->prev = q; \
(q)->next = q // 如果表 h == h 的上一个,那么就是一个空队列,其实用 next 也可以的
#define ngx_queue_empty(h) \
(h == (h)->prev) // 向 h 后面插入一个 x
#define ngx_queue_insert_head(h, x) \
(x)->next = (h)->next; \
(x)->next->prev = x; \
(x)->prev = h; \
(h)->next = x // 在后面插入 insert after,就是 insert head
#define ngx_queue_insert_after ngx_queue_insert_head // 向 h 前面插入一个 x
#define ngx_queue_insert_tail(h, x) \
(x)->prev = (h)->prev; \
(x)->prev->next = x; \
(x)->next = h; \
(h)->prev = x // h 表示队列,第一个元素为 h->next
#define ngx_queue_head(h) \
(h)->next // h 是头,h 的上一个就是尾
#define ngx_queue_last(h) \
(h)->prev #define ngx_queue_sentinel(h) \
(h) // 返回节点 q 的下一个
#define ngx_queue_next(q) \
(q)->next // 返回节点 q 的上一个
#define ngx_queue_prev(q) \
(q)->prev #if (NGX_DEBUG) // debug 模式下要把 x 的 prev、next 成员置为 0,release 版可以省去此两句
#define ngx_queue_remove(x) \
(x)->next->prev = (x)->prev; \
(x)->prev->next = (x)->next; \
(x)->prev = NULL; \
(x)->next = NULL #else // 删除一个节点
#define ngx_queue_remove(x) \
(x)->next->prev = (x)->prev; \
(x)->prev->next = (x)->next #endif //
// split 作用如下图所示:
//
// ________________________________________________
// ||--------------------------------------------||
// || ||
// |---| => |---| => … => |---| => |---| => … => |---| |---|
// | h | | | | q | | | | | | n |
// |---| <= |---| <= … <= |---| <= |---| <= … <= |---| |---|
//
// __________________________ __________________________________
// ||----------------------|| ||------------------------------||
// || || || ||
// |---| => |---| => … => |---| |---| => |---| => … => |---| => |---|
// | 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; // 将 n 代表的队列(n 为队列头)接到 h 表示的队列后面
//
// _________________________ _________________________
// ||---------------------|| ||---------------------||
// || || || ||
// |---| => |---| => … => |---| |---| => |---| => … => |---|
// | h | | | | | | n | |n1 | | |
// |---| <= |---| <= … <= |---| |---| <= |---| <= … <= |---|
//
// ________________________________________________________
// ||----------------------------------------------------||
// || ||
// |---| => |---| => … => |---| =========> |---| => … => |---|
// | h | | | | | |n1 | | |
// |---| <= |---| <= … <= |---| <========= |---| <= … <= |---|
//
#define ngx_queue_add(h, n) \
(h)->prev->next = (n)->next; \
(n)->next->prev = (h)->prev; \
(h)->prev = (n)->prev; \
(h)->prev->next = h; // 一般type如下:
// typedef struct {
// …
// LINK q
// } TYPE
// 所以这个宏可以通过 q 找到其所在的结构体 TYPE 变量的地址
#define ngx_queue_data(q, type, link) \
(type *) ((u_char *) q - offsetof(type, link)) ngx_queue_t *ngx_queue_middle(ngx_queue_t *queue); void ngx_queue_sort(ngx_queue_t *queue,
ngx_int_t (*cmp)(const ngx_queue_t *, const ngx_queue_t *)); #endif /* _NGX_QUEUE_H_INCLUDED_ */

队列源文件ngx_queue.c


#include
#include /*
* find the middle queue element if the queue has odd number of elements
* or the first element of the queue's second part otherwise
*/ //
// 这是用两个指针来找链表中点的典型应用,在很多技巧性问答中常出现。
//
ngx_queue_t *
ngx_queue_middle(ngx_queue_t *queue)
{
ngx_queue_t *middle, *next; // middle 从第一个节点开始
middle = ngx_queue_head(queue); // 如果队列只有一个节点,或者为空
if (middle == ngx_queue_last(queue)) {
return middle;
} // next 也从第一个节点开始
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;
}
}
} /* the stable insertion sort */
//
// 这是一个稳定就地排序(Stable In-place Sorting)。算法的三个性能指标(时间复杂度,空间复杂度,稳定性)
// 相比之下,quick sort 和 merge sort 更快。但 quick sort 是非稳定的,merge sort 使用 O(n) 额外空间
//
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); // 在已排好序的节点中,向前找比 q 的内容小的。比较的标准由 cmp 确定
do {
if (cmp(prev, q) <= 0) {
break;
} prev = ngx_queue_prev(prev); } while (prev != ngx_queue_sentinel(queue)); // 找到 prev 是比 q 的内容小的,在 prev 后面插入
ngx_queue_insert_after(prev, q);
}
}

从上面可以看出,create 是从 pool 分配定义 list 结构的内存,分配表头节点的内存。init 是初始化已有的 list。

Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c的更多相关文章

  1. Nginx源码完全注释(6)core/murmurhash

    下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...

  2. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...

  3. Nginx源码完全注释(2)ngx_array.h / ngx_array.c

    数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...

  4. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  5. Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c

    ngx_palloc.h /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windo ...

  6. Nginx源码完全注释(3)ngx_list.h / ngx_list.c

    列表头文件ngx_list.h #ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config. ...

  7. Nginx 源码完全注释(10)ngx_radix_tree

    ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...

  8. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  9. Nginx源码完全注释(8)ngx_errno.c

    errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中 ...

随机推荐

  1. rollupjs 基本试用

    备注:    前端构建工具   1. 安装 yarn global add rollup yarn global add rollup 2. 基本使用 touch index.js index.js ...

  2. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  3. 【转】Linux 静态库与共享库的使用

    原文网址:http://blog.csdn.net/heyabo/article/details/11688517 申明: 正如题如示,本篇讲的是Linux下是静态库与共享库,而Window下的动态链 ...

  4. ecmall页面空白解决方案(转)

    页面空白解决方案: ------------------------------------------------------------------------------------------ ...

  5. bzoj1017(JSOI2008)魔兽地图

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1017 钱数很少,所以它也能压进状态里. 还有向上贡献几个物品.所以状态就是第 i 号物品,向 ...

  6. RK3288 双屏异显时,触摸屏(USB接口)无反应

    系统版本:RK3288 android 5.1 设备同时有两个lcd,主屏是mipi接口,带有触摸屏,触摸屏是usb接口,副屏是hdmi接口,没有触摸屏,正常情况下,两个lcd显示相同内容,触摸屏一切 ...

  7. elastic search6.2.2 实现用户搜索记录查询(去重、排序)

    elastic search6.2.2 实现搜索记录查询 ,类似新浪微博这种,同样的搜索记录后面时间点的会覆盖前面的(主要思路:关键词去重,然后按时间排序) 先创建索引 //我的搜索 PUT my_s ...

  8. elasticsearch 6.0.0及之后移除了一个索引允许映射多个类型的操作(Removal of mapping types)

    分给线一下内容为理解错误内容,实际允许建立父子分档,只是类型改成来 join 官方demo: join datatypeedit The join datatype is a special fiel ...

  9. Python基本序列-字典

    Python 基本序列-字典 字典(dict)是"键-值 对"的无序可变序列,字典中的每个元素包含两部分,"键"和"值". 字典中的&quo ...

  10. 怎样优化CPU

    大家写好的代码,在浏览器上运行,总会有怎样才能让他效率更高,不卡顿...等问题,就本人而言,我觉得是以下这几个导致CPU 过高 1.不要直接监听scroll,等到鼠标滚动停止的时候再去触发事件2.控制 ...