NGINX源码之:ngx_arrayngx_listngx_string
1、array
老规矩,先看下array的结构
/* 数组Array结构体 */
typedef struct {void *elts; /* 指向数组第一个元素*/ngx_uint_t nelts; /* 已使用元素的计数*/size_t size; /* 每个元素的大小*/ngx_uint_t nalloc; /* 分配元素个数,可调整 */ngx_pool_t *pool; /* 内存池*/
} ngx_array_t;
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 ;
} test2;
void testArray(){ngx_pool_t * pool = ngx_create_pool(POOL_SIZE,NULL);ngx_array_t *arr = ngx_array_create(pool, 2, sizeof(test2));test2 *test0;test2 *test1;test2 *test00;void *e0 = ngx_array_push(arr);test0 = e0;test0->a=12;test0->b=21;void *e1 = ngx_array_push(arr);void *e2 = ngx_array_push(arr);test1 = e1;test1->a = 2;test1->b = 3;void *elist = ngx_array_push_n(arr, 3);test00 = arr->elts;printf("a: %d,b: %d \n",test00[0].a,test00[0].b);printf("a: %d,b: %d \n",test00[1].a,test00[1].b);printf("a: %d,b: %d \n",test00[5].a,test00[5].b);ngx_array_destroy(arr);ngx_array_t *arr1 = ngx_array_create(pool, 2, 5000);ngx_array_destroy(arr1);
}
void main(){testArray();
}
原理:
2、list
list的数据结构是多个节点(part),通过链表方式组织在一起,单个part与数组的结构非常类似,代码逻辑也差不多,这里就不再分析代码了。
使用场景上,条目较多但每条数据不大的数据list会比array更合适。
图如下:
每个part元素个数固定,本demo设为2,超过单个part的元素个数限制,另创新的part节点,旧节点next指针指向新节点。
debug的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 ;
} test2;
void testList(){test2 *test00;ngx_pool_t * pool = ngx_create_pool(POOL_SIZE,NULL);ngx_list_t *list = ngx_list_create(pool, 2, sizeof(test2));void *e0 = ngx_list_push(list);void *e1 = ngx_list_push(list);void *e2 = ngx_list_push(list);void *e3 = ngx_list_push(list);void *e4 = ngx_list_push(list);void *e5 = ngx_list_push(list);test2 *test0 = e0;test0->a=12;test0->b=21;test2 *test2 = e2;test2->a = 2;test2->b = 3;test00 = list->part.elts;//一个part两个元素,多个part形成listprintf("a: %d,b: %d \n",test00[0].a,test00[0].b);printf("a: %d,b: %d \n",test00[1].a,test00[1].b);printf("a: %d,b: %d \n",test00[2].a,test00[2].b);//越界非正常结果test00 = list->part.next->elts;printf("a: %d,b: %d \n",test00[0].a,test00[0].b);printf("a: %d,b: %d \n",test00[1].a,test00[1].b);printf("a: %d,b: %d \n",test00[2].a,test00[2].b);//越界非正常结果
}
void main(){testList();
}
3、string
demo
ngx_string的很多方法都是调用系统函数,查查对应的系统函数功能就知道作用了
void testString(){printf("Hello Runoob! %d \n" ,"hello" );ngx_str_t name = ngx_string("hello");printf(name.data);/*** ngx_string中有一些对系统方法的封装* ngx_strcmp 比较两个字符串大小* ngx_strstr 查找str1中str2的位置* ngx_strlen 字符串长度*/size_t len = strlen("helloworld");char men[] = "helloeshin";//将字符串开头两个位置设置为fngx_memset(men,'f',2);//开头五个位置设置为0;ngx_memzero(men,5);char src[] = "fffff";char dest[] = "helloeshin";//拷贝src到dest的开头几位ngx_memcpy(dest,src,strlen(src));char src1[] = "00000";//拷贝后并移动指针char *pos = ngx_cpymem(dest,src1,strlen(src1));//拷贝到上面移动指针后的位置。char *pos1 = ngx_cpymem(pos,src,strlen(src));
}
参考:
菜鸟nginx源码剖析
Nginx源码分析
NGINX源码之:ngx_arrayngx_listngx_string相关推荐
- 从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的作为服务端软件,表现的主要特点是更快.高扩展.高可靠性.低内存消 ...
最新文章
- linux里引号注意点
- 北斗核心器件100%国产化,已接入大部分智能手机
- CDN监控系统(三 业务架构)
- HTML 表格tablecaptionthtrtdtheadtbodytfootcolcolgroup
- 创业第一天,有三AI扔出了深度学习的150多篇文章和10多个专栏
- python的print怎么输出utf-8的编码_原创反转精度算法:小数的终极编码
- 反演控制 matlab,基于matlab的反演程序
- debian 10安装ssh依赖openssh-client版本错误的解决办法及开启ssh远程登录设置
- 2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
- 简单通用的Makefile编写例子
- 移植emwin到stm32f205上卡死在gui_init();
- parallel scavenge 与parnew 区别:
- HDU 6441 Find Integer(费马大定理)
- TypeError: can‘t subtract offset-naive and offset-aware datetimes
- 联通系统升级服务器地址,联通iptv升级服务器地址
- 论文期刊一般的审稿流程
- (精品)运用PS的液化滤镜制作逼真的石头效果-PS滤镜教程
- 深度学习正则化(L1 norm/L2 norm)以及dropout理解
- 无人驾驶1——自动驾驶硬件、软件概述
- 从头学习爬虫(三十)实战篇----动漫之家漫画(分析)