Nginx源码完全注释(2)ngx_array.h / ngx_array.c
数组头文件ngx_array.h
#include <ngx_config.h>
#include <ngx_core.h>
struct ngx_array_s {
void *elts;
ngx_uint_t nelts;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *pool;
};
ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size);
void ngx_array_destroy(ngx_array_t *a);
void *ngx_array_push(ngx_array_t *a);
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n);
static ngx_inline ngx_int_t
ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
array->nelts = 0;
array->size = size;
array->nalloc = n;
array->pool = pool;
array->elts = ngx_palloc(pool, n * size);
if (array->elts == NULL) {
return NGX_ERROR;
}
return NGX_OK;
}
数组源文件ngx_array.c
#include <ngx_config.h>
#include <ngx_core.h>
ngx_array_t *
ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
{
ngx_array_t *a;
a = ngx_palloc(p, sizeof(ngx_array_t));
if (a == NULL) {
return NULL;
}
a->elts = ngx_palloc(p, n * size);
if (a->elts == NULL) {
return NULL;
}
a->nelts = 0;
a->size = size;
a->nalloc = n;
a->pool = p;
return a;
}
void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;
p = a->pool;
if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
p->d.last -= a->size * a->nalloc;
}
if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
p->d.last = (u_char *) a;
}
}
void *
ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;
if (a->nelts == a->nalloc) {
size = a->size * a->nalloc;
p = a->pool;
if ((u_char *) a->elts + size == p->d.last
&& p->d.last + a->size <= p->d.end)
{
p->d.last += a->size;
a->nalloc++;
} else {
new = ngx_palloc(p, 2 * size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, a->elts, size);
a->elts = new;
a->nalloc *= 2;
}
}
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;
return elt;
}
void *
ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
{
void *elt, *new;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *p;
size = n * a->size;
if (a->nelts + n > a->nalloc) {
p = a->pool;
if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
&& p->d.last + size <= p->d.end)
{
p->d.last += size;
a->nalloc += n;
} else {
nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
new = ngx_palloc(p, nalloc * a->size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, a->elts, a->nelts * a->size);
a->elts = new;
a->nalloc = nalloc;
}
}
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts += n;
return elt;
}
Reference
- http://blog.csdn.net/v_july_v/article/details/7040425
- http://www.cnblogs.com/sld666666/archive/2010/06/27/1766255.html
- http://www.cnblogs.com/jzhlin/archive/2012/06/05/Nginx_pool.html
- http://code.google.com/p/nginxsrp/wiki/NginxCodeReview#ngx的内存池
-
转载请注明来自柳大Poechant(钟超Michael)的CSDN博客:
钟超Michael的博客:Blog.CSDN.net/Poechant
钟超Michael的微博:钟超Michael的新浪微博
-
分享到:
相关推荐
ngx_sqlite 是嵌入 sqlite 数据库的 nginx 模块。通过强大的nginx server,可以使用http协议访问sqlite数据库。环境- sqlite 3- nginx-1.6.3 安装```sh $ git clone https://github.com/rryqszq4/ngx_sqlite.git...
nginx-1.18.0-2.el7.ngx.x86_64.rpm安装包(含有部署手册) nginx-1.18.0-2.el7.ngx.x86_64.rpm安装包(含有部署手册) nginx-1.18.0-2.el7.ngx.x86_64.rpm安装包(含有部署手册) nginx-1.18.0-2.el7.ngx.x86_64.rpm...
nginx1.20.2
ngx_php功能是为nginx模块嵌入php脚本语言。别名为php-nginx-module。 特性 * 支持加载php.ini配置文件 * 支持原生php的全局变量$_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_SESSION... * 支持运行php代码...
ngx_python 是为 nginx 嵌入 python 脚本的模块。环境- python 2.7.*- nginx-1.6.3 安装```sh git clone https://github.com/rryqszq4/ngx_python.git wget 'http://nginx.org/download/nginx-1.6.3.tar.gz' ...
ngx_http_dav_ext_module.so centos7 nginx 1.18 可以作为模块加载
2022年5月30日 官方当前最新稳定版本nginx 二进制rpm包 适用于x86架构centos7 rhel7版本操作系统升级安装nginx 该包开启了ipv6支持,添加了nginx-rtmp模块支持
ngx_cache_purge 是 nginx 模块,此模块可以清理 nginx 的 FastCGI、proxy、 SCGI 和 uWSGI 的缓存。配置指令(相同位置语法)fastcgi_cache_purgesyntax: fastcgi_cache_purge on|off|<method> [from all|<ip> [.....
很难找的 centos7 nginx-1.6.2-1.el7.centos.ngx.x86_64.rpm 包
nginx_1.7.11.3_Gryphon主要用于媒体直播服务器用,和FFmpeg+nginx+实例视频
nginx rpm package for centos7 nginx rpm package for centos7 nginx rpm package for centos7 nginx rpm package for centos7 nginx rpm package for centos7 nginx rpm package for centos7 nginx rpm package ...
nginx-1.20.0-1.el7.ngx.x86_64
备用nginx代理模块
lua-upstream-nginx-module, Nginx C 模块将Lua向ngx_lua公开,用于 Nginx upstreams 电子邮件名称ngx_http_lua_upstream - Nginx MODULE,用于向 Nginx upstreams公开Lua到 ngx_lua目录NAME状态概要说明函数get_...
2022年5月30日,官方当前最新稳定版本nginx 1.22.0 已经开启ipv6支持,nginx-http-flv-module模块支持 适用于x86架构的centos7 rhel7系列版本操作系统安装升级使用或者修复安全漏洞等
nginx 1.21.6 带rtmp模块,当前最新版rpm 包
linux下的nginx--rpm安装包
CentOS下nginx服务安装包,直接yum即可完成安装。Nginx (engine x) 是一个高性能的HTTP和web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рам...
CentOS下nginx服务安装包,直接yum即可完成安装。Nginx (engine x) 是一个高性能的HTTP和web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рам...
nginx-1.16.1-1.el6.ngx.x86_64.rpm nginx 最新版 nginx-1.16.1-1.el6.ngx.x86_64.rpm nginx 最新版