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() { | 43 StringIOBuffer::~StringIOBuffer() { |
50 // We haven't allocated the buffer, so remove it before the base class | 44 // We haven't allocated the buffer, so remove it before the base class |
51 // destructor tries to delete[] it. | 45 // destructor tries to delete[] it. |
52 data_ = NULL; | 46 data_ = NULL; |
53 } | 47 } |
54 | 48 |
55 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) | 49 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size) |
56 : IOBuffer(base->data()), | 50 : IOBuffer(base->data()), base_(base), size_(size), used_(0) { |
57 base_(base), | |
58 size_(size), | |
59 used_(0) { | |
60 } | 51 } |
61 | 52 |
62 void DrainableIOBuffer::DidConsume(int bytes) { | 53 void DrainableIOBuffer::DidConsume(int bytes) { |
63 SetOffset(used_ + bytes); | 54 SetOffset(used_ + bytes); |
64 } | 55 } |
65 | 56 |
66 int DrainableIOBuffer::BytesRemaining() const { | 57 int DrainableIOBuffer::BytesRemaining() const { |
67 return size_ - used_; | 58 return size_ - used_; |
68 } | 59 } |
69 | 60 |
70 // Returns the number of consumed bytes. | 61 // Returns the number of consumed bytes. |
71 int DrainableIOBuffer::BytesConsumed() const { | 62 int DrainableIOBuffer::BytesConsumed() const { |
72 return used_; | 63 return used_; |
73 } | 64 } |
74 | 65 |
75 void DrainableIOBuffer::SetOffset(int bytes) { | 66 void DrainableIOBuffer::SetOffset(int bytes) { |
76 DCHECK_GE(bytes, 0); | 67 DCHECK_GE(bytes, 0); |
77 DCHECK_LE(bytes, size_); | 68 DCHECK_LE(bytes, size_); |
78 used_ = bytes; | 69 used_ = bytes; |
79 data_ = base_->data() + used_; | 70 data_ = base_->data() + used_; |
80 } | 71 } |
81 | 72 |
82 DrainableIOBuffer::~DrainableIOBuffer() { | 73 DrainableIOBuffer::~DrainableIOBuffer() { |
83 // The buffer is owned by the |base_| instance. | 74 // The buffer is owned by the |base_| instance. |
84 data_ = NULL; | 75 data_ = NULL; |
85 } | 76 } |
86 | 77 |
87 GrowableIOBuffer::GrowableIOBuffer() | 78 GrowableIOBuffer::GrowableIOBuffer() : IOBuffer(), capacity_(0), offset_(0) { |
88 : IOBuffer(), | |
89 capacity_(0), | |
90 offset_(0) { | |
91 } | 79 } |
92 | 80 |
93 void GrowableIOBuffer::SetCapacity(int capacity) { | 81 void GrowableIOBuffer::SetCapacity(int capacity) { |
94 DCHECK_GE(capacity, 0); | 82 DCHECK_GE(capacity, 0); |
95 // realloc will crash if it fails. | 83 // realloc will crash if it fails. |
96 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); | 84 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity))); |
97 capacity_ = capacity; | 85 capacity_ = capacity; |
98 if (offset_ > capacity) | 86 if (offset_ > capacity) |
99 set_offset(capacity); | 87 set_offset(capacity); |
100 else | 88 else |
(...skipping 12 matching lines...) Expand all Loading... |
113 } | 101 } |
114 | 102 |
115 char* GrowableIOBuffer::StartOfBuffer() { | 103 char* GrowableIOBuffer::StartOfBuffer() { |
116 return real_data_.get(); | 104 return real_data_.get(); |
117 } | 105 } |
118 | 106 |
119 GrowableIOBuffer::~GrowableIOBuffer() { | 107 GrowableIOBuffer::~GrowableIOBuffer() { |
120 data_ = NULL; | 108 data_ = NULL; |
121 } | 109 } |
122 | 110 |
123 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {} | 111 PickledIOBuffer::PickledIOBuffer() : IOBuffer() { |
| 112 } |
124 | 113 |
125 void PickledIOBuffer::Done() { | 114 void PickledIOBuffer::Done() { |
126 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); | 115 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data())); |
127 } | 116 } |
128 | 117 |
129 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; } | 118 PickledIOBuffer::~PickledIOBuffer() { |
| 119 data_ = NULL; |
| 120 } |
130 | 121 |
131 WrappedIOBuffer::WrappedIOBuffer(const char* data) | 122 WrappedIOBuffer::WrappedIOBuffer(const char* data) |
132 : IOBuffer(const_cast<char*>(data)) { | 123 : IOBuffer(const_cast<char*>(data)) { |
133 } | 124 } |
134 | 125 |
135 WrappedIOBuffer::~WrappedIOBuffer() { | 126 WrappedIOBuffer::~WrappedIOBuffer() { |
136 data_ = NULL; | 127 data_ = NULL; |
137 } | 128 } |
138 | 129 |
139 } // namespace net | 130 } // namespace net |
OLD | NEW |