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

Side by Side Diff: mojo/edk/system/local_data_pipe_impl.cc

Issue 926553006: Make DataPipe own an impl. (Closed) Base URL: https://github.com/domokit/mojo.git@local_data_pipe_impl_1
Patch Set: rebased Created 5 years, 10 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 // TODO(vtl): I currently potentially overflow in doing index calculations. 5 // TODO(vtl): I currently potentially overflow in doing index calculations.
6 // E.g., |start_index_| and |current_num_bytes_| fit into a |uint32_t|, but 6 // E.g., |start_index_| and |current_num_bytes_| fit into a |uint32_t|, but
7 // their sum may not. This is bad and poses a security risk. (We're currently 7 // their sum may not. This is bad and poses a security risk. (We're currently
8 // saved by the limit on capacity -- the maximum size of the buffer, checked in 8 // saved by the limit on capacity -- the maximum size of the buffer, checked in
9 // |DataPipe::ValidateOptions()|, is currently sufficiently small.) 9 // |DataPipe::ValidateOptions()|, is currently sufficiently small.)
10 10
11 #include "mojo/edk/system/local_data_pipe_impl.h" 11 #include "mojo/edk/system/local_data_pipe_impl.h"
12 12
13 #include <string.h> 13 #include <string.h>
14 14
15 #include <algorithm> 15 #include <algorithm>
16 16
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "mojo/edk/system/configuration.h" 18 #include "mojo/edk/system/configuration.h"
19 #include "mojo/edk/system/data_pipe.h"
19 20
20 namespace mojo { 21 namespace mojo {
21 namespace system { 22 namespace system {
22 23
23 LocalDataPipeImpl::LocalDataPipeImpl(const MojoCreateDataPipeOptions& options) 24 LocalDataPipeImpl::LocalDataPipeImpl()
24 : DataPipe(true, true, options), start_index_(0), current_num_bytes_(0) { 25 : start_index_(0), current_num_bytes_(0) {
25 // Note: |buffer_| is lazily allocated, since a common case will be that one 26 // Note: |buffer_| is lazily allocated, since a common case will be that one
26 // of the handles is immediately passed off to another process. 27 // of the handles is immediately passed off to another process.
27 } 28 }
28 29
29 LocalDataPipeImpl::~LocalDataPipeImpl() { 30 LocalDataPipeImpl::~LocalDataPipeImpl() {
30 } 31 }
31 32
32 void LocalDataPipeImpl::ProducerCloseImplNoLock() { 33 // static
34 DataPipe* LocalDataPipeImpl::Create(
35 const MojoCreateDataPipeOptions& validated_options) {
36 return new DataPipe(true, true, validated_options,
37 make_scoped_ptr(new LocalDataPipeImpl()));
38 }
39
40 void LocalDataPipeImpl::ProducerClose() {
33 // If the consumer is still open and we still have data, we have to keep the 41 // If the consumer is still open and we still have data, we have to keep the
34 // buffer around. Currently, we won't free it even if it empties later. (We 42 // buffer around. Currently, we won't free it even if it empties later. (We
35 // could do this -- requiring a check on every read -- but that seems to be 43 // could do this -- requiring a check on every read -- but that seems to be
36 // optimizing for the uncommon case.) 44 // optimizing for the uncommon case.)
37 if (!consumer_open_no_lock() || !current_num_bytes_) { 45 if (!consumer_open() || !current_num_bytes_) {
38 // Note: There can only be a two-phase *read* (by the consumer) if we still 46 // Note: There can only be a two-phase *read* (by the consumer) if we still
39 // have data. 47 // have data.
40 DCHECK(!consumer_in_two_phase_read_no_lock()); 48 DCHECK(!consumer_in_two_phase_read());
41 DestroyBufferNoLock(); 49 DestroyBuffer();
42 } 50 }
43 } 51 }
44 52
45 MojoResult LocalDataPipeImpl::ProducerWriteDataImplNoLock( 53 MojoResult LocalDataPipeImpl::ProducerWriteData(
46 UserPointer<const void> elements, 54 UserPointer<const void> elements,
47 UserPointer<uint32_t> num_bytes, 55 UserPointer<uint32_t> num_bytes,
48 uint32_t max_num_bytes_to_write, 56 uint32_t max_num_bytes_to_write,
49 uint32_t min_num_bytes_to_write) { 57 uint32_t min_num_bytes_to_write) {
50 DCHECK_EQ(max_num_bytes_to_write % element_num_bytes(), 0u); 58 DCHECK_EQ(max_num_bytes_to_write % element_num_bytes(), 0u);
51 DCHECK_EQ(min_num_bytes_to_write % element_num_bytes(), 0u); 59 DCHECK_EQ(min_num_bytes_to_write % element_num_bytes(), 0u);
52 DCHECK_GT(max_num_bytes_to_write, 0u); 60 DCHECK_GT(max_num_bytes_to_write, 0u);
53 DCHECK(consumer_open_no_lock()); 61 DCHECK(consumer_open());
54 62
55 size_t num_bytes_to_write = 0; 63 size_t num_bytes_to_write = 0;
56 if (may_discard()) { 64 if (may_discard()) {
57 if (min_num_bytes_to_write > capacity_num_bytes()) 65 if (min_num_bytes_to_write > capacity_num_bytes())
58 return MOJO_RESULT_OUT_OF_RANGE; 66 return MOJO_RESULT_OUT_OF_RANGE;
59 67
60 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write), 68 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write),
61 capacity_num_bytes()); 69 capacity_num_bytes());
62 if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { 70 if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
63 // Discard as much as needed (discard oldest first). 71 // Discard as much as needed (discard oldest first).
64 MarkDataAsConsumedNoLock(num_bytes_to_write - 72 MarkDataAsConsumed(num_bytes_to_write -
65 (capacity_num_bytes() - current_num_bytes_)); 73 (capacity_num_bytes() - current_num_bytes_));
66 // No need to wake up write waiters, since we're definitely going to leave 74 // No need to wake up write waiters, since we're definitely going to leave
67 // the buffer full. 75 // the buffer full.
68 } 76 }
69 } else { 77 } else {
70 if (min_num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { 78 if (min_num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
71 // Don't return "should wait" since you can't wait for a specified amount 79 // Don't return "should wait" since you can't wait for a specified amount
72 // of data. 80 // of data.
73 return MOJO_RESULT_OUT_OF_RANGE; 81 return MOJO_RESULT_OUT_OF_RANGE;
74 } 82 }
75 83
76 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write), 84 num_bytes_to_write = std::min(static_cast<size_t>(max_num_bytes_to_write),
77 capacity_num_bytes() - current_num_bytes_); 85 capacity_num_bytes() - current_num_bytes_);
78 } 86 }
79 if (num_bytes_to_write == 0) 87 if (num_bytes_to_write == 0)
80 return MOJO_RESULT_SHOULD_WAIT; 88 return MOJO_RESULT_SHOULD_WAIT;
81 89
82 // The amount we can write in our first |memcpy()|. 90 // The amount we can write in our first |memcpy()|.
83 size_t num_bytes_to_write_first = 91 size_t num_bytes_to_write_first =
84 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock()); 92 std::min(num_bytes_to_write, GetMaxNumBytesToWrite());
85 // Do the first (and possibly only) |memcpy()|. 93 // Do the first (and possibly only) |memcpy()|.
86 size_t first_write_index = 94 size_t first_write_index =
87 (start_index_ + current_num_bytes_) % capacity_num_bytes(); 95 (start_index_ + current_num_bytes_) % capacity_num_bytes();
88 EnsureBufferNoLock(); 96 EnsureBuffer();
89 elements.GetArray(buffer_.get() + first_write_index, 97 elements.GetArray(buffer_.get() + first_write_index,
90 num_bytes_to_write_first); 98 num_bytes_to_write_first);
91 99
92 if (num_bytes_to_write_first < num_bytes_to_write) { 100 if (num_bytes_to_write_first < num_bytes_to_write) {
93 // The "second write index" is zero. 101 // The "second write index" is zero.
94 elements.At(num_bytes_to_write_first) 102 elements.At(num_bytes_to_write_first)
95 .GetArray(buffer_.get(), num_bytes_to_write - num_bytes_to_write_first); 103 .GetArray(buffer_.get(), num_bytes_to_write - num_bytes_to_write_first);
96 } 104 }
97 105
98 current_num_bytes_ += num_bytes_to_write; 106 current_num_bytes_ += num_bytes_to_write;
99 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); 107 DCHECK_LE(current_num_bytes_, capacity_num_bytes());
100 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write)); 108 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_write));
101 return MOJO_RESULT_OK; 109 return MOJO_RESULT_OK;
102 } 110 }
103 111
104 MojoResult LocalDataPipeImpl::ProducerBeginWriteDataImplNoLock( 112 MojoResult LocalDataPipeImpl::ProducerBeginWriteData(
105 UserPointer<void*> buffer, 113 UserPointer<void*> buffer,
106 UserPointer<uint32_t> buffer_num_bytes, 114 UserPointer<uint32_t> buffer_num_bytes,
107 uint32_t min_num_bytes_to_write) { 115 uint32_t min_num_bytes_to_write) {
108 DCHECK(consumer_open_no_lock()); 116 DCHECK(consumer_open());
109 117
110 // The index we need to start writing at. 118 // The index we need to start writing at.
111 size_t write_index = 119 size_t write_index =
112 (start_index_ + current_num_bytes_) % capacity_num_bytes(); 120 (start_index_ + current_num_bytes_) % capacity_num_bytes();
113 121
114 size_t max_num_bytes_to_write = GetMaxNumBytesToWriteNoLock(); 122 size_t max_num_bytes_to_write = GetMaxNumBytesToWrite();
115 if (min_num_bytes_to_write > max_num_bytes_to_write) { 123 if (min_num_bytes_to_write > max_num_bytes_to_write) {
116 // In "may discard" mode, we can always write from the write index to the 124 // In "may discard" mode, we can always write from the write index to the
117 // end of the buffer. 125 // end of the buffer.
118 if (may_discard() && 126 if (may_discard() &&
119 min_num_bytes_to_write <= capacity_num_bytes() - write_index) { 127 min_num_bytes_to_write <= capacity_num_bytes() - write_index) {
120 // To do so, we need to discard an appropriate amount of data. 128 // To do so, we need to discard an appropriate amount of data.
121 // We should only reach here if the start index is after the write index! 129 // We should only reach here if the start index is after the write index!
122 DCHECK_GE(start_index_, write_index); 130 DCHECK_GE(start_index_, write_index);
123 DCHECK_GT(min_num_bytes_to_write - max_num_bytes_to_write, 0u); 131 DCHECK_GT(min_num_bytes_to_write - max_num_bytes_to_write, 0u);
124 MarkDataAsConsumedNoLock(min_num_bytes_to_write - max_num_bytes_to_write); 132 MarkDataAsConsumed(min_num_bytes_to_write - max_num_bytes_to_write);
125 max_num_bytes_to_write = min_num_bytes_to_write; 133 max_num_bytes_to_write = min_num_bytes_to_write;
126 } else { 134 } else {
127 // Don't return "should wait" since you can't wait for a specified amount 135 // Don't return "should wait" since you can't wait for a specified amount
128 // of data. 136 // of data.
129 return MOJO_RESULT_OUT_OF_RANGE; 137 return MOJO_RESULT_OUT_OF_RANGE;
130 } 138 }
131 } 139 }
132 140
133 // Don't go into a two-phase write if there's no room. 141 // Don't go into a two-phase write if there's no room.
134 if (max_num_bytes_to_write == 0) 142 if (max_num_bytes_to_write == 0)
135 return MOJO_RESULT_SHOULD_WAIT; 143 return MOJO_RESULT_SHOULD_WAIT;
136 144
137 EnsureBufferNoLock(); 145 EnsureBuffer();
138 buffer.Put(buffer_.get() + write_index); 146 buffer.Put(buffer_.get() + write_index);
139 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_write)); 147 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_write));
140 set_producer_two_phase_max_num_bytes_written_no_lock( 148 set_producer_two_phase_max_num_bytes_written(
141 static_cast<uint32_t>(max_num_bytes_to_write)); 149 static_cast<uint32_t>(max_num_bytes_to_write));
142 return MOJO_RESULT_OK; 150 return MOJO_RESULT_OK;
143 } 151 }
144 152
145 MojoResult LocalDataPipeImpl::ProducerEndWriteDataImplNoLock( 153 MojoResult LocalDataPipeImpl::ProducerEndWriteData(uint32_t num_bytes_written) {
146 uint32_t num_bytes_written) { 154 DCHECK_LE(num_bytes_written, producer_two_phase_max_num_bytes_written());
147 DCHECK_LE(num_bytes_written,
148 producer_two_phase_max_num_bytes_written_no_lock());
149 current_num_bytes_ += num_bytes_written; 155 current_num_bytes_ += num_bytes_written;
150 DCHECK_LE(current_num_bytes_, capacity_num_bytes()); 156 DCHECK_LE(current_num_bytes_, capacity_num_bytes());
151 set_producer_two_phase_max_num_bytes_written_no_lock(0); 157 set_producer_two_phase_max_num_bytes_written(0);
152 return MOJO_RESULT_OK; 158 return MOJO_RESULT_OK;
153 } 159 }
154 160
155 HandleSignalsState LocalDataPipeImpl::ProducerGetHandleSignalsStateImplNoLock() 161 HandleSignalsState LocalDataPipeImpl::ProducerGetHandleSignalsState() const {
156 const {
157 HandleSignalsState rv; 162 HandleSignalsState rv;
158 if (consumer_open_no_lock()) { 163 if (consumer_open()) {
159 if ((may_discard() || current_num_bytes_ < capacity_num_bytes()) && 164 if ((may_discard() || current_num_bytes_ < capacity_num_bytes()) &&
160 !producer_in_two_phase_write_no_lock()) 165 !producer_in_two_phase_write())
161 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_WRITABLE; 166 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_WRITABLE;
162 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_WRITABLE; 167 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_WRITABLE;
163 } else { 168 } else {
164 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; 169 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
165 } 170 }
166 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; 171 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
167 return rv; 172 return rv;
168 } 173 }
169 174
170 void LocalDataPipeImpl::ProducerStartSerializeImplNoLock( 175 void LocalDataPipeImpl::ProducerStartSerialize(Channel* channel,
171 Channel* channel, 176 size_t* max_size,
172 size_t* max_size, 177 size_t* max_platform_handles) {
173 size_t* max_platform_handles) {
174 // TODO(vtl): Support serializing producer data pipe handles. 178 // TODO(vtl): Support serializing producer data pipe handles.
175 *max_size = 0; 179 *max_size = 0;
176 *max_platform_handles = 0; 180 *max_platform_handles = 0;
177 } 181 }
178 182
179 bool LocalDataPipeImpl::ProducerEndSerializeImplNoLock( 183 bool LocalDataPipeImpl::ProducerEndSerialize(
180 Channel* channel, 184 Channel* channel,
181 void* destination, 185 void* destination,
182 size_t* actual_size, 186 size_t* actual_size,
183 embedder::PlatformHandleVector* platform_handles) { 187 embedder::PlatformHandleVector* platform_handles) {
184 // TODO(vtl): Support serializing producer data pipe handles. 188 // TODO(vtl): Support serializing producer data pipe handles.
185 ProducerCloseNoLock(); 189 owner()->ProducerCloseNoLock();
186 return false; 190 return false;
187 } 191 }
188 192
189 void LocalDataPipeImpl::ConsumerCloseImplNoLock() { 193 void LocalDataPipeImpl::ConsumerClose() {
190 // If the producer is around and in a two-phase write, we have to keep the 194 // If the producer is around and in a two-phase write, we have to keep the
191 // buffer around. (We then don't free it until the producer is closed. This 195 // buffer around. (We then don't free it until the producer is closed. This
192 // could be rectified, but again seems like optimizing for the uncommon case.) 196 // could be rectified, but again seems like optimizing for the uncommon case.)
193 if (!producer_open_no_lock() || !producer_in_two_phase_write_no_lock()) 197 if (!producer_open() || !producer_in_two_phase_write())
194 DestroyBufferNoLock(); 198 DestroyBuffer();
195 current_num_bytes_ = 0; 199 current_num_bytes_ = 0;
196 } 200 }
197 201
198 MojoResult LocalDataPipeImpl::ConsumerReadDataImplNoLock( 202 MojoResult LocalDataPipeImpl::ConsumerReadData(UserPointer<void> elements,
199 UserPointer<void> elements, 203 UserPointer<uint32_t> num_bytes,
200 UserPointer<uint32_t> num_bytes, 204 uint32_t max_num_bytes_to_read,
201 uint32_t max_num_bytes_to_read, 205 uint32_t min_num_bytes_to_read,
202 uint32_t min_num_bytes_to_read, 206 bool peek) {
203 bool peek) {
204 DCHECK_EQ(max_num_bytes_to_read % element_num_bytes(), 0u); 207 DCHECK_EQ(max_num_bytes_to_read % element_num_bytes(), 0u);
205 DCHECK_EQ(min_num_bytes_to_read % element_num_bytes(), 0u); 208 DCHECK_EQ(min_num_bytes_to_read % element_num_bytes(), 0u);
206 DCHECK_GT(max_num_bytes_to_read, 0u); 209 DCHECK_GT(max_num_bytes_to_read, 0u);
207 210
208 if (min_num_bytes_to_read > current_num_bytes_) { 211 if (min_num_bytes_to_read > current_num_bytes_) {
209 // Don't return "should wait" since you can't wait for a specified amount of 212 // Don't return "should wait" since you can't wait for a specified amount of
210 // data. 213 // data.
211 return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE 214 return producer_open() ? MOJO_RESULT_OUT_OF_RANGE
212 : MOJO_RESULT_FAILED_PRECONDITION; 215 : MOJO_RESULT_FAILED_PRECONDITION;
213 } 216 }
214 217
215 size_t num_bytes_to_read = 218 size_t num_bytes_to_read =
216 std::min(static_cast<size_t>(max_num_bytes_to_read), current_num_bytes_); 219 std::min(static_cast<size_t>(max_num_bytes_to_read), current_num_bytes_);
217 if (num_bytes_to_read == 0) { 220 if (num_bytes_to_read == 0) {
218 return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT 221 return producer_open() ? MOJO_RESULT_SHOULD_WAIT
219 : MOJO_RESULT_FAILED_PRECONDITION; 222 : MOJO_RESULT_FAILED_PRECONDITION;
220 } 223 }
221 224
222 // The amount we can read in our first |memcpy()|. 225 // The amount we can read in our first |memcpy()|.
223 size_t num_bytes_to_read_first = 226 size_t num_bytes_to_read_first =
224 std::min(num_bytes_to_read, GetMaxNumBytesToReadNoLock()); 227 std::min(num_bytes_to_read, GetMaxNumBytesToRead());
225 elements.PutArray(buffer_.get() + start_index_, num_bytes_to_read_first); 228 elements.PutArray(buffer_.get() + start_index_, num_bytes_to_read_first);
226 229
227 if (num_bytes_to_read_first < num_bytes_to_read) { 230 if (num_bytes_to_read_first < num_bytes_to_read) {
228 // The "second read index" is zero. 231 // The "second read index" is zero.
229 elements.At(num_bytes_to_read_first) 232 elements.At(num_bytes_to_read_first)
230 .PutArray(buffer_.get(), num_bytes_to_read - num_bytes_to_read_first); 233 .PutArray(buffer_.get(), num_bytes_to_read - num_bytes_to_read_first);
231 } 234 }
232 235
233 if (!peek) 236 if (!peek)
234 MarkDataAsConsumedNoLock(num_bytes_to_read); 237 MarkDataAsConsumed(num_bytes_to_read);
235 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_read)); 238 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_read));
236 return MOJO_RESULT_OK; 239 return MOJO_RESULT_OK;
237 } 240 }
238 241
239 MojoResult LocalDataPipeImpl::ConsumerDiscardDataImplNoLock( 242 MojoResult LocalDataPipeImpl::ConsumerDiscardData(
240 UserPointer<uint32_t> num_bytes, 243 UserPointer<uint32_t> num_bytes,
241 uint32_t max_num_bytes_to_discard, 244 uint32_t max_num_bytes_to_discard,
242 uint32_t min_num_bytes_to_discard) { 245 uint32_t min_num_bytes_to_discard) {
243 DCHECK_EQ(max_num_bytes_to_discard % element_num_bytes(), 0u); 246 DCHECK_EQ(max_num_bytes_to_discard % element_num_bytes(), 0u);
244 DCHECK_EQ(min_num_bytes_to_discard % element_num_bytes(), 0u); 247 DCHECK_EQ(min_num_bytes_to_discard % element_num_bytes(), 0u);
245 DCHECK_GT(max_num_bytes_to_discard, 0u); 248 DCHECK_GT(max_num_bytes_to_discard, 0u);
246 249
247 if (min_num_bytes_to_discard > current_num_bytes_) { 250 if (min_num_bytes_to_discard > current_num_bytes_) {
248 // Don't return "should wait" since you can't wait for a specified amount of 251 // Don't return "should wait" since you can't wait for a specified amount of
249 // data. 252 // data.
250 return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE 253 return producer_open() ? MOJO_RESULT_OUT_OF_RANGE
251 : MOJO_RESULT_FAILED_PRECONDITION; 254 : MOJO_RESULT_FAILED_PRECONDITION;
252 } 255 }
253 256
254 // Be consistent with other operations; error if no data available. 257 // Be consistent with other operations; error if no data available.
255 if (current_num_bytes_ == 0) { 258 if (current_num_bytes_ == 0) {
256 return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT 259 return producer_open() ? MOJO_RESULT_SHOULD_WAIT
257 : MOJO_RESULT_FAILED_PRECONDITION; 260 : MOJO_RESULT_FAILED_PRECONDITION;
258 } 261 }
259 262
260 size_t num_bytes_to_discard = std::min( 263 size_t num_bytes_to_discard = std::min(
261 static_cast<size_t>(max_num_bytes_to_discard), current_num_bytes_); 264 static_cast<size_t>(max_num_bytes_to_discard), current_num_bytes_);
262 MarkDataAsConsumedNoLock(num_bytes_to_discard); 265 MarkDataAsConsumed(num_bytes_to_discard);
263 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_discard)); 266 num_bytes.Put(static_cast<uint32_t>(num_bytes_to_discard));
264 return MOJO_RESULT_OK; 267 return MOJO_RESULT_OK;
265 } 268 }
266 269
267 MojoResult LocalDataPipeImpl::ConsumerQueryDataImplNoLock( 270 MojoResult LocalDataPipeImpl::ConsumerQueryData(
268 UserPointer<uint32_t> num_bytes) { 271 UserPointer<uint32_t> num_bytes) {
269 // Note: This cast is safe, since the capacity fits into a |uint32_t|. 272 // Note: This cast is safe, since the capacity fits into a |uint32_t|.
270 num_bytes.Put(static_cast<uint32_t>(current_num_bytes_)); 273 num_bytes.Put(static_cast<uint32_t>(current_num_bytes_));
271 return MOJO_RESULT_OK; 274 return MOJO_RESULT_OK;
272 } 275 }
273 276
274 MojoResult LocalDataPipeImpl::ConsumerBeginReadDataImplNoLock( 277 MojoResult LocalDataPipeImpl::ConsumerBeginReadData(
275 UserPointer<const void*> buffer, 278 UserPointer<const void*> buffer,
276 UserPointer<uint32_t> buffer_num_bytes, 279 UserPointer<uint32_t> buffer_num_bytes,
277 uint32_t min_num_bytes_to_read) { 280 uint32_t min_num_bytes_to_read) {
278 size_t max_num_bytes_to_read = GetMaxNumBytesToReadNoLock(); 281 size_t max_num_bytes_to_read = GetMaxNumBytesToRead();
279 if (min_num_bytes_to_read > max_num_bytes_to_read) { 282 if (min_num_bytes_to_read > max_num_bytes_to_read) {
280 // Don't return "should wait" since you can't wait for a specified amount of 283 // Don't return "should wait" since you can't wait for a specified amount of
281 // data. 284 // data.
282 return producer_open_no_lock() ? MOJO_RESULT_OUT_OF_RANGE 285 return producer_open() ? MOJO_RESULT_OUT_OF_RANGE
283 : MOJO_RESULT_FAILED_PRECONDITION; 286 : MOJO_RESULT_FAILED_PRECONDITION;
284 } 287 }
285 288
286 // Don't go into a two-phase read if there's no data. 289 // Don't go into a two-phase read if there's no data.
287 if (max_num_bytes_to_read == 0) { 290 if (max_num_bytes_to_read == 0) {
288 return producer_open_no_lock() ? MOJO_RESULT_SHOULD_WAIT 291 return producer_open() ? MOJO_RESULT_SHOULD_WAIT
289 : MOJO_RESULT_FAILED_PRECONDITION; 292 : MOJO_RESULT_FAILED_PRECONDITION;
290 } 293 }
291 294
292 buffer.Put(buffer_.get() + start_index_); 295 buffer.Put(buffer_.get() + start_index_);
293 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_read)); 296 buffer_num_bytes.Put(static_cast<uint32_t>(max_num_bytes_to_read));
294 set_consumer_two_phase_max_num_bytes_read_no_lock( 297 set_consumer_two_phase_max_num_bytes_read(
295 static_cast<uint32_t>(max_num_bytes_to_read)); 298 static_cast<uint32_t>(max_num_bytes_to_read));
296 return MOJO_RESULT_OK; 299 return MOJO_RESULT_OK;
297 } 300 }
298 301
299 MojoResult LocalDataPipeImpl::ConsumerEndReadDataImplNoLock( 302 MojoResult LocalDataPipeImpl::ConsumerEndReadData(uint32_t num_bytes_read) {
300 uint32_t num_bytes_read) { 303 DCHECK_LE(num_bytes_read, consumer_two_phase_max_num_bytes_read());
301 DCHECK_LE(num_bytes_read, consumer_two_phase_max_num_bytes_read_no_lock());
302 DCHECK_LE(start_index_ + num_bytes_read, capacity_num_bytes()); 304 DCHECK_LE(start_index_ + num_bytes_read, capacity_num_bytes());
303 MarkDataAsConsumedNoLock(num_bytes_read); 305 MarkDataAsConsumed(num_bytes_read);
304 set_consumer_two_phase_max_num_bytes_read_no_lock(0); 306 set_consumer_two_phase_max_num_bytes_read(0);
305 return MOJO_RESULT_OK; 307 return MOJO_RESULT_OK;
306 } 308 }
307 309
308 HandleSignalsState LocalDataPipeImpl::ConsumerGetHandleSignalsStateImplNoLock() 310 HandleSignalsState LocalDataPipeImpl::ConsumerGetHandleSignalsState() const {
309 const {
310 HandleSignalsState rv; 311 HandleSignalsState rv;
311 if (current_num_bytes_ > 0) { 312 if (current_num_bytes_ > 0) {
312 if (!consumer_in_two_phase_read_no_lock()) 313 if (!consumer_in_two_phase_read())
313 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_READABLE; 314 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_READABLE;
314 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE; 315 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE;
315 } else if (producer_open_no_lock()) { 316 } else if (producer_open()) {
316 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE; 317 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_READABLE;
317 } 318 }
318 if (!producer_open_no_lock()) 319 if (!producer_open())
319 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; 320 rv.satisfied_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
320 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED; 321 rv.satisfiable_signals |= MOJO_HANDLE_SIGNAL_PEER_CLOSED;
321 return rv; 322 return rv;
322 } 323 }
323 324
324 void LocalDataPipeImpl::ConsumerStartSerializeImplNoLock( 325 void LocalDataPipeImpl::ConsumerStartSerialize(Channel* channel,
325 Channel* channel, 326 size_t* max_size,
326 size_t* max_size, 327 size_t* max_platform_handles) {
327 size_t* max_platform_handles) {
328 // TODO(vtl): Support serializing consumer data pipe handles. 328 // TODO(vtl): Support serializing consumer data pipe handles.
329 *max_size = 0; 329 *max_size = 0;
330 *max_platform_handles = 0; 330 *max_platform_handles = 0;
331 } 331 }
332 332
333 bool LocalDataPipeImpl::ConsumerEndSerializeImplNoLock( 333 bool LocalDataPipeImpl::ConsumerEndSerialize(
334 Channel* channel, 334 Channel* channel,
335 void* destination, 335 void* destination,
336 size_t* actual_size, 336 size_t* actual_size,
337 embedder::PlatformHandleVector* platform_handles) { 337 embedder::PlatformHandleVector* platform_handles) {
338 // TODO(vtl): Support serializing consumer data pipe handles. 338 // TODO(vtl): Support serializing consumer data pipe handles.
339 ConsumerCloseNoLock(); 339 owner()->ConsumerCloseNoLock();
340 return false; 340 return false;
341 } 341 }
342 342
343 void LocalDataPipeImpl::EnsureBufferNoLock() { 343 void LocalDataPipeImpl::EnsureBuffer() {
344 DCHECK(producer_open_no_lock()); 344 DCHECK(producer_open());
345 if (buffer_) 345 if (buffer_)
346 return; 346 return;
347 buffer_.reset(static_cast<char*>( 347 buffer_.reset(static_cast<char*>(
348 base::AlignedAlloc(capacity_num_bytes(), 348 base::AlignedAlloc(capacity_num_bytes(),
349 GetConfiguration().data_pipe_buffer_alignment_bytes))); 349 GetConfiguration().data_pipe_buffer_alignment_bytes)));
350 } 350 }
351 351
352 void LocalDataPipeImpl::DestroyBufferNoLock() { 352 void LocalDataPipeImpl::DestroyBuffer() {
353 #ifndef NDEBUG 353 #ifndef NDEBUG
354 // Scribble on the buffer to help detect use-after-frees. (This also helps the 354 // Scribble on the buffer to help detect use-after-frees. (This also helps the
355 // unit test detect certain bugs without needing ASAN or similar.) 355 // unit test detect certain bugs without needing ASAN or similar.)
356 if (buffer_) 356 if (buffer_)
357 memset(buffer_.get(), 0xcd, capacity_num_bytes()); 357 memset(buffer_.get(), 0xcd, capacity_num_bytes());
358 #endif 358 #endif
359 buffer_.reset(); 359 buffer_.reset();
360 } 360 }
361 361
362 size_t LocalDataPipeImpl::GetMaxNumBytesToWriteNoLock() { 362 size_t LocalDataPipeImpl::GetMaxNumBytesToWrite() {
363 size_t next_index = start_index_ + current_num_bytes_; 363 size_t next_index = start_index_ + current_num_bytes_;
364 if (next_index >= capacity_num_bytes()) { 364 if (next_index >= capacity_num_bytes()) {
365 next_index %= capacity_num_bytes(); 365 next_index %= capacity_num_bytes();
366 DCHECK_GE(start_index_, next_index); 366 DCHECK_GE(start_index_, next_index);
367 DCHECK_EQ(start_index_ - next_index, 367 DCHECK_EQ(start_index_ - next_index,
368 capacity_num_bytes() - current_num_bytes_); 368 capacity_num_bytes() - current_num_bytes_);
369 return start_index_ - next_index; 369 return start_index_ - next_index;
370 } 370 }
371 return capacity_num_bytes() - next_index; 371 return capacity_num_bytes() - next_index;
372 } 372 }
373 373
374 size_t LocalDataPipeImpl::GetMaxNumBytesToReadNoLock() { 374 size_t LocalDataPipeImpl::GetMaxNumBytesToRead() {
375 if (start_index_ + current_num_bytes_ > capacity_num_bytes()) 375 if (start_index_ + current_num_bytes_ > capacity_num_bytes())
376 return capacity_num_bytes() - start_index_; 376 return capacity_num_bytes() - start_index_;
377 return current_num_bytes_; 377 return current_num_bytes_;
378 } 378 }
379 379
380 void LocalDataPipeImpl::MarkDataAsConsumedNoLock(size_t num_bytes) { 380 void LocalDataPipeImpl::MarkDataAsConsumed(size_t num_bytes) {
381 DCHECK_LE(num_bytes, current_num_bytes_); 381 DCHECK_LE(num_bytes, current_num_bytes_);
382 start_index_ += num_bytes; 382 start_index_ += num_bytes;
383 start_index_ %= capacity_num_bytes(); 383 start_index_ %= capacity_num_bytes();
384 current_num_bytes_ -= num_bytes; 384 current_num_bytes_ -= num_bytes;
385 } 385 }
386 386
387 } // namespace system 387 } // namespace system
388 } // namespace mojo 388 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698