Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / base / io_buffer.cc
blob3ab762aad22e0414cf3f107b2603e6779e089143
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
3 // found in the LICENSE file.
5 #include "net/base/io_buffer.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
10 namespace net {
12 namespace {
14 // TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets
15 // be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate
16 // this move (since LOTS of code needs to be updated), both "size_t" and "int
17 // are being accepted. When using "size_t" this function ensures that it can be
18 // safely converted to an "int" without truncation.
19 void AssertValidBufferSize(size_t size) {
20 base::CheckedNumeric<int>(size).ValueOrDie();
23 void AssertValidBufferSize(int size) {
24 CHECK_GE(size, 0);
27 } // namespace
29 IOBuffer::IOBuffer()
30 : data_(NULL) {
33 IOBuffer::IOBuffer(int buffer_size) {
34 AssertValidBufferSize(buffer_size);
35 data_ = new char[buffer_size];
38 IOBuffer::IOBuffer(size_t buffer_size) {
39 AssertValidBufferSize(buffer_size);
40 data_ = new char[buffer_size];
43 IOBuffer::IOBuffer(char* data)
44 : data_(data) {
47 IOBuffer::~IOBuffer() {
48 delete[] data_;
49 data_ = NULL;
52 IOBufferWithSize::IOBufferWithSize(int size)
53 : IOBuffer(size),
54 size_(size) {
55 AssertValidBufferSize(size);
58 IOBufferWithSize::IOBufferWithSize(size_t size) : IOBuffer(size), size_(size) {
59 // Note: Size check is done in superclass' constructor.
62 IOBufferWithSize::IOBufferWithSize(char* data, int size)
63 : IOBuffer(data),
64 size_(size) {
65 AssertValidBufferSize(size);
68 IOBufferWithSize::IOBufferWithSize(char* data, size_t size)
69 : IOBuffer(data), size_(size) {
70 AssertValidBufferSize(size);
73 IOBufferWithSize::~IOBufferWithSize() {
76 StringIOBuffer::StringIOBuffer(const std::string& s)
77 : IOBuffer(static_cast<char*>(NULL)),
78 string_data_(s) {
79 AssertValidBufferSize(s.size());
80 data_ = const_cast<char*>(string_data_.data());
83 StringIOBuffer::StringIOBuffer(scoped_ptr<std::string> s)
84 : IOBuffer(static_cast<char*>(NULL)) {
85 AssertValidBufferSize(s->size());
86 string_data_.swap(*s.get());
87 data_ = const_cast<char*>(string_data_.data());
90 StringIOBuffer::~StringIOBuffer() {
91 // We haven't allocated the buffer, so remove it before the base class
92 // destructor tries to delete[] it.
93 data_ = NULL;
96 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
97 : IOBuffer(base->data()),
98 base_(base),
99 size_(size),
100 used_(0) {
101 AssertValidBufferSize(size);
104 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, size_t size)
105 : IOBuffer(base->data()), base_(base), size_(size), used_(0) {
106 AssertValidBufferSize(size);
109 void DrainableIOBuffer::DidConsume(int bytes) {
110 SetOffset(used_ + bytes);
113 int DrainableIOBuffer::BytesRemaining() const {
114 return size_ - used_;
117 // Returns the number of consumed bytes.
118 int DrainableIOBuffer::BytesConsumed() const {
119 return used_;
122 void DrainableIOBuffer::SetOffset(int bytes) {
123 DCHECK_GE(bytes, 0);
124 DCHECK_LE(bytes, size_);
125 used_ = bytes;
126 data_ = base_->data() + used_;
129 DrainableIOBuffer::~DrainableIOBuffer() {
130 // The buffer is owned by the |base_| instance.
131 data_ = NULL;
134 GrowableIOBuffer::GrowableIOBuffer()
135 : IOBuffer(),
136 capacity_(0),
137 offset_(0) {
140 void GrowableIOBuffer::SetCapacity(int capacity) {
141 DCHECK_GE(capacity, 0);
142 // realloc will crash if it fails.
143 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
144 capacity_ = capacity;
145 if (offset_ > capacity)
146 set_offset(capacity);
147 else
148 set_offset(offset_); // The pointer may have changed.
151 void GrowableIOBuffer::set_offset(int offset) {
152 DCHECK_GE(offset, 0);
153 DCHECK_LE(offset, capacity_);
154 offset_ = offset;
155 data_ = real_data_.get() + offset;
158 int GrowableIOBuffer::RemainingCapacity() {
159 return capacity_ - offset_;
162 char* GrowableIOBuffer::StartOfBuffer() {
163 return real_data_.get();
166 GrowableIOBuffer::~GrowableIOBuffer() {
167 data_ = NULL;
170 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {
173 void PickledIOBuffer::Done() {
174 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
177 PickledIOBuffer::~PickledIOBuffer() {
178 data_ = NULL;
181 WrappedIOBuffer::WrappedIOBuffer(const char* data)
182 : IOBuffer(const_cast<char*>(data)) {
185 WrappedIOBuffer::~WrappedIOBuffer() {
186 data_ = NULL;
189 } // namespace net