nginx中ngx_list的数据结构

时间:2023-03-09 23:18:28
nginx中ngx_list的数据结构

今天没事了,在查看nginx源代码中看到ngx_list的结构,发现设计为链表数组的形式,不知道为什么这样设计

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 part;//链表的首个数组元素
size_t size;//限制每一个数组元素的占用的空间大小
ngx_uint_t nalloc; //链表中数组元素一旦分配之后是不可更改的,nalloc表示每个ngx_list_part_t数组的容量,即最多可存储多少个数据
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);私有方法 在ngx_list_create中调用

void *ngx_list_push(ngx_list_t *list);