【Nginx 源码学习】动态数组

}

/**

  • 数组初始化,并且分配内存空间给数组元素

  • PS:这边数组的数据结构和数组元素的存储分成了两次在pool上分配,笔者认为可以一次进行分配

  • 但是Nginx是多进程的,程序执行流程是线性的,所以分两次分配也无伤大雅。

*/

if (ngx_array_init(a, p, n, size) != NGX_OK) {

return NULL;

}

return a;

}

创建数组后内存池的结构如下:

在这里插入图片描述

static ngx_inline ngx_int_t

ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)

{

/*

  • set “array->nelts” before “array->elts”, otherwise MSVC thinks

  • that “array->nelts” may be used without having been initialized

*/

/* 初始化数组成员,注意:nelts必须比elts先初始化 */

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;

}


销毁数组


销毁动作实际上就是修改内存池的 last 指针,即数组的内存被内存池回收,并没有调用 free 等释放内存的操作。

void

ngx_array_destroy(ngx_array_t *a)

{

ngx_pool_t *p;

p = a->pool;

/*

  • 如果数组元素的末尾地址和内存池pool的可用开始的地址相同

  • 则将内存池pool->d.last移动到数组元素的开始地址,相当于清除当前数组的内容

*/

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;

}

}

有点奇怪哈。。。

那个调整 last 的条件有点苛刻了吧???

这能进入分支么?

欲知后事如何,且听下回分解哈哈


添加元素操作


实际的添加操作并不在这两个函数中完成,例如ngx_array_push返回可以在该数组数据区中添加这个元素的位置,ngx_array_push_n则返回可以在该数组数据区中添加n个元素的起始位置,而添加操作即在获得添加位置之后进行

void *ngx_array_push(ngx_array_t *a)

{

void *elt, *new;

size_t size;

ngx_pool_t *p;

if (a->nelts == a->nalloc) { //数组数据区满

/* the array is full */

size = a->size * a->nalloc; //这是干嘛,有点猛啊,哦,size是局部变量

p = a->pool;

if ((u_char *) a->elts + size == p->d.last

&& p->d.last + a->size <= p->d.end) //还没弹尽粮绝呢?那也不对啊,怎么是小等于,那不就会山穷水尽吗

{ //除非,这个end指的是最后一块内存,而不是最后一位地址

/*

  • the array allocation is the last in the pool

  • and there is space for new allocation

*/

p->d.last += a->size;

a->nalloc++;

} else {

/* allocate a new array */

new = ngx_palloc(p, 2 * size); //否则,扩展数组数据区为原来的2倍

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) {

/* the array is full */

p = a->pool;

if ((u_char *) a->elts + a->size * a->nalloc == p->d.last

&& p->d.last + size <= p->d.end)

{

/*

  • the array allocation is the last in the pool

  • and there is space for new allocation

*/

p->d.last += size;

a->nalloc += n;

} else {

/* allocate a new array */

nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);

new = ngx_palloc(p, nalloc * a->size);

if (new == NULL) {

return NULL;

}

写在最后

还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

image

oc(p, nalloc * a->size);

if (new == NULL) {

return NULL;

}

写在最后

还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

[外链图片转存中…(img-5OjiR8Ex-1721162762903)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值