- /* src/core/ngx_list.h */
- typedef struct ngx_list_part_s ngx_list_part_t;
- struct ngx_list_part_s {
- void * elts; // 数组的首地址 ngx_uint_t nelts; // 数组中已经使用的元素个数 ngx_list_part_t *next;};typedef struct { ngx_list_part_t *last; // 指向最后一个ngx_list_part_t ngx_list_part_t part; // 第一个ngx_list_part_t size_t size; // 每个数组元素占用的字节数 ngx_uint_t nalloc; // 数组中能够容纳的最大元素个数 ngx_pool_t *pool;} ngx_list_t;ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size);// 初始化一个链表static ngx_inline ngx_int_tngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size){ list->part.elts = ngx_palloc(pool, n * size); if (list->part.elts == NULL) { return NGX_ERROR; } list->part.nelts = 0; list->part.next = NULL; list->last = &list->part; list->size = size; list->nalloc = n; list->pool = pool; return NGX_OK;}/* * * the iteration through the list: * * part = &list.part; * data = part->elts; * * for (i = 0 ;; i++) { * * if (i >= part->nelts) { * if (part->next == NULL) { * break; * } * * part = part->next; * data = part->elts; * i = 0; * } * * ... data[i] ... * * } */void *ngx_list_push(ngx_list_t *list);
- /* src/core/ngx_list.c */
- // 创建并初始化一个链表ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size){ ngx_list_t *list; list = ngx_palloc(pool, sizeof(ngx_list_t)); if (list == NULL) { return NULL; } if (ngx_list_init(list, pool, n, size) != NGX_OK) { return NULL; } return list;}// 向链表中添加新元素,返回新元素的地址void *ngx_list_push(ngx_list_t *l){ void *elt; ngx_list_part_t *last; last = l->last; if (last->nelts == l->nalloc) { /* the last part is full, allocate a new list part */ last = ngx_palloc(l->pool, sizeof(ngx_list_part_t)); if (last == NULL) { return NULL; } last->elts = ngx_palloc(l->pool, l->nalloc * l->size); if (last->elts == NULL) { return NULL; } last->nelts = 0; last->next = NULL; l->last->next = last; l->last = last; } elt = (char *) last->elts + l->size * last->nelts; last->nelts++; return elt;}
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: