builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTL
The calls to sysctl() assume a 64-bit memory size for the variable holding the value, but the actual size depends on the key name and platform, at least for HW_PHYSMEM. Detect any mismatched reads, and retry with a shorter variable when needed. Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Carlo Marcelo Arenas Belón committed
Jul 7, 2025 at 09:45 UTC
781c1cf5712f1768278f7f926f39ebad3be4aae0
1 file changed
+10
-3
builtin/gc.c
+10
-3
@@ -517,7 +517,7 @@ static uint64_t total_ram(void)
517
return total;
518
}
519
#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
520
- int64_t physical_memory;
520
+ uint64_t physical_memory;
521
int mib[2];
522
size_t length;
523
@@ -529,9 +529,16 @@ static uint64_t total_ram(void)
529
# else
530
mib[1] = HW_PHYSMEM;
531
# endif
532
- length = sizeof(int64_t);
533
- if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
532
+ length = sizeof(physical_memory);
533
+ if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) {
534
+ if (length == 4) {
535
+ uint32_t mem;
536
+
537
+ if (!sysctl(mib, 2, &mem, &length, NULL, 0))
538
+ physical_memory = mem;
539
+ }
540
return physical_memory;
541
+ }
542
#elif defined(GIT_WINDOWS_NATIVE)
543
MEMORYSTATUSEX memInfo;
544