Welcome! Log In Create A New Profile

Advanced

Re: NGX_INT32_LEN and NGX_INT64_LEN

Dean Pucsek
December 03, 2013 07:46PM
On December 3, 2013 at 4:38:18 AM, Maxim Dounin (mdounin@mdounin.ru) wrote:


Hello!

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.
Noted, sounds like this may be a case of not fully reading ngx_config.h on my part.


> 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).
I had a suspicion that it was something like that but I didn’t quite get the pieces together.  



Thanks for the clarification.



-- 
Maxim Dounin
http://nginx.org/en/donation.html

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

NGX_INT32_LEN and NGX_INT64_LEN

Dean Pucsek 1126 December 02, 2013 06:46PM

Re: NGX_INT32_LEN and NGX_INT64_LEN

Igor Sysoev 692 December 03, 2013 02:48AM

Re: NGX_INT32_LEN and NGX_INT64_LEN

Maxim Dounin 359 December 03, 2013 07:40AM

Re: NGX_INT32_LEN and NGX_INT64_LEN

Dean Pucsek 426 December 03, 2013 07:46PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 184
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready