From 67ccd6b909d28ab84c77acecdfee927337489cc2 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 8 Dec 2020 23:12:44 -0500 Subject: Unhardcode tick-to-ms conversion Division by 100000.0 worked because `sysconf(_SC_CLK_TCK)` happened to be 100. By unhardcoding: 1) It becomes more clear what this 100000.0 figure comes from. 2) It protects against bugs in the case `sysconf(_SC_CLK_TCK)` ever changes. --- darwin/Platform.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'darwin/Platform.c') diff --git a/darwin/Platform.c b/darwin/Platform.c index a0ea6721..a4ed4647 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -10,16 +10,18 @@ in the source distribution for its full text. #include "Platform.h" +#include #include #include - -#include +#include #include +#include #include #include #include "ClockMeter.h" #include "CPUMeter.h" +#include "CRT.h" #include "DarwinProcessList.h" #include "DateMeter.h" #include "DateTimeMeter.h" @@ -112,6 +114,8 @@ const MeterClass* const Platform_meterTypes[] = { double Platform_timebaseToNS = 1.0; +long Platform_clockTicksPerSec = -1; + void Platform_init(void) { // Check if we can determine the timebase used on this system. // If the API is unavailable assume we get our timebase in nanoseconds. @@ -122,6 +126,14 @@ void Platform_init(void) { #else Platform_timebaseToNS = 1.0; #endif + + // Determine the number of clock ticks per second + errno = 0; + Platform_clockTicksPerSec = sysconf(_SC_CLK_TCK); + + if (errno || Platform_clockTicksPerSec < 1) { + CRT_fatalError("Unable to retrieve clock tick rate"); + } } void Platform_done(void) { -- cgit v1.2.3