Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10949
  • 博文数量: 4
  • 博客积分: 146
  • 博客等级: 入伍新兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-24 10:22
文章分类

全部博文(4)

文章存档

2011年(3)

2010年(1)

我的朋友
最近访客

分类: 系统运维

2011-02-05 15:58:17

  ngx_array是ngx封装的数组容器 在src/core/ngx_array.{c,h}中
  1. struct ngx_array_s {
  2.     void *elts;//指向存储数组元素的内存
  3.     ngx_uint_t nelts;//当前数组中元素个数
  4.     size_t size;//每个数据元素的大小
  5.     ngx_uint_t nalloc;//数组大小
  6.     ngx_pool_t *pool;//内存池
  7. };
  ngx_array_create建立函数 在src/core/ngx_array.c中
  1. ngx_array_t *
  2. ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
  3. {
  4.     ngx_array_t *a;

  5.     a = ngx_palloc(p, sizeof(ngx_array_t));//分配数组管理结构
  6.     if (a == NULL) {
  7.         return NULL;
  8.     }

  9.     a->elts = ngx_palloc(p, n * size);//分配存储数据元素的内存
  10.     if (a->elts == NULL) {
  11.         return NULL;
  12.     }
  13. //初始化其他结构
  14.     a->nelts = 0;
  15.     a->size = size;
  16.     a->nalloc = n;
  17.     a->pool = p;

  18.     return a;
  19. }
  ngx_array_push   在src/core/ngx_array.c中
  1. void *
  2. ngx_array_push(ngx_array_t *a)
  3. {
  4.     void *elt, *new;
  5.     size_t size;
  6.     ngx_pool_t *p;

  7.     if (a->nelts == a->nalloc) {//数组满了

  8.         /* the array is full */

  9.         size = a->size * a->nalloc;

  10.         p = a->pool;

  11.         if ((u_char *) a->elts + size == p->d.last
  12.             && p->d.last + a->size <= p->d.end)
  13.         {//如果数组在内存池的最后 并且内存池还有空间存储一个数据元素
  14.         //则直接使用
  15.             /*
  16.              * the array allocation is the last in the pool
  17.              * and there is space for new allocation
  18.              */

  19.             p->d.last += a->size;
  20.             a->nalloc++;

  21.         } else {
  22.             /* allocate a new array */

  23.             new = ngx_palloc(p, 2 * size);//分配一段新内存
  24.             if (new == NULL) {
  25.                 return NULL;
  26.             }

  27.             ngx_memcpy(new, a->elts, size);//拷贝原数组
  28.             a->elts = new;
  29.             a->nalloc *= 2;
  30.         }
  31.     }

  32.     elt = (u_char *) a->elts + a->size * a->nelts;
  33.     a->nelts++;
  34. //返回元素指针
  35.     return elt;
  36. }
  ngx_array_push_n   在src/core/ngx_array.c中 这个函数的代码与ngx_array_push十分类似
  1. void *
  2. ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
  3. {
  4.     void *elt, *new;
  5.     size_t size;
  6.     ngx_uint_t nalloc;
  7.     ngx_pool_t *p;

  8.     size = n * a->size;

  9.     if (a->nelts + n > a->nalloc) {

  10.         /* the array is full */

  11.         p = a->pool;

  12.         if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
  13.             && p->d.last + size <= p->d.end)
  14.         {
  15.             /*
  16.              * the array allocation is the last in the pool
  17.              * and there is space for new allocation
  18.              */

  19.             p->d.last += size;
  20.             a->nalloc += n;

  21.         } else {
  22.             /* allocate a new array */

  23.             nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);

  24.             new = ngx_palloc(p, nalloc * a->size);
  25.             if (new == NULL) {
  26.                 return NULL;
  27.             }

  28.             ngx_memcpy(new, a->elts, a->nelts * a->size);
  29.             a->elts = new;
  30.             a->nalloc = nalloc;
  31.         }
  32.     }

  33.     elt = (u_char *) a->elts + a->size * a->nelts;
  34.     a->nelts += n;

  35.     return elt;
  36. }
  ngx_array_push(a) 可以被当做ngx_array_push_n(a,1)
 
  销毁数组函数 
  1. void
  2. ngx_array_destroy(ngx_array_t *a)
  3. {
  4.     ngx_pool_t *p;

  5.     p = a->pool;

  6.     if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {//销毁数组
  7.         p->d.last -= a->size * a->nalloc;
  8.     }

  9.     if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {//销毁数组控制结构
  10.         p->d.last = (u_char *) a;
  11.     }
  12. }

阅读(600) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~