• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/public/cpp/bindings/lib/message_internal.h"
6 
7 #include "mojo/public/cpp/bindings/lib/array_internal.h"
8 #include "mojo/public/cpp/bindings/message.h"
9 
10 namespace mojo {
11 namespace internal {
12 
13 namespace {
14 
ComputeHeaderSize(uint32_t flags,size_t payload_interface_id_count)15 size_t ComputeHeaderSize(uint32_t flags, size_t payload_interface_id_count) {
16   if (payload_interface_id_count > 0) {
17     // Version 2
18     return sizeof(MessageHeaderV2);
19   } else if (flags &
20              (Message::kFlagExpectsResponse | Message::kFlagIsResponse)) {
21     // Version 1
22     return sizeof(MessageHeaderV1);
23   } else {
24     // Version 0
25     return sizeof(MessageHeader);
26   }
27 }
28 
29 }  // namespace
30 
ComputeSerializedMessageSize(uint32_t flags,size_t payload_size,size_t payload_interface_id_count)31 size_t ComputeSerializedMessageSize(uint32_t flags,
32                                     size_t payload_size,
33                                     size_t payload_interface_id_count) {
34   const size_t header_size =
35       ComputeHeaderSize(flags, payload_interface_id_count);
36   if (payload_interface_id_count > 0) {
37     return Align(header_size + Align(payload_size) +
38                  ArrayDataTraits<uint32_t>::GetStorageSize(
39                      static_cast<uint32_t>(payload_interface_id_count)));
40   }
41   return internal::Align(header_size + payload_size);
42 }
43 
44 }  // namespace internal
45 }  // namespace mojo
46