| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "mojo/edk/system/data_pipe_impl.h" | 5 #include "mojo/edk/system/data_pipe_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
| 9 #include <utility> |
| 8 | 10 |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "mojo/edk/system/configuration.h" | 12 #include "mojo/edk/system/configuration.h" |
| 12 #include "mojo/edk/system/message_in_transit.h" | 13 #include "mojo/edk/system/message_in_transit.h" |
| 13 #include "mojo/edk/system/message_in_transit_queue.h" | 14 #include "mojo/edk/system/message_in_transit_queue.h" |
| 14 | 15 |
| 15 namespace mojo { | 16 namespace mojo { |
| 16 namespace system { | 17 namespace system { |
| 17 | 18 |
| 18 void DataPipeImpl::ConvertDataToMessages(const char* buffer, | 19 void DataPipeImpl::ConvertDataToMessages(const char* buffer, |
| 19 size_t* start_index, | 20 size_t* start_index, |
| 20 size_t* current_num_bytes, | 21 size_t* current_num_bytes, |
| 21 MessageInTransitQueue* message_queue) { | 22 MessageInTransitQueue* message_queue) { |
| 22 // The maximum amount of data to send per message (make it a multiple of the | 23 // The maximum amount of data to send per message (make it a multiple of the |
| 23 // element size. | 24 // element size. |
| 24 size_t max_message_num_bytes = GetConfiguration().max_message_num_bytes; | 25 size_t max_message_num_bytes = GetConfiguration().max_message_num_bytes; |
| 25 max_message_num_bytes -= max_message_num_bytes % element_num_bytes(); | 26 max_message_num_bytes -= max_message_num_bytes % element_num_bytes(); |
| 26 DCHECK_GT(max_message_num_bytes, 0u); | 27 DCHECK_GT(max_message_num_bytes, 0u); |
| 27 | 28 |
| 28 while (*current_num_bytes > 0) { | 29 while (*current_num_bytes > 0) { |
| 29 size_t current_contiguous_num_bytes = | 30 size_t current_contiguous_num_bytes = |
| 30 (*start_index + *current_num_bytes > capacity_num_bytes()) | 31 (*start_index + *current_num_bytes > capacity_num_bytes()) |
| 31 ? (capacity_num_bytes() - *start_index) | 32 ? (capacity_num_bytes() - *start_index) |
| 32 : *current_num_bytes; | 33 : *current_num_bytes; |
| 33 size_t message_num_bytes = | 34 size_t message_num_bytes = |
| 34 std::min(max_message_num_bytes, current_contiguous_num_bytes); | 35 std::min(max_message_num_bytes, current_contiguous_num_bytes); |
| 35 | 36 |
| 36 // Note: |message_num_bytes| fits in a |uint32_t| since the capacity does. | 37 // Note: |message_num_bytes| fits in a |uint32_t| since the capacity does. |
| 37 scoped_ptr<MessageInTransit> message(new MessageInTransit( | 38 std::unique_ptr<MessageInTransit> message(new MessageInTransit( |
| 38 MessageInTransit::Type::ENDPOINT_CLIENT, | 39 MessageInTransit::Type::ENDPOINT_CLIENT, |
| 39 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, | 40 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, |
| 40 static_cast<uint32_t>(message_num_bytes), buffer + *start_index)); | 41 static_cast<uint32_t>(message_num_bytes), buffer + *start_index)); |
| 41 message_queue->AddMessage(message.Pass()); | 42 message_queue->AddMessage(std::move(message)); |
| 42 | 43 |
| 43 DCHECK_LE(message_num_bytes, *current_num_bytes); | 44 DCHECK_LE(message_num_bytes, *current_num_bytes); |
| 44 *start_index += message_num_bytes; | 45 *start_index += message_num_bytes; |
| 45 *start_index %= capacity_num_bytes(); | 46 *start_index %= capacity_num_bytes(); |
| 46 *current_num_bytes -= message_num_bytes; | 47 *current_num_bytes -= message_num_bytes; |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 } // namespace system | 51 } // namespace system |
| 51 } // namespace mojo | 52 } // namespace mojo |
| OLD | NEW |