From b9d8a5e512ac7e01233accf54746cd00636b627a Mon Sep 17 00:00:00 2001 From: Rich Neswold Date: Thu, 21 Oct 2021 01:55:27 -0500 Subject: [PATCH] Fix compiler errors/warnings for NetBSD/arm. (#102) * Fix compilation error for NetBSD/arm. The prototype for `get_cpu_speed()` had an incompatible return type as defined in `c/netbsd.c`. This makes it compatible. * Fix compilation warnings for NetBSD/arm. The scope of local variables was moved into the conditional sections to eliminate compilation warnings. * Add ARM section for NetBSD. `get_cpu_speed()` now has a section specific to NetBSD/arm targets. Previously it would default to 1 Ghz. --- c/netbsd.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/c/netbsd.c b/c/netbsd.c index 0dccdbd..b860e3b 100644 --- a/c/netbsd.c +++ b/c/netbsd.c @@ -41,20 +41,27 @@ const char *get_os_release(void) { return (os_release); } -uint64_t get_cpu_speed(void) { +unsigned long get_cpu_speed(void) { +#if defined(__i386__) || defined(__amd64__) uint64_t tsc_freq; size_t len; int error; -#if defined(__i386__) || defined(__amd64__) len = sizeof(tsc_freq); error = sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0); if (error == -1) return (0); + return (unsigned long) (tsc_freq / ONE_DECIMAL_K / ONE_DECIMAL_K); +#elif defined(__arm__) || defined(__aarch64__) + uint32_t tsc_freq; + size_t len = sizeof(tsc_freq); + int const result = sysctlbyname("machdep.cpu.frequency.current", + &tsc_freq, &len, NULL, 0); + + return result == -1 ? ONE_DECIMAL_K : tsc_freq; #else - tsc_freq = ONE_DECIMAL_K * ONE_DECIMAL_K * ONE_DECIMAL_K; + return ONE_DECIMAL_K; #endif - return (tsc_freq / ONE_DECIMAL_K / ONE_DECIMAL_K); } unsigned long get_proc_total(void) {