Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 Construct(bytes, len); | 36 Construct(bytes, len); |
| 37 } | 37 } |
| 38 | 38 |
| 39 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len, | 39 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len, |
| 40 ByteOrder byte_order) | 40 ByteOrder byte_order) |
| 41 : ByteBuffer(byte_order) { | 41 : ByteBuffer(byte_order) { |
| 42 Construct(bytes, len); | 42 Construct(bytes, len); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ByteBufferWriter::Construct(const char* bytes, size_t len) { | 45 void ByteBufferWriter::Construct(const char* bytes, size_t len) { |
| 46 start_ = 0; | |
| 47 size_ = len; | 46 size_ = len; |
| 48 bytes_ = new char[size_]; | 47 bytes_ = new char[size_]; |
| 49 | 48 |
| 50 if (bytes) { | 49 if (bytes) { |
| 51 end_ = len; | 50 end_ = len; |
| 52 memcpy(bytes_, bytes, end_); | 51 memcpy(bytes_, bytes, end_); |
| 53 } else { | 52 } else { |
| 54 end_ = 0; | 53 end_ = 0; |
| 55 } | 54 } |
| 56 } | 55 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) { | 111 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) { |
| 113 if (Length() + len > Capacity()) | 112 if (Length() + len > Capacity()) |
| 114 Resize(Length() + len); | 113 Resize(Length() + len); |
| 115 | 114 |
| 116 char* start = bytes_ + end_; | 115 char* start = bytes_ + end_; |
| 117 end_ += len; | 116 end_ += len; |
| 118 return start; | 117 return start; |
| 119 } | 118 } |
| 120 | 119 |
| 121 void ByteBufferWriter::Resize(size_t size) { | 120 void ByteBufferWriter::Resize(size_t size) { |
| 122 size_t len = std::min(end_ - start_, size); | 121 size_t len = std::min(end_, size); |
| 123 if (size <= size_) { | 122 if (size <= size_) { |
| 124 // Don't reallocate, just move data backwards | 123 // Don't reallocate, just move data backwards |
| 125 memmove(bytes_, bytes_ + start_, len); | 124 memmove(bytes_, bytes_, len); |
|
joachim
2016/10/21 13:11:19
Sorry, didn't notice this in the first place, but
kwiberg-webrtc
2016/10/21 15:08:42
So it DOES take three people to review this... :-)
tommi (sloooow) - chröme
2016/10/21 15:15:39
Indeed!
nisse-chromium (ooo August 14)
2016/10/24 06:51:06
Done.
| |
| 126 } else { | 125 } else { |
| 127 // Reallocate a larger buffer. | 126 // Reallocate a larger buffer. |
| 128 size_ = std::max(size, 3 * size_ / 2); | 127 size_ = std::max(size, 3 * size_ / 2); |
| 129 char* new_bytes = new char[size_]; | 128 char* new_bytes = new char[size_]; |
| 130 memcpy(new_bytes, bytes_ + start_, len); | 129 memcpy(new_bytes, bytes_, len); |
| 131 delete [] bytes_; | 130 delete [] bytes_; |
| 132 bytes_ = new_bytes; | 131 bytes_ = new_bytes; |
| 133 } | 132 } |
| 134 start_ = 0; | |
| 135 end_ = len; | 133 end_ = len; |
| 136 } | 134 } |
| 137 | 135 |
| 138 void ByteBufferWriter::Clear() { | 136 void ByteBufferWriter::Clear() { |
| 139 memset(bytes_, 0, size_); | 137 memset(bytes_, 0, size_); |
| 140 start_ = end_ = 0; | 138 end_ = 0; |
| 141 } | 139 } |
| 142 | 140 |
| 143 | 141 |
| 144 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) | 142 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) |
| 145 : ByteBuffer(ORDER_NETWORK) { | 143 : ByteBuffer(ORDER_NETWORK) { |
| 146 Construct(bytes, len); | 144 Construct(bytes, len); |
| 147 } | 145 } |
| 148 | 146 |
| 149 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len, | 147 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len, |
| 150 ByteOrder byte_order) | 148 ByteOrder byte_order) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 } | 277 } |
| 280 | 278 |
| 281 bool ByteBufferReader::Consume(size_t size) { | 279 bool ByteBufferReader::Consume(size_t size) { |
| 282 if (size > Length()) | 280 if (size > Length()) |
| 283 return false; | 281 return false; |
| 284 start_ += size; | 282 start_ += size; |
| 285 return true; | 283 return true; |
| 286 } | 284 } |
| 287 | 285 |
| 288 } // namespace rtc | 286 } // namespace rtc |
| OLD | NEW |