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..36ba55272e7b51f2d31c5073313fb78a673a76fb 100644 |
--- a/mojo/system/local_data_pipe.cc |
+++ b/mojo/system/local_data_pipe.cc |
@@ -51,13 +51,30 @@ 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 { |
+ 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; |