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/remote_producer_data_pipe_impl.h" | 5 #include "mojo/edk/system/remote_producer_data_pipe_impl.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> |
10 | 11 |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "mojo/edk/system/channel.h" | 14 #include "mojo/edk/system/channel.h" |
14 #include "mojo/edk/system/channel_endpoint.h" | 15 #include "mojo/edk/system/channel_endpoint.h" |
15 #include "mojo/edk/system/configuration.h" | 16 #include "mojo/edk/system/configuration.h" |
16 #include "mojo/edk/system/data_pipe.h" | 17 #include "mojo/edk/system/data_pipe.h" |
17 #include "mojo/edk/system/message_in_transit.h" | 18 #include "mojo/edk/system/message_in_transit.h" |
18 #include "mojo/edk/system/message_in_transit_queue.h" | 19 #include "mojo/edk/system/message_in_transit_queue.h" |
19 #include "mojo/edk/system/remote_consumer_data_pipe_impl.h" | 20 #include "mojo/edk/system/remote_consumer_data_pipe_impl.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 RemoteProducerDataPipeImpl::RemoteProducerDataPipeImpl( | 62 RemoteProducerDataPipeImpl::RemoteProducerDataPipeImpl( |
62 ChannelEndpoint* channel_endpoint) | 63 ChannelEndpoint* channel_endpoint) |
63 : channel_endpoint_(channel_endpoint), | 64 : channel_endpoint_(channel_endpoint), |
64 start_index_(0), | 65 start_index_(0), |
65 current_num_bytes_(0) { | 66 current_num_bytes_(0) { |
66 // Note: |buffer_| is lazily allocated. | 67 // Note: |buffer_| is lazily allocated. |
67 } | 68 } |
68 | 69 |
69 RemoteProducerDataPipeImpl::RemoteProducerDataPipeImpl( | 70 RemoteProducerDataPipeImpl::RemoteProducerDataPipeImpl( |
70 ChannelEndpoint* channel_endpoint, | 71 ChannelEndpoint* channel_endpoint, |
71 scoped_ptr<char, base::AlignedFreeDeleter> buffer, | 72 std::unique_ptr<char, base::AlignedFreeDeleter> buffer, |
72 size_t start_index, | 73 size_t start_index, |
73 size_t current_num_bytes) | 74 size_t current_num_bytes) |
74 : channel_endpoint_(channel_endpoint), | 75 : channel_endpoint_(channel_endpoint), |
75 buffer_(buffer.Pass()), | 76 buffer_(std::move(buffer)), |
76 start_index_(start_index), | 77 start_index_(start_index), |
77 current_num_bytes_(current_num_bytes) { | 78 current_num_bytes_(current_num_bytes) { |
78 DCHECK(buffer_ || !current_num_bytes); | 79 DCHECK(buffer_ || !current_num_bytes); |
79 } | 80 } |
80 | 81 |
81 // static | 82 // static |
82 bool RemoteProducerDataPipeImpl::ProcessMessagesFromIncomingEndpoint( | 83 bool RemoteProducerDataPipeImpl::ProcessMessagesFromIncomingEndpoint( |
83 const MojoCreateDataPipeOptions& validated_options, | 84 const MojoCreateDataPipeOptions& validated_options, |
84 MessageInTransitQueue* messages, | 85 MessageInTransitQueue* messages, |
85 scoped_ptr<char, base::AlignedFreeDeleter>* buffer, | 86 std::unique_ptr<char, base::AlignedFreeDeleter>* buffer, |
86 size_t* buffer_num_bytes) { | 87 size_t* buffer_num_bytes) { |
87 DCHECK(!*buffer); // Not wrong, but unlikely. | 88 DCHECK(!*buffer); // Not wrong, but unlikely. |
88 | 89 |
89 const size_t element_num_bytes = validated_options.element_num_bytes; | 90 const size_t element_num_bytes = validated_options.element_num_bytes; |
90 const size_t capacity_num_bytes = validated_options.capacity_num_bytes; | 91 const size_t capacity_num_bytes = validated_options.capacity_num_bytes; |
91 | 92 |
92 scoped_ptr<char, base::AlignedFreeDeleter> new_buffer(static_cast<char*>( | 93 std::unique_ptr<char, base::AlignedFreeDeleter> new_buffer(static_cast<char*>( |
93 base::AlignedAlloc(capacity_num_bytes, | 94 base::AlignedAlloc(capacity_num_bytes, |
94 GetConfiguration().data_pipe_buffer_alignment_bytes))); | 95 GetConfiguration().data_pipe_buffer_alignment_bytes))); |
95 | 96 |
96 size_t current_num_bytes = 0; | 97 size_t current_num_bytes = 0; |
97 if (messages) { | 98 if (messages) { |
98 while (!messages->IsEmpty()) { | 99 while (!messages->IsEmpty()) { |
99 scoped_ptr<MessageInTransit> message(messages->GetMessage()); | 100 scoped_ptr<MessageInTransit> message(messages->GetMessage()); |
100 if (!ValidateIncomingMessage(element_num_bytes, capacity_num_bytes, | 101 if (!ValidateIncomingMessage(element_num_bytes, capacity_num_bytes, |
101 current_num_bytes, message.get())) { | 102 current_num_bytes, message.get())) { |
102 messages->Clear(); | 103 messages->Clear(); |
103 return false; | 104 return false; |
104 } | 105 } |
105 | 106 |
106 memcpy(new_buffer.get() + current_num_bytes, message->bytes(), | 107 memcpy(new_buffer.get() + current_num_bytes, message->bytes(), |
107 message->num_bytes()); | 108 message->num_bytes()); |
108 current_num_bytes += message->num_bytes(); | 109 current_num_bytes += message->num_bytes(); |
109 } | 110 } |
110 } | 111 } |
111 | 112 |
112 *buffer = new_buffer.Pass(); | 113 *buffer = std::move(new_buffer); |
113 *buffer_num_bytes = current_num_bytes; | 114 *buffer_num_bytes = current_num_bytes; |
114 return true; | 115 return true; |
115 } | 116 } |
116 | 117 |
117 RemoteProducerDataPipeImpl::~RemoteProducerDataPipeImpl() { | 118 RemoteProducerDataPipeImpl::~RemoteProducerDataPipeImpl() { |
118 } | 119 } |
119 | 120 |
120 void RemoteProducerDataPipeImpl::ProducerClose() { | 121 void RemoteProducerDataPipeImpl::ProducerClose() { |
121 NOTREACHED(); | 122 NOTREACHED(); |
122 } | 123 } |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 // If the consumer is still open and we still have data, we have to keep the | 467 // If the consumer is still open and we still have data, we have to keep the |
467 // buffer around. Currently, we won't free it even if it empties later. (We | 468 // buffer around. Currently, we won't free it even if it empties later. (We |
468 // could do this -- requiring a check on every read -- but that seems to be | 469 // could do this -- requiring a check on every read -- but that seems to be |
469 // optimizing for the uncommon case.) | 470 // optimizing for the uncommon case.) |
470 if (!consumer_open() || !current_num_bytes_) | 471 if (!consumer_open() || !current_num_bytes_) |
471 DestroyBuffer(); | 472 DestroyBuffer(); |
472 } | 473 } |
473 | 474 |
474 } // namespace system | 475 } // namespace system |
475 } // namespace mojo | 476 } // namespace mojo |
OLD | NEW |