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.
5 #ifndef MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_
6 #define MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_
9 #include <stdlib.h> // For |free()|.
11 #include "base/basictypes.h"
12 #include "mojo/public/system/system_export.h"
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 {
21 // Creates a |MessageInTransit| with the data given by |bytes|/|num_bytes|.
22 static MessageInTransit* Create(const void* bytes, uint32_t num_bytes);
24 // Destroys a |MessageInTransit| created using |Create()|.
25 inline void Destroy() {
26 // No need to call the destructor, since we're POD.
30 // Gets the size of the data (in number of bytes).
31 uint32_t data_size() const {
35 // Gets the data (of size |size()| bytes).
36 const void* data() const {
37 return reinterpret_cast<const char*>(this) + sizeof(*this);
40 size_t size_with_header_and_padding() const {
41 return RoundUpMessageAlignment(sizeof(*this) + size_);
44 // TODO(vtl): Add whatever's necessary to transport handles.
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;
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);
56 explicit MessageInTransit(uint32_t size)
57 : size_(size), reserved_(0), user_1_(0), user_2_(0) {}
59 // "Header" for the data.
65 // Intentionally unimplemented (and private): Use |Destroy()| instead (which
66 // simply frees the memory).
69 DISALLOW_COPY_AND_ASSIGN(MessageInTransit);
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);
79 #endif // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_