Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: webrtc/base/bytebuffer.cc

Issue 1821083002: Split ByteBuffer into writer/reader objects. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/bytebuffer.h ('k') | webrtc/base/bytebuffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/bytebuffer.h" 11 #include "webrtc/base/bytebuffer.h"
12 12
13 #include <assert.h> 13 #include <assert.h>
14 #include <string.h> 14 #include <string.h>
15 15
16 #include <algorithm> 16 #include <algorithm>
17 17
18 #include "webrtc/base/basictypes.h" 18 #include "webrtc/base/basictypes.h"
19 #include "webrtc/base/byteorder.h" 19 #include "webrtc/base/byteorder.h"
20 20
21 namespace rtc { 21 namespace rtc {
22 22
23 static const int DEFAULT_SIZE = 4096; 23 static const int DEFAULT_SIZE = 4096;
24 24
25 ByteBuffer::ByteBuffer() { 25 ByteBufferWriter::ByteBufferWriter()
26 Construct(NULL, DEFAULT_SIZE, ORDER_NETWORK); 26 : ByteBuffer(ORDER_NETWORK) {
27 Construct(NULL, DEFAULT_SIZE);
27 } 28 }
28 29
29 ByteBuffer::ByteBuffer(ByteOrder byte_order) { 30 ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
30 Construct(NULL, DEFAULT_SIZE, byte_order); 31 : ByteBuffer(byte_order) {
32 Construct(NULL, DEFAULT_SIZE);
31 } 33 }
32 34
33 ByteBuffer::ByteBuffer(const char* bytes, size_t len) { 35 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
34 Construct(bytes, len, ORDER_NETWORK); 36 : ByteBuffer(ORDER_NETWORK) {
37 Construct(bytes, len);
35 } 38 }
36 39
37 ByteBuffer::ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order) { 40 ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len,
38 Construct(bytes, len, byte_order); 41 ByteOrder byte_order)
42 : ByteBuffer(byte_order) {
43 Construct(bytes, len);
39 } 44 }
40 45
41 ByteBuffer::ByteBuffer(const char* bytes) { 46 void ByteBufferWriter::Construct(const char* bytes, size_t len) {
42 Construct(bytes, strlen(bytes), ORDER_NETWORK);
43 }
44
45 ByteBuffer::ByteBuffer(const Buffer& buf) {
46 Construct(buf.data<char>(), buf.size(), ORDER_NETWORK);
47 }
48
49 void ByteBuffer::Construct(const char* bytes, size_t len,
50 ByteOrder byte_order) {
51 version_ = 0;
52 start_ = 0; 47 start_ = 0;
53 size_ = len; 48 size_ = len;
54 byte_order_ = byte_order;
55 bytes_ = new char[size_]; 49 bytes_ = new char[size_];
56 50
57 if (bytes) { 51 if (bytes) {
58 end_ = len; 52 end_ = len;
59 memcpy(bytes_, bytes, end_); 53 memcpy(bytes_, bytes, end_);
60 } else { 54 } else {
61 end_ = 0; 55 end_ = 0;
62 } 56 }
63 } 57 }
64 58
65 ByteBuffer::~ByteBuffer() { 59 ByteBufferWriter::~ByteBufferWriter() {
66 delete[] bytes_; 60 delete[] bytes_;
67 } 61 }
68 62
69 bool ByteBuffer::ReadUInt8(uint8_t* val) { 63 void ByteBufferWriter::WriteUInt8(uint8_t val) {
64 WriteBytes(reinterpret_cast<const char*>(&val), 1);
65 }
66
67 void ByteBufferWriter::WriteUInt16(uint16_t val) {
68 uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
69 WriteBytes(reinterpret_cast<const char*>(&v), 2);
70 }
71
72 void ByteBufferWriter::WriteUInt24(uint32_t val) {
73 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
74 char* start = reinterpret_cast<char*>(&v);
75 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
76 ++start;
77 }
78 WriteBytes(start, 3);
79 }
80
81 void ByteBufferWriter::WriteUInt32(uint32_t val) {
82 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
83 WriteBytes(reinterpret_cast<const char*>(&v), 4);
84 }
85
86 void ByteBufferWriter::WriteUInt64(uint64_t val) {
87 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
88 WriteBytes(reinterpret_cast<const char*>(&v), 8);
89 }
90
91 void ByteBufferWriter::WriteString(const std::string& val) {
92 WriteBytes(val.c_str(), val.size());
93 }
94
95 void ByteBufferWriter::WriteBytes(const char* val, size_t len) {
96 memcpy(ReserveWriteBuffer(len), val, len);
97 }
98
99 char* ByteBufferWriter::ReserveWriteBuffer(size_t len) {
100 if (Length() + len > Capacity())
101 Resize(Length() + len);
102
103 char* start = bytes_ + end_;
104 end_ += len;
105 return start;
106 }
107
108 void ByteBufferWriter::Resize(size_t size) {
109 size_t len = std::min(end_ - start_, size);
110 if (size <= size_) {
111 // Don't reallocate, just move data backwards
112 memmove(bytes_, bytes_ + start_, len);
113 } else {
114 // Reallocate a larger buffer.
115 size_ = std::max(size, 3 * size_ / 2);
116 char* new_bytes = new char[size_];
117 memcpy(new_bytes, bytes_ + start_, len);
118 delete [] bytes_;
119 bytes_ = new_bytes;
120 }
121 start_ = 0;
122 end_ = len;
123 }
124
125 void ByteBufferWriter::Clear() {
126 memset(bytes_, 0, size_);
127 start_ = end_ = 0;
128 }
129
130
131 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
132 : ByteBuffer(ORDER_NETWORK) {
133 Construct(bytes, len);
134 }
135
136 ByteBufferReader::ByteBufferReader(const char* bytes, size_t len,
137 ByteOrder byte_order)
138 : ByteBuffer(byte_order) {
139 Construct(bytes, len);
140 }
141
142 ByteBufferReader::ByteBufferReader(const char* bytes)
143 : ByteBuffer(ORDER_NETWORK) {
144 Construct(bytes, strlen(bytes));
145 }
146
147 ByteBufferReader::ByteBufferReader(const Buffer& buf)
148 : ByteBuffer(ORDER_NETWORK) {
149 Construct(buf.data<char>(), buf.size());
150 }
151
152 ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
153 : ByteBuffer(buf.Order()) {
154 Construct(buf.Data(), buf.Length());
155 }
156
157 void ByteBufferReader::Construct(const char* bytes, size_t len) {
158 bytes_ = bytes;
159 size_ = len;
160 start_ = 0;
161 end_ = len;
162 }
163
164 bool ByteBufferReader::ReadUInt8(uint8_t* val) {
70 if (!val) return false; 165 if (!val) return false;
71 166
72 return ReadBytes(reinterpret_cast<char*>(val), 1); 167 return ReadBytes(reinterpret_cast<char*>(val), 1);
73 } 168 }
74 169
75 bool ByteBuffer::ReadUInt16(uint16_t* val) { 170 bool ByteBufferReader::ReadUInt16(uint16_t* val) {
76 if (!val) return false; 171 if (!val) return false;
77 172
78 uint16_t v; 173 uint16_t v;
79 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) { 174 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
80 return false; 175 return false;
81 } else { 176 } else {
82 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost16(v) : v; 177 *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
83 return true; 178 return true;
84 } 179 }
85 } 180 }
86 181
87 bool ByteBuffer::ReadUInt24(uint32_t* val) { 182 bool ByteBufferReader::ReadUInt24(uint32_t* val) {
88 if (!val) return false; 183 if (!val) return false;
89 184
90 uint32_t v = 0; 185 uint32_t v = 0;
91 char* read_into = reinterpret_cast<char*>(&v); 186 char* read_into = reinterpret_cast<char*>(&v);
92 if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) { 187 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
93 ++read_into; 188 ++read_into;
94 } 189 }
95 190
96 if (!ReadBytes(read_into, 3)) { 191 if (!ReadBytes(read_into, 3)) {
97 return false; 192 return false;
98 } else { 193 } else {
99 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v; 194 *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
100 return true; 195 return true;
101 } 196 }
102 } 197 }
103 198
104 bool ByteBuffer::ReadUInt32(uint32_t* val) { 199 bool ByteBufferReader::ReadUInt32(uint32_t* val) {
105 if (!val) return false; 200 if (!val) return false;
106 201
107 uint32_t v; 202 uint32_t v;
108 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) { 203 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
109 return false; 204 return false;
110 } else { 205 } else {
111 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v; 206 *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
112 return true; 207 return true;
113 } 208 }
114 } 209 }
115 210
116 bool ByteBuffer::ReadUInt64(uint64_t* val) { 211 bool ByteBufferReader::ReadUInt64(uint64_t* val) {
117 if (!val) return false; 212 if (!val) return false;
118 213
119 uint64_t v; 214 uint64_t v;
120 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) { 215 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
121 return false; 216 return false;
122 } else { 217 } else {
123 *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v; 218 *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
124 return true; 219 return true;
125 } 220 }
126 } 221 }
127 222
128 bool ByteBuffer::ReadString(std::string* val, size_t len) { 223 bool ByteBufferReader::ReadString(std::string* val, size_t len) {
129 if (!val) return false; 224 if (!val) return false;
130 225
131 if (len > Length()) { 226 if (len > Length()) {
132 return false; 227 return false;
133 } else { 228 } else {
134 val->append(bytes_ + start_, len); 229 val->append(bytes_ + start_, len);
135 start_ += len; 230 start_ += len;
136 return true; 231 return true;
137 } 232 }
138 } 233 }
139 234
140 bool ByteBuffer::ReadBytes(char* val, size_t len) { 235 bool ByteBufferReader::ReadBytes(char* val, size_t len) {
141 if (len > Length()) { 236 if (len > Length()) {
142 return false; 237 return false;
143 } else { 238 } else {
144 memcpy(val, bytes_ + start_, len); 239 memcpy(val, bytes_ + start_, len);
145 start_ += len; 240 start_ += len;
146 return true; 241 return true;
147 } 242 }
148 } 243 }
149 244
150 void ByteBuffer::WriteUInt8(uint8_t val) { 245 bool ByteBufferReader::Consume(size_t size) {
151 WriteBytes(reinterpret_cast<const char*>(&val), 1);
152 }
153
154 void ByteBuffer::WriteUInt16(uint16_t val) {
155 uint16_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork16(val) : val;
156 WriteBytes(reinterpret_cast<const char*>(&v), 2);
157 }
158
159 void ByteBuffer::WriteUInt24(uint32_t val) {
160 uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
161 char* start = reinterpret_cast<char*>(&v);
162 if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
163 ++start;
164 }
165 WriteBytes(start, 3);
166 }
167
168 void ByteBuffer::WriteUInt32(uint32_t val) {
169 uint32_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
170 WriteBytes(reinterpret_cast<const char*>(&v), 4);
171 }
172
173 void ByteBuffer::WriteUInt64(uint64_t val) {
174 uint64_t v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork64(val) : val;
175 WriteBytes(reinterpret_cast<const char*>(&v), 8);
176 }
177
178 void ByteBuffer::WriteString(const std::string& val) {
179 WriteBytes(val.c_str(), val.size());
180 }
181
182 void ByteBuffer::WriteBytes(const char* val, size_t len) {
183 memcpy(ReserveWriteBuffer(len), val, len);
184 }
185
186 char* ByteBuffer::ReserveWriteBuffer(size_t len) {
187 if (Length() + len > Capacity())
188 Resize(Length() + len);
189
190 char* start = bytes_ + end_;
191 end_ += len;
192 return start;
193 }
194
195 void ByteBuffer::Resize(size_t size) {
196 size_t len = std::min(end_ - start_, size);
197 if (size <= size_) {
198 // Don't reallocate, just move data backwards
199 memmove(bytes_, bytes_ + start_, len);
200 } else {
201 // Reallocate a larger buffer.
202 size_ = std::max(size, 3 * size_ / 2);
203 char* new_bytes = new char[size_];
204 memcpy(new_bytes, bytes_ + start_, len);
205 delete [] bytes_;
206 bytes_ = new_bytes;
207 }
208 start_ = 0;
209 end_ = len;
210 ++version_;
211 }
212
213 bool ByteBuffer::Consume(size_t size) {
214 if (size > Length()) 246 if (size > Length())
215 return false; 247 return false;
216 start_ += size; 248 start_ += size;
217 return true; 249 return true;
218 } 250 }
219 251
220 ByteBuffer::ReadPosition ByteBuffer::GetReadPosition() const {
221 return ReadPosition(start_, version_);
222 }
223
224 bool ByteBuffer::SetReadPosition(const ReadPosition &position) {
225 if (position.version_ != version_) {
226 return false;
227 }
228 start_ = position.start_;
229 return true;
230 }
231
232 void ByteBuffer::Clear() {
233 memset(bytes_, 0, size_);
234 start_ = end_ = 0;
235 ++version_;
236 }
237
238 } // namespace rtc 252 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/bytebuffer.h ('k') | webrtc/base/bytebuffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698