Home >Backend Development >PHP Tutorial >Nginx advanced data structure source code analysis (3) ----- linked list
ngx_list_t is a linked list container encapsulated by Nginx and is used frequently. It has two structures, ngx_list_t describes the entire linked list, and ngx_list_part_t only describes one element of the linked list. For ease of understanding, we can call it a linked list of arrays. In other words, ngx_list_t is a linked list container, and the elements in the linked list are an array. In fact, the elements in the ngx_list_part_t array are what the user needs to store.
What are the benefits of such a structural expression:
(1) The elements stored in the linked list are flexible, it can be any kind of data structure;
(2) Linked list The memory that the elements need to occupy is managed by ngx_list_t, which has been allocated through the array;
(3) Using linked lists to access small pieces of memory is inefficient, but using arrays to access memory through offsets is much more efficient.
Definition of ngx_list_t structure:
typedef struct { ngx_list_part_t *last; //指向链表的最后一个数组元素
ngx_list_part_t part; //链表的首个数组元素 size_t size; //每一个用户要存储的一个数据必须小于或等于size ngx_uint_t nalloc; //表示数组元素的个数还是每个数组元素中的元素个数???问徐 ngx_pool_t *pool; //内存池对象 } ngx_list_t;Definition of ngx_list_part_t structure:
struct ngx_list_part_s { void *elts; //指向数组的起始地址 ngx_uint_t nelts; //表示数组中已经用了多少个元素 ngx_list_part_t *next; //下一个链表元素的地址 };Initialization of array linked list:
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; //开始数组元素中只有0个元素 list->part.next = NULL; list->last = &list->part;//指向第一个节点 list->size = size; list->nalloc = n; list->pool = pool;//内存池对象 return NGX_OK; }Create array linked list:
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));//申请ngx_list_t的内存 if (list == NULL) { return NULL; } if (ngx_list_init(list, pool, n, size) != NGX_OK) {//调用初始化函数对ngx_list_t初始化 return NULL; } return list; }Add New elements:
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; }
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the Nginx advanced data structure source code analysis (3) - linked list, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.