NGINX源码之:ngx_bufchain
老规矩,先来看下buf和chain的大概结构
首先温习下指针的指针,在ngx_buf的几个方法中用到了chain指针的指针,用于chain链表的遍历值替换。
test2 * test;test2 ** testP = &test;test2 * test_2;test2 * test_3;*testP = test_2;testP = &test_3;
指针本身是占用少量的地址空间的,存放的数据则是指向实际数据的内存地址。而指针的指针则是把指针的占用空间当做数据来指向。示例中:
最开始:testp–>test–>实际内存1;test_2–>实际内存2;test_3–>实际内存3
*testP = test_2后:testp–>test == test_2–>实际内存2;
testP = &test_3后;test==test_2–>实际内存2;testp–>test3–>实际内存3;
在chain中,指针的指针,通常用来遍历指针。
dubug的demo
#include <stdio.h>
#include <string.h>
#include "nginx.h"
#include "ngx_hash.h"
#include "ngx_string.h"
#include "ngx_config.h"
#include "ngx_conf_file.h"
#include "ngx_core.h"
#include "ngx_palloc.h"
#define POOL_SIZE 2000typedef struct Test_2
{int a ;int b ;ngx_queue_t testq;
} test2;void testBuf(){printf("size of buf struct: %d \n" , sizeof(unsigned long) );ngx_pool_t * pool = ngx_create_pool(POOL_SIZE,NULL);ngx_buf_t *buf1 = ngx_create_temp_buf(pool, 64);ngx_buf_t *buf2 = ngx_create_temp_buf(pool, 1980);ngx_buf_t *buf3 = ngx_create_temp_buf(pool, 64);ngx_buf_t *buf4 = ngx_create_temp_buf(pool, 1980);ngx_buf_t *buf6 = ngx_create_temp_buf(pool, 64);ngx_buf_t *buf7 = ngx_create_temp_buf(pool, 1980);ngx_chain_t *cl = ngx_alloc_chain_link(pool);cl->buf = buf1;ngx_chain_t *cl2 = ngx_alloc_chain_link(pool);cl2->buf = buf2;cl->next = cl2;ngx_chain_t *cl3= ngx_alloc_chain_link(pool);cl3->buf = buf3;ngx_chain_t *cl4= ngx_alloc_chain_link(pool);cl4->buf = buf4;cl3->next = cl4;char src[] = "eshin";
// buf4->last = ngx_cpymem(buf4->last, src,sizeof(src) );//src最后一位是0;buf4->last = ngx_cpymem(buf4->last, src,sizeof(src)-1 );char dest[] = "xxxxx is handsome";ngx_memcpy(dest,buf4->pos,buf4->last - buf4->pos);/*** 当未处理位置pos,与空余空间首地址相同,表示buf内没有未处理数据,* 当update_chain时,该buf的chain节点如果在busy链表,将被放到free链表*/buf4->pos = buf4->last;//将cl3中的每个节点拷贝到cl中
// ngx_chain_add_copy(pool,&cl,cl3);//获取cl链表中首chain节点,cl中为null时,通过pool创建
// ngx_chain_t *cl5 = ngx_chain_get_free_buf(pool,&cl);//将cl赋值给pool的chain,free时,将cl5头插入到chain中
// pool->chain = cl;
// ngx_free_chain(pool,cl5);ngx_chain_t *cl6 = ngx_alloc_chain_link(pool);cl6->buf = buf6;ngx_chain_t *cl7 = ngx_alloc_chain_link(pool);cl7->buf = buf7;cl6->next = cl7;ngx_chain_update_chains(pool,&cl,&cl3,&cl6,cl->buf->tag);}
void main(){testBuf();
}
1、buf创建ngx_create_temp_buf
2、chain链表创建ngx_alloc_chain_link
3、chain复制连接ngx_chain_add_copy
4、获取空闲节点ngx_chain_get_free_buf
5、busy转换到free:ngx_chain_update_chains
参考:
菜鸟nginx源码剖析
Nginx源码分析
NGINX源码之:ngx_bufchain相关推荐
- 从Nginx源码谈大小写字符转化的最高效代码以及ASCII码表的科学
说起大小写字母转换,大家很容易想起系统函数是不是,几乎所有的编程语言都提供了这种转换函数,但是你有没有想过这背后是怎么实现的? 让你写怎么实现? 我们都知道Nginx是目前用的最多的Http服务器,那 ...
- Nginx源码分析链接
nginx-0.8.38源码探秘:http://blog.csdn.net/ccdd14/article/details/5872312 nginx源码分析: http://blog.sina.com ...
- Nginx源码分析--数据对齐posix_memalign和memalign函数
posix_memalign函数() /* * 背景: * 1)POSIX 1003.1d * 2)POSIX 标明了通过malloc( ), calloc( ), 和 re ...
- nginx源码编译、负载均衡及模块的扩展
1.nginx源码编译 实验环境: iptables和selinux关闭 redhat6.5 nginx:test1: 172.25.1.11 [root@test1 ~]# ls nginx-1.1 ...
- Nginx源码分析:epoll事件处理模块概述
nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 事件处理模块概述 Nginx的高效请求的处理依赖于事件管理机制,本次默认的场景是Linux操 ...
- Nginx源码分析:惊群处理与负载均衡
nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的惊群处理与负载均衡概述 当Nginx工作在master/worker模式下时,就 ...
- Nginx源码分析:核心数据结构ngx_cycle_t与内存池概述
nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 核心数据结构与内存池概述 在Nginx中的核心数据结构就是ngx_cycle_t结构,在初始 ...
- Nginx源码分析:master/worker工作流程概述
nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的master与worker工作模式 在生成环境中的Nginx启动模式基本都是以m ...
- Nginx源码分析:启动流程
nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> nginx简介 Nginx的作为服务端软件,表现的主要特点是更快.高扩展.高可靠性.低内存消 ...
最新文章
- 通过transpose和flip实现图像旋转90/180/270度
- CSDN中的如何转载博文
- kotlin 用协程做网络请求_中国电信营业厅: 感受 Kotlin 的 quot;加速度quot;
- 数据传输服务 DTS > 产品简介 > 功能特性 > 数据订阅(新版)
- linux 日志编程(总结)
- java 密码生成器_[Java小白]WIFI纯数字密码字典生成器
- Java框架全开源商城PC+手机版+微商城独立版+全开源系统源码
- mtk无线网卡 linux,在树莓派上使用基于MT7601的无线网卡(如360/百度/腾讯Wifi)
- 计算机网络知识点总结
- 李宏毅2020机器学习笔记1——CXK
- 32位与64位到底什么区别?
- 1.ESP32文件夹配置,创建自己的工程 Vscode+Idf插件
- 微信中各种代码/符号合集
- 快递鸟即时查询接口的连接和使用
- xcode5 Localized 多语言 本地化
- 使用指针实现strcpy函数的功能
- “电梯”英文域名Lifts.com超63.5万结拍
- 全面总结C++类模板使用的基础知识
- 文件下载成excel
- Nvidia Caffe User Guide
热门文章
- Realm JavaScript
- 混沌序列加密matlab,基于三维Lorenz混沌系统和Matlab仿真工具实现混沌数字视频加密...
- Mysql拼接查询结果
- java中cbrt_JavaScript中带有示例的Math.cbrt()方法
- POJ 1625 Censored!
- python爬取58同城租房信息_分页爬取58同城租房信息.py
- 影响利率风险结构的因素_利率风险结构是什么意思 影响利率的因素
- Typora图床设置
- Ubuntu安装中文字体
- 如何在 Windows 10 中安装 WSL2 的 Linux 子系统