nginx 的数据结构 分析

一, nginx中的结构体

1, 整型的封装

nginx使用ngx_int_t 封装有符号整型, 使用nginx_uint_t封装无符号整型。 nginx各模块的变量定义都是如此使用,

 typedef intptr_tngx_int_t;
    typedef uintptr_t   ngx_uint_t;
    typedef intptr_tngx_flag_t;
2,ngx_str_t 数据结构

在nginx的领域中, ngx_str_t结构就是字符串。 ngx_str_t的定义如下:

    typedef struct {
        size_t      len;
        u_char     *data;
    } ngx_str_t;

注意:
ngx_str_t的data成员指向的并不是普通的字符串, 因为这段字符串未必会以’\0’作为结尾, 所以使用时必须根据长度len来使用data成员。

3, ngx_list_t 数据结构
typedef struct ngx_list_part_s  ngx_list_part_t;

    struct ngx_list_part_s {
        void             *elts;
        ngx_uint_t        nelts; //记录ngx_list_t数组使用多少的容量
        ngx_list_part_t  *next;
    };


    typedef struct {
        ngx_list_part_t  *last;
        ngx_list_part_t   part;
        size_t            size;  //用户存储字节数要小于 size的大小
        ngx_uint_t        nalloc;
        ngx_pool_t       *pool;
    } ngx_list_t;
①, ngx_list_t 成员的含义

1,part:链表的首个数组元素

2,last:指向链表的最后一个数组元素

3,size:链表中每个ngx_list_part_t元素都是一个数组。 因为数组存储的是某种类型的数据, 只是通过size限制每一个数组元素的占用的空间大小, 也就是用户存储的一个数据所占用的字节数必须小于或等于size。

4,nalloc:链表的数组元素一旦分配后是不可更改的。 nalloc表示每个ngx_list_part_t数组的容量, 即最多可存储多少数据。

5,pool:链表中管理内存分配的内存池对象。 用户要存储的数据占用的内存都是由pool分配的。

②, ngx_list_part_t 数据结构成员含义

1,elts:指向数组的开始的地址

2,nelts:表示数组中已经使用了多少元素。 当然, nelts必须小于ngx_list_t结构体中的nalloc。

3,next:下一个链表元素 ngx_list_part_t的地址

ngx_list_t结构体

github分析ngx_list_t 案例

4,ngx_table_elt_t 数据结构
typedef struct {
        ngx_uint_t        hash;
        ngx_str_t         key;
        ngx_str_t         value;
        u_char           *lowcase_key;
    } ngx_table_elt_t;
5, ngx_buf_t 数据结构
typedef struct ngx_buf_s  ngx_buf_t;
    //缓冲区结构
    struct ngx_buf_s 
    {
    u_char          *pos;       //缓冲区有效数据开始位置
    u_char          *last;      //缓冲区有效数据结束位置
    off_t            file_pos;      //文件开始位置
    off_t            file_last;     //文件结束位置

    u_char          *start;              //整个缓冲区的首地址
    u_char          *end;                //整个缓冲区的末地址
    ngx_buf_tag_t    tag;       //由哪个模块使用就执行那个模块的ngx_module_t结构,例如ngx_http_proxy_module 
    ngx_file_t      *file;      //文件对象
    ngx_buf_t       *shadow;        //影子缓冲区链表头结点,该链表下的所有节点的buf指向同一个空间

    unsigned         temporary:1;   //临时内存标记,值为1表示这段数据在内存中可以修改,
                            //默认创建的都是可以修改的临时缓冲

    unsigned         memory:1;      //值为1表示这段数据在内存中 且不可以修改

    unsigned         mmap:1;        //表示这个buf是否通过调用mmap获取到的

    unsigned         recycled:1;    //值为1时表示可以回收
    unsigned         in_file:1;     //是否发送文件
    unsigned         flush:1;
    unsigned         sync:1;
    unsigned         last_buf:1;    //表示是否为最后一个缓冲区  
    unsigned         last_in_chain:1;

    unsigned         last_shadow:1; //是否为影子缓冲区链表的最后一个节点
    unsigned         temp_file:1;
    };
6, ngx_chain_t 数据结构
    struct ngx_chain_s {
        ngx_buf_t    *buf;
        ngx_chain_t  *next;
    };


buf 指向当前的ngx_buf_t缓冲区, next是用来指向下一个ngx_chain_t, 如果只是最后一个ngx_chain_t, 需要把next置为NULL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值