[email protected] | df18fe1c | 2011-04-12 23:46:21 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 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/base/io_buffer.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | |
| 9 | namespace net { |
| 10 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 11 | IOBuffer::IOBuffer() |
| 12 | : data_(NULL) { |
| 13 | } |
| 14 | |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 15 | IOBuffer::IOBuffer(int buffer_size) { |
[email protected] | bbbb0374 | 2009-03-11 20:55:54 | [diff] [blame] | 16 | DCHECK(buffer_size > 0); |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 17 | data_ = new char[buffer_size]; |
| 18 | } |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 19 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 20 | IOBuffer::IOBuffer(char* data) |
| 21 | : data_(data) { |
| 22 | } |
| 23 | |
| 24 | IOBuffer::~IOBuffer() { |
| 25 | delete[] data_; |
[email protected] | df18fe1c | 2011-04-12 23:46:21 | [diff] [blame] | 26 | data_ = NULL; |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | IOBufferWithSize::IOBufferWithSize(int size) |
| 30 | : IOBuffer(size), |
| 31 | size_(size) { |
| 32 | } |
| 33 | |
[email protected] | 7e4468d5 | 2010-09-22 19:42:00 | [diff] [blame] | 34 | IOBufferWithSize::~IOBufferWithSize() { |
| 35 | } |
| 36 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 37 | StringIOBuffer::StringIOBuffer(const std::string& s) |
| 38 | : IOBuffer(static_cast<char*>(NULL)), |
| 39 | string_data_(s) { |
| 40 | data_ = const_cast<char*>(string_data_.data()); |
| 41 | } |
| 42 | |
| 43 | StringIOBuffer::~StringIOBuffer() { |
| 44 | // We haven't allocated the buffer, so remove it before the base class |
| 45 | // destructor tries to delete[] it. |
| 46 | data_ = NULL; |
| 47 | } |
| 48 | |
| 49 | DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) |
| 50 | : IOBuffer(base->data()), |
| 51 | base_(base), |
| 52 | size_(size), |
| 53 | used_(0) { |
| 54 | } |
| 55 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 56 | void DrainableIOBuffer::DidConsume(int bytes) { |
| 57 | SetOffset(used_ + bytes); |
| 58 | } |
| 59 | |
| 60 | int DrainableIOBuffer::BytesRemaining() const { |
| 61 | return size_ - used_; |
| 62 | } |
| 63 | |
| 64 | // Returns the number of consumed bytes. |
| 65 | int DrainableIOBuffer::BytesConsumed() const { |
| 66 | return used_; |
| 67 | } |
| 68 | |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 69 | void DrainableIOBuffer::SetOffset(int bytes) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame^] | 70 | DCHECK_GE(bytes, 0); |
| 71 | DCHECK_LE(bytes, size_); |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 72 | used_ = bytes; |
| 73 | data_ = base_->data() + used_; |
| 74 | } |
| 75 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 76 | DrainableIOBuffer::~DrainableIOBuffer() { |
| 77 | // The buffer is owned by the |base_| instance. |
| 78 | data_ = NULL; |
| 79 | } |
| 80 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 81 | GrowableIOBuffer::GrowableIOBuffer() |
| 82 | : IOBuffer(), |
| 83 | capacity_(0), |
| 84 | offset_(0) { |
| 85 | } |
| 86 | |
[email protected] | 776e175 | 2009-11-10 18:51:34 | [diff] [blame] | 87 | void GrowableIOBuffer::SetCapacity(int capacity) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame^] | 88 | DCHECK_GE(capacity, 0); |
[email protected] | 776e175 | 2009-11-10 18:51:34 | [diff] [blame] | 89 | // realloc will crash if it fails. |
| 90 | real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 91 | capacity_ = capacity; |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 92 | if (offset_ > capacity) |
| 93 | set_offset(capacity); |
| 94 | else |
| 95 | set_offset(offset_); // The pointer may have changed. |
| 96 | } |
| 97 | |
| 98 | void GrowableIOBuffer::set_offset(int offset) { |
[email protected] | 52af5d5 | 2011-05-05 09:59:28 | [diff] [blame^] | 99 | DCHECK_GE(offset, 0); |
| 100 | DCHECK_LE(offset, capacity_); |
[email protected] | c19c715 | 2009-10-14 17:35:37 | [diff] [blame] | 101 | offset_ = offset; |
| 102 | data_ = real_data_.get() + offset; |
[email protected] | 84d4cee | 2009-06-18 23:46:58 | [diff] [blame] | 103 | } |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 104 | |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 105 | int GrowableIOBuffer::RemainingCapacity() { |
| 106 | return capacity_ - offset_; |
| 107 | } |
| 108 | |
| 109 | char* GrowableIOBuffer::StartOfBuffer() { |
| 110 | return real_data_.get(); |
| 111 | } |
| 112 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 113 | GrowableIOBuffer::~GrowableIOBuffer() { |
| 114 | data_ = NULL; |
| 115 | } |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 116 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 117 | PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 118 | |
| 119 | void PickledIOBuffer::Done() { |
| 120 | data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); |
| 121 | } |
| 122 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 123 | PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } |
[email protected] | 6d22a97 | 2010-07-21 15:47:19 | [diff] [blame] | 124 | |
| 125 | WrappedIOBuffer::WrappedIOBuffer(const char* data) |
| 126 | : IOBuffer(const_cast<char*>(data)) { |
| 127 | } |
| 128 | |
| 129 | WrappedIOBuffer::~WrappedIOBuffer() { |
| 130 | data_ = NULL; |
| 131 | } |
| 132 | |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 133 | } // namespace net |