ngx_queue的理解

这儿http://code.google.com/p/nginxsrp/wiki/NginxCodeReview,针对nginx的一些细节进行了讲解,看了受益良多.看到ngx_queue的时候,发现其实是一个简单的双向链表,居然看来看去理不清这个链表是怎么运作的,只好画图,然后发现,原来这个双向列表的prev实际表示的是next,这样一来理解起来就容易多了.

以下的代码为转载:

下面是一个queue操作的例子

#include<stdio.h> 
#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,constchar*fmt,...){} 
// 用雅虎的成员列表作为一个简单的例子 
typedef struct yahoo_s 
{     
    ngx_queue_t   queue; 
} yahoo_t; 
typedef struct yahoo_guy_s 
{     
    ngx_uint_t    id;  
    u_char*       name;  
    ngx_queue_t   queue;
} yahoo_guy_t;  

// 排序使用的比较函数, 按照id的大小排序,id大放到到前面 
ngx_int_t yahoo_no_cmp(const ngx_queue_t* p, const ngx_queue_t* n) 
{     
    yahoo_guy_t *pre, *next;     
    pre  = (yahoo_guy_t*) ngx_queue_data(p, yahoo_guy_t, queue);   
    next = (yahoo_guy_t*) ngx_queue_data(n, yahoo_guy_t, queue); 
    return ((pre->id > next->id) ? 1:0); 
}  

int main() 
{      
    int i; 
    ngx_pool_t*     pool;   
    yahoo_guy_t*    guy;    
    ngx_queue_t*    q;   
    yahoo_t*        yahoo;    
   
    // 构建队列     
    const ngx_str_t   names[] = 
    {  ngx_string("rainx"),ngx_string("xiaozhe"), ngx_string("zhoujian")   } ;     
    const int ids[]  = 
    { 4611,  8322, 6111 };      
    
    pool = ngx_create_pool(1024*10, NULL); //初始化内存池   
    yahoo = ngx_palloc(pool, sizeof(yahoo_t)); 
    ngx_queue_init(&yahoo->queue); //初始化queue      

    for(i = 0; i < 3; i++)     
    {       
        guy = (yahoo_guy_t*) ngx_palloc(pool, sizeof(yahoo_guy_t));    
        guy->id   = ids[i];       
        //guy->name = (char*) ngx_palloc(pool, (size_t) (strlen(names[i]) + 1) );     
        guy->name = (u_char*) ngx_pstrdup(pool, (ngx_str_t*) &(names[i]) );     
        ngx_queue_init(&guy->queue);       
        // 从头部进入队列      
        ngx_queue_insert_head(&yahoo->queue, &guy->queue);   
    }      
    // 从尾部遍历输出     
    for(q = ngx_queue_last(&yahoo->queue); q != ngx_queue_sentinel(&yahoo->queue); q = ngx_queue_prev(q) )
    {          
        guy = ngx_queue_data(q, yahoo_guy_t, queue);      
        printf("No. %d guy in yahoo is %s \n", guy->id, guy->name);     
    }      
    // 排序从头部输出   
    ngx_queue_sort(&yahoo->queue, yahoo_no_cmp);  
    printf("sorting....\n");    
    for(q = ngx_queue_prev(&yahoo->queue); q != ngx_queue_sentinel(&yahoo->queue); q = ngx_queue_last(q) )
    {         
        guy = ngx_queue_data(q, yahoo_guy_t, queue);    
        printf("No. %d guy in yahoo is %s \n", guy->id, guy->name);   
    }     
    ngx_destroy_pool(pool);    
    return 0;
}

运行结果为

rainx@rainx-laptop:~/land/nginxsrp/src/demo/basic_types$ ./queue_op  
No.4611 guy in yahoo is rainx  
No.8322 guy in yahoo is xiaozhe  
No.6111 guy in yahoo is zhoujian  
sorting
.... 
No.8322 guy in yahoo is xiaozhe  
No.6111 guy in yahoo is zhoujian  
No.4611 guy in yahoo is rainx

插入的示意图如下图所示:

转载于:https://www.cnblogs.com/luhouxiang/archive/2012/10/23/2735406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值