1变量和常量.docx

上传人:b****7 文档编号:9519035 上传时间:2023-02-05 格式:DOCX 页数:15 大小:752.13KB
下载 相关 举报
1变量和常量.docx_第1页
第1页 / 共15页
1变量和常量.docx_第2页
第2页 / 共15页
1变量和常量.docx_第3页
第3页 / 共15页
1变量和常量.docx_第4页
第4页 / 共15页
1变量和常量.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

1变量和常量.docx

《1变量和常量.docx》由会员分享,可在线阅读,更多相关《1变量和常量.docx(15页珍藏版)》请在冰豆网上搜索。

1变量和常量.docx

1变量和常量

一、变量命名规则宏:

大写字母+下划线

变量名:

小写字母+下划线

后缀:

_s后缀:

结构体类型(只用于定义结构类型,但不用于定义变量),使用时通过typedef间接定义一个_t的类型

_t后缀:

结构体类型

_pt后缀:

函数指针类型

二、基础类型

1、linux系统类型:

/usr/include/bits/types.h:

/*Conveniencetypes.*/

typedefunsignedchar__u_char;

typedefunsignedshortint__u_short;

typedefunsignedint__u_int;

typedefunsignedlongint__u_long;

/*Fixed-sizetypes,underlyingtypesdependonwordsizeandcompiler.*/

typedefsignedchar__int8_t;

typedefunsignedchar__uint8_t;

typedefsignedshortint__int16_t;

typedefunsignedshortint__uint16_t;

typedefsignedint__int32_t;

typedefunsignedint__uint32_t;

#if__WORDSIZE==64

typedefsignedlongint__int64_t;

typedefunsignedlongint__uint64_t;

#elifdefined__GLIBC_HAVE_LONG_LONG

__extension__typedefsignedlonglongint__int64_t;

__extension__typedefunsignedlonglongint__uint64_t;

#endif

/usr/include/sys/types.h:

#ifdef__USE_BSD

#ifndef__u_char_defined

typedef__u_charu_char;

typedef__u_shortu_short;

typedef__u_intu_int;

typedef__u_longu_long;

typedef__quad_tquad_t;

#define__u_char_defined

#endif

#endif

/usr/include/stdint.h:

#ifndef__int8_t_defined

#define__int8_t_defined

typedefsignedcharint8_t;

typedefshortintint16_t;

typedefintint32_t;

#if__WORDSIZE==64

typedeflongintint64_t;

#else

__extension__

typedeflonglongintint64_t;

#endif

#endif

/*Unsigned.*/

typedefunsignedcharuint8_t;

typedefunsignedshortintuint16_t;

#ifndef__uint32_t_defined

typedefunsignedintuint32_t;

#define__uint32_t_defined

#endif

#if__WORDSIZE==64

typedefunsignedlongintuint64_t;

#else

__extension__

typedefunsignedlonglongintuint64_t;

#endif

/*Typesfor`void*'pointers.*/

#if__WORDSIZE==64

#ifndef__intptr_t_defined

typedeflongintintptr_t;

#define__intptr_t_defined

#endif

typedefunsignedlongintuintptr_t;

#else

#ifndef__intptr_t_defined

typedefintintptr_t;

#define__intptr_t_defined

#endif

typedefunsignedintuintptr_t;

#endif

2、nginx自定义基础类型src/core/ngx_config.h/*不建议使用,和机器字长有关*/typedefintptr_tngx_int_t;

typedefuintptr_tngx_uint_t;

typedefintptr_tngx_flag_t;

src/os/unix/ngx_errno.h

typedefintngx_err_t;

src/core/ngx_string.h

typedefstruct{

size_tlen;

u_char*data;

}ngx_str_t;

typedefstruct{

ngx_str_tkey;

ngx_str_tvalue;

}ngx_keyval_t;

src/core/ngx_array.h

typedefstructngx_array_sngx_array_t;

structngx_array_s{

void*elts;

ngx_uint_tnelts;

size_tsize;

ngx_uint_tnalloc;

ngx_pool_t*pool;

};

src/core/ngx_queue.h

typedefstructngx_queue_sngx_queue_t;

structngx_queue_s{

ngx_queue_t*prev;

ngx_queue_t*next;

};

src/core/ngx_list.h

typedefstructngx_list_part_sngx_list_part_t;

structngx_list_part_s{/*链表节点结构体*/

void*elts;/*指向该节点实际的数据区(该数据区可以存放nalloc个大小为size的元素)*/

ngx_uint_tnelts;/*实际存放的元素个数*/

ngx_list_part_t*next;/*指向下一个元素*/

};

typedefstruct{/*链表头结构*/

ngx_list_part_t*last;/*指向链表最后一个节点*/

ngx_list_part_tpart;/*链表头中包含的第一个节点*/

size_tsize;/*链表中每个元素的大小*/

ngx_uint_tnalloc;/*链表可以容纳的元素个数*/

ngx_pool_t*pool;/*该链表节点空间在此内存池中分配*/

}ngx_list_t;

