ChianXu 发表于 2011-12-22 08:54

nginx之数据结构list结构

<DIV>
<DIV class=cnt id=blog_text>在任何一套成熟的C语言写的服务器或者语言或者应用里都包含有一些基本的数据结构操作,<BR><BR>一般是 array、list、hash、buf等,剖析nginx的list是下面这种结构:<BR><BR>元素本身的长度是size,每个桶可承担n项,那么当你插入第n+1个元素的时候,要创建一个新桶。因此nginx的list内部元素是定长结构。<BR><BR>例如:<BR><BR>mctx-&gt;variables = ngx_list_create(r-&gt;pool, 4,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sizeof(ngx_http_ssi_var_t));<BR><BR>前提是nginx的所有内存操作都建立在pool基础上,其中每4个元素为一个桶,当插入第五个元素的时候新建一个桶。nginx把这种桶成为part。应该就是一个子列表的意思。<BR><BR>下面具体来看和list有关的结构和方法:<BR><BR>typedef struct ngx_list_part_s ngx_list_part_t;<BR><BR>struct ngx_list_part_s {<BR>&nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *elts;<BR>&nbsp;&nbsp;&nbsp; ngx_uint_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nelts;<BR>&nbsp;&nbsp;&nbsp; ngx_list_part_t *next;<BR>};<BR><BR>这是一个桶(子列表),elts是首元素的指针,nelts是元素总个数,next是下一个桶的指针。<BR><BR>typedef struct {<BR>&nbsp;&nbsp;&nbsp; ngx_list_part_t *last;<BR>&nbsp;&nbsp;&nbsp; ngx_list_part_t&nbsp;&nbsp; part;<BR>&nbsp;&nbsp;&nbsp; size_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size;<BR>&nbsp;&nbsp;&nbsp; ngx_uint_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nalloc;<BR>&nbsp;&nbsp;&nbsp; ngx_pool_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *pool;<BR>} ngx_list_t;<BR><BR>这是list的上层结构,last是最后一个桶的指针,part是第一个桶,size是每个元素的大小,nalloc是每个桶里存放多少个元素,当nelts和nalloc相等表明子列表已经到达了桶底。需要分配一个新桶来使用。pool是这个列表所基于的pool地址。<BR><BR>列表一共有三种操作:<BR><BR>ngx_list_create(); //创建及初始化队列<BR>ngx_list_init();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //初始化队列<BR>ngx_list_push(); //找到下一个插入位置的指针并返回<BR><BR>nginx的list遍历和list表的实际插入并不是在这三个函数中完成的,而是散落在外面,可能是作者觉得这样足够灵活。<BR><BR>由于空间是提前分配的,因此ngx_list_push()实际上返回的只是一段定长空间的指针,我们举个例子:<BR><BR>遍历列表<BR><BR>part = &amp;cycle-&gt;open_files.part;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file = part-&gt;elts;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; /* void */ ; i++) {<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &gt;= part-&gt;nelts) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &amp;file;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR><BR>由于存在桶的概念,所以遍历需要一个for循环和一个if语句双重嵌套,以满足当一个桶遍历结束后,遍历下一个桶。<BR><BR>插入一个元素<BR><BR>file = ngx_list_push(&amp;cycle-&gt;open_files);<BR>&nbsp;&nbsp;&nbsp; if (file == NULL) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NULL;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; if (name) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file-&gt;fd = NGX_INVALID_FILE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file-&gt;name = full;<BR><BR>&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file-&gt;fd = ngx_stderr_fileno;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file-&gt;name.len = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file-&gt;name.data = NULL;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; file-&gt;buffer = NULL;<BR><BR>&nbsp;&nbsp;&nbsp; return file;<BR><BR>首先返回file指针,然后对file进行赋值。</DIV><BR>本文摘自:</DIV>
<DIV><a href="http://hi.baidu.com/langwan/blog/item/02eea1ef8698481efdfa3cd0.html" target="_blank">http://hi.baidu.com/langwan/blog/item/02eea1ef8698481efdfa3cd0.html</A></DIV>
<DIV>非常感谢langwan先生给我们的帮助</DIV>
页: [1]
查看完整版本: nginx之数据结构list结构