From c86fa7bb30f338c2df03ad5391fa06cddcf717f1 Mon Sep 17 00:00:00 2001 From: Palana Date: Mon, 14 Apr 2014 22:30:11 +0200 Subject: [PATCH] Use libc++ inspired clock instead of the deprecated AbsoluteToNanoseconds This also makes the clang static analyzer happy (it complained about uninitialized fields in the AbsoluteTime struct) --- libobs/util/platform-cocoa.m | 41 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index a70b19ed8..e5f99ca9f 100644 --- a/libobs/util/platform-cocoa.m +++ b/libobs/util/platform-cocoa.m @@ -87,14 +87,43 @@ void os_sleep_ms(uint32_t duration) usleep(duration*1000); } + +/* clock function selection taken from libc++ */ +static uint64_t ns_time_simple() +{ + return mach_absolute_time(); +} + +static double ns_time_compute_factor() +{ + mach_timebase_info_data_t info = {1, 1}; + mach_timebase_info(&info); + return ((double)info.numer) / info.denom; +} + +static uint64_t ns_time_full() +{ + static double factor = 0.; + if (factor == 0.) factor = ns_time_compute_factor(); + return (uint64_t)(mach_absolute_time() * factor); +} + +typedef uint64_t (*time_func)(); + +static time_func ns_time_select_func() +{ + mach_timebase_info_data_t info = {1, 1}; + mach_timebase_info(&info); + if (info.denom == info.numer) + return ns_time_simple; + return ns_time_full; +} + uint64_t os_gettime_ns(void) { - uint64_t t = mach_absolute_time(); -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - Nanoseconds nano = AbsoluteToNanoseconds(*(AbsoluteTime*) &t); -#pragma clang diagnostic pop - return *(uint64_t*) &nano; + static time_func f = NULL; + if (!f) f = ns_time_select_func(); + return f(); } /* gets the location ~/Library/Application Support/[name] */ -- GitLab