nginx是一门很强大的工具,我们可以将里面的代码提取出来,以便可以在实际工程中使用。想直接下载本博客所有源码以及CMake的,可以点击https://download.csdn.net/download/wfh2015/13944429
nginx的队列实际上是采用了双向循环链表实现的,可以直接当做链表来使用,nginx内部实现了以下功能:
- 链表初始化
- 元素删除
- 元素的值获取
- 链表排序
- 链表以某个节点作为分割
- 链表的中间一个元素获取
如果Linux内核的链表不熟悉的话,需要着重阅读元素的值获取;
// ngx_queue.h
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_QUEUE_H_INCLUDED_
#define _NGX_QUEUE_H_INCLUDED_
typedef int ngx_int_t;
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)->next
#define ngx_queue_prev(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)->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) \
(type *) ((u_char *) q - offsetof(type, link))
ngx_queue_t *ngx_queue_middle(ngx_queue_t *queue);
void ngx_queue_sort(ngx_queue_t *queue,
ngx_int_t (*cmp)(const ngx_queue_t *, const ngx_queue_t *));
#endif /* _NGX_QUEUE_H_INCLUDED_ */
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include "ngx_queue.h"
/*
* find the middle queue element if the queue has odd number of elements
* or the first element of the queue's second part otherwise
*/
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)) {
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);
next = ngx_queue_next(q);
ngx_queue_remove(q);
do {
if (cmp(prev, q) <= 0) {
break;
}
prev = ngx_queue_prev(prev);
} while (prev != ngx_queue_sentinel(queue));
ngx_queue_insert_after(prev, q);
}
}
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "ngx_queue.h"
typedef struct tagNGX_QueData
{
int lData;
ngx_queue_t stLoc;
}NGX_QUEUE_DATA_S;
ngx_int_t CmpFunc(const ngx_queue_t *pstNode1, const ngx_queue_t *pstNode2)
{
NGX_QUEUE_DATA_S *pstData1 = NULL;
NGX_QUEUE_DATA_S *pstData2 = NULL;
pstData1 = ngx_queue_data(pstNode1, NGX_QUEUE_DATA_S, stLoc);
pstData2 = ngx_queue_data(pstNode2, NGX_QUEUE_DATA_S, stLoc);
if (pstData1->lData > pstData2->lData)
{
return 1;
}
return 0;
}
void print_que(char *pcPrompt, ngx_queue_t *pstQue)
{
ngx_queue_t *pstNode = NULL;
NGX_QUEUE_DATA_S *pstData = NULL;
printf("====================start: %s.============ .\n", pcPrompt);
for (pstNode = pstQue->next; pstNode != pstQue; pstNode = pstNode->next)
{
pstData = ngx_queue_data(pstNode, NGX_QUEUE_DATA_S, stLoc);
printf("%d.\t", pstData->lData);
}
printf("\n====================End: %s.============ .\n", pcPrompt);
}
void InsertHeadDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = i + 1;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_head(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"Insert Head", &stQue);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
void InsertTailDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = i + 1;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_tail(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"Insert Tail", &stQue);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
void SortDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = (i + 1) % 4;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_tail(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"Before sort", &stQue);
ngx_queue_sort(&stQue, CmpFunc);
print_que((char*)"After sort", &stQue);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
void MiddleDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = i + 1;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_tail(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"MiddleDemo-Source", &stQue);
ngx_queue_t *pstNode = ngx_queue_middle(&stQue);
NGX_QUEUE_DATA_S *pstData = ngx_queue_data(pstNode, NGX_QUEUE_DATA_S, stLoc);
int lData = pstData->lData;
printf("Middledemo-Data: %d.\n", lData);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
void SplitDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = i;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_tail(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"SplitDemo-Full", &stQue);
ngx_queue_t stQue2 = {0};
ngx_queue_split(&stQue, &apstData[3]->stLoc, &stQue2); // stQue2包括分割的节点
print_que((char*)"SplitDemo-Que1", &stQue);
print_que((char*)"SplitDemo-Que2", &stQue2);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
void RemoveDemo()
{
NGX_QUEUE_DATA_S* apstData[10] = {0};
for (int i = 0; i < 10; ++i)
{
apstData[i] = (NGX_QUEUE_DATA_S *)malloc(sizeof(NGX_QUEUE_DATA_S));
if (NULL == apstData[i])
{
continue;
}
memset(apstData[i], 0, sizeof(NGX_QUEUE_DATA_S));
apstData[i]->lData = i;
}
ngx_queue_t stQue = {0};
ngx_queue_init(&stQue);
for (int i = 0; i < 10; ++i)
{
ngx_queue_insert_tail(&stQue, &(apstData[i]->stLoc));
}
print_que((char*)"RemoveDemo-Full", &stQue);
ngx_queue_remove(&apstData[3]->stLoc);
print_que((char*)"RemoveDemo-Del", &stQue);
for (int i = 0; i < 10; ++i)
{
if (NULL == apstData[i])
{
continue;
}
free(apstData[i]);
}
}
int main()
{
InsertHeadDemo();
InsertTailDemo();
SortDemo();
MiddleDemo();
SplitDemo();
RemoveDemo();
return 0;
}
#ifdef __cplusplus
}
#endif
运行结果如下:
fh@192 bin % ./queue
====================start: Insert Head.============ .
10. 9. 8. 7. 6. 5. 4. 3. 2. 1.
====================End: Insert Head.============ .
====================start: Insert Tail.============ .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
====================End: Insert Tail.============ .
====================start: Before sort.============ .
1. 2. 3. 0. 1. 2. 3. 0. 1. 2.
====================End: Before sort.============ .
====================start: After sort.============ .
0. 0. 1. 1. 1. 2. 2. 2. 3. 3.
====================End: After sort.============ .
====================start: MiddleDemo-Source.============ .
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
====================End: MiddleDemo-Source.============ .
Middledemo-Data: 6.
====================start: SplitDemo-Full.============ .
0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
====================End: SplitDemo-Full.============ .
====================start: SplitDemo-Que1.============ .
0. 1. 2.
====================End: SplitDemo-Que1.============ .
====================start: SplitDemo-Que2.============ .
3. 4. 5. 6. 7. 8. 9.
====================End: SplitDemo-Que2.============ .
====================start: RemoveDemo-Full.============ .
0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
====================End: RemoveDemo-Full.============ .
====================start: RemoveDemo-Del.============ .
0. 1. 2. 4. 5. 6. 7. 8. 9.
====================End: RemoveDemo-Del.============ .