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

Side by Side Diff: content/common/partial_circular_buffer.cc

Issue 15741003: Moving WebRTC logging related files from content to chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "content/common/partial_circular_buffer.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11 namespace content {
12
13 namespace {
14
15 inline uint32 Min3(uint32 a, uint32 b, uint32 c) {
16 return std::min(a, std::min(b, c));
17 }
18
19 } // namespace
20
21 PartialCircularBuffer::PartialCircularBuffer(void* buffer,
22 uint32 buffer_size)
23 : buffer_data_(reinterpret_cast<BufferData*>(buffer)),
24 memory_buffer_size_(buffer_size),
25 data_size_(0),
26 position_(0),
27 total_read_(0) {
28 uint32 header_size =
29 buffer_data_->data - reinterpret_cast<uint8*>(buffer_data_);
30 data_size_ = memory_buffer_size_ - header_size;
31
32 DCHECK(buffer_data_);
33 DCHECK_GE(memory_buffer_size_, header_size);
34 DCHECK_LE(buffer_data_->total_written, data_size_);
35 DCHECK_LT(buffer_data_->wrap_position, data_size_);
36 DCHECK_LT(buffer_data_->end_position, data_size_);
37 }
38
39 PartialCircularBuffer::PartialCircularBuffer(void* buffer,
40 uint32 buffer_size,
41 uint32 wrap_position)
42 : buffer_data_(reinterpret_cast<BufferData*>(buffer)),
43 memory_buffer_size_(buffer_size),
44 data_size_(0),
45 position_(0),
46 total_read_(0) {
47 uint32 header_size =
48 buffer_data_->data - reinterpret_cast<uint8*>(buffer_data_);
49 data_size_ = memory_buffer_size_ - header_size;
50
51 DCHECK(buffer_data_);
52 DCHECK_GE(memory_buffer_size_, header_size);
53 DCHECK_LT(wrap_position, data_size_);
54
55 buffer_data_->total_written = 0;
56 buffer_data_->wrap_position = wrap_position;
57 buffer_data_->end_position = 0;
58 }
59
60 uint32 PartialCircularBuffer::Read(void* buffer, uint32 buffer_size) {
61 DCHECK(buffer_data_);
62 if (total_read_ >= buffer_data_->total_written)
63 return 0;
64
65 uint8* buffer_uint8 = reinterpret_cast<uint8*>(buffer);
66 uint32 read = 0;
67
68 // Read from beginning part.
69 if (position_ < buffer_data_->wrap_position) {
70 uint32 to_wrap_pos = buffer_data_->wrap_position - position_;
71 uint32 to_eow = buffer_data_->total_written - total_read_;
72 uint32 to_read = Min3(buffer_size, to_wrap_pos, to_eow);
73 memcpy(buffer_uint8, buffer_data_->data + position_, to_read);
74 position_ += to_read;
75 total_read_ += to_read;
76 read += to_read;
77 if (position_ == buffer_data_->wrap_position &&
78 buffer_data_->total_written == data_size_) {
79 // We've read all the beginning part, set the position to the middle part.
80 // (The second condition above checks if the wrapping part is filled, i.e.
81 // writing has wrapped.)
82 position_ = buffer_data_->end_position;
83 }
84 if (read >= buffer_size) {
85 DCHECK_EQ(read, buffer_size);
86 return read;
87 }
88 if (read >= to_eow) {
89 DCHECK_EQ(read, to_eow);
90 DCHECK_EQ(total_read_, buffer_data_->total_written);
91 return read;
92 }
93 }
94
95 // Read from middle part.
96 DCHECK_GE(position_, buffer_data_->wrap_position);
97 if (position_ >= buffer_data_->end_position) {
98 uint32 remaining_buffer_size = buffer_size - read;
99 uint32 to_eof = data_size_ - position_;
100 uint32 to_eow = buffer_data_->total_written - total_read_;
101 uint32 to_read = Min3(remaining_buffer_size, to_eof, to_eow);
102 memcpy(buffer_uint8 + read, buffer_data_->data + position_, to_read);
103 position_ += to_read;
104 total_read_ += to_read;
105 read += to_read;
106 if (position_ == data_size_) {
107 // We've read all the middle part, set position to the end part.
108 position_ = buffer_data_->wrap_position;
109 }
110 if (read >= buffer_size) {
111 DCHECK_EQ(read, buffer_size);
112 return read;
113 }
114 if (total_read_ >= buffer_data_->total_written) {
115 DCHECK_EQ(total_read_, buffer_data_->total_written);
116 return read;
117 }
118 }
119
120 // Read from end part.
121 DCHECK_GE(position_, buffer_data_->wrap_position);
122 DCHECK_LT(position_, buffer_data_->end_position);
123 uint32 remaining_buffer_size = buffer_size - read;
124 uint32 to_eob = buffer_data_->end_position - position_;
125 uint32 to_eow = buffer_data_->total_written - total_read_;
126 uint32 to_read = Min3(remaining_buffer_size, to_eob, to_eow);
127 memcpy(buffer_uint8 + read, buffer_data_->data + position_, to_read);
128 position_ += to_read;
129 total_read_ += to_read;
130 read += to_read;
131 DCHECK_LE(read, buffer_size);
132 DCHECK_LE(total_read_, buffer_data_->total_written);
133 return read;
134 }
135
136 void PartialCircularBuffer::Write(const void* buffer, uint32 buffer_size) {
137 DCHECK(buffer_data_);
138 uint32 position_before_write = position_;
139
140 uint32 to_eof = data_size_ - position_;
141 uint32 to_write = std::min(buffer_size, to_eof);
142 DoWrite(buffer_data_->data + position_, buffer, to_write);
143 if (position_ >= data_size_) {
144 DCHECK_EQ(position_, data_size_);
145 position_ = buffer_data_->wrap_position;
146 }
147
148 if (to_write < buffer_size) {
149 uint32 remainder_to_write = buffer_size - to_write;
150 DCHECK_LT(position_, position_before_write);
151 DCHECK_LE(position_ + remainder_to_write, position_before_write);
152 DoWrite(buffer_data_->data + position_,
153 reinterpret_cast<const uint8*>(buffer) + to_write,
154 remainder_to_write);
155 }
156 }
157
158 void PartialCircularBuffer::DoWrite(void* dest, const void* src, uint32 num) {
159 memcpy(dest, src, num);
160 position_ += num;
161 buffer_data_->total_written =
162 std::min(buffer_data_->total_written + num, data_size_);
163 buffer_data_->end_position = position_;
164 }
165
166 } // namespace content
OLDNEW
« no previous file with comments | « content/common/partial_circular_buffer.h ('k') | content/common/partial_circular_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698