Nginx中封装的数据结构

Nginx中封装的数据结构

Nginx中封装的数据结构

整型

typedef intptr_t        ngx_int_t;
typedef uintptr_t       ngx_uint_t;

ngx_str_t【字符串】

c 复制代码
typedef struct {
    size_t      len; // 表示字符串的有效长度
    u_char     *data;// 表示字符串起始地址
} ngx_str_t;

ngx_list_t【链表】

c 复制代码
typedef struct ngx_list_part_s  ngx_list_part_t;// 表示链表中的一个元素
struct ngx_list_part_s {
    void             *elts; // 指向数组的起始位置
    ngx_uint_t        nelts;//  表示数组中已经使用了多少个元素
    ngx_list_part_t  *next;// 下一个链表元素的地址
};

// 存储数组的链表
typedef struct {
    ngx_list_part_t  *last; // 指向链表的最后一个元素
    ngx_list_part_t   part; // 指向链表的第一个元素
    size_t            size; // 限制存储值的大小
    ngx_uint_t        nalloc; // 每个ngx_list_part_t数组的容量
    ngx_pool_t       *pool; // 链表中管理内存分配的内存池对象
} ngx_list_t;

图解如下:

ngx_table_elt_t【key/value】

c 复制代码
typedef struct ngx_table_elt_s  ngx_table_elt_t;

struct ngx_table_elt_s {
    ngx_uint_t        hash; // 用于快速检索头部
    ngx_str_t         key; // 名字
    ngx_str_t         value; // 值
    u_char           *lowcase_key; // key全是小写
    ngx_table_elt_t  *next; // 
};

用于存储HTTP头部

ngx_buf_t

c 复制代码
typedef void *            ngx_buf_tag_t;

typedef struct ngx_buf_s  ngx_buf_t;

    u_char          *pos;  // 从这个位置开始处理内存中的数据
    u_char          *last; // 表示有效的内容的结束位置
    off_t            file_pos;  //  要处理文件的位置
    off_t            file_last; //  要处理文件的截止位置

    u_char          *start;         /* start of buffer */  // 要处理内存的起始地址
    u_char          *end;           /* end of buffer */    // 要处理内存的末尾地址
    ngx_buf_tag_t    tag;  // 缓冲区的类型,例如由哪个模块使用就指向这个模块变量的地址
    ngx_file_t      *file;  // 引用的文件类型
    ngx_buf_t       *shadow;


    /* the buf's content could be changed */
    unsigned         temporary:1; // 临时内存标记位,为1时表示数据在内存中且这段内存可以修改

    /*
     * the buf's content is in a memory cache or in a read only memory
     * and must not be changed
     */
    unsigned         memory:1; // 标志位,为1时表示数据在内存中且这段内存不可以被修改

    /* the buf's content is mmap()ed and must not be changed */
    unsigned         mmap:1; // 标志位,为1时表示这段内存是用mmap系统调用映射过来的,不可以被修改

    unsigned         recycled:1; // 标志位, 为1表示可回收
    unsigned         in_file:1; // 标志位,为1表示这段缓冲区处理的是文件而不是内存
    unsigned         flush:1; // 标志位,为1时表示需要执行flush操作
    unsigned         sync:1; // 标志位,对操作这块缓冲区是否使用同步方式,
    unsigned         last_buf:1; // 标志位,表示是否是最后一块缓冲区,因为ngx_buf_t可以由ngx_chain_t链表串联起来
    unsigned         last_in_chain:1; // 标志位,表示是否是ngx_chain_t中的最后一块缓冲区

    unsigned         last_shadow:1; // 标志位,表示是否为最后一个影子缓冲区
    unsigned         temp_file:1; // 标志位,表示当前缓冲区是否属于临时文件

    /* STUB */ int   num;
};

缓冲区,处理大数据,既用于存储内存数据,也用于存储磁盘数据。

ngx_chain_t

typedef struct ngx_chain_s           ngx_chain_t;

struct ngx_chain_s {
    ngx_buf_t    *buf; // 指向当前的ngx_buf_t缓冲区
    ngx_chain_t  *next; // 指向下一个ngx_chain_t,如果是最后一个,则为NULL
};

在向用户发送HTTP包体时,就要传入ngx_chain_t链表对象,注意,如果是最后一个ngx_chain_t,那么必须将next置为NULL,否则永远不会发送成功,而且这个请求将一直不会结束

相关推荐
大小科圣3 分钟前
lnmp平台
运维·服务器·nginx
苏言の狗5 分钟前
小R的并集大小期望计算 | 蛮力
数据结构·算法
听风吹等浪起8 分钟前
计算机网络基础:IIS服务器(WEB服务器)
运维·服务器·计算机网络
沐千熏9 分钟前
Liunx(CentOS-6-x86_64)使用Nginx部署Vue项目
vue.js·nginx·centos
橘颂TA12 分钟前
每日一练之链表的回文结构
数据结构·链表
刃神太酷啦16 分钟前
数据结构(蓝桥杯常考点)
数据结构·c++·蓝桥杯c++组
niuTaylor1 小时前
【Linux和RTOS简析】
linux·运维·服务器·macos·macbook air·换硬盘·扩内存
落——枫1 小时前
操作系统知识点23
linux·运维·服务器
冠位观测者1 小时前
【Leetcode 每日一题 - 补卡】2070. 每一个查询的最大美丽值
数据结构·算法·leetcode
Arbori_262151 小时前
linux 命令sed
linux·运维·服务器