


nginx advanced data structure source code analysis (1) ----- doubly linked list
ng_queue_t is a sequential container provided by Nginx, which organizes data together in a doubly linked list.
The advantage of a linked list as a sequential container is that it can efficiently perform operations such as insertion, deletion, and merging. When moving elements in the linked list, you only need to modify the pointing of the pointer. Therefore, it is very suitable for frequently modifying the container. occasion.
Compared with other sequential containers, it has the following three advantages:
(1) It implements the sorting function and uses insertion sorting, although it is not suitable for sorting very large-scale data. But simple and practical.
(2) It is very lightweight and is not responsible for the allocation of memory occupied by linked list elements. ngx_queue_t just links these allocated memory elements with a doubly linked list
(3) Supports the merging of two linked lists.
When Nginx designed this doubly linked list, since the container and elements share the ngx_queue_t structure, in order to avoid confusion in the meaning of the members of this structure, Nginx encapsulates all methods of the linked list container and elements.
ngx_queue_t header file:
typedef struct ngx_queue_s ngx_queue_t; struct ngx_queue_s { ngx_queue_t *prev; ngx_queue_t *next; }; #define ngx_queue_init(q) \初始化,为空,都指向容器结构体 (q)->prev = q; \ (q)->next = q #define ngx_queue_empty(h) \是否为空 (h == (h)->prev) #define ngx_queue_insert_head(h, x) \插入链表容器头部 (x)->next = (h)->next; \ (x)->next->prev = x; \ (x)->prev = h; \ (h)->next = x #define ngx_queue_insert_after ngx_queue_insert_head #define ngx_queue_insert_tail(h, x) \插入链表容器尾部 (x)->prev = (h)->prev; \ (x)->prev->next = x; \ (x)->next = h; \ (h)->prev = x #define ngx_queue_head(h) \返回第一个结构体指针 (h)->next #define ngx_queue_last(h) \返回最后一个结构体指针 (h)->prev #define ngx_queue_sentinel(h) \返回容器结构体指针 (h) #define ngx_queue_next(q) \返回q的下一个元素 (q)->next #define ngx_queue_prev(q) \返回q的上一个元素 (q)->prev #if (NGX_DEBUG) #define ngx_queue_remove(x) \ (x)->next->prev = (x)->prev; \ (x)->prev->next = (x)->next; \ (x)->prev = NULL; \ (x)->next = NULL #else #define ngx_queue_remove(x) \删除x (x)->next->prev = (x)->prev; \ (x)->prev->next = (x)->next #endif #define ngx_queue_split(h, q, n) \拆分成两个链表 (n)->prev = (h)->prev; \ (n)->prev->next = n; \ (n)->next = q; \ (h)->prev = (q)->prev; \ (h)->prev->next = h; \ (q)->prev = n; #define ngx_queue_add(h, n) \合并链表 (h)->prev->next = (n)->next; \ (n)->next->prev = (h)->prev; \ (h)->prev = (n)->prev; \ (h)->prev->next = h; #define ngx_queue_data(q, type, link) \返回q所属结构体地址 (type *) ((u_char *) q - offsetof(type, link))
n There are only two methods in the implementation file of gx_queue_t: one is to return the The central element and one is to sort the linked list.
ngx_queue_t * ngx_queue_middle(ngx_queue_t *queue) //返回链表中心元素 { ngx_queue_t *middle, *next; middle = ngx_queue_head(queue); if (middle == ngx_queue_last(queue)) { //特殊情况也要单独判断,如果只有一个,则返回这个元素 return middle; } next = ngx_queue_head(queue); for ( ;; ) { middle = ngx_queue_next(middle); //一个指针走一步,另外一个指针走两步 next = ngx_queue_next(next); if (next == ngx_queue_last(queue)) { //next每走一步都要判断一下的 return middle; } next = ngx_queue_next(next); if (next == ngx_queue_last(queue)) { //这也要判断,考虑真全面啊 return middle; } } } /* the stable insertion sort */ void ngx_queue_sort(ngx_queue_t *queue, ngx_int_t (*cmp)(const ngx_queue_t *, const ngx_queue_t *)) //用插入法排序 { ngx_queue_t *q, *prev, *next; q = ngx_queue_head(queue); if (q == ngx_queue_last(queue)) { //只有一个节点就不用排了 return; } for (q = ngx_queue_next(q); q != ngx_queue_sentinel(queue); q = next) { prev = ngx_queue_prev(q); //分别保存q的前后节点 next = ngx_queue_next(q); ngx_queue_remove(q); do { if (cmp(prev, q) <= 0) { //若prev小于,这个循环结束 break; } prev = ngx_queue_prev(prev);//否则跟前面一个元素比 } while (prev != ngx_queue_sentinel(queue));//这个为结束条件,prev为链表结构体结束 ngx_queue_insert_after(prev, q);//插入q } }
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces nginx advanced data structure source code analysis (1) ----- doubly linked list, including aspects of 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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

As applications become more complex, handling and managing large amounts of data and processes is a challenge. In order to handle this situation, Laravel provides users with a very powerful tool, the Laravel Queue (Queue). It allows developers to run tasks like sending emails, generating PDFs, handling image cropping, etc. in the background without any impact on the user interface. In this article, we will take a deep dive into how to use Laravel queues. What is LaravelQueue queue

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Security issues and solutions for JavaQueue queues in multi-threaded environments Introduction: In multi-threaded programming, shared resources in the program may face race conditions, which may lead to data inconsistency or errors. In Java, Queue is a commonly used data structure. When multiple threads operate the queue at the same time, there are security issues. This article will discuss the security issues of JavaQueue queues in multi-threaded environments, and introduce several solutions, focusing on explanations in the form of code examples. one

Usage of Queue in Java In Java, Queue (queue) is a commonly used data structure that follows the first-in, first-out (FIFO) principle. Queue can be used to implement message queues, task scheduling and other scenarios, and can well manage the arrangement and processing order of data. This article will introduce the usage of Queue and provide specific code examples. The definition and common methods of Queue are in Java. Queue is an interface in JavaCollectionsFramework

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.
