OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_bandwidth.h" | 5 #include "net/quic/quic_bandwidth.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "net/quic/quic_time.h" | 10 #include "net/quic/quic_time.h" |
11 #include "net/quic/quic_types.h" | 11 #include "net/quic/quic_types.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 // Highest number that QuicBandwidth can hold. | 15 // Highest number that QuicBandwidth can hold. |
16 const int64 kQuicInfiniteBandwidth = INT64_C(0x7fffffffffffffff); | 16 const int64_t kQuicInfiniteBandwidth = INT64_C(0x7fffffffffffffff); |
17 | 17 |
18 // static | 18 // static |
19 QuicBandwidth QuicBandwidth::Zero() { | 19 QuicBandwidth QuicBandwidth::Zero() { |
20 return QuicBandwidth(0); | 20 return QuicBandwidth(0); |
21 } | 21 } |
22 | 22 |
23 // static | 23 // static |
24 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64 bits_per_second) { | 24 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64_t bits_per_second) { |
25 return QuicBandwidth(bits_per_second); | 25 return QuicBandwidth(bits_per_second); |
26 } | 26 } |
27 | 27 |
28 // static | 28 // static |
29 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64 k_bits_per_second) { | 29 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64_t k_bits_per_second) { |
30 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000); | 30 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000); |
31 return QuicBandwidth(k_bits_per_second * 1000); | 31 return QuicBandwidth(k_bits_per_second * 1000); |
32 } | 32 } |
33 | 33 |
34 // static | 34 // static |
35 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64 bytes_per_second) { | 35 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64_t bytes_per_second) { |
36 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8); | 36 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8); |
37 return QuicBandwidth(bytes_per_second * 8); | 37 return QuicBandwidth(bytes_per_second * 8); |
38 } | 38 } |
39 | 39 |
40 // static | 40 // static |
41 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64 k_bytes_per_second) { | 41 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64_t k_bytes_per_second) { |
42 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000); | 42 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000); |
43 return QuicBandwidth(k_bytes_per_second * 8000); | 43 return QuicBandwidth(k_bytes_per_second * 8000); |
44 } | 44 } |
45 | 45 |
46 // static | 46 // static |
47 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes, | 47 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes, |
48 QuicTime::Delta delta) { | 48 QuicTime::Delta delta) { |
49 DCHECK_LT(bytes, static_cast<uint64>(kQuicInfiniteBandwidth / | 49 DCHECK_LT(bytes, static_cast<uint64_t>(kQuicInfiniteBandwidth / |
50 (8 * kNumMicrosPerSecond))); | 50 (8 * kNumMicrosPerSecond))); |
51 int64 bytes_per_second = | 51 int64_t bytes_per_second = |
52 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds(); | 52 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds(); |
53 return QuicBandwidth(bytes_per_second * 8); | 53 return QuicBandwidth(bytes_per_second * 8); |
54 } | 54 } |
55 | 55 |
56 QuicBandwidth::QuicBandwidth(int64 bits_per_second) | 56 QuicBandwidth::QuicBandwidth(int64_t bits_per_second) |
57 : bits_per_second_(bits_per_second) { | 57 : bits_per_second_(bits_per_second) { |
58 DCHECK_GE(bits_per_second, 0); | 58 DCHECK_GE(bits_per_second, 0); |
59 } | 59 } |
60 | 60 |
61 int64 QuicBandwidth::ToBitsPerSecond() const { | 61 int64_t QuicBandwidth::ToBitsPerSecond() const { |
62 return bits_per_second_; | 62 return bits_per_second_; |
63 } | 63 } |
64 | 64 |
65 int64 QuicBandwidth::ToKBitsPerSecond() const { | 65 int64_t QuicBandwidth::ToKBitsPerSecond() const { |
66 return bits_per_second_ / 1000; | 66 return bits_per_second_ / 1000; |
67 } | 67 } |
68 | 68 |
69 int64 QuicBandwidth::ToBytesPerSecond() const { | 69 int64_t QuicBandwidth::ToBytesPerSecond() const { |
70 return bits_per_second_ / 8; | 70 return bits_per_second_ / 8; |
71 } | 71 } |
72 | 72 |
73 int64 QuicBandwidth::ToKBytesPerSecond() const { | 73 int64_t QuicBandwidth::ToKBytesPerSecond() const { |
74 return bits_per_second_ / 8000; | 74 return bits_per_second_ / 8000; |
75 } | 75 } |
76 | 76 |
77 QuicByteCount QuicBandwidth::ToBytesPerPeriod( | 77 QuicByteCount QuicBandwidth::ToBytesPerPeriod( |
78 QuicTime::Delta time_period) const { | 78 QuicTime::Delta time_period) const { |
79 return ToBytesPerSecond() * time_period.ToMicroseconds() / | 79 return ToBytesPerSecond() * time_period.ToMicroseconds() / |
80 kNumMicrosPerSecond; | 80 kNumMicrosPerSecond; |
81 } | 81 } |
82 | 82 |
83 int64 QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const { | 83 int64_t QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const { |
84 return ToKBytesPerSecond() * time_period.ToMicroseconds() / | 84 return ToKBytesPerSecond() * time_period.ToMicroseconds() / |
85 kNumMicrosPerSecond; | 85 kNumMicrosPerSecond; |
86 } | 86 } |
87 | 87 |
88 bool QuicBandwidth::IsZero() const { | 88 bool QuicBandwidth::IsZero() const { |
89 return (bits_per_second_ == 0); | 89 return (bits_per_second_ == 0); |
90 } | 90 } |
91 | 91 |
92 QuicBandwidth QuicBandwidth::Add(const QuicBandwidth& delta) const { | 92 QuicBandwidth QuicBandwidth::Add(const QuicBandwidth& delta) const { |
93 return QuicBandwidth(bits_per_second_ + delta.bits_per_second_); | 93 return QuicBandwidth(bits_per_second_ + delta.bits_per_second_); |
94 } | 94 } |
95 | 95 |
96 QuicBandwidth QuicBandwidth::Subtract(const QuicBandwidth& delta) const { | 96 QuicBandwidth QuicBandwidth::Subtract(const QuicBandwidth& delta) const { |
97 return QuicBandwidth(bits_per_second_ - delta.bits_per_second_); | 97 return QuicBandwidth(bits_per_second_ - delta.bits_per_second_); |
98 } | 98 } |
99 | 99 |
100 QuicBandwidth QuicBandwidth::Scale(float scale_factor) const { | 100 QuicBandwidth QuicBandwidth::Scale(float scale_factor) const { |
101 return QuicBandwidth(static_cast<int64>(bits_per_second_ * scale_factor)); | 101 return QuicBandwidth(static_cast<int64_t>(bits_per_second_ * scale_factor)); |
102 } | 102 } |
103 | 103 |
104 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const { | 104 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const { |
105 if (bits_per_second_ == 0) { | 105 if (bits_per_second_ == 0) { |
106 return QuicTime::Delta::Zero(); | 106 return QuicTime::Delta::Zero(); |
107 } | 107 } |
108 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / | 108 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / |
109 bits_per_second_); | 109 bits_per_second_); |
110 } | 110 } |
111 | 111 |
112 } // namespace net | 112 } // namespace net |
OLD | NEW |