当前位置: 首页 > 工具软件 > ngx-meta > 使用案例 >

Nginx基本数据结构(三)——ngx_chain_t、ngx_buf_t

陶腾
2023-12-01

ngx_chain_t

Nginx的filter模块用来处理从别的filterhandler模块传递过来的数据。这个传递的过程是以一个链表(ngx_chain_t)的形式。而且数据可能会被多次传递。

struct ngx_chain_s
{
	ngx_buf_t		*buf;
	ngx_chain_t		*next;
}

相关操作API

ngx_chain_t* ngx_alloc_chain_link(ngx_pool_t* pool);
//创建一个ngx_chain_t对象,并返回指向对象的指针,失败返回null
#define ngx_free_chain(pool, cl)
	cl->next = pool->chain;
	pool->chain = cl

//该宏释放一个ngx_chain_t对象
//对ngx_chain_t的释放,不是真正的释放内存,而是把这个对象挂在了这个pool对象的一个叫chain字段对应的chain上

ngx_buf_t

这个就是上面ngx_chain_t链表的每个节点的实际数据。

struct ngx_buf_s
{
	u_char			*pos;			//数据的开始位置
	u_char			*last;			//数据结束位置
	off_t			file_pos;		//如果是文件的话,数据开始位置
	off_t			file_last;		//如果是文件,数据结束位置

	u_char			*start;			//内存起始位置
	u_char			*end;			//内存结束位置
	ngx_buf_tag_t	tag;
	ngx_file_t		*file;			//对应的文件
	ngx_buf_t		*shadow;		
	
	unsigned		temporary:1;

	unsigned		memory:1;

	unsigned		mmap:1;

	unsigned		recycled:1;
	unsigned		in_file:1;
	unsigned		flush:1;
	unsigned		sync:1;
	unsigned		last_buf:1;
	unsigned		last_in_chain:1;

	unsigned		last_shadow:1;
	unsigned		temp_file:1;
};

相关操作API

#define ngx_alloc_buf(pool) ngx_palloc(pool, sizeof(ngx_buf_t))
#define ngx_calloc_buf(pool) ngx_pcalloc(pool, ngx_buf_t)

ngx_buf_t* ngx_create_temp_buf(ngx_pool_t* pool, size_t size);
/*创建一个ngx_buf_t对象,返回指向这个对象的指针
* pool			分配该buf和buf使用的内存所使用的pool
* size			该buf使用的内存的大小
*/
//以下宏配合bgx_buf_t使用

#define ngx_buf_in_memory(b)	(b->temporary || b-<memory ||b->mmap)
//返回buf是否有内容

#define ngx_buf_in_memory_only(b)	(ngx_buf_in_memory(b) && !b->infile)
//返回buf内容只在文件而不在文件中

#define ngx_buf_special(b)
	((b->flush || b->last_buf || b->sync)
	 && !ngx_buf_in_memory(b) && !b->in_file)
//返回这个buf是否是一个特殊的buf

#define ngx_buf_synconly(b)
	(b->sync && !ngx_buf_in_memory(b) && !b->in_file)
//返回该buf是否是一个只包含sync标志而不包含真正数据的特殊buf

#define ngx_buf_size(b)
	(ngx_buf_in_memory(b) ? (off_t)(b->last - b->pos):(b->file_last - b->file_pos))
//返回该buf所含数据的大小,不管数据在哪里

参考文献

[1] Nginx开发从入门到精通
 类似资料: