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

Unified 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, 8 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/system/message.h
diff --git a/mojo/public/cpp/system/message.h b/mojo/public/cpp/system/message.h
new file mode 100644
index 0000000000000000000000000000000000000000..f9002e4c51aa5884e2be91ad94be0d360d0e3bb5
--- /dev/null
+++ b/mojo/public/cpp/system/message.h
@@ -0,0 +1,76 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
+#define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
+
+#include <limits>
+
+#include "base/macros.h"
+#include "mojo/public/c/system/message_pipe.h"
+#include "mojo/public/cpp/system/handle.h"
+
+namespace mojo {
+
+const MojoMessageHandle kInvalidMessageHandleValue =
+ MOJO_MESSAGE_HANDLE_INVALID;
+
+// Handle wrapper base class for a |MojoMessageHandle|.
+class MessageHandle {
+ public:
+ MessageHandle() : value_(kInvalidMessageHandleValue) {}
+ explicit MessageHandle(MojoMessageHandle value) : value_(value) {}
+ ~MessageHandle() {}
+
+ void swap(MessageHandle& other) {
+ MojoMessageHandle temp = value_;
+ value_ = other.value_;
+ other.value_ = temp;
+ }
+
+ bool is_valid() const { return value_ != kInvalidMessageHandleValue; }
+
+ const MojoMessageHandle& value() const { return value_; }
+ MojoMessageHandle* mutable_value() { return &value_; }
+ void set_value(MojoMessageHandle value) { value_ = value; }
+
+ void Close() {
+ DCHECK(is_valid());
+ MojoResult result = MojoFreeMessage(value_);
+ ALLOW_UNUSED_LOCAL(result);
+ DCHECK_EQ(MOJO_RESULT_OK, result);
+ }
+
+ private:
+ MojoMessageHandle value_;
+};
+
+using ScopedMessageHandle = ScopedHandleBase<MessageHandle>;
+
+inline MojoResult AllocMessage(size_t num_bytes,
+ const MojoHandle* handles,
+ size_t num_handles,
+ MojoAllocMessageFlags flags,
+ ScopedMessageHandle* handle) {
+ DCHECK_LE(num_bytes, std::numeric_limits<uint32_t>::max());
+ DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max());
+ MojoMessageHandle raw_handle;
+ MojoResult rv = MojoAllocMessage(static_cast<uint32_t>(num_bytes), handles,
+ static_cast<uint32_t>(num_handles), flags,
+ &raw_handle);
+ if (rv != MOJO_RESULT_OK)
+ return rv;
+
+ handle->reset(MessageHandle(raw_handle));
+ return MOJO_RESULT_OK;
+}
+
+inline MojoResult GetMessageBuffer(MessageHandle message, void** buffer) {
+ DCHECK(message.is_valid());
+ return MojoGetMessageBuffer(message.value(), buffer);
+}
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_H_
« 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