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