Refactor Platform_CompareKernelVersion API

This commit is contained in:
Alexander Momchilov 2021-09-23 19:39:13 -04:00
parent dadcb87ad0
commit d527bc9132
3 changed files with 32 additions and 26 deletions

View File

@ -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);

View File

@ -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.

View File

@ -11,18 +11,23 @@ in the source distribution for its full text.
#include <sys/types.h>
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);