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

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

Issue 13473005: Adding partially circular memory buffer wrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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/circular_memory_buffer.h"
6
7 #include "base/logging.h"
8
9 #define MIN3(a, b, c) \
10 ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
tommi (sloooow) - chröme 2013/04/02 19:34:08 nit: std::min(a, std::min(b, c)) ? Since you only
Henrik Grunell 2013/04/03 12:47:27 Done.
11
12 namespace content {
13
14 CircularMemoryBuffer::CircularMemoryBuffer(void* buffer,
15 size_t buffer_size)
16 : memory_buffer_(static_cast<int8*>(buffer)),
17 memory_buffer_size_(buffer_size),
18 position_(0),
19 wrap_position_(0),
20 end_position_(0),
21 total_written_(0),
22 total_read_(0) {
23 DCHECK(memory_buffer_);
24 DCHECK_GT(memory_buffer_size_, 3 * sizeof(int32));
tommi (sloooow) - chröme 2013/04/02 19:34:08 document this requirement?
Henrik Grunell 2013/04/03 12:47:27 Done.
25 }
26
27 void CircularMemoryBuffer::InitRead() {
28 GetHeaderData();
29 position_ = 3;
tommi (sloooow) - chröme 2013/04/02 19:34:08 comment?
Henrik Grunell 2013/04/03 12:47:27 I'm using sizeof on the new header struct to deter
30 total_read_ = 0;
31 DCHECK_LE(total_written_, memory_buffer_size_);
32 DCHECK_LT(wrap_position_, memory_buffer_size_);
33 DCHECK_LT(end_position_, memory_buffer_size_);
34 }
35
36 void CircularMemoryBuffer::InitWrite(size_t wrap_position) {
37 DCHECK_LT(wrap_position, memory_buffer_size_);
38 position_ = 3;
tommi (sloooow) - chröme 2013/04/02 19:34:08 seems like it would be good to have a well named c
Henrik Grunell 2013/04/03 12:47:27 I'm using sizeof on the new header struct to deter
39 wrap_position_ = wrap_position;
40 SetHeaderData(0, wrap_position_, position_);
41 }
42
43 size_t CircularMemoryBuffer::Read(void* buffer, size_t buffer_size) {
44 if (total_read_ >= total_written_)
45 return 0;
46
47 int8* buffer_int8 = static_cast<int8*>(buffer);
tommi (sloooow) - chröme 2013/04/02 19:34:08 reinterpret_cast (void and int8 aren't related typ
Henrik Grunell 2013/04/03 12:47:27 Done.
48 size_t read = 0;
49
50 // Read from beginning part.
51 if (position_ < wrap_position_) {
52 size_t to_marked_pos = wrap_position_ - position_;
53 size_t to_eow = total_written_ - total_read_;
54 size_t to_read = MIN3(buffer_size, to_marked_pos, to_eow);
55 memcpy(buffer_int8 + read, memory_buffer_ + position_, to_read);
56 position_ += to_read;
57 read += to_read;
58 if (position_ == wrap_position_ &&
59 total_written_ == memory_buffer_size_) {
60 // We've read all the beginning part, set the position to the middle part.
61 // (The second condition above checks if the wrapping part is filled, i.e.
62 // writing has wrapped.)
63 position_ = end_position_;
64 }
65 if (read >= buffer_size) {
66 DCHECK(read == buffer_size);
67 return read;
68 }
69 if (read >= to_eow) {
70 DCHECK(read == to_eow);
71 return read;
72 }
73 }
74
75 // Read from middle part.
76 DCHECK(position_ >= wrap_position_);
77 if (position_ >= end_position_) {
78 size_t remaining_buffer_size = buffer_size - read;
79 size_t to_eof = memory_buffer_size_ - position_;
80 size_t to_read = remaining_buffer_size < to_eof ?
tommi (sloooow) - chröme 2013/04/02 19:34:08 std::min?
Henrik Grunell 2013/04/03 12:47:27 Changed to use MIN3 macro.
81 remaining_buffer_size : to_eof;
82 memcpy(buffer_int8 + read, memory_buffer_ + position_, to_read);
83 position_ += to_read;
84 read += to_read;
85 if (position_ == memory_buffer_size_) {
86 // We've read all the middle part, set position to the end part.
87 position_ = wrap_position_;
88 }
89 if (read >= buffer_size) {
90 DCHECK(read == buffer_size);
91 return read;
92 }
93 }
94
95 // Read from end part.
96 DCHECK(position_ >= wrap_position_ && position_ < end_position_);
97 size_t remaining_buffer_size = buffer_size - read;
98 size_t to_eob = end_position_ - position_;
99 size_t to_read = remaining_buffer_size < to_eob ?
tommi (sloooow) - chröme 2013/04/02 19:34:08 std::min
Henrik Grunell 2013/04/03 12:47:27 Changed to use MIN3 macro.
100 remaining_buffer_size : to_eob;
101 memcpy(buffer_int8 + read, memory_buffer_ + position_, to_read);
102 position_ += to_read;
103 read += to_read;
104 DCHECK(read <= buffer_size);
105 return read;
106 }
107
108 void CircularMemoryBuffer::Write(const void* buffer, size_t buffer_size) {
109 size_t position_before_write = position_;
110
111 size_t to_eof = memory_buffer_size_ - position_;
112 size_t to_write = buffer_size < to_eof ? buffer_size : to_eof;
tommi (sloooow) - chröme 2013/04/02 19:34:08 here as well (and elsewhere if there are more)
Henrik Grunell 2013/04/03 12:47:27 Done. No other places.
113 memcpy(memory_buffer_ + position_, buffer, to_write);
114 position_ += to_write;
115
116 if (position_ >= memory_buffer_size_) {
117 DCHECK_EQ(position_, memory_buffer_size_);
118 position_ = wrap_position_;
119 }
120
121 if (to_write < buffer_size) {
122 size_t remainder_to_write = buffer_size - to_write;
123 DCHECK_LT(position_, position_before_write);
124 DCHECK_LE(position_ + remainder_to_write, position_before_write);
125 memcpy(memory_buffer_ + position_,
126 static_cast<const int8*>(buffer) + to_write,
tommi (sloooow) - chröme 2013/04/02 19:34:08 reinterpret_cast
Henrik Grunell 2013/04/03 12:47:27 Done.
127 remainder_to_write);
128 position_ += remainder_to_write;
129 }
130
131 UpdateHeaderData(buffer_size, position_);
132 }
133
134 void CircularMemoryBuffer::GetHeaderData() {
135 int32* memory_buffer_int32 = reinterpret_cast<int32*>(memory_buffer_);
136
137 total_written_ = static_cast<size_t>(*memory_buffer_int32);
tommi (sloooow) - chröme 2013/04/02 19:34:08 nit: what about using a struct or indices instead
Henrik Grunell 2013/04/03 12:47:27 Using a struct now. Not sure if you wanted it only
138 ++memory_buffer_int32;
139 wrap_position_ = static_cast<size_t>(*memory_buffer_int32);
140 ++memory_buffer_int32;
141 end_position_ = static_cast<size_t>(*memory_buffer_int32);
142 }
143
144 void CircularMemoryBuffer::SetHeaderData(size_t length,
145 size_t wrap_position,
146 size_t position) {
147 int32* memory_buffer_int32 = reinterpret_cast<int32*>(memory_buffer_);
tommi (sloooow) - chröme 2013/04/02 19:34:08 same here and below
Henrik Grunell 2013/04/03 12:47:27 Done.
148
149 *memory_buffer_int32 = static_cast<int32>(length);
150 ++memory_buffer_int32;
151 *memory_buffer_int32 = static_cast<int32>(wrap_position);
152 ++memory_buffer_int32;
153 *memory_buffer_int32 = static_cast<int32>(position);
154 }
155
156 void CircularMemoryBuffer::UpdateHeaderData(size_t added_length,
157 size_t new_position) {
158 int32* memory_buffer_int32 = reinterpret_cast<int32*>(memory_buffer_);
159
160 size_t new_length = *memory_buffer_int32 + added_length;
161 if (new_length > memory_buffer_size_)
162 new_length = memory_buffer_size_;
163 *memory_buffer_int32 = static_cast<int32>(new_length);
164 memory_buffer_int32 += 2;
165 *memory_buffer_int32 = static_cast<int32>(new_position);
166 }
167
168 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698