Nginx常用数据结构(二)

来源:互联网 发布:阿里云华南机房 编辑:程序博客网 时间:2024/05/29 09:41
nginx中有很多结构体,一般命名为ngx_xxx_t。分散在许多头文件中。src/core/ngx_core.h中把几乎所有的头文件都集合起来。也因此造成了nginx各部分源代码的耦合。但实际上nginx各个部分逻辑划分还是很明确的,整体上是一种松散的结构。

1、ngx_str_t

typedef struct{    size_t len;    u_char *data;}ngx_str_t;
这是nginx对字符串的实现,源码在ngx_string.h中。len指的是字符串的长度(不包括\0),data指向字符串。这种设计一方面,在计算字符创长度时只需要读取len字段即可,另一方面可以重复引用一段字符串内存。
常用api
#define ngx_string(str) { sizof(str) - 1},(u_char *) str } //从一个普通字符串构造出一个nginx字符串,用sizeof计算长度,故参数必须是一个常量字符串。#define ngx_null_string {0,NULL}ngx_strncmp(s1,s2,n)ngx_strcm(s1,s2)

2、ngx_pool_t

struct ngx_pool_s {    ngx_pool_data_t       d;    size_t                max;    ngx_pool_t           *current;    ngx_chain_t          *chain;    ngx_pool_large_t     *large;    ngx_pool_cleanup_t   *cleanup;    ngx_log_t            *log;};
用来管理一系列的资源(如内存、文件等 ,使得对这些资源的使用和释放统一进行。

3、ngx_array_t

struct ngx_array_s {    void        *elts; //指向实际的存储区域    ngx_uint_t   nelts; //数组实际元素个数    size_t       size; //数组单个元素的大小,单位是字节    ngx_uint_t   nalloc; //数组的容量    ngx_pool_t  *pool; //该数组用来分配内存的内存池};

4、ngx_hash_t

//ngx_hash_t不像其他的hash表的实现,可以插入删除元素,只能一次初始化。//解决冲突使用的是开链法,但实际上是开了一段连续的存储空间,和数组差不多。 ngx_int_t ngx_hash_init(ngx_hash_init_t hinit, ngx_hash_key_t names,ngx_uint_t nelts);//ngx_hash_t的初始化。//ngx_hash_init_t提供了初始化一个hash表所需要的一些基本信息typedef struct {    ngx_hash_t       *hash; //指向hash表    ngx_hash_key_pt   key; //指向从字符串生成hash值的hash函数。默认的实现为ngx_hash_key_lc    ngx_uint_t        max_size; //hash表中的桶的个数    ngx_uint_t        bucket_size; //每个桶的最大限制大小,单位是字节    char             *name; //hash表的名字    ngx_pool_t       *pool; //hash表分配内存使用的pool    ngx_pool_t       *temp_pool; //使用的临时pool,初始化完成后,可以释放和销毁} ngx_hash_init_t;typedef struct {    ngx_str_t         key;    ngx_uint_t        key_hash;    void             *value;}ngx_hash_key_t;void *ngx_hash_find(ngx_hash_t *hash, ngx_uint_t key, u_char *name, size_t len); //在hash里面查找key对应的value。 

5、ngx_buf_t(core/ngx_buf.h)

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;         /* 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;    /*     * the buf's content is in a memory cache or in a read only memory     * and must not be changed     */    unsigned         memory:1;    /* the buf's content is mmap()ed and must not be changed */    unsigned         mmap:1;    unsigned         recycled: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;    /* STUB */ int   num;};

6、ngx_chain_t

 struct ngx_chain_s {     ngx_buf_t    *buf;     ngx_chain_t  *next; }; ngx_chain_t *ngx_alloc_chain_link(ngx_pool_t *pool);//创建ngx_chain_t对象
nginx的filter模块在处理从别的filter模块或者是handler模块传递过来的数据,数据一个链表的形式(ngx_chain_t)进行传递。

7、ngx_list_t

typedef struct {    ngx_list_part_t  *last; //指向该链表的最后一个节点    ngx_list_part_t   part; //指向该链表首个存放具体元素的节点    size_t            size; //链表中存放的具体元素所需内存大小    ngx_uint_t        nalloc; //每个节点所含的固定大小的数组的容量    ngx_pool_t       *pool; //该list使用的分配内存的pool} ngx_list_t;struct ngx_list_part_s {    void             *elts; //节点中存放具体元素的内存的开始地址       ngx_uint_t        nelts; //节点中已有元素个数,不能大于 nalloc    ngx_list_part_t  *next; //指向下一个节点};ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size); //创建一个ngx_list_t类型的对象,并对该list的第一个节点分配存放元素的内存空间。pool:   分配内存使用的pool。n:  每个节点固定长度的数组的长度。size:   存放的具体元素的个数。
和普通的链表实现相比,它的节点是一个固定大小的数组。在初始化的时候,我们需要设定元素需要占用的空间大小,每个节点数组的容量大小。在添加元素到这个list里面的时候,会在最尾部的节点里的数组上添加元素,如果这个节点的数组存满了,就再增加一个新的节点到这个list里面去。

8、ngx_queue_t

struct ngx_queue_s {    ngx_queue_t  *prev;    ngx_queue_t  *next;};
链表节点的数据成员并没有声明在链表节点的结构体中,只是声明了前向和后向指针。使用的时候需要定义一个哨兵节点。具体存放数据的节点称之为数据节点。对于数据节点,需要在数据结构体中加入一个类型为ngx_queue_t的域。使用下面的函数进行数据插入,其中x为数据节点的ngx_queue_t域。
#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//获得数据时,使用ngx_queue_data()宏。其中type是数据节点类型,link是数据节点中ngx_queue_t的域名字。#define ngx_queue_data(q, type, link)                        \        (type *) ((u_char *) q - offsetof(type, link))

9、ngx_command_t(core/ngx_core.h)

typedef struct ngx_command_s ngx_command_t;struct ngx_command_s {    ngx_str_t             name;    ngx_uint_t            type;    char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);    ngx_uint_t            conf;    ngx_uint_t            offset;    void                 *post;};

10、ngx_module_t(core/ngx_core.h)

typedef struct ngx_module_s ngx_module_t;struct ngx_module_s {    ngx_uint_t            ctx_index;    ngx_uint_t            index;    char                 *name;    ngx_uint_t            spare0;    ngx_uint_t            spare1;    ngx_uint_t            version;    const char           *signature;    void                 *ctx;    ngx_command_t        *commands;    ngx_uint_t            type;    ngx_int_t           (*init_master)(ngx_log_t *log);    ngx_int_t           (*init_module)(ngx_cycle_t *cycle);    ngx_int_t           (*init_process)(ngx_cycle_t *cycle);    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);    void                (*exit_thread)(ngx_cycle_t *cycle);    void                (*exit_process)(ngx_cycle_t *cycle);    void                (*exit_master)(ngx_cycle_t *cycle);    uintptr_t             spare_hook0;    uintptr_t             spare_hook1;    uintptr_t             spare_hook2;    uintptr_t             spare_hook3;    uintptr_t             spare_hook4;    uintptr_t             spare_hook5;    uintptr_t             spare_hook6;    uintptr_t             spare_hook7;};

