Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / quic / quic_bandwidth.cc
blob911eb6c83cf4582e4cff41f2416c0fb3a11d5010
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_bandwidth.h"
7 #include <stdint.h>
9 #include "base/logging.h"
10 #include "net/quic/quic_time.h"
11 #include "net/quic/quic_types.h"
13 namespace net {
15 // Highest number that QuicBandwidth can hold.
16 const int64 kQuicInfiniteBandwidth = INT64_C(0x7fffffffffffffff);
18 // static
19 QuicBandwidth QuicBandwidth::Zero() {
20 return QuicBandwidth(0);
23 // static
24 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64 bits_per_second) {
25 return QuicBandwidth(bits_per_second);
28 // static
29 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64 k_bits_per_second) {
30 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000);
31 return QuicBandwidth(k_bits_per_second * 1000);
34 // static
35 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64 bytes_per_second) {
36 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8);
37 return QuicBandwidth(bytes_per_second * 8);
40 // static
41 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64 k_bytes_per_second) {
42 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000);
43 return QuicBandwidth(k_bytes_per_second * 8000);
46 // static
47 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes,
48 QuicTime::Delta delta) {
49 DCHECK_LT(bytes, static_cast<uint64>(kQuicInfiniteBandwidth /
50 (8 * kNumMicrosPerSecond)));
51 int64 bytes_per_second =
52 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds();
53 return QuicBandwidth(bytes_per_second * 8);
56 QuicBandwidth::QuicBandwidth(int64 bits_per_second)
57 : bits_per_second_(bits_per_second) {
58 DCHECK_GE(bits_per_second, 0);
61 int64 QuicBandwidth::ToBitsPerSecond() const {
62 return bits_per_second_;
65 int64 QuicBandwidth::ToKBitsPerSecond() const {
66 return bits_per_second_ / 1000;
69 int64 QuicBandwidth::ToBytesPerSecond() const {
70 return bits_per_second_ / 8;
73 int64 QuicBandwidth::ToKBytesPerSecond() const {
74 return bits_per_second_ / 8000;
77 QuicByteCount QuicBandwidth::ToBytesPerPeriod(
78 QuicTime::Delta time_period) const {
79 return ToBytesPerSecond() * time_period.ToMicroseconds() /
80 kNumMicrosPerSecond;
83 int64 QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const {
84 return ToKBytesPerSecond() * time_period.ToMicroseconds() /
85 kNumMicrosPerSecond;
88 bool QuicBandwidth::IsZero() const {
89 return (bits_per_second_ == 0);
92 QuicBandwidth QuicBandwidth::Add(const QuicBandwidth& delta) const {
93 return QuicBandwidth(bits_per_second_ + delta.bits_per_second_);
96 QuicBandwidth QuicBandwidth::Subtract(const QuicBandwidth& delta) const {
97 return QuicBandwidth(bits_per_second_ - delta.bits_per_second_);
100 QuicBandwidth QuicBandwidth::Scale(float scale_factor) const {
101 return QuicBandwidth(static_cast<int64>(bits_per_second_ * scale_factor));
104 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const {
105 if (bits_per_second_ == 0) {
106 return QuicTime::Delta::Zero();
108 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond /
109 bits_per_second_);
112 } // namespace net