Nginx source code first read (1) -- Let's start with main.

Source: Internet
Author: User
: This article mainly introduces the nginx source code first read (1) -- Let's start with main. if you are interested in PHP tutorials, refer to it. Throwing away all modules and various defined data structures is too messy for a guy who hasn't seen such a big project! Don't be confused. just the meaning of various data structures makes me crazy. Well, I am not saying it has a bad structure. On the contrary, I feel that the code is very good .. Is a time .. No. Don't talk nonsense. let's get started.

First: ngx_cdecl

int ngx_cdecl main(int argc, char *const *argv);

Yes, it's this ngx_cdecl. Before that, we know that there are _ cdecl and _ stdcall in the source code. what is ngx_cdecl here? it should be said that it is the same, however, the definition is as follows:

#define ngx_cdecl

Yes, it is an empty define. what is the use of define? Of course, it is useful and well-used. how can we say that people are good at code? How good is the future. This macro is used in nginx for cross-platform support and convenient adjustment of function call methods. When a problem occurs, you can modify the definition above:

#define ngx_cdecl stdcal

It is helpful to read more code. this is the idea. Now let's explain cdecl and stdcall:
_ Cdecl: C Declaration refers to the default function call method of C language. all parameters are pushed to the stack from right to left. These parameters are cleared by the caller, which is called manual stack clearing. The called function does not require the caller to pass many parameters. too many or too few parameters are passed by the caller, and even different parameters do not produce compilation errors.
The code for calling a function and the called function must use the same function call Convention to run the program normally.
_ Cdecl and _ stdcall are different: __cdecl is the stack occupied by the caller to clean up the parameter, __stdcall is the stack occupied by the parameter by the called function to clean up the parameter. Assume that function A is _ stdcall, and function B calls function. You must use the function declaration to tell the compiler that function A is _ stdcall. The compiler will naturally generate the correct call code. If function A is _ stdcall, but where function A is referenced, you tell the compiler that function A is in the _ cdecl mode and the compiler generates code in the _ cdecl mode, an error occurs if it is inconsistent with the call Convention of function.
Note: Since the called function of _ stdcall must know the exact number of input parameters during compilation (the called function must clear the stack), parameter changing functions are not supported, for example, printf. In addition, if the caller uses an incorrect number of parameters, a stack error occurs.

Second, ngx_int_t & ngx_uint_t

typedef intptr_t ngx_int_t;typedef uintptr_t ngx_uint_t;

Find the intptr definition in stdint. h:

117/* Types for `void *' pointers.  */118#if __WORDSIZE == 64  119# ifndef __intptr_t_defined  120 typedef longint        intptr_t;      121#  define __intptr_t_defined  122# endif123 typedef unsigned longint   uintptr_t;      124#else125# ifndef __intptr_t_defined  126 typedef int         intptr_t;      127#  define __intptr_t_defined  128# endif129 typedef unsigned int        uintptr_t;      130#endif

The annotations in the definition indicate that these two types are used as pointers, because the length of the pointer is the same as that of the long integer, because the pointer is an array index, when operating on the memory, the system kernel regards the memory as a large array, and the pointer is the array index/subscript, kernel programmers use this special integer to accept the memory address value and operate the memory, which is more intuitive than using pointers and is not prone to mistakes.
Intptr_t can be safely converted between void * and integer. it is very important to write programs across 64-bit platforms. That is to say, when you need to calculate the pointer as an integer, it is safe to convert it into intptr_t. you can safely convert it back to the pointer type after computation, it also avoids the bug of pointer resolution reference.

The macro in the code shows that the length of intptr_t is applicable to different platforms. when the compiling environment is 64-bit, intptr_t is a long int; otherwise, intptr_t is an int.

So what does nginx want to use it for typedef to output ngx_int_t? Why not use int?
From the type name, it is easy to understand as a common int type. I think nginx uses it because intptr adapts to the platform when defining it and changes its length according to the platform, the author does not need to define it again.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces the nginx source code first read (1) -- Let's start with main, including some content, hope to be helpful to friends who are interested in PHP tutorials.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.