Structure definition of array:
<code><span>typedef</span><span>struct</span> ngx_array_s ngx_array_t; <span>struct</span> ngx_array_s { <span>void</span> *elts; <span>// 指向数组存储位置的首地址</span> ngx_uint_t nelts; <span>// 当前数组中已经存放的元素个数</span> size_t size; <span>// 数组中每个元素的大小</span> ngx_uint_t nalloc; <span>// 当前最多能容纳的元素个数,类似cpp中的Vector,当nelts大于nalloc时扩容</span> ngx_pool_t *pool; <span>// 该数组对应的内存池</span> };</code>
The array operation function is introduced below:
<code>ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size); <span>/* 从pool中申请array结构体内存,并调用init初始化(申请n*size内存,改变array内的属性), * 所以元素与结构体内存可能并不连续,但肯定在同一个pool里,失败返回NULL */</span><span>void</span> ngx_array_destroy(ngx_array_t *a); <span>/* 依次销毁数组的数据区和结构体内存,将内存返还给pool(last-=) * if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { p->d.last = (u_char *) a; } * 销毁结构体的代码如上,因为这代码看起来很奇怪,它怎么知道数组肯定再pool的最后,没加过其他东西了? 看了源码数组也并不是通过单独的pool来管理的,也就是说pool中还可能有很多其他的数据。 在nginx整个代码中没有找到对ngx_array_destroy的引用 */</span><span>void</span> *ngx_array_push(ngx_array_t *a); <span>void</span> *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n); <span>/* 在数组a上新追加元素,并返回指向新元素的指针。需要把返回的指针转换为具体类型, 然后再给新元素本身或者是各字段(如果数组的元素是复杂类型)赋值。*/</span><span>static</span> ngx_inline ngx_int_t ngx_array_init(ngx_array_t *<span>array</span>, ngx_pool_t *pool, ngx_uint_t n, size_t size) <span>/* 如果一个数组对象是被分配在堆上的,那么当调用ngx_array_destroy销毁以后,如果想再次使用,就可以调用此函数。 如果一个数组对象是被分配在栈上的,那么就需要调用此函数,进行初始化的工作以后,才可以使用。*/</span></code>
Have you noticed a serious problem from the above code? Whether it is destroy or expansion, the original memory location in the source code is not free. Yes, this will definitely cause a waste of memory. Why does the nginx author care about memory so much that there is such a problem? I really don’t understand it. I think it is easy to solve. But there is definitely a reason. When we use it, it is best to plan the size of the array in advance to avoid the waste caused by multiple expansions.
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces the first reading of nginx source code (5) - let the trouble start from main ngx_array, including the content, I hope it will be helpful to friends who are interested in PHP tutorials.