| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/io_buffer.h" | 5 #include "net/base/io_buffer.h" | 
| 6 | 6 | 
| 7 #include "base/logging.h" | 7 #include "base/logging.h" | 
| 8 | 8 | 
| 9 namespace net { | 9 namespace net { | 
| 10 | 10 | 
| 11 IOBuffer::IOBuffer() | 11 IOBuffer::IOBuffer() : data_(NULL) { | 
| 12     : data_(NULL) { |  | 
| 13 } | 12 } | 
| 14 | 13 | 
| 15 IOBuffer::IOBuffer(int buffer_size) { | 14 IOBuffer::IOBuffer(int buffer_size) { | 
| 16   CHECK_GE(buffer_size, 0); | 15   CHECK_GE(buffer_size, 0); | 
| 17   data_ = new char[buffer_size]; | 16   data_ = new char[buffer_size]; | 
| 18 } | 17 } | 
| 19 | 18 | 
| 20 IOBuffer::IOBuffer(char* data) | 19 IOBuffer::IOBuffer(char* data) : data_(data) { | 
| 21     : data_(data) { |  | 
| 22 } | 20 } | 
| 23 | 21 | 
| 24 IOBuffer::~IOBuffer() { | 22 IOBuffer::~IOBuffer() { | 
| 25   delete[] data_; | 23   delete[] data_; | 
| 26   data_ = NULL; | 24   data_ = NULL; | 
| 27 } | 25 } | 
| 28 | 26 | 
| 29 IOBufferWithSize::IOBufferWithSize(int size) | 27 IOBufferWithSize::IOBufferWithSize(int size) : IOBuffer(size), size_(size) { | 
| 30     : IOBuffer(size), |  | 
| 31       size_(size) { |  | 
| 32 } | 28 } | 
| 33 | 29 | 
| 34 IOBufferWithSize::IOBufferWithSize(char* data, int size) | 30 IOBufferWithSize::IOBufferWithSize(char* data, int size) | 
| 35     : IOBuffer(data), | 31     : IOBuffer(data), size_(size) { | 
| 36       size_(size) { |  | 
| 37 } | 32 } | 
| 38 | 33 | 
| 39 IOBufferWithSize::~IOBufferWithSize() { | 34 IOBufferWithSize::~IOBufferWithSize() { | 
| 40 } | 35 } | 
| 41 | 36 | 
| 42 StringIOBuffer::StringIOBuffer(const std::string& s) | 37 StringIOBuffer::StringIOBuffer(const std::string& s) | 
| 43     : IOBuffer(static_cast<char*>(NULL)), | 38     : IOBuffer(static_cast<char*>(NULL)), string_data_(s) { | 
| 44       string_data_(s) { |  | 
| 45   CHECK_LT(s.size(), static_cast<size_t>(INT_MAX)); | 39   CHECK_LT(s.size(), static_cast<size_t>(INT_MAX)); | 
| 46   data_ = const_cast<char*>(string_data_.data()); | 40   data_ = const_cast<char*>(string_data_.data()); | 
| 47 } | 41 } | 
| 48 | 42 | 
| 49 StringIOBuffer::StringIOBuffer(scoped_ptr<std::string> s) | 43 StringIOBuffer::StringIOBuffer(scoped_ptr<std::string> s) | 
| 50     : IOBuffer(static_cast<char*>(NULL)) { | 44     : IOBuffer(static_cast<char*>(NULL)) { | 
| 51   CHECK_LT(s->size(), static_cast<size_t>(INT_MAX)); | 45   CHECK_LT(s->size(), static_cast<size_t>(INT_MAX)); | 
| 52   string_data_.swap(*s.get()); | 46   string_data_.swap(*s.get()); | 
| 53   data_ = const_cast<char*>(string_data_.data()); | 47   data_ = const_cast<char*>(string_data_.data()); | 
| 54 } | 48 } | 
| 55 | 49 | 
| 56 StringIOBuffer::~StringIOBuffer() { | 50 StringIOBuffer::~StringIOBuffer() { | 
| 57   // We haven't allocated the buffer, so remove it before the base class | 51   // We haven't allocated the buffer, so remove it before the base class | 
| 58   // destructor tries to delete[] it. | 52   // destructor tries to delete[] it. | 
| 59   data_ = NULL; | 53   data_ = NULL; | 
| 60 } | 54 } | 
| 61 | 55 | 
| 62 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) | 56 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) | 
| 63     : IOBuffer(base->data()), | 57     : IOBuffer(base->data()), base_(base), size_(size), used_(0) { | 
| 64       base_(base), |  | 
| 65       size_(size), |  | 
| 66       used_(0) { |  | 
| 67 } | 58 } | 
| 68 | 59 | 
| 69 void DrainableIOBuffer::DidConsume(int bytes) { | 60 void DrainableIOBuffer::DidConsume(int bytes) { | 
| 70   SetOffset(used_ + bytes); | 61   SetOffset(used_ + bytes); | 
| 71 } | 62 } | 
| 72 | 63 | 
| 73 int DrainableIOBuffer::BytesRemaining() const { | 64 int DrainableIOBuffer::BytesRemaining() const { | 
| 74   return size_ - used_; | 65   return size_ - used_; | 
| 75 } | 66 } | 
| 76 | 67 | 
| 77 // Returns the number of consumed bytes. | 68 // Returns the number of consumed bytes. | 
| 78 int DrainableIOBuffer::BytesConsumed() const { | 69 int DrainableIOBuffer::BytesConsumed() const { | 
| 79   return used_; | 70   return used_; | 
| 80 } | 71 } | 
| 81 | 72 | 
| 82 void DrainableIOBuffer::SetOffset(int bytes) { | 73 void DrainableIOBuffer::SetOffset(int bytes) { | 
| 83   DCHECK_GE(bytes, 0); | 74   DCHECK_GE(bytes, 0); | 
| 84   DCHECK_LE(bytes, size_); | 75   DCHECK_LE(bytes, size_); | 
| 85   used_ = bytes; | 76   used_ = bytes; | 
| 86   data_ = base_->data() + used_; | 77   data_ = base_->data() + used_; | 
| 87 } | 78 } | 
| 88 | 79 | 
| 89 DrainableIOBuffer::~DrainableIOBuffer() { | 80 DrainableIOBuffer::~DrainableIOBuffer() { | 
| 90   // The buffer is owned by the |base_| instance. | 81   // The buffer is owned by the |base_| instance. | 
| 91   data_ = NULL; | 82   data_ = NULL; | 
| 92 } | 83 } | 
| 93 | 84 | 
| 94 GrowableIOBuffer::GrowableIOBuffer() | 85 GrowableIOBuffer::GrowableIOBuffer() : IOBuffer(), capacity_(0), offset_(0) { | 
| 95     : IOBuffer(), |  | 
| 96       capacity_(0), |  | 
| 97       offset_(0) { |  | 
| 98 } | 86 } | 
| 99 | 87 | 
| 100 void GrowableIOBuffer::SetCapacity(int capacity) { | 88 void GrowableIOBuffer::SetCapacity(int capacity) { | 
| 101   DCHECK_GE(capacity, 0); | 89   DCHECK_GE(capacity, 0); | 
| 102   // realloc will crash if it fails. | 90   // realloc will crash if it fails. | 
| 103   real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); | 91   real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); | 
| 104   capacity_ = capacity; | 92   capacity_ = capacity; | 
| 105   if (offset_ > capacity) | 93   if (offset_ > capacity) | 
| 106     set_offset(capacity); | 94     set_offset(capacity); | 
| 107   else | 95   else | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 120 } | 108 } | 
| 121 | 109 | 
| 122 char* GrowableIOBuffer::StartOfBuffer() { | 110 char* GrowableIOBuffer::StartOfBuffer() { | 
| 123   return real_data_.get(); | 111   return real_data_.get(); | 
| 124 } | 112 } | 
| 125 | 113 | 
| 126 GrowableIOBuffer::~GrowableIOBuffer() { | 114 GrowableIOBuffer::~GrowableIOBuffer() { | 
| 127   data_ = NULL; | 115   data_ = NULL; | 
| 128 } | 116 } | 
| 129 | 117 | 
| 130 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} | 118 PickledIOBuffer::PickledIOBuffer() : IOBuffer() { | 
|  | 119 } | 
| 131 | 120 | 
| 132 void PickledIOBuffer::Done() { | 121 void PickledIOBuffer::Done() { | 
| 133   data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); | 122   data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); | 
| 134 } | 123 } | 
| 135 | 124 | 
| 136 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } | 125 PickledIOBuffer::~PickledIOBuffer() { | 
|  | 126   data_ = NULL; | 
|  | 127 } | 
| 137 | 128 | 
| 138 WrappedIOBuffer::WrappedIOBuffer(const char* data) | 129 WrappedIOBuffer::WrappedIOBuffer(const char* data) | 
| 139     : IOBuffer(const_cast<char*>(data)) { | 130     : IOBuffer(const_cast<char*>(data)) { | 
| 140 } | 131 } | 
| 141 | 132 | 
| 142 WrappedIOBuffer::~WrappedIOBuffer() { | 133 WrappedIOBuffer::~WrappedIOBuffer() { | 
| 143   data_ = NULL; | 134   data_ = NULL; | 
| 144 } | 135 } | 
| 145 | 136 | 
| 146 }  // namespace net | 137 }  // namespace net | 
| OLD | NEW | 
|---|