diff options
author | gavinp@chromium.org <gavinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-26 17:57:40 +0000 |
---|---|---|
committer | gavinp@chromium.org <gavinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-26 17:57:40 +0000 |
commit | 37a78c32eaf3b6c35c944373505bed37afe16b45 (patch) | |
tree | fc0d367eab34e2032f43294204e8a208b6ee4f2e | |
parent | 69de8e069b343f0c1a6c206e09ab0b028a517b5e (diff) | |
download | chromium_src-37a78c32eaf3b6c35c944373505bed37afe16b45.zip chromium_src-37a78c32eaf3b6c35c944373505bed37afe16b45.tar.gz chromium_src-37a78c32eaf3b6c35c944373505bed37afe16b45.tar.bz2 |
Add base::TimeDelta::Max().
TimeDelta::Max() is a maximum timedelta, larger than any timedelta you
can compare it to. The results of adding it to a time, or another time
delta are undefined.
R=ajwong@chromium.org,mkwst@chromium.org,jar@chromium.org
TBR=jamesr@chomium.org,acolwell@chromium.org,nick@chromium.org
BUG=None
Review URL: https://codereview.chromium.org/163413004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253502 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/time/time.cc | 41 | ||||
-rw-r--r-- | base/time/time.h | 40 | ||||
-rw-r--r-- | base/time/time_unittest.cc | 40 | ||||
-rw-r--r-- | cc/debug/rasterize_and_record_benchmark.cc | 3 | ||||
-rw-r--r-- | cc/debug/rasterize_and_record_benchmark_impl.cc | 3 | ||||
-rw-r--r-- | cc/resources/picture_pile.cc | 3 | ||||
-rw-r--r-- | cc/resources/picture_pile_impl.cc | 3 | ||||
-rw-r--r-- | content/renderer/media/websourcebuffer_impl.cc | 2 | ||||
-rw-r--r-- | media/base/buffers.h | 2 | ||||
-rw-r--r-- | media/filters/chunk_demuxer.cc | 1 | ||||
-rw-r--r-- | net/http/http_response_headers.cc | 2 | ||||
-rw-r--r-- | net/spdy/spdy_session.cc | 4 | ||||
-rw-r--r-- | sync/sessions/nudge_tracker.cc | 6 |
13 files changed, 127 insertions, 23 deletions
diff --git a/base/time/time.cc b/base/time/time.cc index 4605d56..2c63886 100644 --- a/base/time/time.cc +++ b/base/time/time.cc @@ -17,40 +17,81 @@ namespace base { // TimeDelta ------------------------------------------------------------------ +// static +TimeDelta TimeDelta::Max() { + return TimeDelta(std::numeric_limits<int64>::max()); +} + int TimeDelta::InDays() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int>::max(); + } return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); } int TimeDelta::InHours() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int>::max(); + } return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); } int TimeDelta::InMinutes() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int>::max(); + } return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); } double TimeDelta::InSecondsF() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<double>::infinity(); + } return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; } int64 TimeDelta::InSeconds() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int64>::max(); + } return delta_ / Time::kMicrosecondsPerSecond; } double TimeDelta::InMillisecondsF() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<double>::infinity(); + } return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; } int64 TimeDelta::InMilliseconds() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int64>::max(); + } return delta_ / Time::kMicrosecondsPerMillisecond; } int64 TimeDelta::InMillisecondsRoundedUp() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int64>::max(); + } return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / Time::kMicrosecondsPerMillisecond; } int64 TimeDelta::InMicroseconds() const { + if (is_max()) { + // Preserve max to prevent overflow. + return std::numeric_limits<int64>::max(); + } return delta_; } diff --git a/base/time/time.h b/base/time/time.h index 40566b9..69a1324 100644 --- a/base/time/time.h +++ b/base/time/time.h @@ -61,9 +61,9 @@ class BASE_EXPORT TimeDelta { } // Converts units of time to TimeDeltas. - static TimeDelta FromDays(int64 days); - static TimeDelta FromHours(int64 hours); - static TimeDelta FromMinutes(int64 minutes); + static TimeDelta FromDays(int days); + static TimeDelta FromHours(int hours); + static TimeDelta FromMinutes(int minutes); static TimeDelta FromSeconds(int64 secs); static TimeDelta FromMilliseconds(int64 ms); static TimeDelta FromMicroseconds(int64 us); @@ -79,6 +79,11 @@ class BASE_EXPORT TimeDelta { return TimeDelta(delta); } + // Returns the maximum time delta, which should be greater than any reasonable + // time delta we might compare it to. Adding or subtracting the maximum time + // delta to a time or another time delta has an undefined result. + static TimeDelta Max(); + // Returns the internal numeric value of the TimeDelta object. Please don't // use this and do arithmetic on it, as it is more error prone than using the // provided operators. @@ -87,6 +92,11 @@ class BASE_EXPORT TimeDelta { return delta_; } + // Returns true if the time delta is the maximum time delta. + bool is_max() const { + return delta_ == std::numeric_limits<int64>::max(); + } + #if defined(OS_POSIX) struct timespec ToTimeSpec() const; #endif @@ -493,32 +503,50 @@ class BASE_EXPORT Time { // Inline the TimeDelta factory methods, for fast TimeDelta construction. // static -inline TimeDelta TimeDelta::FromDays(int64 days) { +inline TimeDelta TimeDelta::FromDays(int days) { + // Preserve max to prevent overflow. + if (days == std::numeric_limits<int>::max()) + return Max(); return TimeDelta(days * Time::kMicrosecondsPerDay); } // static -inline TimeDelta TimeDelta::FromHours(int64 hours) { +inline TimeDelta TimeDelta::FromHours(int hours) { + // Preserve max to prevent overflow. + if (hours == std::numeric_limits<int>::max()) + return Max(); return TimeDelta(hours * Time::kMicrosecondsPerHour); } // static -inline TimeDelta TimeDelta::FromMinutes(int64 minutes) { +inline TimeDelta TimeDelta::FromMinutes(int minutes) { + // Preserve max to prevent overflow. + if (minutes == std::numeric_limits<int>::max()) + return Max(); return TimeDelta(minutes * Time::kMicrosecondsPerMinute); } // static inline TimeDelta TimeDelta::FromSeconds(int64 secs) { + // Preserve max to prevent overflow. + if (secs == std::numeric_limits<int64>::max()) + return Max(); return TimeDelta(secs * Time::kMicrosecondsPerSecond); } // static inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { + // Preserve max to prevent overflow. + if (ms == std::numeric_limits<int64>::max()) + return Max(); return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); } // static inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { + // Preserve max to prevent overflow. + if (us == std::numeric_limits<int64>::max()) + return Max(); return TimeDelta(us); } diff --git a/base/time/time_unittest.cc b/base/time/time_unittest.cc index b5d2493..40d7bd4 100644 --- a/base/time/time_unittest.cc +++ b/base/time/time_unittest.cc @@ -481,6 +481,46 @@ TEST_F(TimeTest, ExplodeBeforeUnixEpoch) { EXPECT_EQ(1, exploded.millisecond); } +TEST_F(TimeTest, TimeDeltaMax) { + TimeDelta max = TimeDelta::Max(); + EXPECT_TRUE(max.is_max()); + EXPECT_EQ(max, TimeDelta::Max()); + EXPECT_GT(max, TimeDelta::FromDays(100 * 365)); + EXPECT_GT(max, TimeDelta()); +} + +TEST_F(TimeTest, TimeDeltaMaxConversions) { + TimeDelta t = TimeDelta::Max(); + EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue()); + + EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays()); + EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours()); + EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes()); + EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF()); + EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds()); + EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF()); + EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds()); + EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp()); + + t = TimeDelta::FromDays(std::numeric_limits<int>::max()); + EXPECT_TRUE(t.is_max()); + + t = TimeDelta::FromHours(std::numeric_limits<int>::max()); + EXPECT_TRUE(t.is_max()); + + t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); + EXPECT_TRUE(t.is_max()); + + t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max()); + EXPECT_TRUE(t.is_max()); + + t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max()); + EXPECT_TRUE(t.is_max()); + + t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max()); + EXPECT_TRUE(t.is_max()); +} + TEST_F(TimeTest, Max) { Time max = Time::Max(); EXPECT_TRUE(max.is_max()); diff --git a/cc/debug/rasterize_and_record_benchmark.cc b/cc/debug/rasterize_and_record_benchmark.cc index 55f5677..ab8d1cb 100644 --- a/cc/debug/rasterize_and_record_benchmark.cc +++ b/cc/debug/rasterize_and_record_benchmark.cc @@ -108,8 +108,7 @@ void RasterizeAndRecordBenchmark::RunOnLayer(PictureLayer* layer) { return; - base::TimeDelta min_time = - base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max()); + base::TimeDelta min_time = base::TimeDelta::Max(); for (int i = 0; i < record_repeat_count_; ++i) { base::TimeTicks start = Now(); scoped_refptr<Picture> picture = Picture::Create( diff --git a/cc/debug/rasterize_and_record_benchmark_impl.cc b/cc/debug/rasterize_and_record_benchmark_impl.cc index 15cfd7a..fd0b7bd 100644 --- a/cc/debug/rasterize_and_record_benchmark_impl.cc +++ b/cc/debug/rasterize_and_record_benchmark_impl.cc @@ -104,8 +104,7 @@ void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) { int tile_size = content_rect.width() * content_rect.height(); - base::TimeDelta min_time = - base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max()); + base::TimeDelta min_time = base::TimeDelta::Max(); bool is_solid_color = false; for (int i = 0; i < rasterize_repeat_count_; ++i) { diff --git a/cc/resources/picture_pile.cc b/cc/resources/picture_pile.cc index 7e5a822..43b13c5 100644 --- a/cc/resources/picture_pile.cc +++ b/cc/resources/picture_pile.cc @@ -228,8 +228,7 @@ bool PicturePile::Update( bool gather_pixel_refs = num_raster_threads > 1; { - base::TimeDelta best_duration = base::TimeDelta::FromInternalValue( - std::numeric_limits<int64>::max()); + base::TimeDelta best_duration = base::TimeDelta::Max(); for (int i = 0; i < repeat_count; i++) { base::TimeTicks start_time = stats_instrumentation->StartRecording(); picture = Picture::Create(record_rect, diff --git a/cc/resources/picture_pile_impl.cc b/cc/resources/picture_pile_impl.cc index b88a439..1254f13 100644 --- a/cc/resources/picture_pile_impl.cc +++ b/cc/resources/picture_pile_impl.cc @@ -247,8 +247,7 @@ void PicturePileImpl::RasterCommon( total_clip.Union(positive_clip); #endif // NDEBUG - base::TimeDelta best_duration = - base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max()); + base::TimeDelta best_duration = base::TimeDelta::Max(); int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); int rasterized_pixel_count = 0; diff --git a/content/renderer/media/websourcebuffer_impl.cc b/content/renderer/media/websourcebuffer_impl.cc index a7d8d8a..de55d41 100644 --- a/content/renderer/media/websourcebuffer_impl.cc +++ b/content/renderer/media/websourcebuffer_impl.cc @@ -15,6 +15,8 @@ static base::TimeDelta DoubleToTimeDelta(double time) { if (time == std::numeric_limits<double>::infinity()) return media::kInfiniteDuration(); + // Don't use base::TimeDelta::Max() here, as we want the largest finite time + // delta. base::TimeDelta max_time = base::TimeDelta::FromInternalValue(kint64max - 1); double max_time_in_seconds = max_time.InSecondsF(); diff --git a/media/base/buffers.h b/media/base/buffers.h index 6a6c730..5c5c47b 100644 --- a/media/base/buffers.h +++ b/media/base/buffers.h @@ -37,7 +37,7 @@ MEDIA_EXPORT extern inline base::TimeDelta kNoTimestamp() { // Represents an infinite stream duration. MEDIA_EXPORT extern inline base::TimeDelta kInfiniteDuration() { - return base::TimeDelta::FromMicroseconds(kint64max); + return base::TimeDelta::Max(); } } // namespace media diff --git a/media/filters/chunk_demuxer.cc b/media/filters/chunk_demuxer.cc index b7244b9..3f8618a 100644 --- a/media/filters/chunk_demuxer.cc +++ b/media/filters/chunk_demuxer.cc @@ -1523,6 +1523,7 @@ void ChunkDemuxer::SetDuration(double duration) { // This can be different if the value of |duration| doesn't fit the range or // precision of TimeDelta. TimeDelta min_duration = TimeDelta::FromInternalValue(1); + // Don't use TimeDelta::Max() here, as we want the largest finite time delta. TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1); double min_duration_in_seconds = min_duration.InSecondsF(); double max_duration_in_seconds = max_duration.InSecondsF(); diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc index 0072976..f26292a 100644 --- a/net/http/http_response_headers.cc +++ b/net/http/http_response_headers.cc @@ -1009,7 +1009,7 @@ TimeDelta HttpResponseHeaders::GetFreshnessLifetime( // These responses are implicitly fresh (unless otherwise overruled): if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410) - return TimeDelta::FromMicroseconds(kint64max); + return TimeDelta::Max(); return TimeDelta(); // not fresh } diff --git a/net/spdy/spdy_session.cc b/net/spdy/spdy_session.cc index a6f29c5..8e6a55a 100644 --- a/net/spdy/spdy_session.cc +++ b/net/spdy/spdy_session.cc @@ -2723,9 +2723,7 @@ void SpdySession::CheckPingStatus(base::TimeTicks last_check_time) { if (delay.InMilliseconds() < 0 || last_activity_time_ < last_check_time) { // Track all failed PING messages in a separate bucket. - const base::TimeDelta kFailedPing = - base::TimeDelta::FromInternalValue(INT_MAX); - RecordPingRTTHistogram(kFailedPing); + RecordPingRTTHistogram(base::TimeDelta::Max()); CloseSessionResult result = DoCloseSession(ERR_SPDY_PING_FAILED, "Failed ping."); DCHECK_EQ(result, SESSION_CLOSED_AND_REMOVED); diff --git a/sync/sessions/nudge_tracker.cc b/sync/sessions/nudge_tracker.cc index 212500a..b69d753 100644 --- a/sync/sessions/nudge_tracker.cc +++ b/sync/sessions/nudge_tracker.cc @@ -192,11 +192,9 @@ bool NudgeTracker::IsTypeThrottled(ModelType type) const { base::TimeDelta NudgeTracker::GetTimeUntilNextUnthrottle( base::TimeTicks now) const { DCHECK(IsAnyTypeThrottled()) << "This function requires a pending unthrottle"; - const base::TimeDelta kMaxTimeDelta = - base::TimeDelta::FromInternalValue(kint64max); // Return min of GetTimeUntilUnthrottle() values for all IsThrottled() types. - base::TimeDelta time_until_next_unthrottle = kMaxTimeDelta; + base::TimeDelta time_until_next_unthrottle = base::TimeDelta::Max(); for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); it != type_trackers_.end(); ++it) { if (it->second.IsThrottled()) { @@ -205,7 +203,7 @@ base::TimeDelta NudgeTracker::GetTimeUntilNextUnthrottle( it->second.GetTimeUntilUnthrottle(now)); } } - DCHECK(kMaxTimeDelta != time_until_next_unthrottle); + DCHECK(!time_until_next_unthrottle.is_max()); return time_until_next_unthrottle; } |