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,否则永远不会发送成功,而且这个请求将一直不会结束

相关推荐
JeffersonZU1 分钟前
【数据结构】2-3-2 单链表的插入删除
数据结构
m0_738206545 分钟前
嵌入式学习的第二十二天-数据结构-栈+队列
数据结构·学习
小羊Linux客栈6 分钟前
自动化:批量文件重命名
运维·人工智能·python·自动化·游戏程序
伤不起bb1 小时前
MySQL 高可用
linux·运维·数据库·mysql·安全·高可用
whgjjim3 小时前
docker迅雷自定义端口号、登录用户名密码
运维·docker·容器
先做个垃圾出来………4 小时前
哈夫曼树(Huffman Tree)
数据结构·算法
Inverse1626 小时前
C语言_动态内存管理
c语言·数据结构·算法
瀚高PG实验室6 小时前
连接指定数据库时提示not currently accepting connections
运维·数据库
QQ2740287566 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
淡忘_cx6 小时前
【frp XTCP 穿透配置教程
运维