| 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/system/transport_data.h" | 5 #include "mojo/system/transport_data.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "mojo/system/channel.h" | 11 #include "mojo/system/channel.h" |
| 12 #include "mojo/system/constants.h" | 12 #include "mojo/system/constants.h" |
| 13 #include "mojo/system/message_in_transit.h" | 13 #include "mojo/system/message_in_transit.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace system { | 16 namespace system { |
| 17 | 17 |
| 18 // The maximum amount of space needed per platform handle. | 18 // The maximum amount of space needed per platform handle. |
| 19 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always | 19 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always |
| 20 // return a value which is at most this. This is only used to calculate | 20 // return a value which is at most this. This is only used to calculate |
| 21 // |TransportData::kMaxBufferSize|. This value should be a multiple of the | 21 // |TransportData::kMaxBufferSize|. This value should be a multiple of the |
| 22 // alignment in order to simplify calculations, even though the actual amount of | 22 // alignment in order to simplify calculations, even though the actual amount of |
| 23 // space needed need not be a multiple of the alignment. | 23 // space needed need not be a multiple of the alignment. |
| 24 const size_t kMaxSizePerPlatformHandle = 8; | 24 const size_t kMaxSizePerPlatformHandle = 8; |
| 25 COMPILE_ASSERT(kMaxSizePerPlatformHandle % | 25 static_assert(kMaxSizePerPlatformHandle % MessageInTransit::kMessageAlignment == |
| 26 MessageInTransit::kMessageAlignment == | 26 0, |
| 27 0, | 27 "kMaxSizePerPlatformHandle not a multiple of alignment"); |
| 28 kMaxSizePerPlatformHandle_not_a_multiple_of_alignment); | |
| 29 | 28 |
| 30 STATIC_CONST_MEMBER_DEFINITION const size_t | 29 STATIC_CONST_MEMBER_DEFINITION const size_t |
| 31 TransportData::kMaxSerializedDispatcherSize; | 30 TransportData::kMaxSerializedDispatcherSize; |
| 32 STATIC_CONST_MEMBER_DEFINITION const size_t | 31 STATIC_CONST_MEMBER_DEFINITION const size_t |
| 33 TransportData::kMaxSerializedDispatcherPlatformHandles; | 32 TransportData::kMaxSerializedDispatcherPlatformHandles; |
| 34 | 33 |
| 35 // static | 34 // static |
| 36 const size_t TransportData::kMaxPlatformHandles = | 35 const size_t TransportData::kMaxPlatformHandles = |
| 37 kMaxMessageNumHandles * kMaxSerializedDispatcherPlatformHandles; | 36 kMaxMessageNumHandles * kMaxSerializedDispatcherPlatformHandles; |
| 38 | 37 |
| 39 // In additional to the header, for each attached (Mojo) handle there'll be a | 38 // In additional to the header, for each attached (Mojo) handle there'll be a |
| 40 // handle table entry and serialized dispatcher data. | 39 // handle table entry and serialized dispatcher data. |
| 41 // Note: This definition must follow the one for |kMaxPlatformHandles|; | 40 // Note: This definition must follow the one for |kMaxPlatformHandles|; |
| 42 // otherwise, we get a static initializer with gcc (but not clang). | 41 // otherwise, we get a static initializer with gcc (but not clang). |
| 43 // static | 42 // static |
| 44 const size_t TransportData::kMaxBufferSize = | 43 const size_t TransportData::kMaxBufferSize = |
| 45 sizeof(Header) + | 44 sizeof(Header) + |
| 46 kMaxMessageNumHandles * | 45 kMaxMessageNumHandles * |
| 47 (sizeof(HandleTableEntry) + kMaxSerializedDispatcherSize) + | 46 (sizeof(HandleTableEntry) + kMaxSerializedDispatcherSize) + |
| 48 kMaxPlatformHandles * kMaxSizePerPlatformHandle; | 47 kMaxPlatformHandles * kMaxSizePerPlatformHandle; |
| 49 | 48 |
| 50 struct TransportData::PrivateStructForCompileAsserts { | 49 struct TransportData::PrivateStructForCompileAsserts { |
| 51 // The size of |Header| must be a multiple of the alignment. | 50 static_assert(sizeof(Header) % MessageInTransit::kMessageAlignment == 0, |
| 52 COMPILE_ASSERT(sizeof(Header) % MessageInTransit::kMessageAlignment == 0, | 51 "sizeof(MessageInTransit::Header) not a multiple of alignment"); |
| 53 sizeof_MessageInTransit_Header_invalid); | 52 static_assert(kMaxSerializedDispatcherSize % |
| 54 | 53 MessageInTransit::kMessageAlignment == |
| 55 // The maximum serialized dispatcher size must be a multiple of the alignment. | 54 0, |
| 56 COMPILE_ASSERT(kMaxSerializedDispatcherSize % | 55 "kMaxSerializedDispatcherSize not a multiple of alignment"); |
| 57 MessageInTransit::kMessageAlignment == | 56 static_assert(sizeof(HandleTableEntry) % |
| 58 0, | 57 MessageInTransit::kMessageAlignment == |
| 59 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); | 58 0, |
| 60 | 59 "sizeof(MessageInTransit::HandleTableEntry) not a multiple of " |
| 61 // The size of |HandleTableEntry| must be a multiple of the alignment. | 60 "alignment"); |
| 62 COMPILE_ASSERT(sizeof(HandleTableEntry) % | |
| 63 MessageInTransit::kMessageAlignment == | |
| 64 0, | |
| 65 sizeof_MessageInTransit_HandleTableEntry_invalid); | |
| 66 }; | 61 }; |
| 67 | 62 |
| 68 TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers, | 63 TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers, |
| 69 Channel* channel) { | 64 Channel* channel) { |
| 70 DCHECK(dispatchers); | 65 DCHECK(dispatchers); |
| 71 DCHECK(channel); | 66 DCHECK(channel); |
| 72 | 67 |
| 73 const size_t num_handles = dispatchers->size(); | 68 const size_t num_handles = dispatchers->size(); |
| 74 DCHECK_GT(num_handles, 0u); | 69 DCHECK_GT(num_handles, 0u); |
| 75 | 70 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 header->num_handles = static_cast<uint32_t>(num_handles); | 128 header->num_handles = static_cast<uint32_t>(num_handles); |
| 134 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and | 129 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and |
| 135 // |unused| be zero; we'll set the former two later if necessary.) | 130 // |unused| be zero; we'll set the former two later if necessary.) |
| 136 | 131 |
| 137 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( | 132 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( |
| 138 buffer_.get() + handle_table_start_offset); | 133 buffer_.get() + handle_table_start_offset); |
| 139 size_t current_offset = serialized_dispatcher_start_offset; | 134 size_t current_offset = serialized_dispatcher_start_offset; |
| 140 for (size_t i = 0; i < num_handles; i++) { | 135 for (size_t i = 0; i < num_handles; i++) { |
| 141 Dispatcher* dispatcher = (*dispatchers)[i].get(); | 136 Dispatcher* dispatcher = (*dispatchers)[i].get(); |
| 142 if (!dispatcher) { | 137 if (!dispatcher) { |
| 143 COMPILE_ASSERT(Dispatcher::kTypeUnknown == 0, | 138 static_assert(Dispatcher::kTypeUnknown == 0, |
| 144 value_of_Dispatcher_kTypeUnknown_must_be_zero); | 139 "Value of Dispatcher::kTypeUnknown must be 0"); |
| 145 continue; | 140 continue; |
| 146 } | 141 } |
| 147 | 142 |
| 148 #if DCHECK_IS_ON | 143 #if DCHECK_IS_ON |
| 149 size_t old_platform_handles_size = | 144 size_t old_platform_handles_size = |
| 150 platform_handles_ ? platform_handles_->size() : 0; | 145 platform_handles_ ? platform_handles_->size() : 0; |
| 151 #endif | 146 #endif |
| 152 | 147 |
| 153 void* destination = buffer_.get() + current_offset; | 148 void* destination = buffer_.get() + current_offset; |
| 154 size_t actual_size = 0; | 149 size_t actual_size = 0; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 const void* source = static_cast<const char*>(buffer) + offset; | 337 const void* source = static_cast<const char*>(buffer) + offset; |
| 343 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | 338 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( |
| 344 channel, handle_table[i].type, source, size, platform_handles.get()); | 339 channel, handle_table[i].type, source, size, platform_handles.get()); |
| 345 } | 340 } |
| 346 | 341 |
| 347 return dispatchers.Pass(); | 342 return dispatchers.Pass(); |
| 348 } | 343 } |
| 349 | 344 |
| 350 } // namespace system | 345 } // namespace system |
| 351 } // namespace mojo | 346 } // namespace mojo |
| OLD | NEW |