Skip to content

Commit 781c1cf

Browse files
carenasgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35c1d59 commit 781c1cf

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

builtin/gc.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ static uint64_t total_ram(void)
517517
return total;
518518
}
519519
#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
520-
int64_t physical_memory;
520+
uint64_t physical_memory;
521521
int mib[2];
522522
size_t length;
523523

@@ -529,9 +529,16 @@ static uint64_t total_ram(void)
529529
# else
530530
mib[1] = HW_PHYSMEM;
531531
# 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+
}
534540
return physical_memory;
541+
}
535542
#elif defined(GIT_WINDOWS_NATIVE)
536543
MEMORYSTATUSEX memInfo;
537544

0 commit comments

Comments
 (0)