闲不下来-nginx 数组结构
nginx 为了让主要业务逻辑看的更加清晰了然,对常见的数组、链表和队列都进行了一定封装。
数组
在 Nginx 数组中,内存分配是基于内存池的,并不是固定不变的,也不是需要多少内存就申请多少,若当前内存不足以存储所需元素时,按照当前数组的两倍内存大小进行申请,这样做减少内存分配的次数,提高效率。很多语言都有着这样的特性,比如 Java,它就对数组扩容做了一定的封装。
不得不夸一下 nginx 的代码模块化,使得我们平时阅读更加方便,那么言归正传,数组的封装在core/ngx_array.h
文件中:
数组结构
struct ngx_array_s {
void *elts;
ngx_uint_t nelts;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *pool;
};
*elts
:指向数组数据区域的首地址nelts
:数组实际数据的个数size
:单个元素所占据的字节大小nalloc
:数组容量*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);
*ngx_array_create
:创建新的动态数组ngx_array_destroy
:毁数组对象,内存被内存池回收*ngx_array_push
:在现有数组中增加一个新的元素*ngx_array_push_n
:在现有数组中增加 n 个新的元素
数组方法
ngx_array_create
在文件core/ngx_array.c
文件中,注意,源码版本 nginx-0.5
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;
}
// 给数组首地址分配 n个size
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;
}
创建数组的过程:
- 首先分配数组头
- 然后分配数组数据区,两次分配均在传入的内存池(pool 指向的内存池)中进行。
- 然后简单初始化数组头并返回数组头的起始位置。
小 tips:
sizeof
:sizeof 是 C 语言的一种单目操作符,如 C 语言的其他操作符++、--等,简单的说其作用就是:返回一个对象或者类型所占的内存字节数
所以,它并不是函数,sizeof 操作符以字节形式给出了其操作数的存储大小。
- 操作数可以是一个表达式或括在括号内的类型名。
- 操作数的存储大小由操作数的类型决定。
sizeof 的使用方法,简单介绍几个:
sizeof( object ); // sizeof( 对象 );
sizeof( type_name ); // sizeof( 类型 );
sizeof object; // sizeof 对象;
而 sizeof 操作符的结果类型是 size_t,它在头文件中定义为: typedef unsigned int size_t
,该类型保证能容纳实现所建立的最大对象的字节大小.
sizeof 的主要用途:
- 分配内存时使用
- 计算数组中元素个数时使用
ngx_array_destroy
有创建就有销毁,毕竟不像 Java 一样有着对象垃圾回收的功能
void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;
p = a->pool;
// 移动内存池的last指针,释放数组所有元素所占据的内存
if ((u_char *) a->elts + a->size * a->nalloc == p->last) {
p->last -= a->size * a->nalloc;
}
// 释放数组首指针所占据的内存
if ((u_char *) a + sizeof(ngx_array_t) == p->last) {
p->last = (u_char *) a;
}
}
从这里看到,销毁数组的操作实现是销毁数组数据区和数组头,销毁动作实际上就是修改内存池的 last 指针,即数组的内存被内存池回收,并没有调用 free 等释放内存的操作。
ngx_array_push
void *
ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;
// 判断数组是否已经满
// nlelts: 实际元素个数
// nalloc: 容量
if (a->nelts == a->nalloc) {
/* the array is full */
// 计算数组所有元素占据的内存大小
size = a->size * a->nalloc;
p = a->pool;
if ((u_char *) a->elts + size == p->last && p->last + a->size <= p->end)
{
// 若当前内存池的内存空间至少可容纳一个元素大小
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/
p->last += a->size;
a->nalloc++;
} else {
// 若当前内存池不足以容纳一个元素,则分配新的数组内存
/* allocate a new array */
// 从这里可以看到2倍
new = ngx_palloc(p, 2 * size);
if (new == NULL) {
return NULL;
}
// 复制 or 拷贝
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;
}
ngx_array_push_n
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->last
&& p->last + size <= p->end)
{
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/
p->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;
}
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;
}
数组添加元素的操作有两个,ngx_array_push
和 ngx_array_push_n
,分别添加一个和多个元素。实际的添加操作并不在这两个函数中完成,只是在这两个函数中申请元素所需的内存空间,并返回指向该内存空间的首地址,在利用指针赋值的形式添加元素。
写个测试用例:
测试代码如下:
#include "ngx_config.h"
#include <stdio.h>
#include "ngx_conf_file.h"
#include "nginx.h"
#include "ngx_core.h"
#include "ngx_string.h"
#include "ngx_palloc.h"
#include "ngx_array.h"
volatile ngx_cycle_t *ngx_cycle;
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)
{
}
void dump_array(ngx_array_t *a)
{
if (a)
{
printf("array = 0x%x\n", a);
printf(" .elts = 0x%x\n", a->elts);
printf(" .nelts = %d\n", a->nelts);
printf(" .size = %d\n", a->size);
printf(" .nalloc = %d\n", a->nalloc);
printf(" .pool = %d\n", a->pool);
printf("elements: ");
int *ptr = (int *)(a->elts);
for (; ptr < (int *)(a->elts + a->nalloc * a->size);)
{
printf("%d ", *ptr++);
}
printf("\n");
}
}
int main()
{
ngx_pool_t *pool;
int i;
printf("------------\n");
printf("create a new pool: \n");
printf("------\n");
pool = ngx_create_pool(1024, NULL);
printf("------------\n");
printf("alloc an array from the pool: \n");
printf("------------\n");
ngx_array_t *a = ngx_array_create(pool, 5, sizeof(int));
for (i = 0; i < 5; i++)
{
int *ptr = ngx_array_push(a);
*ptr = 2 * i;
}
dump_array(a);
ngx_array_destroy(a);
ngx_destroy_pool(pool);
return 0;
}
编写 Makefile
CXX = gcc
CXXFLAGS +=-g -Wall -Wextra
NGX_ROOT =/Users/mf/Documents/mysource/nginx-0.5
TARGETS =ngx_array_t_test
TARGETS_C_FILE= $(TARGETS).c
CLEANUP = rm-f $(TARGETS) *.o
all:$(TARGETS)
clean:
$(CLEANUP)
CORE_INCS =-I. \
-I$(NGX_ROOT)/src/core \
-I$(NGX_ROOT)/src/event \
-I$(NGX_ROOT)/src/event/modules \
-I$(NGX_ROOT)/src/os/unix \
-I$(NGX_ROOT)/objs \
NGX_PALLOC =$(NGX_ROOT)/objs/src/core/ngx_palloc.o
NGX_STRING =$(NGX_ROOT)/objs/src/core/ngx_string.o
NGX_ALLOC =$(NGX_ROOT)/objs/src/os/unix/ngx_alloc.o
NGX_ARRAY =$(NGX_ROOT)/objs/src/core/ngx_array.o
$(TARGETS):$(TARGETS_C_FILE)
$(CXX) $(CXXFLAGS) $(CORE_INCS) $(NGX_PALLOC) $(NGX_STRING) $(NGX_ALLOC) $(NGX_ARRAY) $^ -o $@
终端运行:make all
,过后接着运行:./ngx_array_t_test
运行结果:
↳ ./ngx_array_t_test 00:14:46
------------
create a new pool:
------
------------
alloc an array from the pool:
------------
array = 0x74009840
.elts = 0x74009868
.nelts = 5
.size = 4
.nalloc = 5
.pool = 1946195968
elements: 0 2 4 6 8