菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t

  • Author:Echo Chen(陈斌)

  • Email:chenb19870707@gmail.com

  • Blog:Blog.csdn.net/chen19870707

    Date:October 20h, 2014

    1.ngx_array优势和特点

    ngx_array _t是一个顺序容器。支持达到数组容量上限时动态改变数组的大小,类似于STL中vector。具有下面特性:

    • 下标直接索引。訪问速度快
    • 动态增长
    • 由slab内存池统一管理分配出的内存,效率高

    2.源码位置

    头文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.h

    源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.c

    3.数据结构定义

    typedef struct {
        void      *elts;                     //elts指向数组的首地址
        ngx_uint_t   nelts;                  //nelts是数组中已经使用的元素个数
        size_t       size;                   //每一个数组元素占用内存大小
        ngx_uint_t   nalloc;                 //当前数组中能容纳袁术个数的总大小
        ngx_pool_t  *pool;                   //内存池对象
    } ngx_array_t;

    其结构例如以下图所看到的:

    4.动态数组创建ngx_array_create和初始化ngx_array_init

    
    //p为内存池,n为初始分配的元素的个数,size为每一个元素占的内存大小
    
    ngx_array_t *
    ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
    {
        ngx_array_t *a;     //分配动态数组指针
        a = ngx_palloc(p, sizeof(ngx_array_t));
        if (a == NULL) {
            return NULL;
        }     if (ngx_array_init(a, p, n, size) != NGX_OK) {
            return NULL;
        }     return a;
    } static ngx_inline ngx_int_t
    ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
    {
        /*
         * set "array->nelts" before "array->elts", otherwise MSVC thinks
         * that "array->nelts" may be used without having been initialized
         */
       
        //初始化数据
        array->nelts = 0;
        array->size = size;
        array->nalloc = n;
        array->pool = pool;     //分配n个大小为size的内存。elts指向首地址
        array->elts = ngx_palloc(pool, n * size);
        if (array->elts == NULL) {
            return NGX_ERROR;
        }     return NGX_OK;
    }
     

    5.动态数组释放ngx_array_destroy

    void
    ngx_array_destroy(ngx_array_t *a)
    {
        ngx_pool_t  *p;     p = a->pool;     //释放动态数组每一个元素
        if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
            p->d.last -= a->size * a->nalloc;
        }     //释放动态数组首指针
        if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
            p->d.last = (u_char *) a;
        }
    }

    这里用到内存池的释放操作,再后面具体解说,这里把它当作释放就可以。

    6.动态数组的加入元素操作ngx_array_push和ngx_array_push_n

     

    1.ngx_array_push

    //a为要加入到的动态数组的指针
    void * ngx_array_push(ngx_array_t *a)
    {
        void        *elt, *new;
        size_t       size;
        ngx_pool_t  *p;     //使用的和预先分配的个数相等。数组已满
        if (a->nelts == a->nalloc) {                   /* the array is full */         //再分配预分配nalloc个,如今就有2*nalloc个了
            size = a->size * a->nalloc;         p = a->pool;         //假设内存池内存还够。直接从内存池分配,仅仅分配一个
            if ((u_char *) a->elts + size == p->d.last
                && p->d.last + a->size <= p->d.end)
            {
                /*
                 * the array allocation is the last in the pool
                 * and there is space for new allocation
                 */
               
                //内存池尾指针后移一个元素大小。分配内存一个元素,并把nalloc+1
                p->d.last += a->size;
                a->nalloc++;         //假设内存池内存不够了,分配一个新的数组。大小为两倍的nalloc
            } else {
                /* allocate a new array */             //内存分配
                new = ngx_palloc(p, 2 * size);
                if (new == NULL) {
                    return NULL;
                }
               
                //将曾经的数组复制到新数组。并将数组大小设置为曾经二倍
                ngx_memcpy(new, a->elts, size);
                a->elts = new;
                a->nalloc *= 2;
            }
        }     //已分配个数+1 。并返回之
        elt = (u_char *) a->elts + a->size * a->nelts;
        a->nelts++;     return elt;
    }


             能够看到,当内存池有空间时。数组满后仅添加一个元素,当内存池没有未分配空间时,直接分配2*nalloc 个大小。有了内存池,比vector直接2n+1更加有效。

    2.ngx_array_push_n

    //a为要放入的数组,n为要放入的个数
    void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
    {
        void        *elt, *new;
        size_t       size;
        ngx_uint_t   nalloc;
        ngx_pool_t  *p;     size = n * a->size;     //假设数组剩余个数不够分配
        if (a->nelts + n > a->nalloc) {         /* the array is full */         p = a->pool;         //假设内存池中剩余的够分配n个元素,从内存池中分配
            if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
                && p->d.last + size <= p->d.end)
            {
                /*
                 * the array allocation is the last in the pool
                 * and there is space for new allocation
                 */             p->d.last += size;
                a->nalloc += n;
            //假设内存池中剩余的不够分配n个元素
            } else {
                /* allocate a new array */
               
                //当n比数组预分配个数nalloc大时。分配2n个,否则分配2*nalloc个
                nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);             new = ngx_palloc(p, nalloc * a->size);
                if (new == NULL) {
                    return NULL;
                }
               
                //拷贝曾经元素,设置nalloc
                ngx_memcpy(new, a->elts, a->nelts * a->size);
                a->elts = new;
                a->nalloc = nalloc;
            }
        }     //添加已分配个数,并返回
        elt = (u_char *) a->elts + a->size * a->nelts;
        a->nelts += n;     return elt;
    }

    7.实战

    研究开源码的主要意义就在于理解设计的原理和适用的场合,并在合适的场合使用代码,假设单纯的分析代码,但都不能使用。肯定达不到学习的目的。这里就给出ngx_array的測试代码。希望多动手。

       1: typedef struct
       2: {    
       3:     u_char *name;    
       4:     int age;
       5: }Student;
       6:  
       7: ngx_array_t* pArray = ngx_array_create(cf->pool,1,sizeof(Student));
       8:  
       9: Student *pStudent = ngx_array_push(pArray);
      10: pStudent->age = 10;
      11:  
      12: Students *pStudents  = ngx_array_push_n(pArray,3);
      13: pStudents->age = 1;
      14: (pStudents  + 1 )->age =2;
      15: (pStudents  + 2 )->age = 3; 
      16:  
      17: //遍历
      18: Student *pStudent = pArray->elts;
      19: ngx_uint_i = 0;
      20: for(; i < pArray->nelts;i++)
      21: {
      22:     a = pStudent  + i;
      23:     //....
      24: }
     

    -

    Echo Chen:Blog.csdn.net/chen19870707

    -

  • 菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t的更多相关文章

    1. 菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)

      菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...

    2. 菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t

      菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...

    3. 菜鸟nginx源代码剖析数据结构篇(七) 哈希表 ngx_hash_t(下)

        菜鸟nginx源代码剖析数据结构篇(七) 哈希表 ngx_hash_t(下)   Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:B ...

    4. 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t

      菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...

    5. 菜鸟nginx源代码剖析数据结构篇(十) 自旋锁ngx_spinlock

      菜鸟nginx源代码剖析数据结构篇(十) 自旋锁ngx_spinlock Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.cs ...

    6. 菜鸟nginx源代码剖析 框架篇(一) 从main函数看nginx启动流程

      菜鸟nginx源代码剖析 框架篇(一) 从main函数看nginx启动流程 Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...

    7. 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t

      新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

    8. 新秀nginx源代码分析数据结构篇(两) 双链表ngx_queue_t

      nginx源代码分析数据结构篇(两) 双链表ngx_queue_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...

    9. 菜鸟nginx源代码剖析 配置与部署篇(一) 手把手实现nginx &quot;I love you&quot;

      菜鸟nginx源代码剖析 配置与部署篇(一) 手把手配置nginx "I love you" Author:Echo Chen(陈斌) Email:chenb19870707@gm ...

    随机推荐

    1. 如何成为CSDN博客专家

      先看一下官方给出的要求: 申请CSDN博客专家应具备的条件: 1.原创IT类文章总数超过20篇,并且最近一个月内发布了新的原创IT类文章. 2.博客文章总的浏览量超过5万次以上. 3.文章内容的质量很 ...

    2. 【redis】windows

      官方网站:http://www.redis.io 百度百科:http://baike.baidu.com/view/4595959.htm?fr=aladdin windows下安装redis: 下载 ...

    3. 自定义UIViewController与xib文件关系深入分析

      6月14日 上海 OSC 源创会开始报名啦,有很多机械键盘送哦!!! 用xcode模板向工程加入UIViewController sub class的时候,如果选中了with xib for inte ...

    4. ios多视图开发中:xib与UIViewController的关联

      个人感觉ios中的UIViewController和xib文件,分别相当于android的Activity 和Layout文件 当时两者的关联比android稍微复杂些. ios上分别新建的UIVie ...

    5. IDEA 中使用Maven Compile 找不到本地 Jar

      本文地址:http://www.cnblogs.com/duwei/p/4656410.html 在IDEA 的子 Maven Module 中使用 compile 进行编译, 一开始提示从私有远程仓 ...

    6. Redis 突然报错 NOAUTH Authentication required

      查找相关资料,说是添加了密码 只需要在redis的配置文件redis.conf中开启requirepass就可以了,比如我设置我的访问密码是mypassword requirepass mypassw ...

    7. shell的wc命令统计 head tail命令详解

      Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

    8. c++參数传递

      定义: 形參:指出如今Sub 和Function过程形參表中的变量名.数组名,该过程在被调用前.没有为它们分配内存.其作用是说明自变量的类型和形态以及在过程中的作用.形參能够是除定长字符串变量之外的合 ...

    9. HFS - 简单的将个人电脑变服务器!

      网络硬盘   HTTP File Server(HFS)是我目前所知道的最简便的P2P文件分享方式,只一个大小为559KB的单文件绿色软件(hfs.exe)就可以在瞬间不经过任何系统设置将一台普通的联 ...

    10. UVa 10330 Power Transmission / 最大流

      最大流 这题有很多起点和终点 在取2个点(0和n+1) 作为唯一的起点和终点 此外每个点也有容量限制 建图时每条边上的容量为这条边和2个端的容量的最小值 然后EK就行 #include <cst ...