Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1062286
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2016-11-10 10:57:27

nginx中的队列实现就是用的双向链表这种,节点代码如下:

     

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. typedef struct ngx_queue_s  ngx_queue_t;  
  2.   
  3. struct ngx_queue_s {  
  4.     ngx_queue_t  *prev;  
  5.     ngx_queue_t  *next;  
  6. };  


    所以,nginx的队列实现其实就是常用的链表操作,具体操作如下:


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #define ngx_queue_init(q)                                                     \  
  2.     (q)->prev = q;                                                            \  
  3.     (q)->next = q  
  4.   
  5.   
  6. #define ngx_queue_empty(h)                                                    \  
  7.     (h == (h)->prev)  
  8.   
  9.   
  10. #define ngx_queue_insert_head(h, x)                                           \  
  11.     (x)->next = (h)->next;                                                    \  
  12.     (x)->next->prev = x;                                                      \  
  13.     (x)->prev = h;                                                            \  
  14.     (h)->next = x  
  15.   
  16. #define ngx_queue_insert_tail(h, x)                                           \  
  17.     (x)->prev = (h)->prev;                                                    \  
  18.     (x)->prev->next = x;                                                      \  
  19.     (x)->next = h;                                                            \  
  20.     (h)->prev = x  
  21.   
  22.   
  23. #define ngx_queue_head(h)                                                     \  
  24.     (h)->next  
  25.   
  26.   
  27. #define ngx_queue_last(h)                                                     \  
  28.     (h)->prev  
  29.   
  30.   
  31. #define ngx_queue_sentinel(h)                                                 \  
  32.     (h)  
  33.   
  34.   
  35. #define ngx_queue_next(q)                                                     \  
  36.     (q)->next  
  37.   
  38.   
  39. #define ngx_queue_prev(q)                                                     \  
  40.     (q)->prev  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #define ngx_queue_data(q, type, link)                                         \  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">    span>(type *) ((unsigned char *) q - offsetof(type, link))<span style="white-space:pre">  
  2. span>  
  3. #define ngx_queue_add(h, n)                                                   \  
  4.     (h)->prev->next = (n)->next;                                              \  
  5.     (n)->next->prev = (h)->prev;                                              \  
  6.     (h)->prev = (n)->prev;                                                    \  
  7.     (h)->prev->next = h;  
  8.       

 在使用队列之前需要初始化一下队列,这样是为了设立一个队列的哨兵节点,便于之后的操作 
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ngx_queue_init 使当前的节点成为哨兵节点,也就是自己指向自己  


 这里主要记录下队列获取队列数据的方法,这也是c中常用的一种技巧:

 #define ngx_queue_data(q, type, link)     (type *) ((unsigned char *) q - offsetof(type, link))

q为想要获取节点数据的当前节点,type是当前节点保存的数据类型 ,link是什么呢?关键点就在这,link一定要是type数据结构中的ngx_queue_t*结构的成员名,这样就可以根据成员的偏移量找到整个数据的内存指针


具体使用方式如下:

  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. typedef struct {  
  2.     ngx_queue_t qEle;  
  3.     void* data;  
  4. }node_t;  
  5.   
  6. ngx_queue_t* channel_queue_head;  
  7. ngx_queue_init(channel_queue_head);  
  8. node_t node;  
  9. ngx_queue_insert_tail(channel_queue_head, &(node.qEle));  
  10.   
  11. node_t *getnode;  
  12. getnode = (node_t*)ngx_queue_data(&(node.qEle),node_t,qEle); 
阅读(3443) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~