src/core/ngx_log.h

在创建很多应用程结构体或执行函数时,通常都至少会包含一个向下传递的日志指针,用于日志输出。

typedefstructngx_log_sngx_log_t;

structngx_log_s{

ngx_uint_tlog_level;

ngx_open_file_t*file;

ngx_atomic_uint_tconnection;

ngx_log_handler_pthandler;

void*data;

char*action;

};

src/http/ngx_http_conf.h

typedefstruct{

void**main_conf;

void**srv_conf;

void**loc_conf;

}ngx_http_conf_ctx_t;

src/core/ngx_conf_file.h

typedefstructngx_conf_sngx_conf_t;

structngx_conf_s{

char*name;

ngx_array_t*args;

ngx_cycle_t*cycle;

ngx_pool_t*pool;

ngx_pool_t*temp_pool;

ngx_conf_file_t*conf_file;

ngx_log_t*log;

void*ctx;

ngx_uint_tmodule_type;

ngx_uint_tcmd_type;

ngx_conf_handler_pthandler;

char*handler_conf;

};

typedefstruct{

ngx_str_tname;/*模块名称*/

void*(*create_conf)(ngx_cycle_t*cycle);/*创建配置文件,在ngx_init_cycle等函数中调用*/

char*(*init_conf)(ngx_cycle_t*cycle,void*conf);/*初始化配置文件,在ngx_init_cycle等函数中调用*/

}ngx_core_module_t;

typedefstructngx_module_sngx_module_t;

structngx_module_s{

ngx_uint_tctx_index;/*模块,在自身模块类型(http|event等)对应的全局配置结构中数组的下标*/

/*e.g.如果该模块是httpmodule,则ctx_index是该模块的config信息(main、srv、loc)在ngx_http_conf_ctx_t中的下标*/

ngx_uint_tindex;

/*nginx把所有模块(ngx_module_t)存放到ngx_modules数组中,index属性就是该模块在ngx_modules数组中的下标。

同时nginx把所有的coremodule的配置结构存放到ngx_cycle的conf_ctx数组中,index也是该模块的配置结构在ngx_cycle->conf_ctx数组中的下标*/

ngx_uint_tspare0;

ngx_uint_tspare1;

ngx_uint_tspare2;

ngx_uint_tspare3;

ngx_uint_tversion;

void*ctx;

/*模块的上下文属性,同一类型的模块的属性是相同的,比如coremodule的ctx是ngx_core_module_t类型,而httpmodule的ctx是ngx_http_moduel_t类型,eventmodule的ctx是ngx_event_module_t类型等等。

相应类型的模块是分开处理的,比如所有的httpmodule由ngx_http_module解析处理,而所有的eventmodule由ngx_events_module解析处理*/

ngx_command_t*commands;/*该模块支持的指令的数组,最后以一个空指令结尾*/

ngx_uint_ttype;/*模块的类型,nginx所有的模块类型:

NGX_CORE_MODULE、NGX_CONF_MODULE、NGX_HTTP_MODULE、NGX_EVENT_MODULE、NGX_MAIL_MODULE*/

ngx_int_t(*init_master)(ngx_log_t*log);/*在nginx初始化过程的特定时间点调用*/

ngx_int_t(*init_module)(ngx_cycle_t*cycle);/*初始化完所有模块后调用,在ngx_int_cycle函数中*/

ngx_int_t(*init_process)(ngx_cycle_t*cycle);/*初始化完worker进程后调用,在ngx_worker_process_init函数中*/

ngx_int_t(*init_thread)(ngx_cycle_t*cycle);

void(*exit_thread)(ngx_cycle_t*cycle);

void(*exit_process)(ngx_cycle_t*cycle);

void(*exit_master)(ngx_cycle_t*cycle);

uintptr_tspare_hook0;

uintptr_tspare_hook1;

uintptr_tspare_hook2;

uintptr_tspare_hook3;

uintptr_tspare_hook4;

uintptr_tspare_hook5;

uintptr_tspare_hook6;

uintptr_tspare_hook7;

};

下面的表格是type与ctx的对应关系:

模块类型(type)

上下文属性类型(ctx)

NGX_CORE_MODULE

ngx_core_module_t(src/core/ngx_conf_file.h)

NGX_CONF_MODULE

NULL

NGX_HTTP_MODULE

ngx_http_module_t(src/http/ngx_http_config.h)

NGX_EVENT_MODULE

ngx_event_module_t(src/event/ngx_event.h)

NGX_MAIL_MODULE

ngx_mail_module_t(src/mail/ngx_mail.h)

typedefstructngx_command_sngx_command_t;

/*ngx_command_t描述的是模块的配置指令,也就是出现在配置文件的指令。

nginx模块支持多个配置指令,所以是以ngx_commond_t数组形式存储的。

这个结构在配置文件解析和模块的配置结构信息初始化时会用到*/

