[nginx]第一篇

世界太大,我无法安心学习,决定看一个简单的。

nginx-1.11.9的代码是nginx-0.5.38的两倍,决定看前者的。

阅读工具:UnderStand 3.1。

入口在nginx.c的195行。

第一点:ngx_cdecl

int ngx_cdecl
main(int argc, char *const *argv)
#define ngx_cdecl

使用这个宏是为了跨平台支持,方便调整函数调用方式(__cdecl、__stdcall)

__cdecl:C Declaration的缩写,表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。被调用函数不会要求调用者传递多少参数,调用者传递过多或者过少的参数,甚至完全不同的参数都不会产生编译阶段的错误。 
调用函数的代码和被调函数必须采用相同的函数的调用约定,程序才能正常运行。 
__cdecl和__stdcall的区别:__cdecl是调用者清理参数占用的堆栈,__stdcall是被调函数清理参数占用的堆栈。假设函数A是__stdcall,函数B调用函数A。你必须通过函数声明告诉编译器,函数A是__stdcall。编译器自然会产生正确的调用代码。如果函数A是__stdcall,但在引用函数A的地方,你却告诉编译器,函数A是__cdecl方式,编译器产生__cdecl方式的代码,与函数A的调用约定不一致,就会发生错误。 
注意事项:由于__stdcall的被调函数在编译时就必须知道传入参数的准确数目(被调函数要清理堆栈),所以不能支持变参数函数,例如printf。而且如果调用者使用了不正确的参数数目,会导致堆栈错误。

http://blog.csdn.net/leehong2005/article/details/8607536

http://blog.csdn.net/wuchunlai_2012/article/details/50686295

 intptr_t不是指针类型,一个可以持有一个指针值的整型变量。

第二点:ngx_config.h 统一控制了跨平台的各类头文件的引入(下面是ngx_linux_config.h

#include <sys/types.h>  /** 基本系统数据类型32/64 **/
#include <sys/time.h>   /** 日期时间头文件 **/
#include <unistd.h>     /** POSIX系统调用如fork,pipe,IO原语read write close **/
#include <stdarg.h>     /** 可变参数 **/
#include <stddef.h>     /** offsetof() 获得字段在结构体中的偏移量 **/
#include <stdio.h>      /** 标准IO **/
#include <stdlib.h>     /** 五种类型、若干宏和工具类:随机、内存、数字转换 **/
#include <errno.h>      /** 错误码字典 **/
#include <string.h>     /** 字符串工具,strlen strcmp strcpy strncpy strcat strncat strchr strstr **/
#include <signal.h>     /** 信号处理,signal raise **/
#include <pwd.h>        /** 密码数据结构 **/
#include <grp.h>        /** linux权限group概念 **/
#include <dirent.h>     /** 目录操作 opendir readdir **/
#include <glob.h>       /** glob返回指定模式匹配的路径数组 **/

#include <sys/uio.h>    /** 原子操作下读取或写入多个缓冲区 **/
#include <sys/stat.h>   /** 获取文件属性,如长度 fstat stat **/
#include <fcntl.h>      /** fcntl改变文件性质 **/

#include <sys/wait.h>   /** 停止进程执行,直到信号来wait waitpid **/
#include <sys/mman.h>   /** 文件或对象映射到内存 mmap **/
#include <sys/resource.h> /** 进程优先级 get/setpriority **/
#include <sched.h>   /** 内核进程task_struct,CPU亲和 CPU_ZERO、CPU_ISSET **/

#include <sys/socket.h>   /** accept bind connect socket listen recv send... **/
#include <netinet/in.h>   /** IPv4 in_addr 地址族 sockaddr_in **/
#include <netinet/tcp.h>        /* TCP_NODELAY, TCP_CORK */
#include <arpa/inet.h>    /** IP转换inet_pton,主机数转换ntohl ntohs htonl htons **/
#include <netdb.h>        /** 网络有关 gethostbyaddr gethostbyname **/
#include <sys/un.h>       /** UNIX Domain Socket: sockaddr_un **/

#include <time.h>               /* 时区设置 tzset() */
#include <malloc.h>             /* 分配较大内存 memalign() */
#include <limits.h>             /* iovec的数组长度 IOV_MAX */
#include <sys/ioctl.h>          /** 影响IO的flag **/
#include <sys/sysctl.h>         /** 设置系统参数,文件系统、虚拟内存、网络 **/
#include <crypt.h>              /** 加密 **/
#include <sys/utsname.h>        /* 获取当前内核名称 uname() */


#include <ngx_auto_config.h>


#if (NGX_HAVE_SYS_PRCTL_H)
#include <sys/prctl.h>     /** 设置线程名字prctl(PR_SET_NAME, **/
#endif


#if (NGX_HAVE_SENDFILE64)
#include <sys/sendfile.h>  /** zero-copy **/
#else
extern ssize_t sendfile(int s, int fd, int32_t *offset, size_t size);
#define NGX_SENDFILE_LIMIT  0x80000000
#endif


#if (NGX_HAVE_POLL)
#include <poll.h>  /** poll **/
#endif


#if (NGX_HAVE_EPOLL)
#include <sys/epoll.h> /** epoll_create epoll_ctl epoll_wait **/
#endif
原文地址:https://www.cnblogs.com/hero4china/p/6367805.html