定义在src\core\ngx_conf_file.c
static ngx_int_t
ngx_conf_add_dump(ngx_conf_t *cf, ngx_str_t *filename)
{
off_t size;
u_char *p;
uint32_t hash;
ngx_buf_t *buf;
ngx_str_node_t *sn;
ngx_conf_dump_t *cd;
hash = ngx_crc32_long(filename->data, filename->len);
sn = ngx_str_rbtree_lookup(&cf->cycle->config_dump_rbtree, filename, hash);
if (sn) {
cf->conf_file->dump = NULL;
return NGX_OK;
}
p = ngx_pstrdup(cf->cycle->pool, filename);
if (p == NULL) {
return NGX_ERROR;
}
cd = ngx_array_push(&cf->cycle->config_dump);
if (cd == NULL) {
return NGX_ERROR;
}
size = ngx_file_size(&cf->conf_file->file.info);
buf = ngx_create_temp_buf(cf->cycle->pool, (size_t) size);
if (buf == NULL) {
return NGX_ERROR;
}
cd->name.data = p;
cd->name.len = filename->len;
cd->buffer = buf;
cf->conf_file->dump = buf;
sn = ngx_palloc(cf->temp_pool, sizeof(ngx_str_node_t));
if (sn == NULL) {
return NGX_ERROR;
}
sn->node.key = hash;
sn->str = cd->name;
ngx_rbtree_insert(&cf->cycle->config_dump_rbtree, &sn->node);
return NGX_OK;
}
函数 ngx_conf_add_dump
的作用和意义可以通俗理解为:
作用
这个函数负责将 Nginx 的配置文件内容缓存到内存中,并记录文件信息,避免重复加载相同的配置文件。
具体步骤
意义
简单来说,这个函数就像是给配置文件做了一个“快照”,需要时直接从内存读取,而不是每次都从磁盘重新加载,从而让 Nginx 更高效地管理配置。
static ngx_int_t ngx_conf_add_dump(ngx_conf_t *cf, ngx_str_t *filename);
static
关键字ngx_conf_file.c
)中被调用,其他文件无法直接访问。ngx_int_t
ngx_int_t
是 Nginx 自定义的整数类型(通常为 typedef int ngx_int_t
),用于统一表示函数执行结果。NGX_OK
:表示函数执行成功(通常返回值为 0)。NGX_ERROR
:表示函数执行失败(通常返回值为 -1)。ngx_conf_t *cf
ngx_conf_t
是 Nginx 的配置解析上下文结构体,包含解析配置时所需的所有状态信息。cf->pool
)用于动态内存分配。cf->conf_file
)。cf->cycle
,即 ngx_cycle_t
结构体)。cf->cycle->config_dump_rbtree
:红黑树,用于记录已缓存的配置文件信息。cf->cycle->config_dump
:数组,存储实际缓存的配置文件内容。cf->temp_pool
:临时内存池,用于分配短期使用的数据结构。ngx_str_t *filename
ngx_str_t
是 Nginx 自定义的字符串类型,定义为:typedef struct {
size_t len; // 字符串长度
u_char *data; // 字符串内容(指向二进制数据)
} ngx_str_t;
nginx.conf
)。len
明确长度,而非依赖 \0
结尾)。static ngx_int_t
ngx_conf_add_dump(ngx_conf_t *cf, ngx_str_t *filename)
{
off_t size;
u_char *p;
uint32_t hash;
ngx_buf_t *buf;
ngx_str_node_t *sn;
ngx_conf_dump_t *cd;
off_t size
:用于存储配置文件的大小(字节数)。u_char *p
:指向动态分配的内存,用于复制文件名。uint32_t hash
:存储文件名的哈希值,用于快速查找。ngx_buf_t *buf
:指向缓冲区,用于存储配置文件内容。ngx_str_node_t *sn
:红黑树节点,记录文件名和哈希值。ngx_conf_dump_t *cd
:配置转储结构体,包含文件名和缓冲区。 hash = ngx_crc32_long(filename->data, filename->len);
ngx_crc32_long
函数计算文件名的 CRC32 哈希值。 sn = ngx_str_rbtree_lookup(&cf->cycle->config_dump_rbtree, filename, hash);
config_dump_rbtree
)中查找是否已存在相同文件名的节点。&cf->cycle->config_dump_rbtree
:全局红黑树,存储所有已缓存的文件信息。filename
:当前要缓存的文件名。hash
:文件名的哈希值。ngx_str_node_t
;否则返回 NULL
。 if (sn) {
cf->conf_file->dump = NULL;
return NGX_OK;
}
sn != NULL
),则直接标记当前配置文件的 dump
为 NULL
,表示不需要重复缓存。 p = ngx_pstrdup(cf->cycle->pool, filename);
if (p == NULL) {
return NGX_ERROR;
}
ngx_pstrdup
在内存池中复制文件名。cf->cycle->pool
:全局内存池,生命周期与 Nginx 进程一致。filename
:需要复制的文件名。NGX_ERROR
。 cd = ngx_array_push(&cf->cycle->config_dump);
if (cd == NULL) {
return NGX_ERROR;
}
ngx_conf_dump_t
结构体添加到 config_dump
数组中。&cf->cycle->config_dump
是存储所有缓存文件信息的数组。NGX_ERROR
。 size = ngx_file_size(&cf->conf_file->file.info);
ngx_file_size
宏获取当前配置文件的大小。cf->conf_file->file.info
是文件的元信息结构体。 buf = ngx_create_temp_buf(cf->cycle->pool, (size_t) size);
if (buf == NULL) {
return NGX_ERROR;
}
buf
,大小为文件内容长度。cf->cycle->pool
:全局内存池。(size_t) size
:缓冲区大小。NGX_ERROR
。 cd->name.data = p;
cd->name.len = filename->len;
cd->buffer = buf;
cd
结构体。cd->name
:保存文件名的字符串(data
和 len
)。cd->buffer
:指向存储文件内容的缓冲区。 cf->conf_file->dump = buf;
dump
指针指向缓冲区 buf
。dump
判断是否需要从内存中读取内容。 sn = ngx_palloc(cf->temp_pool, sizeof(ngx_str_node_t));
if (sn == NULL) {
return NGX_ERROR;
}
sn
。cf->temp_pool
:临时内存池,生命周期可能短于全局内存池。sizeof(ngx_str_node_t)
:节点大小。NGX_ERROR
。 sn->node.key = hash;
sn->str = cd->name;
sn->node.key
:哈希值作为红黑树的键。sn->str
:文件名字符串,用于后续比较(解决哈希冲突)。 ngx_rbtree_insert(&cf->cycle->config_dump_rbtree, &sn->node);
config_dump_rbtree
中。 return NGX_OK;
}