Chromium Code Reviews| Index: mojo/system/local_data_pipe.cc |
| diff --git a/mojo/system/local_data_pipe.cc b/mojo/system/local_data_pipe.cc |
| index 936c68c4b80a0995074ff82c7f50c5351cbc84e8..81bc7b557aab31c970bf14149f7b16e9ce1b8866 100644 |
| --- a/mojo/system/local_data_pipe.cc |
| +++ b/mojo/system/local_data_pipe.cc |
| @@ -51,13 +51,32 @@ MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(const void* elements, |
| DCHECK_EQ(*num_bytes % element_num_bytes(), 0u); |
| DCHECK_GT(*num_bytes, 0u); |
| - // TODO(vtl): Consider this return value. |
| - if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) |
| - return MOJO_RESULT_OUT_OF_RANGE; |
| - |
| - size_t num_bytes_to_write = |
| - std::min(static_cast<size_t>(*num_bytes), |
| - capacity_num_bytes() - current_num_bytes_); |
| + size_t num_bytes_to_write = 0; |
| + if (may_discard()) { |
| + if (all_or_none && *num_bytes > capacity_num_bytes()) |
| + return MOJO_RESULT_OUT_OF_RANGE; |
| + |
| + num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes), |
| + capacity_num_bytes()); |
| + if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) { |
| + // Discard as much as needed (discard oldest first). |
| + size_t num_bytes_to_discard = |
| + num_bytes_to_write - (capacity_num_bytes() - current_num_bytes_); |
| + start_index_ += num_bytes_to_discard; |
| + start_index_ %= capacity_num_bytes(); |
| + current_num_bytes_ -= num_bytes_to_discard; |
| + } |
| + } else { |
| + // TODO(vtl): Consider this return value. Maybe "out of range" when greater |
|
darin (slow to review)
2014/01/06 21:29:28
hmm, your approach makes sense to me.
|
| + // than capacity, and "should wait" for the other case? |
| + if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) { |
| + return (*num_bytes > capacity_num_bytes()) ? MOJO_RESULT_OUT_OF_RANGE : |
| + MOJO_RESULT_SHOULD_WAIT; |
| + } |
| + |
| + num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes), |
| + capacity_num_bytes() - current_num_bytes_); |
| + } |
| if (num_bytes_to_write == 0) |
| return MOJO_RESULT_SHOULD_WAIT; |