Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: mojo/public/cpp/system/message.h

Issue 1932083002: Mojo: Use new message APIs to reduce copying (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/system/handle.h ('k') | mojo/public/cpp/system/message.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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_PUBLIC_CPP_SYSTEM_MESSAGE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
7
8 #include <limits>
9
10 #include "base/macros.h"
11 #include "mojo/public/c/system/message_pipe.h"
12 #include "mojo/public/cpp/system/handle.h"
13
14 namespace mojo {
15
16 const MojoMessageHandle kInvalidMessageHandleValue =
17 MOJO_MESSAGE_HANDLE_INVALID;
18
19 // Handle wrapper base class for a |MojoMessageHandle|.
20 class MessageHandle {
21 public:
22 MessageHandle() : value_(kInvalidMessageHandleValue) {}
23 explicit MessageHandle(MojoMessageHandle value) : value_(value) {}
24 ~MessageHandle() {}
25
26 void swap(MessageHandle& other) {
27 MojoMessageHandle temp = value_;
28 value_ = other.value_;
29 other.value_ = temp;
30 }
31
32 bool is_valid() const { return value_ != kInvalidMessageHandleValue; }
33
34 const MojoMessageHandle& value() const { return value_; }
35 MojoMessageHandle* mutable_value() { return &value_; }
36 void set_value(MojoMessageHandle value) { value_ = value; }
37
38 void Close() {
39 DCHECK(is_valid());
40 MojoResult result = MojoFreeMessage(value_);
41 ALLOW_UNUSED_LOCAL(result);
42 DCHECK_EQ(MOJO_RESULT_OK, result);
43 }
44
45 private:
46 MojoMessageHandle value_;
47 };
48
49 using ScopedMessageHandle = ScopedHandleBase<MessageHandle>;
50
51 inline MojoResult AllocMessage(size_t num_bytes,
52 const MojoHandle* handles,
53 size_t num_handles,
54 MojoAllocMessageFlags flags,
55 ScopedMessageHandle* handle) {
56 DCHECK_LE(num_bytes, std::numeric_limits<uint32_t>::max());
57 DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max());
58 MojoMessageHandle raw_handle;
59 MojoResult rv = MojoAllocMessage(static_cast<uint32_t>(num_bytes), handles,
60 static_cast<uint32_t>(num_handles), flags,
61 &raw_handle);
62 if (rv != MOJO_RESULT_OK)
63 return rv;
64
65 handle->reset(MessageHandle(raw_handle));
66 return MOJO_RESULT_OK;
67 }
68
69 inline MojoResult GetMessageBuffer(MessageHandle message, void** buffer) {
70 DCHECK(message.is_valid());
71 return MojoGetMessageBuffer(message.value(), buffer);
72 }
73
74 } // namespace mojo
75
76 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/handle.h ('k') | mojo/public/cpp/system/message.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698