diff --git a/include/serial/impl/unix-timespec.h b/include/serial/impl/unix-timespec.h index 79c1d8c..dc642f6 100644 --- a/include/serial/impl/unix-timespec.h +++ b/include/serial/impl/unix-timespec.h @@ -38,20 +38,22 @@ /*! Smooth over platform variances in getting an accurate timespec * representing the present moment. */ -static inline void -get_time_now (struct timespec &time) +static inline struct timespec +get_time_now () { + struct timespec ts; # ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); - time.tv_sec = mts.tv_sec; - time.tv_nsec = mts.tv_nsec; + ts.tv_sec = mts.tv_sec; + ts.tv_nsec = mts.tv_nsec; # else - clock_gettime(CLOCK_REALTIME, &time); + clock_gettime(CLOCK_REALTIME, &ts); # endif + return ts; } /*! Simple function to normalize the tv_nsec field to [0..1e9), carrying diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 115f94f..9a502fb 100755 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -438,12 +438,11 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size) FD_ZERO (&readfds); FD_SET (fd_, &readfds); // Begin timing select - struct timespec start, end; - get_time_now (start); + struct timespec start(get_time_now()); // Call select to block for serial data or a timeout int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); // Calculate difference and update the structure - get_time_now (end); + struct timespec end(get_time_now()); // Calculate the time select took struct timespec diff(end - start); @@ -535,14 +534,13 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length) // does not occur. #if !defined(__linux__) // Begin timing select - struct timespec start, end; - get_time_now(start); + struct timespec start(get_time_now()); #endif // Do the select int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout); #if !defined(__linux__) // Calculate difference and update the structure - get_time_now(end); + struct timespec end(get_time_now()); // Calculate the time select took struct timespec diff(end - start); // Update the timeout