Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65186
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 160
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-04 17:12
个人简介

It

文章分类

全部博文(15)

文章存档

2015年(13)

2014年(2)

我的朋友

分类: LINUX

2015-06-02 11:20:55

nginx的链表ngx_list_t很有特色,其特别之处是链表的链表的元素分层次(权且这么理解),即链表是由部分(ngx_list_part_t)组成,每个部分使用指针链接,每个部分可以看作一个数组,该数组的元素是我们平常链表的元素。即ngx_list_t -> ngx_list_part_t -> element

ngx_list.h 代码

点击(此处)折叠或打开

  1. typedef struct ngx_list_part_s ngx_list_part_t;

  2. struct ngx_list_part_s {
  3.     // 该部分的元素首地址
  4.     void *elts;
  5.     // 该部分已有的元素个数
  6.     ngx_uint_t nelts;
  7.     // 指向下个的指针
  8.     ngx_list_part_t *next;
  9. };


  10. typedef struct {
  11.     // 链表最后一个部分的指针
  12.     ngx_list_part_t *last;
  13.     // 链表的首个部分
  14.     ngx_list_part_t part;
  15.     // 链表中part数组中的元素大小
  16.     size_t size;
  17.     // 链表part数组的容量
  18.     ngx_uint_t nalloc;
  19.     // 内存池
  20.     ngx_pool_t *pool;
  21. } ngx_list_t;

  22. // 在内存池pool分配 ngx_list_t 对象,并初始化
  23. ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size);

  24. static ngx_inline ngx_int_t
  25. ngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size)
  26. {
  27.     list->part.elts = ngx_palloc(pool, n * size);
  28.     if (list->part.elts == NULL) {
  29.         return NGX_ERROR;
  30.     }

  31.     list->part.nelts = 0;
  32.     list->part.next = NULL;
  33.     list->last = &list->part;
  34.     list->size = size;
  35.     list->nalloc = n;
  36.     list->pool = pool;

  37.     return NGX_OK;
  38. }

  39. // 遍历链表的基本代码结构
  40. /*
  41.  *
  42.  * the iteration through the list:
  43.  *
  44.  * part = &list.part;
  45.  * data = part->elts;
  46.  *
  47.  * for (i = 0 ;; i++) {
  48.  *
  49.  * if (i >= part->nelts) {
  50.  * if (part->next == NULL) {
  51.  * break;
  52.  * }
  53.  *
  54.  * part = part->next;
  55.  * data = part->elts;
  56.  * i = 0;
  57.  * }
  58.  *
  59.  * ... data[i] ...
  60.  *
  61.  * }
  62.  */

  63. // 从链表尾部获取一个空元素
  64. void *ngx_list_push(ngx_list_t *list);


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