Maxim Dounin mdounin at
Tue Dec 3 12:38:13 UTC 2013


On Mon, Dec 02, 2013 at 03:44:53PM -0800, Dean Pucsek wrote:

> Hello,
> While reading through the source code for nginx I came across the following two lines in ngx_config.h 
> #define NGX_INT32_LEN   (sizeof("-2147483648") - 1)
> #define NGX_INT64_LEN   (sizeof("-9223372036854775808") - 1)
> I was wondering if someone could explain the intention of these 
> lines to me because it is not clear.  My understanding is that 
> using sizeof() on a string will return the number of characters 
> in that string.  Conversely, I get the feeling these lines are 
> supposed to somehow act as replacements for INT32_MAX and 
> INT64_MAX in stdint.h.

These macros are used as a maximum length of a string 
representation of the relevant types.  Note "_LEN", not "_MAX".

Relevant maximum value for int32_t is NGX_MAX_INT32_VALUE defined 
below in the same file.

> Looking at code where these #define’s are used doesn’t really 
> help clarify things either.  For example, in nginx.c there is:
>     var = ngx_alloc(sizeof(NGINX_VAR)
>                     + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
>                     cycle->log);
> The code clearly allocates memory, but it’s not clear why the 
> allocation is a multiple of NGX_INT32_LEN (or why we’re adding 
> 1, or 2 for that matter).
> Any direction would be much appreciated.

This code allocates memory for the "NGINX=x;y;z;\0" string, where 
"x", "y", "z" - are string representations of listening sockets.  
The "+ 1" is for ";" after each socket.  The "+ 2" is for "=" 
and trailing "\0" (well, it looks like a only "+ 1" is actually 
needed here, as sizeof(NGINX_VAR) already includes an extra byte).

Maxim Dounin

More information about the nginx-devel mailing list