Nginx two-way linked list ngx_queue_t

Source: Internet
Author: User
: This article mainly introduces nginx two-way linked list ngx_queue_t. For more information about PHP tutorials, see. I. INTRODUCTION

Ngx_queue_t is a two-way linked list implemented in nginx. you can embed ngx_queue_t into the user-defined struct that requires a two-way linked list. Another feature is that ngx_queue_t does not involve memory allocation.

II. example

The source code below is to integrate the source code about ngx_queue_t in chapter 7th of nginx. For more information about Makefile, see http://blog.csdn.net/livelylittlefish/article/details/6586946.

# Include

# Include "ngx_config.h"

# Include "ngx_conf_file.h"

# Include "nginx. h"

# Include "ngx_core.h"

# Include "ngx_string.h"

# Include "ngx_palloc.h"

# Include "ngx_queue.h"

Volatile ngx_cycle_t * ngx_cycle;

Void ngx_log_error_core (ngx_uint_t level, ngx_log_t * log, ngx_err_t err,

Const char * fmt ,...)

{

}

Typedef struct {

U_char * str;

Ngx_queue_tqEle;

Intnum;

} TestNode;

Ngx_int_t compTestNode (const ngx_queue_t * a, const ngx_queue_t * B)

{

TestNode * aNode = ngx_queue_data (a, TestNode, qEle );

TestNode * bNode = ngx_queue_data (B, TestNode, qEle );

ReturnaNode-> num> bNode-> num;

}

Int main ()

{

Ngx_queue_tqueueContainer;

Ngx_queue_init (& queueContainer );

Inti = 0;

TestNodenode [5];

For (; I <5; ++ I ){

Node [I]. num = I;

}

Ngx_queue_insert_tail (& queueContainer, & node [0]. qEle );

Ngx_queue_insert_head (& queueContainer, & node [1]. qEle );

Ngx_queue_insert_tail (& queueContainer, & node [2]. qEle );

Ngx_queue_insert_after (& queueContainer, & node [3]. qEle );

Ngx_queue_insert_tail (& queueContainer, & node [4]. qEle );

Ngx_queue_t * q;

For (q = ngx_queue_head (& queueContainer );

Q! = Ngx_queue_sentinel (& queueContainer );

Q = ngx_queue_next (q )){

TestNode * eleNode = ngx_queue_data (q, TestNode, qEle );

Printf ("% d \ n", eleNode-> num );

}

Printf ("****** sort ****** \ n ");

Ngx_queue_sort (& queueContainer, compTestNode );

For (q = ngx_queue_head (& queueContainer );

Q! = Ngx_queue_sentinel (& queueContainer );

Q = ngx_queue_next (q )){

TestNode * eleNode = ngx_queue_data (q, TestNode, qEle );

Printf ("% d \ n", eleNode-> num );

}

Return0;

}

Makefile

CXX = gcc

CXXFLAGS + =-g-Wall-Wextra

Nginx root =/usr/src/nginx-1.0.4

TARGETS = test_queue

TARGETS_C_FILE = $ (TARGETS). c

CLEANUP = rm-f $ (TARGETS) *. o

All: $ (TARGETS)

Clean:

$ (CLEANUP)

CORE_INCS =-I .\

-I $ (NGX_ROOT)/src/core \

-I $ (NGX_ROOT)/src/event \

-I $ (NGX_ROOT)/src/event/modules \

-I $ (NGX_ROOT)/src/OS/unix \

-I $ (NGX_ROOT)/objs \

NGX_PALLOC = $ (NGX_ROOT)/objs/src/core/ngx_palloc.o

NGX_STRING = $ (NGX_ROOT)/objs/src/core/ngx_string.o

NGX_ALLOC = $ (NGX_ROOT)/objs/src/OS/unix/ngx_alloc.o

NGX_QUEUE = $ (NGX_ROOT)/objs/src/core/ngx_queue.o

$ (TARGETS): $ (TARGETS_C_FILE)

$ (CXX) $ (CXXFLAGS) $ (CORE_INCS) $ (NGX_PALLOC) $ (NGX_STRING) $ (NGX_ALLOC) $ (NGX_QUEUE) $ ^-o $ @

III. Summary

1. a linked list is identified by the linked list header and does not contain user-defined data.

The above introduces nginx two-way linked list ngx_queue_t, including some content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.