git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTL

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 2, 2025, 21:14 UTC
Message-ID
<xmqq5xgacn2w.fsf@gitster.g>
In-Reply-To
<20250702202118.48742-1-carenas@gmail.com>
Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
Show 9 quoted lines
> -	length = sizeof(int64_t);
> -	if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
> +	length = sizeof(physical_memory);
> +	if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) {
> +		if (length < sizeof(physical_memory)) {
> +			unsigned bits = (sizeof(physical_memory) - length) * 8;
> +
> +			physical_memory <<= bits;
> +			physical_memory >>= bits;

I do not quite understand this version. Does the correctness of this depend on the machine having a certain byte-order?

The system call treats &physical_memory as a mere blob of bytes, and may tell us that it filled only 4 bytes out of 8, but depending on the endianness, left shifting 4*8 bits first may discard the real information (i.e., big endian).

On a little endian 32-bit box, it might give us length == 4, filling the lower half of the i64, and shifting by 32-bits to the left and then shifting it down by 32-bits to the right may fill the upper half with 1 if the result in the 4-byte long is more than 2GB because the type of physical_memory is signed, and then we cast that value to u64. Which does not sound correct, either.

Would it make more sense to pass &u64 and return it only when length==8 as you did in v2 while removing the need to cast?

Show 5 quoted lines
> +		}
>  		return physical_memory;
> +	}
>  #elif defined(GIT_WINDOWS_NATIVE)
>  	MEMORYSTATUSEX memInfo;
Previous: Carlo Marcelo Arenas BelónNext: Carlo Marcelo Arenas Belón
Message 9 of 15 in “builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTL”
  1. builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 2, 2025
  2. Patrick SteinhardtJul 2, 2025
  3. Carlo Marcelo Arenas BelónJul 2, 2025
  4. builtin/gc: protect against sysctl() failure in total_ramCarlo Marcelo Arenas Belón, Jul 2, 2025
  5. Junio C HamanoJul 2, 2025
  6. Carlo Marcelo Arenas BelónJul 2, 2025
  7. Junio C HamanoJul 2, 2025
  8. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 2, 2025
  9. Junio C HamanoJul 2, 2025
  10. Carlo Marcelo Arenas BelónJul 2, 2025
  11. Junio C HamanoJul 2, 2025
  12. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 3, 2025
  13. Junio C HamanoJul 7, 2025
  14. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 7, 2025
  15. Junio C HamanoJul 7, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.