OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 data_ = NULL; | 45 data_ = NULL; |
46 } | 46 } |
47 | 47 |
48 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) | 48 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) |
49 : IOBuffer(base->data()), | 49 : IOBuffer(base->data()), |
50 base_(base), | 50 base_(base), |
51 size_(size), | 51 size_(size), |
52 used_(0) { | 52 used_(0) { |
53 } | 53 } |
54 | 54 |
55 DrainableIOBuffer::~DrainableIOBuffer() { | |
56 // The buffer is owned by the |base_| instance. | |
57 data_ = NULL; | |
58 } | |
59 | |
60 void DrainableIOBuffer::DidConsume(int bytes) { | 55 void DrainableIOBuffer::DidConsume(int bytes) { |
61 SetOffset(used_ + bytes); | 56 SetOffset(used_ + bytes); |
62 } | 57 } |
63 | 58 |
64 int DrainableIOBuffer::BytesRemaining() const { | 59 int DrainableIOBuffer::BytesRemaining() const { |
65 return size_ - used_; | 60 return size_ - used_; |
66 } | 61 } |
67 | 62 |
68 // Returns the number of consumed bytes. | 63 // Returns the number of consumed bytes. |
69 int DrainableIOBuffer::BytesConsumed() const { | 64 int DrainableIOBuffer::BytesConsumed() const { |
70 return used_; | 65 return used_; |
71 } | 66 } |
72 | 67 |
73 void DrainableIOBuffer::SetOffset(int bytes) { | 68 void DrainableIOBuffer::SetOffset(int bytes) { |
74 DCHECK(bytes >= 0 && bytes <= size_); | 69 DCHECK(bytes >= 0 && bytes <= size_); |
75 used_ = bytes; | 70 used_ = bytes; |
76 data_ = base_->data() + used_; | 71 data_ = base_->data() + used_; |
77 } | 72 } |
78 | 73 |
| 74 DrainableIOBuffer::~DrainableIOBuffer() { |
| 75 // The buffer is owned by the |base_| instance. |
| 76 data_ = NULL; |
| 77 } |
| 78 |
79 GrowableIOBuffer::GrowableIOBuffer() | 79 GrowableIOBuffer::GrowableIOBuffer() |
80 : IOBuffer(), | 80 : IOBuffer(), |
81 capacity_(0), | 81 capacity_(0), |
82 offset_(0) { | 82 offset_(0) { |
83 } | 83 } |
84 | 84 |
85 GrowableIOBuffer::~GrowableIOBuffer() { | |
86 data_ = NULL; | |
87 } | |
88 | |
89 void GrowableIOBuffer::SetCapacity(int capacity) { | 85 void GrowableIOBuffer::SetCapacity(int capacity) { |
90 DCHECK(capacity >= 0); | 86 DCHECK(capacity >= 0); |
91 // realloc will crash if it fails. | 87 // realloc will crash if it fails. |
92 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); | 88 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); |
93 capacity_ = capacity; | 89 capacity_ = capacity; |
94 if (offset_ > capacity) | 90 if (offset_ > capacity) |
95 set_offset(capacity); | 91 set_offset(capacity); |
96 else | 92 else |
97 set_offset(offset_); // The pointer may have changed. | 93 set_offset(offset_); // The pointer may have changed. |
98 } | 94 } |
99 | 95 |
100 void GrowableIOBuffer::set_offset(int offset) { | 96 void GrowableIOBuffer::set_offset(int offset) { |
101 DCHECK(offset >= 0 && offset <= capacity_); | 97 DCHECK(offset >= 0 && offset <= capacity_); |
102 offset_ = offset; | 98 offset_ = offset; |
103 data_ = real_data_.get() + offset; | 99 data_ = real_data_.get() + offset; |
104 } | 100 } |
105 | 101 |
106 int GrowableIOBuffer::RemainingCapacity() { | 102 int GrowableIOBuffer::RemainingCapacity() { |
107 return capacity_ - offset_; | 103 return capacity_ - offset_; |
108 } | 104 } |
109 | 105 |
110 char* GrowableIOBuffer::StartOfBuffer() { | 106 char* GrowableIOBuffer::StartOfBuffer() { |
111 return real_data_.get(); | 107 return real_data_.get(); |
112 } | 108 } |
113 | 109 |
| 110 GrowableIOBuffer::~GrowableIOBuffer() { |
| 111 data_ = NULL; |
| 112 } |
| 113 |
114 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} | 114 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} |
115 | 115 |
116 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } | |
117 | |
118 void PickledIOBuffer::Done() { | 116 void PickledIOBuffer::Done() { |
119 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); | 117 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); |
120 } | 118 } |
121 | 119 |
| 120 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } |
122 | 121 |
123 WrappedIOBuffer::WrappedIOBuffer(const char* data) | 122 WrappedIOBuffer::WrappedIOBuffer(const char* data) |
124 : IOBuffer(const_cast<char*>(data)) { | 123 : IOBuffer(const_cast<char*>(data)) { |
125 } | 124 } |
126 | 125 |
127 WrappedIOBuffer::~WrappedIOBuffer() { | 126 WrappedIOBuffer::~WrappedIOBuffer() { |
128 data_ = NULL; | 127 data_ = NULL; |
129 } | 128 } |
130 | 129 |
131 } // namespace net | 130 } // namespace net |
OLD | NEW |