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

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: remove todo 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
« no previous file with comments | « mojo/system/data_pipe.cc ('k') | mojo/system/local_data_pipe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) {
71 return (*num_bytes > capacity_num_bytes()) ? MOJO_RESULT_OUT_OF_RANGE :
72 MOJO_RESULT_SHOULD_WAIT;
73 }
74
75 num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes),
76 capacity_num_bytes() - current_num_bytes_);
77 }
61 if (num_bytes_to_write == 0) 78 if (num_bytes_to_write == 0)
62 return MOJO_RESULT_SHOULD_WAIT; 79 return MOJO_RESULT_SHOULD_WAIT;
63 80
64 // The amount we can write in our first |memcpy()|. 81 // The amount we can write in our first |memcpy()|.
65 size_t num_bytes_to_write_first = 82 size_t num_bytes_to_write_first =
66 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock()); 83 std::min(num_bytes_to_write, GetMaxNumBytesToWriteNoLock());
67 // Do the first (and possibly only) |memcpy()|. 84 // Do the first (and possibly only) |memcpy()|.
68 size_t first_write_index = 85 size_t first_write_index =
69 (start_index_ + current_num_bytes_) % capacity_num_bytes(); 86 (start_index_ + current_num_bytes_) % capacity_num_bytes();
70 EnsureBufferNoLock(); 87 EnsureBufferNoLock();
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 340 }
324 341
325 size_t LocalDataPipe::GetMaxNumBytesToReadNoLock() { 342 size_t LocalDataPipe::GetMaxNumBytesToReadNoLock() {
326 if (start_index_ + current_num_bytes_ > capacity_num_bytes()) 343 if (start_index_ + current_num_bytes_ > capacity_num_bytes())
327 return capacity_num_bytes() - start_index_; 344 return capacity_num_bytes() - start_index_;
328 return current_num_bytes_; 345 return current_num_bytes_;
329 } 346 }
330 347
331 } // namespace system 348 } // namespace system
332 } // namespace mojo 349 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/data_pipe.cc ('k') | mojo/system/local_data_pipe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698