Chromium Code Reviews| Index: base/time/time_mac.cc |
| diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc |
| index e263d07945917f8acf2eb192c9b131c5366e5384..722cf745b958c2c0d183a771fb60d02a4f518c0d 100644 |
| --- a/base/time/time_mac.cc |
| +++ b/base/time/time_mac.cc |
| @@ -18,10 +18,11 @@ |
| #include "base/mac/mach_logging.h" |
| #include "base/mac/scoped_cftyperef.h" |
| #include "base/mac/scoped_mach_port.h" |
| +#include "base/numerics/safe_conversions.h" |
| namespace { |
| -uint64_t ComputeCurrentTicks() { |
| +int64 ComputeCurrentTicks() { |
|
dcheng
2015/05/21 18:49:04
Nit: we should be using int64_t from <stdint.h>, n
miu
2015/05/22 23:01:03
Done. Sounds like Chromium code throughout should
|
| #if defined(OS_IOS) |
| // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
| // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
| @@ -37,8 +38,6 @@ uint64_t ComputeCurrentTicks() { |
| base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
| return time_difference.InMicroseconds(); |
| #else |
| - uint64_t absolute_micro; |
| - |
| static mach_timebase_info_data_t timebase_info; |
| if (timebase_info.denom == 0) { |
| // Zero-initialization of statics guarantees that denom will be 0 before |
| @@ -56,18 +55,19 @@ uint64_t ComputeCurrentTicks() { |
| // timebase_info converts absolute time tick units into nanoseconds. Convert |
| // to microseconds up front to stave off overflows. |
| - absolute_micro = |
| - mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * |
| - timebase_info.numer / timebase_info.denom; |
| + base::CheckedNumeric<uint64> result( |
| + mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond); |
| + result *= timebase_info.numer; |
| + result /= timebase_info.denom; |
| // Don't bother with the rollover handling that the Windows version does. |
| // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| // reported in nanoseconds is enough to last nearly 585 years. |
| - return absolute_micro; |
| + return base::checked_cast<int64>(result.ValueOrDie()); |
| #endif // defined(OS_IOS) |
| } |
| -uint64_t ComputeThreadTicks() { |
| +int64 ComputeThreadTicks() { |
| #if defined(OS_IOS) |
| NOTREACHED(); |
| return 0; |
| @@ -88,9 +88,11 @@ uint64_t ComputeThreadTicks() { |
| &thread_info_count); |
| MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info"; |
| - return (thread_info_data.user_time.seconds * |
| - base::Time::kMicrosecondsPerSecond) + |
| - thread_info_data.user_time.microseconds; |
| + base::CheckedNumeric<int64> absolute_micros( |
| + thread_info_data.user_time.seconds); |
| + absolute_micros *= base::Time::kMicrosecondsPerSecond; |
| + absolute_micros += thread_info_data.user_time.microseconds; |
| + return absolute_micros.ValueOrDie(); |
| #endif // defined(OS_IOS) |
| } |
| @@ -224,13 +226,13 @@ bool TimeTicks::IsHighResolution() { |
| } |
| // static |
| -TimeTicks TimeTicks::ThreadNow() { |
| - return TimeTicks(ComputeThreadTicks()); |
| +ThreadTicks ThreadTicks::Now() { |
| + return ThreadTicks(ComputeThreadTicks()); |
| } |
| // static |
| -TimeTicks TimeTicks::NowFromSystemTraceTime() { |
| - return Now(); |
| +TraceTicks TraceTicks::Now() { |
| + return TraceTicks(ComputeCurrentTicks()); |
| } |
| } // namespace base |