nginx函数的定义如下:
int ngx_cdecl
main(int argc, char *const *argv)
{
ngx_int_t i;
ngx_log_t *log;
ngx_cycle_t *cycle, init_cycle;
ngx_core_conf_t *ccf;
ngx_debug_init();
if (ngx_strerror_init() != NGX_OK)
{
return 1;
}
if (ngx_get_options(argc, argv) != NGX_OK) {
return 1;
}
...
}
一返回值和调用类型:
首先要说,main函数的返回值是int型的,其调用方式是 ngx_cdecl,也就是C的默认调用方式,是被调用者清除堆栈。
二简单的数据类型解读:
ngx_int_t的定义是
typedef intptr_t ngx_int_t;
而 intptr_t则是 跨平台的始终与地址位数相同,也就是
typedef long int intptr_t;
ngx_log_t的定义是
struct ngx_log_s{
ngx_uint_t log_level;//日志等级
ngx_open_file_t *file;//日志文件名称还是描述符?
ngx_atomic_uint_t connection;
ngx_log_handler_pt handler;//日志文件的操作函数指针
void *data;//用户操作对应的参数?
char* action;//用户进行的操作
}
ngx_uint_t 的定义是 uintptr_t 类似intptr_t是 unsigned long int类型的, log_level表示的是日志等级,比如警告,错误等等
ngx_open_file_t的定义是
struct ngx_open_file_s{
ngx_fd_t fd;//文件描述符
ngx_str_t name;//文件名称
void (*flush)(ngx_open_file_t *file,ngx_log_t*log);//文件操作函数指针
void *data;//要写入的文件的数据缓冲区
}
这里面包含四个数据,fd自然就是文件描述符了,name也就是在文件系统中的文件名称,flush则就是函数指针,是不是写入日志的函数指针?data就是要写入的数据?
这里面的ngx_fd_t的定义就是 int类型的,也就是文件描述符
ngx_str_t的定义是
struct ngx_str_t{
size_t len;//数据长度
u_char*data; //数据缓冲区
}
里面包含了,字符串的内容(data)和长度(len),这样ngx_open_file_s的数据类型就解释完了,接下来继续解释ngx_log_s,
ngx_atomic_uint_t的定义是 typedef AO_t ngx_atomic_uint_t,但是笔者并不清楚AO_t的含义是什么..
现在说下ngx_log_handler_pt函数指针的定义
typedef u_char*(*ngx_log_handler_pt)(ngx_log_t*log,u_char*buf,size_t len);
向ngx_log_t结构体中记录的文件描述符,写入数据buf,还有就是数据的长度。
接下来说下ngx_cycle_t的数据类型
struct ngx_cycle_s{
void****conf_ctx;
ngx_pool_t *pool;
ngx_log_t *log;
ngx_log_t new_log;
ngx_uint_t log_user_stderr;
ngx_connection_t ** file;
ngx_connection_t *free_connections
ngx_uint_t frss_connection_n;
ngx_queue_t reusable_connections_queue;
ngx_array_t listening;
ngx_array_t paths;
ngx_list_t open_files;
ngx_list_t shared_memory;
ngx_uint_t connection_n;
ngx_uint_t files_n;
ngx_connection_t *connections;
ngx_event_t *read_events;
ngx_event_t *write_events;
ngx_cycle_t *old_cycle;
ngx_str_t conf_file;
ngx_str_t conf_param;
ngx_str_t conf_prefix;
ngx_str_t prefix;
ngx_str_t lock_file;
ngx_str_t hostname;
}
这个结构体好复杂的说,不过,功夫不怕有心人不是,然后咱们慢慢一个一个的看看
首先看看ngx_pool_s也就是ngx_pool_t的定义
struct ngx_pool_s{
ngx_pool_data_t d;
size_t max;
ngx_pool_t *current;
ngx_chain_t *chain;
ngx_pool_large_t *large;
ngx_pool_cleanup_t *cleanup;
ngx_log_t *log;
}
ngx_pool_data_t的定义是:
struct ngx_pool_data_t{
u_char*last;//这里应该用的是一个内存池,last指的是内存池中该数据的起始地址
u_char* end;//end指的是该pool中数据的结束位置
ngx_pool_t* next;//下一个内存池的指针
ngx_uint_t failed;//是否可使用的标志?
};
ngx_chain_t的定义是 ngx_chain_s
struct ngx_chain_s{
ngx_buf_t* buf;//指向当前的buf缓冲区,如果是最后一个next是NULL
ngx_chain_t*next;
};
ngx_buf_t的定义是
struct ngx_buf_t{
u_char* pos;//处理内存告诉使用者,本次处理应该从pos位置开始处理数据
u_char *last;//处理内存表示该buf的 有效数据到last为止
off_t file_pos;//处理文件时应该从该位置开始
off_t file_last;//处理文件时应该到此结束
u_char *start;//buf的起始位置,
u_char *end;//buf的结束位置
ngx_buf_tag_t tag;//表示当前缓冲区的类型,那个模块使用,tag指向该模块的ngx_module_t变量地址
ngx_file_t *file;//引用的文件
ngx_buf_t*shadow;//该缓冲区的影子缓冲区,也就是多个ngx_buf_t可能指向的是同一块内存,shadow指向的是多个指针,不怎么使用
unsigned temporary:1;//为1时temporary表示是整形变量,后面的1表示只占据1位,数据在内存中可以修改
unsigned memory:1;//为1时表示数据不可修改
unsigned mmap:1;//为1时表示内存使用mmap系统调用映射过来的,不可修改
unsigned recycled:1;//为1时表示,可以回收
unsigned in_file:1;//为1时标书这段缓冲区处理的是文件而不是内存
unsigned flush:1;//为1时表示需要进行flush操作
unsigned sync:1;//为1表示是否使用同步方式,谨慎使用
unsigned last_buf:1//是否是最后一块缓冲区,因为ngx_buf_t可以用ngx_chain_t串联起来
unsigned last_in_chain:1;//是否是ngx_chain_t中的最后一块
unsigned last_shadow:1;
unsigned temp_fule:1//是否是临时文件
int num;
}
ngx_buf_t里面好多变量,但是现在还不知道具体是做什么的,于是乎,我们只能,开始进行下一部分了,继续探索。
ngx_pool_large_t的定义是
struct ngx_pool_large_t{
ngx_pool_large_t *next;
void* alloc;//这里面是重新开辟的空间??
}
ngx_pool_cleanup_t的定义是
struct ngx_pool_cleanup_t{
ngx_pool_cleanup_pt handler;
void *data;//这里也是数据,除了有一个函数指针跟ngx_pool_large_t一样
ngx_pool_clean_cleanup_t *next;
}
ngx_pool_cleanup_pt的定义是
typedef void(*ngx_pool_cleanup_pt)(void*data);是不是handler的参数就是结构体里面的data?很有可能