From c62c2e4661ab3570bd30418977fc2b4f66ba01bd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 11 Sep 2015 10:20:46 -0700 Subject: [PATCH] macos: avoid allocation when getting time --- src/os.cpp | 9 ++++++--- test/unit_tests.cpp | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 62dca77..742e747 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -127,6 +127,9 @@ static SYSTEM_INFO win32_system_info; #else static atomic_bool initialized = ATOMIC_VAR_INIT(false); static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; +#if defined(__MACH__) +static clock_serv_t cclock; +#endif #endif static int page_size; @@ -137,13 +140,10 @@ double soundio_os_get_time(void) { QueryPerformanceCounter((LARGE_INTEGER*) &time); return time * win32_time_resolution; #elif defined(__MACH__) - clock_serv_t cclock; mach_timespec_t mts; - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); kern_return_t err = clock_get_time(cclock, &mts); assert(!err); - mach_port_deallocate(mach_task_self(), cclock); double seconds = (double)mts.tv_sec; seconds += ((double)mts.tv_nsec) / 1000000000.0; @@ -561,6 +561,9 @@ static int internal_init(void) { page_size = win32_system_info.dwAllocationGranularity; #else page_size = getpagesize(); +#if defined(__MACH__) + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); +#endif #endif return 0; } diff --git a/test/unit_tests.cpp b/test/unit_tests.cpp index b9135ab..ed6b921 100644 --- a/test/unit_tests.cpp +++ b/test/unit_tests.cpp @@ -15,6 +15,7 @@ static inline void ok_or_panic(int err) { } static void test_os_get_time(void) { + soundio_os_init(); double prev_time = soundio_os_get_time(); for (int i = 0; i < 1000; i += 1) { double time = soundio_os_get_time();