OLD | NEW |
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 #include "mojo/system/message_in_transit.h" | 5 #include "mojo/system/message_in_transit.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" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 MessageInTransit::MessageInTransit(Type type, | 88 MessageInTransit::MessageInTransit(Type type, |
89 Subtype subtype, | 89 Subtype subtype, |
90 uint32_t num_bytes, | 90 uint32_t num_bytes, |
91 const void* bytes) | 91 const void* bytes) |
92 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), | 92 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), |
93 main_buffer_(static_cast<char*>(base::AlignedAlloc(main_buffer_size_, | 93 main_buffer_(static_cast<char*>(base::AlignedAlloc(main_buffer_size_, |
94 kMessageAlignment))) { | 94 kMessageAlignment))) { |
95 DCHECK_LE(num_bytes, kMaxMessageNumBytes); | 95 ConstructorHelper(type, subtype, num_bytes); |
96 | |
97 // |total_size| is updated below, from the other values. | |
98 header()->type = type; | |
99 header()->subtype = subtype; | |
100 header()->source_id = kInvalidEndpointId; | |
101 header()->destination_id = kInvalidEndpointId; | |
102 header()->num_bytes = num_bytes; | |
103 header()->unused = 0; | |
104 // Note: If dispatchers are subsequently attached, then |total_size| will have | |
105 // to be adjusted. | |
106 UpdateTotalSize(); | |
107 | |
108 if (bytes) { | 96 if (bytes) { |
109 memcpy(MessageInTransit::bytes(), bytes, num_bytes); | 97 memcpy(MessageInTransit::bytes(), bytes, num_bytes); |
110 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, | 98 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, |
111 main_buffer_size_ - sizeof(Header) - num_bytes); | 99 main_buffer_size_ - sizeof(Header) - num_bytes); |
112 } else { | 100 } else { |
113 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); | 101 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); |
114 } | 102 } |
115 } | 103 } |
116 | 104 |
| 105 MessageInTransit::MessageInTransit(Type type, |
| 106 Subtype subtype, |
| 107 uint32_t num_bytes, |
| 108 UserPointer<const void> bytes) |
| 109 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), |
| 110 main_buffer_(static_cast<char*>(base::AlignedAlloc(main_buffer_size_, |
| 111 kMessageAlignment))) { |
| 112 ConstructorHelper(type, subtype, num_bytes); |
| 113 bytes.GetArray(MessageInTransit::bytes(), num_bytes); |
| 114 } |
| 115 |
117 MessageInTransit::MessageInTransit(const View& message_view) | 116 MessageInTransit::MessageInTransit(const View& message_view) |
118 : main_buffer_size_(message_view.main_buffer_size()), | 117 : main_buffer_size_(message_view.main_buffer_size()), |
119 main_buffer_(static_cast<char*>(base::AlignedAlloc(main_buffer_size_, | 118 main_buffer_(static_cast<char*>(base::AlignedAlloc(main_buffer_size_, |
120 kMessageAlignment))) { | 119 kMessageAlignment))) { |
121 DCHECK_GE(main_buffer_size_, sizeof(Header)); | 120 DCHECK_GE(main_buffer_size_, sizeof(Header)); |
122 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 121 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
123 | 122 |
124 memcpy(main_buffer_.get(), message_view.main_buffer(), main_buffer_size_); | 123 memcpy(main_buffer_.get(), message_view.main_buffer(), main_buffer_size_); |
125 DCHECK_EQ(main_buffer_size_, | 124 DCHECK_EQ(main_buffer_size_, |
126 RoundUpMessageAlignment(sizeof(Header) + num_bytes())); | 125 RoundUpMessageAlignment(sizeof(Header) + num_bytes())); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 185 |
187 if (!dispatchers_ || !dispatchers_->size()) | 186 if (!dispatchers_ || !dispatchers_->size()) |
188 return; | 187 return; |
189 | 188 |
190 transport_data_.reset(new TransportData(dispatchers_.Pass(), channel)); | 189 transport_data_.reset(new TransportData(dispatchers_.Pass(), channel)); |
191 | 190 |
192 // Update the sizes in the message header. | 191 // Update the sizes in the message header. |
193 UpdateTotalSize(); | 192 UpdateTotalSize(); |
194 } | 193 } |
195 | 194 |
| 195 void MessageInTransit::ConstructorHelper(Type type, |
| 196 Subtype subtype, |
| 197 uint32_t num_bytes) { |
| 198 DCHECK_LE(num_bytes, kMaxMessageNumBytes); |
| 199 |
| 200 // |total_size| is updated below, from the other values. |
| 201 header()->type = type; |
| 202 header()->subtype = subtype; |
| 203 header()->source_id = kInvalidEndpointId; |
| 204 header()->destination_id = kInvalidEndpointId; |
| 205 header()->num_bytes = num_bytes; |
| 206 header()->unused = 0; |
| 207 // Note: If dispatchers are subsequently attached, then |total_size| will have |
| 208 // to be adjusted. |
| 209 UpdateTotalSize(); |
| 210 } |
| 211 |
196 void MessageInTransit::UpdateTotalSize() { | 212 void MessageInTransit::UpdateTotalSize() { |
197 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 213 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
198 header()->total_size = static_cast<uint32_t>(main_buffer_size_); | 214 header()->total_size = static_cast<uint32_t>(main_buffer_size_); |
199 if (transport_data_) { | 215 if (transport_data_) { |
200 header()->total_size += | 216 header()->total_size += |
201 static_cast<uint32_t>(transport_data_->buffer_size()); | 217 static_cast<uint32_t>(transport_data_->buffer_size()); |
202 } | 218 } |
203 } | 219 } |
204 | 220 |
205 } // namespace system | 221 } // namespace system |
206 } // namespace mojo | 222 } // namespace mojo |
OLD | NEW |