- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / system / message_in_transit.h
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_
6 #define MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_
7
8 #include <stdint.h>
9 #include <stdlib.h>  // For |free()|.
10
11 #include "base/basictypes.h"
12 #include "mojo/public/system/system_export.h"
13
14 namespace mojo {
15 namespace system {
16
17 // This class is used to represent data in transit. It is thread-unsafe.
18 // Note: This class is POD.
19 class MOJO_SYSTEM_EXPORT MessageInTransit {
20  public:
21   // Creates a |MessageInTransit| with the data given by |bytes|/|num_bytes|.
22   static MessageInTransit* Create(const void* bytes, uint32_t num_bytes);
23
24   // Destroys a |MessageInTransit| created using |Create()|.
25   inline void Destroy() {
26     // No need to call the destructor, since we're POD.
27     free(this);
28   }
29
30   // Gets the size of the data (in number of bytes).
31   uint32_t data_size() const {
32     return size_;
33   }
34
35   // Gets the data (of size |size()| bytes).
36   const void* data() const {
37     return reinterpret_cast<const char*>(this) + sizeof(*this);
38   }
39
40   size_t size_with_header_and_padding() const {
41     return RoundUpMessageAlignment(sizeof(*this) + size_);
42   }
43
44   // TODO(vtl): Add whatever's necessary to transport handles.
45
46   // Messages (the header and data) must always be aligned to a multiple of this
47   // quantity (which must be a power of 2).
48   static const size_t kMessageAlignment = 8;
49
50   // Rounds |n| up to a multiple of |kMessageAlignment|.
51   static inline size_t RoundUpMessageAlignment(size_t n) {
52     return (n + kMessageAlignment - 1) & ~(kMessageAlignment - 1);
53   }
54
55  private:
56   explicit MessageInTransit(uint32_t size)
57       : size_(size), reserved_(0), user_1_(0), user_2_(0) {}
58
59   // "Header" for the data.
60   uint32_t size_;
61   uint32_t reserved_;
62   uint32_t user_1_;
63   uint32_t user_2_;
64
65   // Intentionally unimplemented (and private): Use |Destroy()| instead (which
66   // simply frees the memory).
67   ~MessageInTransit();
68
69   DISALLOW_COPY_AND_ASSIGN(MessageInTransit);
70 };
71
72 // The size of |MessageInTransit| must be appropriate to maintain alignment of
73 // the following data.
74 COMPILE_ASSERT(sizeof(MessageInTransit) == 16, MessageInTransit_has_wrong_size);
75
76 }  // namespace system
77 }  // namespace mojo
78
79 #endif  // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_