OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/transport_data.h" | 5 #include "mojo/edk/system/transport_data.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "mojo/edk/system/channel.h" | 12 #include "mojo/edk/system/channel.h" |
13 #include "mojo/edk/system/configuration.h" | 13 #include "mojo/edk/system/configuration.h" |
14 #include "mojo/edk/system/message_in_transit.h" | 14 #include "mojo/edk/system/message_in_transit.h" |
15 | 15 |
| 16 using mojo::embedder::ScopedPlatformHandle; |
| 17 |
16 namespace mojo { | 18 namespace mojo { |
17 namespace system { | 19 namespace system { |
18 | 20 |
19 // The maximum amount of space needed per platform handle. | 21 // The maximum amount of space needed per platform handle. |
20 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always | 22 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always |
21 // return a value which is at most this. This is only used to calculate | 23 // return a value which is at most this. This is only used to calculate |
22 // |TransportData::kMaxBufferSize|. This value should be a multiple of the | 24 // |TransportData::kMaxBufferSize|. This value should be a multiple of the |
23 // alignment in order to simplify calculations, even though the actual amount of | 25 // alignment in order to simplify calculations, even though the actual amount of |
24 // space needed need not be a multiple of the alignment. | 26 // space needed need not be a multiple of the alignment. |
25 const size_t kMaxSizePerPlatformHandle = 8; | 27 const size_t kMaxSizePerPlatformHandle = 8; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 119 |
118 buffer_.reset(static_cast<char*>( | 120 buffer_.reset(static_cast<char*>( |
119 base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment))); | 121 base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment))); |
120 // Entirely clear out the secondary buffer, since then we won't have to worry | 122 // Entirely clear out the secondary buffer, since then we won't have to worry |
121 // about clearing padding or unused space (e.g., if a dispatcher fails to | 123 // about clearing padding or unused space (e.g., if a dispatcher fails to |
122 // serialize). | 124 // serialize). |
123 memset(buffer_.get(), 0, estimated_size); | 125 memset(buffer_.get(), 0, estimated_size); |
124 | 126 |
125 if (estimated_num_platform_handles > 0) { | 127 if (estimated_num_platform_handles > 0) { |
126 DCHECK(!platform_handles_); | 128 DCHECK(!platform_handles_); |
127 platform_handles_.reset(new embedder::PlatformHandleVector()); | 129 platform_handles_.reset(new std::vector<ScopedPlatformHandle>()); |
128 } | 130 } |
129 | 131 |
130 Header* header = reinterpret_cast<Header*>(buffer_.get()); | 132 Header* header = reinterpret_cast<Header*>(buffer_.get()); |
131 header->num_handles = static_cast<uint32_t>(num_handles); | 133 header->num_handles = static_cast<uint32_t>(num_handles); |
132 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and | 134 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and |
133 // |unused| be zero; we'll set the former two later if necessary.) | 135 // |unused| be zero; we'll set the former two later if necessary.) |
134 | 136 |
135 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( | 137 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( |
136 buffer_.get() + handle_table_start_offset); | 138 buffer_.get() + handle_table_start_offset); |
137 size_t current_offset = serialized_dispatcher_start_offset; | 139 size_t current_offset = serialized_dispatcher_start_offset; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 } | 189 } |
188 | 190 |
189 // There's no aligned realloc, so it's no good way to release unused space (if | 191 // There's no aligned realloc, so it's no good way to release unused space (if |
190 // we overshot our estimated space requirements). | 192 // we overshot our estimated space requirements). |
191 buffer_size_ = current_offset; | 193 buffer_size_ = current_offset; |
192 | 194 |
193 // |dispatchers_| will be destroyed as it goes out of scope. | 195 // |dispatchers_| will be destroyed as it goes out of scope. |
194 } | 196 } |
195 | 197 |
196 TransportData::TransportData( | 198 TransportData::TransportData( |
197 embedder::ScopedPlatformHandleVectorPtr platform_handles, | 199 std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles, |
198 size_t serialized_platform_handle_size) | 200 size_t serialized_platform_handle_size) |
199 : buffer_size_(), platform_handles_(std::move(platform_handles)) { | 201 : buffer_size_(), platform_handles_(std::move(platform_handles)) { |
200 buffer_size_ = MessageInTransit::RoundUpMessageAlignment( | 202 buffer_size_ = MessageInTransit::RoundUpMessageAlignment( |
201 sizeof(Header) + | 203 sizeof(Header) + |
202 platform_handles_->size() * serialized_platform_handle_size); | 204 platform_handles_->size() * serialized_platform_handle_size); |
203 buffer_.reset(static_cast<char*>( | 205 buffer_.reset(static_cast<char*>( |
204 base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment))); | 206 base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment))); |
205 memset(buffer_.get(), 0, buffer_size_); | 207 memset(buffer_.get(), 0, buffer_size_); |
206 | 208 |
207 Header* header = reinterpret_cast<Header*>(buffer_.get()); | 209 Header* header = reinterpret_cast<Header*>(buffer_.get()); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 const Header* header = static_cast<const Header*>(transport_data_buffer); | 306 const Header* header = static_cast<const Header*>(transport_data_buffer); |
305 *num_platform_handles = header->num_platform_handles; | 307 *num_platform_handles = header->num_platform_handles; |
306 *platform_handle_table = static_cast<const char*>(transport_data_buffer) + | 308 *platform_handle_table = static_cast<const char*>(transport_data_buffer) + |
307 header->platform_handle_table_offset; | 309 header->platform_handle_table_offset; |
308 } | 310 } |
309 | 311 |
310 // static | 312 // static |
311 std::unique_ptr<DispatcherVector> TransportData::DeserializeDispatchers( | 313 std::unique_ptr<DispatcherVector> TransportData::DeserializeDispatchers( |
312 const void* buffer, | 314 const void* buffer, |
313 size_t buffer_size, | 315 size_t buffer_size, |
314 embedder::ScopedPlatformHandleVectorPtr platform_handles, | 316 std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles, |
315 Channel* channel) { | 317 Channel* channel) { |
316 DCHECK(buffer); | 318 DCHECK(buffer); |
317 DCHECK_GT(buffer_size, 0u); | 319 DCHECK_GT(buffer_size, 0u); |
318 DCHECK(channel); | 320 DCHECK(channel); |
319 | 321 |
320 const Header* header = static_cast<const Header*>(buffer); | 322 const Header* header = static_cast<const Header*>(buffer); |
321 const size_t num_handles = header->num_handles; | 323 const size_t num_handles = header->num_handles; |
322 std::unique_ptr<DispatcherVector> dispatchers( | 324 std::unique_ptr<DispatcherVector> dispatchers( |
323 new DispatcherVector(num_handles)); | 325 new DispatcherVector(num_handles)); |
324 | 326 |
(...skipping 11 matching lines...) Expand all Loading... |
336 const void* source = static_cast<const char*>(buffer) + offset; | 338 const void* source = static_cast<const char*>(buffer) + offset; |
337 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | 339 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( |
338 channel, handle_table[i].type, source, size, platform_handles.get()); | 340 channel, handle_table[i].type, source, size, platform_handles.get()); |
339 } | 341 } |
340 | 342 |
341 return dispatchers; | 343 return dispatchers; |
342 } | 344 } |
343 | 345 |
344 } // namespace system | 346 } // namespace system |
345 } // namespace mojo | 347 } // namespace mojo |
OLD | NEW |