buffer 在编程的时候经常用到,具体的概念就没啥好说的了。还是直接来看下ngx_buf
ngx_buf 是一个抽象的数据结构,他可以指向很多数据。比如某个内存块。比如某一个文件数据等。来具体看下他的数据结构的定义吧。
struct ngx_buf_s {
u_char *pos;//buf指向的数据在内存的时候,pos指这个buf数据开始的位置
u_char *last;//buf指向的数据在内存的时候,last指这个buf数据结束的位置
off_t file_pos;//当buf指向的数据在文件的时候,这个文件数据的开始偏移量
off_t file_last;//当buf指向的数据在文件的时候,这个文件数据的结束偏移量
u_char *start; /* start of buffer 意思是整个buffer 比如一个内存可能切割成多个buf,这个start是整个的开始地址 end就同理*/
u_char *end; /* end of buffer */
ngx_buf_tag_t tag; /*void* 啥都能放,只要你喜欢*/
ngx_file_t *file; //当buf是文件的时候,这个文件对象
ngx_buf_t *shadow;//自己的影子,就是当这个buf是另外一个buf的拷贝的时候,他们指向的同一个内存
/* the buf's content could be changed */
unsigned temporary:1;//临时内存 可以被修改
/*
* the buf's content is in a memory cache or in a read only memory
* and must not be changed
*/
unsigned memory:1;//不可以被修改的内存
/* the buf's content is mmap()ed and must not be changed */
unsigned mmap:1; // mmap的映射内存 不能修改
unsigned recycled:1; // 是否可以回收。
unsigned in_file:1; //是否在文件标致
unsigned flush:1; //刷出标志
unsigned sync:1;
unsigned last_buf:1;//最后一个buf
unsigned last_in_chain:1;//在chain的最后一个
unsigned last_shadow:1;//一般来说 最原始的那个buf 后面的bu从它拷贝
unsigned temp_file:1; //临时文件
/* STUB */ int num;
};
#define ngx_alloc_buf(pool) ngx_palloc(pool, sizeof(ngx_buf_t))
#define ngx_calloc_buf(pool) ngx_pcalloc(pool, sizeof(ngx_buf_t))
这两个宏,意思很明显了ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size);
创建一个可操作的内存buf/*
* set by ngx_calloc_buf():
*
* b->file_pos = 0;
* b->file_last = 0;
* b->file = NULL;
* b->shadow = NULL;
* b->tag = 0;
* and flags
*/
b->pos = b->start;
b->last = b->start;
b->end = b->last + size;
b->temporary = 1;
值的定义就是酱紫的。#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->in_file)
仅仅是内存buf,不是文件。
#define ngx_buf_size(b) \
(ngx_buf_in_memory(b) ? (off_t) (b->last - b->pos): \
(b->file_last - b->file_pos))
buf的大小。
#define ngx_buf_sync_only(b) \
(b->sync \
&& !ngx_buf_in_memory(b) && !b->in_file && !b->flush && !b->last_buf)
没有数据的buf 只是包含sync标志
#define ngx_buf_special(b) \
((b->flush || b->last_buf || b->sync) \
&& !ngx_buf_in_memory(b) && !b->in_file)