Add some instrumentation to investigate a possible use after free.
[chromium-blink-merge.git] / net / quic / quic_time.cc
blob7da849ebc01e564cebcfe89cce7a164c2e387ec9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_time.h"
7 #include <stdint.h>
9 #include "base/logging.h"
11 namespace net {
13 // Highest number of microseconds that DateTimeOffset can hold.
14 const int64 kQuicInfiniteTimeUs = INT64_C(0x7fffffffffffffff) / 10;
16 QuicTime::Delta::Delta(base::TimeDelta delta)
17 : delta_(delta) {
20 // static
21 QuicTime::Delta QuicTime::Delta::Zero() {
22 return QuicTime::Delta::FromMicroseconds(0);
25 // static
26 QuicTime::Delta QuicTime::Delta::Infinite() {
27 return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs);
30 // static
31 QuicTime::Delta QuicTime::Delta::FromSeconds(int64 seconds) {
32 return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds));
35 // static
36 QuicTime::Delta QuicTime::Delta::FromMilliseconds(int64 ms) {
37 return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms));
40 // static
41 QuicTime::Delta QuicTime::Delta::FromMicroseconds(int64 us) {
42 return QuicTime::Delta(base::TimeDelta::FromMicroseconds(us));
45 int64 QuicTime::Delta::ToSeconds() const {
46 return delta_.InSeconds();
49 int64 QuicTime::Delta::ToMilliseconds() const {
50 return delta_.InMilliseconds();
53 int64 QuicTime::Delta::ToMicroseconds() const {
54 return delta_.InMicroseconds();
57 QuicTime::Delta QuicTime::Delta::Add(const Delta& delta) const {
58 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() +
59 delta.ToMicroseconds());
62 QuicTime::Delta QuicTime::Delta::Subtract(const Delta& delta) const {
63 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() -
64 delta.ToMicroseconds());
67 QuicTime::Delta QuicTime::Delta::Multiply(int i) const {
68 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() * i);
71 QuicTime::Delta QuicTime::Delta::Multiply(double d) const {
72 return QuicTime::Delta::FromMicroseconds(
73 static_cast<int64>(ToMicroseconds() * d));
76 // static
77 QuicTime::Delta QuicTime::Delta::Max(QuicTime::Delta delta1,
78 QuicTime::Delta delta2) {
79 return delta1 < delta2 ? delta2 : delta1;
82 bool QuicTime::Delta::IsZero() const {
83 return delta_.InMicroseconds() == 0;
86 bool QuicTime::Delta::IsInfinite() const {
87 return delta_.InMicroseconds() == kQuicInfiniteTimeUs;
90 // static
91 QuicTime QuicTime::Zero() {
92 return QuicTime(base::TimeTicks());
95 // static
96 QuicTime QuicTime::Infinite() {
97 return QuicTime(base::TimeTicks::FromInternalValue(kQuicInfiniteTimeUs));
100 // static
101 QuicTime QuicTime::Max(QuicTime time1, QuicTime time2) {
102 return time1 > time2 ? time1 : time2;
105 QuicTime::QuicTime(base::TimeTicks ticks)
106 : ticks_(ticks) {
109 int64 QuicTime::ToDebuggingValue() const {
110 return (ticks_ - base::TimeTicks()).InMicroseconds();
113 bool QuicTime::IsInitialized() const {
114 return ticks_ != base::TimeTicks();
117 QuicTime QuicTime::Add(const Delta& delta) const {
118 return QuicTime(ticks_ + delta.delta_);
121 QuicTime QuicTime::Subtract(const Delta& delta) const {
122 return QuicTime(ticks_ - delta.delta_);
125 QuicTime::Delta QuicTime::Subtract(const QuicTime& other) const {
126 return QuicTime::Delta(ticks_ - other.ticks_);
129 // static
130 QuicWallTime QuicWallTime::FromUNIXSeconds(uint64 seconds) {
131 return QuicWallTime(seconds);
134 // static
135 QuicWallTime QuicWallTime::Zero() {
136 return QuicWallTime(0);
139 uint64 QuicWallTime::ToUNIXSeconds() const {
140 return seconds_;
143 bool QuicWallTime::IsAfter(QuicWallTime other) const {
144 return seconds_ > other.seconds_;
147 bool QuicWallTime::IsBefore(QuicWallTime other) const {
148 return seconds_ < other.seconds_;
151 bool QuicWallTime::IsZero() const {
152 return seconds_ == 0;
155 QuicTime::Delta QuicWallTime::AbsoluteDifference(QuicWallTime other) const {
156 uint64 d;
158 if (seconds_ > other.seconds_) {
159 d = seconds_ - other.seconds_;
160 } else {
161 d = other.seconds_ - seconds_;
164 if (d > static_cast<uint64>(kint64max)) {
165 d = kint64max;
167 return QuicTime::Delta::FromSeconds(d);
170 QuicWallTime QuicWallTime::Add(QuicTime::Delta delta) const {
171 uint64 seconds = seconds_ + delta.ToSeconds();
172 if (seconds < seconds_) {
173 seconds = kuint64max;
175 return QuicWallTime(seconds);
178 // TODO(ianswett) Test this.
179 QuicWallTime QuicWallTime::Subtract(QuicTime::Delta delta) const {
180 uint64 seconds = seconds_ - delta.ToSeconds();
181 if (seconds > seconds_) {
182 seconds = 0;
184 return QuicWallTime(seconds);
187 QuicWallTime::QuicWallTime(uint64 seconds)
188 : seconds_(seconds) {
191 } // namespace net