


First reading of nginx source code (5) - Let the troubles start from main ngx_array
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Title: Use the size() method of the TreeSet class to obtain the number of elements in the tree collection. Introduction TreeSet is an ordered collection in the Java collection framework. It implements the SortedSet interface and uses the red-black tree data structure to implement it. TreeSet can be sorted according to the natural order of elements, or by using a Comparator custom comparator. This article will introduce how to use the size() method of the TreeSet class to obtain the number of elements in the tree collection and provide

Java is a very powerful programming language that is widely used in various development fields. However, during Java programming, developers often encounter ArrayIndexOutOfBoundsException exceptions. So, what are the common causes of this anomaly? ArrayIndexOutOfBoundsException is a common runtime exception in Java. It means that when accessing data, the array subscript exceeds the range of the array. Common reasons include

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke
