While improving the NetBSD support in lib/physmem.c, I noticed some old occurrences of ARRAY_SIZE. Since I need to use that or countof for another sysctl, I pushed the attached patch to change the existing calls of ARRAY_SIZE to countof.
Collin
>From 6cf1a20a992104a5e9948a9d449e2b5c41b63004 Mon Sep 17 00:00:00 2001 Message-ID: <6cf1a20a992104a5e9948a9d449e2b5c41b63004.1784170778.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Wed, 15 Jul 2026 19:57:39 -0700 Subject: [PATCH] physmem: Use countof. * modules/physmem (Depends-on): Add stdcountof-h. * lib/physmem.c: Include stdcountof.h. (ARRAY_SIZE): Remove macro. (physmem_total, physmem_claimable): Use countof instead of ARRAY_SIZE. --- ChangeLog | 6 ++++++ lib/physmem.c | 7 +++---- modules/physmem | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index df751bed71..8da6e48ef9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2026-07-15 Collin Funk <[email protected]> + physmem: Use countof. + * modules/physmem (Depends-on): Add stdcountof-h. + * lib/physmem.c: Include stdcountof.h. + (ARRAY_SIZE): Remove macro. + (physmem_total, physmem_claimable): Use countof instead of ARRAY_SIZE. + physmem: allow physmem_claimable to return more than 8 GiB on FreeBSD. Problem reported by Bruno Haible in: <https://lists.gnu.org/r/bug-gnulib/2026-07/msg00046.html>. diff --git a/lib/physmem.c b/lib/physmem.c index b8d5349415..41fb13ab5d 100644 --- a/lib/physmem.c +++ b/lib/physmem.c @@ -23,6 +23,7 @@ #include "physmem.h" #include <fcntl.h> +#include <stdcountof.h> #include <stdio.h> #include <unistd.h> @@ -83,8 +84,6 @@ typedef BOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*); #endif -#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) - /* Return the total amount of physical memory. */ double physmem_total (void) @@ -125,7 +124,7 @@ physmem_total (void) size_t len = sizeof physmem; static int mib[2] = { CTL_HW, HW_PHYSMEM }; - if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 + if (sysctl (mib, countof (mib), &physmem, &len, NULL, 0) == 0 && len == sizeof (physmem)) return (double) physmem; } @@ -304,7 +303,7 @@ physmem_claimable (double aggressivity) size_t len = sizeof usermem; static int mib[2] = { CTL_HW, HW_USERMEM }; - if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 + if (sysctl (mib, countof (mib), &usermem, &len, NULL, 0) == 0 && len == sizeof (usermem)) return (double) usermem; } diff --git a/modules/physmem b/modules/physmem index 0a9acb007d..352d353220 100644 --- a/modules/physmem +++ b/modules/physmem @@ -7,6 +7,7 @@ lib/physmem.c m4/physmem.m4 Depends-on: +stdcountof-h unistd-h full-read -- 2.55.0