11、ngx_conf_t(core/ngx_core.h)

typedef struct ngx_conf_s  ngx_conf_t;struct ngx_conf_s {    char                 *name;    ngx_array_t          *args;    ngx_cycle_t          *cycle;    ngx_pool_t           *pool;    ngx_pool_t           *temp_pool;    ngx_conf_file_t      *conf_file;    ngx_log_t            *log;    void                 *ctx;    ngx_uint_t            module_type;    ngx_uint_t            cmd_type;    ngx_conf_handler_pt   handler;    char                 *handler_conf;};

12、ngx_cycle_t(core/ngx_core.h)

typedef struct ngx_cycle_s ngx_cycle_t;struct ngx_cycle_s {    void                  ****conf_ctx;    ngx_pool_t               *pool;    ngx_log_t                *log;    ngx_log_t                 new_log;    ngx_uint_t                log_use_stderr;  /* unsigned  log_use_stderr:1; */    ngx_connection_t        **files;    ngx_connection_t         *free_connections;    ngx_uint_t                free_connection_n;    ngx_module_t            **modules;    ngx_uint_t                modules_n;    ngx_uint_t                modules_used;    /* unsigned  modules_used:1; */    ngx_queue_t               reusable_connections_queue;    ngx_uint_t                reusable_connections_n;    ngx_array_t               listening;    ngx_array_t               paths;    ngx_array_t               config_dump;    ngx_rbtree_t              config_dump_rbtree;    ngx_rbtree_node_t         config_dump_sentinel;    ngx_list_t                open_files;    ngx_list_t                shared_memory;    ngx_uint_t                connection_n;    ngx_uint_t                files_n;    ngx_connection_t         *connections;    ngx_event_t              *read_events;    ngx_event_t              *write_events;    ngx_cycle_t              *old_cycle;    ngx_str_t                 conf_file;    ngx_str_t                 conf_param;    ngx_str_t                 conf_prefix;    ngx_str_t                 prefix;    ngx_str_t                 lock_file;    ngx_str_t                 hostname;};
原创粉丝点击