structngx_command_s{

ngx_str_tname;/*指令名,与配置文件中一致*/

ngx_uint_ttype;/*指令的类型(在那类模块执行、在哪些配置区域可用),以及参数的个数:

多个常量或的结果*/

char*(*set)(ngx_conf_t*cf,ngx_command_t*cmd,void*conf);

/*cmd:

指令自身;conf:

指令对应的配置和状态结构体;cf包括配置参数信息cf->args(ngx_array_t类型),以及指令对应的模块上下文cf->ctx,在解析不同模块的指令时,这个上下文信息不同。

比如在解析httpmodule时cf->ctx是ngx_http_conf_ctx_t类型的,其中包含所有httpmodule的main、srv、loc,在解析httpmodule时cf->ctx是ngx_cycle->conf_ctx类型*/

ngx_uint_tconf;

/*对httpmodule有效,httpmodule的配置结构信息(main、srv、loc)都存放在ngx_http_conf_ctx_t*中对应的数组,conf属性指示这个指令的配置结构是main、srv还是loc*/

ngx_uint_toffset;/*指令对应属性在模块配置结构中的偏移量*/

void*post;/*一般是函数指针,在set回调函数中调用*/

};

typedefstructngx_open_file_sngx_open_file_t;

structngx_open_file_s{

ngx_fd_tfd;

ngx_str_tname;

u_char*buffer;

u_char*pos;

u_char*last;

};

src/core/ngx_cycle.h

ngx_cycle_t是nginx中最重要的数据结构,包含了全局的配置信息、所有监听的套接字、连接池、读写事件等。

ngx_cycle_t相当于nginx的一个生命周期,从nginx启动后直到向nginx发送stop或者reload信号。

nginx中有一个全局变量ngx_cycle指向当前的cycle。

typedefstructngx_cycle_sngx_cycle_t;

structngx_cycle_s{

void****conf_ctx;/*配置上下文数组(含所有模块)*/

/*所有coremodule的配置结构信息的数组,大小是ngx_max_module。

配置结构信息由ngx_core_module_t的create_config回调函数生成,并由init_config回调函数初始化。

module的index属性指示了该模块的配置结构信息在conf_ctx数组中的下标。

*/

ngx_pool_t*pool;/*内存池:

随ngx_cycle_t同生命周期的内存池,是生命周期最长的,除非nginx关闭或重启。

内存块大小由NGX_CYCLE_POOL_SIZE定义,是16KB*/

ngx_log_t*log;/*日志*/

ngx_log_tnew_log;

ngx_connection_t**files;/*连接文件*/

ngx_connection_t*free_connections;/*空闲连接数组*/

ngx_uint_tfree_connection_n;/*空闲连接个数*/

ngx_queue_treusable_connections_queue;/*可重用的连接队列*/

ngx_array_tlistening;/*监听套接字的数组,类型是ngx_listening_t,ngx_create_listening函数会向这个数组添加新的监听句柄*/

ngx_array_tpathes;/*路径数组ngx_path_t*/

ngx_list_topen_files;/*所有打开文件的描述符的链表,类型ngx_open_file_t,ngx_conf_open_file()在打开文件时,如果文件不在open_files中,则将打开的文件添加到open_files中*/

ngx_list_tshared_memory;/*共享内存链表,类型ngx_shm_zone_t,、nginx会把需要进程共享的数据放在共享内存中,比如,accept锁就放在这里。

worker进程只有获取这个锁之后,才能accept到新的连接,防止惊群*/

ngx_uint_tconnection_n;/*连接个数*/

ngx_uint_tfiles_n;/*打开文件个数*/

ngx_connection_t*connections;/*连接池,nginx把连接池组织成综合数组和链表特性的一种数据结构*/

ngx_event_t*read_events;/*读事件*/

ngx_event_t*write_events;/*写事件*/

ngx_cycle_t*old_cycle;/*旧的cycle指针*/

ngx_str_tconf_file;/*配置文件名*/

ngx_str_tconf_param;/*配置参数,由-g提供的配置参数*/

ngx_str_tconf_prefix;/*配置文件路径前缀*/

ngx_str_tprefix;/*前缀(nginx所在路径)*/

ngx_str_tlock_file;/*锁文件*/

ngx_str_thostname;/*主机名*/

};

src/event/event.h

typedefstruct{

ngx_uint_tconnections;/*最大连接数限制*/

ngx_uint_tuse;/**/

ngx_flag_tmulti_accept;

ngx_flag_taccept_mutex;

ngx_msec_taccept_mutex_delay;

u_char*name;/*event模块名字*/

}ngx_event_conf_t;

三、常量

1、系统错误码

src/core/ngx_core.h

#defineNGX_OK0

#defineNGX_ERROR-1

#defineNGX_AGAIN-2

#defineNGX_BUSY-3

#defineNGX_DONE-4

/*请求需要被发给本阶段的下一个处理器(handler)*/

#defineNGX_DECLINED-5

#defineNGX_ABORT-6

objs/ngx_auto_config.h(由configure生成)

#ifndefNGX_SYS_NERR

/*linux系统错误码个数*/

#defineNGX_SYS_NERR135

#endif

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1