Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "net/quic/quic_data_writer.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "net/quic/quic_protocol.h" | |
| 13 | |
| 14 using std::numeric_limits; | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 QuicDataWriter::QuicDataWriter(size_t size) | |
| 19 : buffer_(new char[size]), | |
|
jar (doing other things)
2012/10/14 23:04:38
nit/comment: It is a bit tempting to have a fixed
Ryan Hamilton
2012/10/15 21:22:08
But then the take() method could not be implemente
| |
| 20 capacity_(size), | |
| 21 length_(0) { | |
| 22 } | |
| 23 | |
| 24 QuicDataWriter::~QuicDataWriter() { | |
| 25 delete[] buffer_; | |
| 26 } | |
| 27 | |
| 28 char* QuicDataWriter::BeginWrite(size_t length) { | |
| 29 size_t offset = length_; | |
| 30 size_t needed_size = length_ + length; | |
| 31 if (needed_size > capacity_) { | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: to handle wrapping, perhaps better would be:
Ryan Hamilton
2012/10/15 21:22:08
Done. FYI, This code was simply copied from the s
| |
| 32 return NULL; | |
| 33 } | |
| 34 | |
| 35 #ifdef ARCH_CPU_64_BITS | |
| 36 DCHECK_LE(length, numeric_limits<uint32>::max()); | |
| 37 #endif | |
| 38 | |
| 39 return buffer_ + offset; | |
|
jar (doing other things)
2012/10/14 23:04:38
Why did you save length_ into offset? Isn't this
Ryan Hamilton
2012/10/15 21:22:08
This code was copied from the spdy variant.
| |
| 40 } | |
| 41 | |
| 42 bool QuicDataWriter::AdvancePointer(uint32 len) { | |
| 43 if (!BeginWrite(len)) return false; | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: probably want to be consistent with braces an
Ryan Hamilton
2012/10/15 21:22:08
Done. But just to be clear, this is perfectly acc
| |
| 44 length_ += len; | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 bool QuicDataWriter::WriteBytes(const void* data, uint32 data_len) { | |
| 49 char* dest = BeginWrite(data_len); | |
| 50 if (!dest) { | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 memcpy(dest, data, data_len); | |
| 55 | |
| 56 length_ += data_len; | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 void QuicDataWriter::WriteUint64ToBuffer(uint64 value, char* buffer) { | |
| 61 memcpy(buffer, &value, sizeof(value)); | |
| 62 } | |
| 63 | |
| 64 void QuicDataWriter::WriteUint128ToBuffer(uint128 value, char* buffer) { | |
| 65 WriteUint64ToBuffer(value.lo, buffer); | |
| 66 WriteUint64ToBuffer(value.hi, buffer + sizeof(value.lo)); | |
| 67 } | |
| 68 | |
| 69 } // namespace net | |
| OLD | NEW |