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]), | |
| 20 capacity_(size), | |
| 21 length_(0) { | |
| 22 } | |
| 23 | |
| 24 QuicDataWriter::~QuicDataWriter() { | |
| 25 delete[] buffer_; | |
| 26 } | |
| 27 | |
| 28 char* QuicDataWriter::BeginWrite(size_t length) { | |
| 29 if (length_ + length > capacity_) { | |
|
jar (doing other things)
2012/10/16 19:24:01
nit: To avoid integer overflow confusion, I think
Ryan Hamilton
2012/10/16 19:43:23
Ah, I see. I didn't realize that was why you wrot
| |
| 30 return NULL; | |
| 31 } | |
| 32 | |
| 33 #ifdef ARCH_CPU_64_BITS | |
| 34 DCHECK_LE(length, numeric_limits<uint32>::max()); | |
| 35 #endif | |
| 36 | |
| 37 return buffer_ + length_; | |
| 38 } | |
| 39 | |
| 40 bool QuicDataWriter::AdvancePointer(uint32 len) { | |
| 41 if (!BeginWrite(len)) { | |
| 42 return false; | |
| 43 } | |
| 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 |