Nginx源码阅读(ngx_list_t)
2017-01-21 21:06
363 查看
/* 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_t ngx_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; }
相关文章推荐
- nginx源码分析—链表结构ngx_list_t
- nginx源码分析—链表结构ngx_list_t
- NGINX----源码阅读---ngx_start_worker_processes(worker进程启动方法)
- [置顶] nginx源码阅读(八).ngx_events_module模块
- 文章3:Nginx源码分析-ngx_list_t单链表
- Nginx源码阅读(ngx_array_t)
- nginx源码阅读笔记.array和list数据结构
- Nginx源码阅读(ngx_worker_process_cycle)
- Nginx源码完全注释(3)ngx_list.h / ngx_list.c
- nginx源码分析之ngx_list_t
- nginx 源码学习笔记(十一)——基本容器——ngx_list
- Nginx源码阅读(ngx_master_process_cycle)
- nginx 源码学习笔记(十一)——基本容器——ngx_list
- 菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t
- Nginx源码分析---链表结构ngx_list_t
- nginx源码初读(6)--让烦恼从数据结构开始(ngx_list)
- [置顶] nginx源码阅读(十).ngx_event_core_module模块
- [置顶] nginx源码阅读(二).初始化:main函数及ngx_init_cycle函数
- Nginx源码阅读(ngx_cycle_t)
- nginx源码分析—链表结构ngx_list_t