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

Side by Side Diff: mojo/system/local_data_pipe.cc

Issue 98013005: Mojo: DataPipe: Implement "may discard" mode for simple writes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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
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
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 } 44 }
45 AwakeConsumerWaitersForStateChangeNoLock(); 45 AwakeConsumerWaitersForStateChangeNoLock();
46 } 46 }
47 47
48 MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(const void* elements, 48 MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(const void* elements,
49 uint32_t* num_bytes, 49 uint32_t* num_bytes,
50 bool all_or_none) { 50 bool all_or_none) {
51 DCHECK_EQ(*num_bytes % element_num_bytes(), 0u); 51 DCHECK_EQ(*num_bytes % element_num_bytes(), 0u);
52 DCHECK_GT(*num_bytes, 0u); 52 DCHECK_GT(*num_bytes, 0u);
53 53
54 // TODO(vtl): Consider this return value. 54 size_t num_bytes_to_write = 0;
55 if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) 55 if (may_discard()) {
56 return MOJO_RESULT_OUT_OF_RANGE; 56 if (all_or_none && *num_bytes > capacity_num_bytes())
57 return MOJO_RESULT_OUT_OF_RANGE;
57 58
58 size_t num_bytes_to_write = 59 num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes),
59 std::min(static_cast<size_t>(*num_bytes), 60 capacity_num_bytes());
60 capacity_num_bytes() - current_num_bytes_); 61 if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
62 // Discard as much as needed (discard oldest first).
63 size_t num_bytes_to_discard =
64 num_bytes_to_write - (capacity_num_bytes() - current_num_bytes_);
65 start_index_ += num_bytes_to_discard;
66 start_index_ %= capacity_num_bytes();
67 current_num_bytes_ -= num_bytes_to_discard;
68 }
69 } else {
70 // 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.
71 // than capacity, and "should wait" for the other case?
72 if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) {
73 return (*num_bytes > capacity_num_bytes()) ? MOJO_RESULT_OUT_OF_RANGE :
74 MOJO_RESULT_SHOULD_WAIT;
75 }
76
77 num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes),
78 capacity_num_bytes() - current_num_bytes_);
79 }
61 if (num_bytes_to_write == 0) 80 if (num_bytes_to_write == 0)
62 return MOJO_RESULT_SHOULD_WAIT; 81 return MOJO_RESULT_SHOULD_WAIT;
63 82
64 // The amount we can write in our first |memcpy()|. 83 // The amount we can write in our first |memcpy()|.
65 size_t num_bytes_to_write_first = 84 size_t num_bytes_to_write_first =
66 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock()); 85 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock());
67 // Do the first (and possibly only) |memcpy()|. 86 // Do the first (and possibly only) |memcpy()|.
68 size_t first_write_index = 87 size_t first_write_index =
69 (start_index_ + current_num_bytes_) % capacity_num_bytes(); 88 (start_index_ + current_num_bytes_) % capacity_num_bytes();
70 EnsureBufferNoLock(); 89 EnsureBufferNoLock();
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 342 }
324 343
325 size_t LocalDataPipe::GetMaxNumBytesToReadNoLock() { 344 size_t LocalDataPipe::GetMaxNumBytesToReadNoLock() {
326 if (start_index_ + current_num_bytes_ > capacity_num_bytes()) 345 if (start_index_ + current_num_bytes_ > capacity_num_bytes())
327 return capacity_num_bytes() - start_index_; 346 return capacity_num_bytes() - start_index_;
328 return current_num_bytes_; 347 return current_num_bytes_;
329 } 348 }
330 349
331 } // namespace system 350 } // namespace system
332 } // namespace mojo 351 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698