From d527bc9132c2adf94666dc5b9593609d357ea1e4 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Thu, 23 Sep 2021 19:39:13 -0400 Subject: Refactor Platform_CompareKernelVersion API --- darwin/DarwinProcessList.c | 2 +- darwin/PlatformHelpers.c | 41 +++++++++++++++++++++-------------------- darwin/PlatformHelpers.h | 15 ++++++++++----- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 1e865f59..bd7821b8 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -176,7 +176,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { } // Disabled for High Sierra due to bug in macOS High Sierra - bool isScanThreadSupported = !(Platform_CompareKernelVersion(17, 0, 0) >= 0 && Platform_CompareKernelVersion(17, 5, 0) < 0); + bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); if (isScanThreadSupported) { DarwinProcess_scanThreads(proc); diff --git a/darwin/PlatformHelpers.c b/darwin/PlatformHelpers.c index 9d4fe120..dc9879c5 100644 --- a/darwin/PlatformHelpers.c +++ b/darwin/PlatformHelpers.c @@ -20,43 +20,44 @@ in the source distribution for its full text. #endif -void Platform_GetKernelVersion(struct kern* k) { - static short int version_[3] = {0}; - if (!version_[0]) { +void Platform_GetKernelVersion(KernelVersion* k) { + static KernelVersion cachedKernelVersion; + + if (!cachedKernelVersion.major) { // just in case it fails someday - version_[0] = version_[1] = version_[2] = -1; + cachedKernelVersion = (KernelVersion) { -1, -1, -1 }; char str[256] = {0}; size_t size = sizeof(str); int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); if (ret == 0) { - sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]); + sscanf(str, "%hd.%hd.%hd", &cachedKernelVersion.major, &cachedKernelVersion.minor, &cachedKernelVersion.patch); } } - memcpy(k->version, version_, sizeof(version_)); + memcpy(k, &cachedKernelVersion, sizeof(cachedKernelVersion)); } -/* compare the given os version with the one installed returns: -0 if equals the installed version -positive value if less than the installed version -negative value if more than the installed version -*/ -int Platform_CompareKernelVersion(short int major, short int minor, short int component) { - struct kern k; - Platform_GetKernelVersion(&k); +int Platform_CompareKernelVersion(KernelVersion v) { + struct KernelVersion actualVersion; + Platform_GetKernelVersion(&actualVersion); - if (k.version[0] != major) { - return k.version[0] - major; + if (actualVersion.major != v.major) { + return actualVersion.major - v.major; } - if (k.version[1] != minor) { - return k.version[1] - minor; + if (actualVersion.minor != v.minor) { + return actualVersion.minor - v.minor; } - if (k.version[2] != component) { - return k.version[2] - component; + if (actualVersion.patch != v.patch) { + return actualVersion.patch - v.patch; } return 0; } +bool Platform_KernelVersionIsBetween(KernelVersion lowerBound, KernelVersion upperBound) { + return 0 <= Platform_CompareKernelVersion(lowerBound) + && Platform_CompareKernelVersion(upperBound) < 0; +} + double Platform_calculateNanosecondsPerMachTick() { // Check if we can determine the timebase used on this system. // If the API is unavailable assume we get our timebase in nanoseconds. diff --git a/darwin/PlatformHelpers.h b/darwin/PlatformHelpers.h index 25c5dcc0..f78ca4ea 100644 --- a/darwin/PlatformHelpers.h +++ b/darwin/PlatformHelpers.h @@ -11,18 +11,23 @@ in the source distribution for its full text. #include -struct kern { - short int version[3]; -}; +typedef struct KernelVersion { + short int major; + short int minor; + short int patch; +} KernelVersion; -void Platform_GetKernelVersion(struct kern* k); +void Platform_GetKernelVersion(KernelVersion* k); /* compare the given os version with the one installed returns: 0 if equals the installed version positive value if less than the installed version negative value if more than the installed version */ -int Platform_CompareKernelVersion(short int major, short int minor, short int component); +int Platform_CompareKernelVersion(KernelVersion v); + +// lowerBound <= currentVersion < upperBound +bool Platform_KernelVersionIsBetween(KernelVersion lowerBound, KernelVersion upperBound); double Platform_calculateNanosecondsPerMachTick(void); -- cgit v1.2.3